Merge PDFs of Different Widths with iText

I am having a problem when merging documents of different widths using iText.

Below is the code that I use to combine.

public static void doMerge(List<InputStream> list, OutputStream outputStream) throws Exception { Rectangle pagesize = new Rectangle(1700f, 20f); com.itextpdf.text.Document document = new com.itextpdf.text.Document(pagesize); PdfWriter writer = PdfWriter.getInstance(document, outputStream); document.open(); document.setPageSize(pagesize); com.itextpdf.text.pdf.PdfContentByte cb = writer.getDirectContent(); for (InputStream in : list){ PdfReader reader = new PdfReader(in); for (int i = 1; i <= reader.getNumberOfPages(); i++){ document.newPage(); //import the page from source pdf com.itextpdf.text.pdf.PdfImportedPage page = writer.getImportedPage(reader, i); //calculate the y for merging it from top float y = document.getPageSize().getHeight() - page.getHeight(); //add the page to the destination pdf cb.addTemplate(page, 0, y); } reader.close(); in.close(); } outputStream.flush(); document.close(); outputStream.close(); } 

The first pdf page will have a width of 14 inches and a height of 13 inches. All other pages in the document will always be smaller.

I want to combine all the documents in one document.

I do not know how to set the width and height of one merged document.

I think Rectangle pagesize = new Rectangle(1700f, 20f); should do it, but it doesn't work if you change it to Rectangle pagesize = new Rectangle(1700f, 200f); , the document has no effect.

I ask you to continue.

+5
source share
2 answers

Using the PdfWriter class to combine documents goes against all the recommendations given in the official documentation, although there are unofficial examples that could lure you into writing bad code. I hope you understand that I find these bad examples even more annoying than you.

Please take a look at table 6.1 in chapter 6 of my book . It gives you an overview of when to use a class. In this case, you should use PdfCopy :

 String[] files = { MovieLinks1.RESULT, MovieHistory.RESULT }; // step 1 Document document = new Document(); // step 2 PdfCopy copy = new PdfCopy(document, new FileOutputStream(RESULT)); // step 3 document.open(); // step 4 PdfReader reader; int n; // loop over the documents you want to concatenate for (int i = 0; i < files.length; i++) { reader = new PdfReader(files[i]); // loop over the pages in that document n = reader.getNumberOfPages(); for (int page = 0; page < n; ) { copy.addPage(copy.getImportedPage(reader, ++page)); } copy.freeReader(reader); reader.close(); } // step 5 document.close(); 

If you are using the latest version of iText, you can even use the addDocument() method, in which case you do not need to addDocument() over all the pages. You should also be especially careful if forms are involved. There are several examples that demonstrate new functionality (starting from the time of writing the book) in Sandbox .

+18
source

using itext 5.5, we can merge PDF more easily using the PdfCopy.addDocument method:

  package tn.com.sf.za.rd.controller; import java.io.FileOutputStream; import java.io.IOException; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.pdf.PdfCopy; import com.itextpdf.text.pdf.PdfReader; public class ReportMerging { public static void main(String[] args) throws DocumentException, IOException { String DOC_ONE_PATH = "D:/s.zaghdoudi/tmp/one.pdf"; String DOC_TWO_PATH = "D:/s.zaghdoudi/tmp/two.pdf"; String DOC_THREE_PATH = "D:/s.zaghdoudi/tmp/three.pdf"; Document document = new Document(); PdfCopy copy = new PdfCopy(document, new FileOutputStream(DOC_THREE_PATH)); document.open(); PdfReader readerOne = new PdfReader(DOC_ONE_PATH); PdfReader readerTwo = new PdfReader(DOC_TWO_PATH); copy.addDocument(readerOne); copy.addDocument(readerTwo); document.close(); } } 
+3
source

Source: https://habr.com/ru/post/1201559/


All Articles