1

I use iText library to merge pdf files. Now I have one file with landscape orientation and many files with portrait orientation. I need to merge this documents into one big document. I have trouble with it because document with landscape orientation merges as portrait orientation. If I try to add document.setPageSize(PageSize.A4.rotate()); string I have landscape orientation, but text on page still in portrait orientation anyway.

Source code for first PDF with landscape orientation only:

Document document    = new Document();
PdfWriter writer     = PdfWriter.getInstance(document, new FileOutputStream(outputFile));
document.open();
PdfContentByte cb    = writer.getDirectContent();
List<File> listFiles = Arrays.asList(Objects.requireNonNull(sourceFile.getParentFile().listFiles()));

PdfReader sourceFileReader = new PdfReader(new FileInputStream(sourceFile));
for (int i = 1; i <= sourceFileReader.getNumberOfPages(); i++) {
    Rectangle r = sourceFileReader.getPageSize(sourceFileReader.getPageN(i));
    //document.setPageSize(PageSize.A4.rotate());
    document.newPage();
    PdfImportedPage page = writer.getImportedPage(sourceFileReader, i);
        cb.addTemplate(page, 0, 0);
    }

// here code for other documents

document.close();

Can anybody help me?

Virkom
  • 373
  • 4
  • 22
  • 1
    for merging documents don't use `PdfWriter`. Use `PdfCopy` instead. – mkl Feb 05 '18 at 05:16
  • @mkl, Thank you very much. It's working now. – Virkom Feb 05 '18 at 07:01
  • Your question essentially is a duplicate of [this question](https://stackoverflow.com/a/15945467/1729265) - strictly speaking the question there is about [tag:c#] while yours is about [tag:java] but the answer covers both languages. – mkl Feb 05 '18 at 12:37

0 Answers0