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?