0

I'm using the MPL/LGPL version of the iText library (the one released in July 2009) to download pdfs through a web application. My issue is that the GSP that is being rendered on the pdf has a landscape layout. Currently I have this code:

public void mergeMultiplePdfFiles(List<String> fileNames, OutputStream os, String fileDirectory, boolean isLandscape = false) {
    FileInputStream is
    PdfReader pdfReader
    PdfWriter pdfWriter
    com.lowagie.text.Document document

    try {
        document = new com.lowagie.text.Document(isLandscape ? PageSize.LETTER.rotate() : PageSize.LETTER)

        pdfWriter = PdfWriter.getInstance(document, os)
        document.open()
        PdfImportedPage page
        PdfContentByte cb = pdfWriter.getDirectContent() // Holds the PDF

        fileNames.each { fileName ->
            def filePath = fileDirectory + fileName + ".pdf"
            is = new FileInputStream(filePath)
            pdfReader = new PdfReader(is)

            for(int i = 1; i <= pdfReader.getNumberOfPages(); i++) {
                document.newPage()
                page = pdfWriter.getImportedPage(pdfReader, i)
                //cb.addTemplate(page, 0.0, -1f, 1f, 0.0, 0.0, pdfReader.getPageSizeWithRotation(i).height);
                cb.addTemplate(page, 0, 0);
            }
            is.close()
            pdfReader.close();

            File file = new File(filePath)
            file.delete()
        }
    }
    catch (Exception e) {
        log.error("ERROR Generating a PDF.")
        //e.printStackTrace()
        throw e
    }
    finally {
        if (document.isOpen())
            document.close()

        try {
            if (os != null) os.close()
            if (is != null) is.close()
            if (pdfReader != null) pdfReader.close()
            if (pdfWriter != null) pdfWriter.close()

        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}

This code creates a pdf that is rendered properly but the page is in portrait. So, the user has to rotate the pdf to landscape to read the document. I want to have the pdf rotated to landscape before downloading the file. I've attempted many solution such as: document = new com.lowagie.text.Document(PageSize.LETTER.rotate())

or alternatively, document.setPageSize(PageSize.LETTER.rotate())

these two solution cause the pdf to be set to landscape but the text is cut off in the portrait view. Also,

and rot = pdfReader.getPageRotation(i); pageDict = pdfReader.getPageN(i); pageDict.put(PdfName.ROTATE, new PdfNumber(rot + 90)); rot = pdfReader.getPageRotation(i);

None of these suggested solutions work.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
Muraad
  • 1,110
  • 1
  • 9
  • 14
  • 1
    Your code is completely wrong. It goes against all the examples given in [the official documentation](http://pages.itextpdf.com/ebook-stackoverflow-questions.html). You are using `PdfWriter` where you should use `PdfCopy` as explained in my answer to the following question: [How to merge documents correctly?](http://stackoverflow.com/questions/21731439/how-to-merge-documents-correctly) – Bruno Lowagie Jul 13 '15 at 15:32
  • Thank you, using PdfCopy helped with my issue of downloading the pdf in landscape. However, I attempted to use you code for the answer you commented and had issues using PageStamp. Errors would be thrown saying "The PDF has no pages" – Muraad Jul 13 '15 at 19:23
  • Why don't you remove all the code related to `PageStamp`? When I look at your code, it seems that you don't need it. – Bruno Lowagie Jul 13 '15 at 19:50

1 Answers1

1

Thanks to Bruno's comment I got the Document to be downloaded in landscape. Here is the code:

public void mergeMultiplePdfFiles(List<String> fileNames, OutputStream os, String fileDirectory, boolean isLandscape = false) {
    FileInputStream is
    PdfReader pdfReader
    com.lowagie.text.Document document

    try {
        document = new com.lowagie.text.Document()
        PdfCopy copy = new PdfCopy(document, os);
        document.open();
        PdfImportedPage page;
        Chunk chunk;

        fileNames.each { fileName ->
            def filePath = fileDirectory + fileName + ".pdf"
            is = new FileInputStream(filePath)
            pdfReader = new PdfReader(is)
            for(int i = 1; i <= pdfReader.getNumberOfPages(); i++) {

                page = copy.getImportedPage(pdfReader, i);
                copy.addPage(page);
            }
            copy.freeReader(pdfReader);
            is.close()
            pdfReader.close();

            File file = new File(filePath)
            file.delete()
        }
    }
    catch (Exception e) {
        log.error("ERROR Generating a PDF.")
        //e.printStackTrace()
        throw e
    }
    finally {
        if (document.isOpen())
            document.close()

        try {
            if (os != null) os.close()
            if (is != null) is.close()
            if (pdfReader != null) pdfReader.close()

        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }

}

Key change was using PdfCopy instead of the PdfWriter and PdfContentByte previously used.

Muraad
  • 1,110
  • 1
  • 9
  • 14