how do i merge pdf using itext when the pdf's i need to merge are of
different size. one is European A4 & other is US A4 and merging also leaves
extra margin on top & left side of the page. I am using the following code.
Please help as in the merged pdf the page difference is visible & even the
margins


import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
 
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PRAcroForm;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfCopy;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.SimpleBookmark;

public class Pdf_Merge {
  public static void main(String[] args) {
    try {
      List<InputStream> pdfs = new ArrayList<InputStream>();
      pdfs.add(new FileInputStream("Input1.pdf"));
      pdfs.add(new FileInputStream("input2.pdf"));
      OutputStream output = new FileOutputStream("Output1.pdf");
      
      Pdf_Merge.concatPDFs(pdfs, output, true);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public static void concatPDFs(List<InputStream> streamOfPDFFiles,
OutputStream outputStream, boolean paginate) {

        
    Document document = new Document(PageSize.A4);
    try {
      List<InputStream> pdfs = streamOfPDFFiles;
      List<PdfReader> readers = new ArrayList<PdfReader>();
      int totalPages = 0;
      Iterator<InputStream> iteratorPDFs = pdfs.iterator();

      // Create Readers for the pdfs.
      while (iteratorPDFs.hasNext()) {
        InputStream pdf = iteratorPDFs.next();
        PdfReader pdfReader = new PdfReader(pdf);
        readers.add(pdfReader);
        totalPages += pdfReader.getNumberOfPages();
      }
      // Create a writer for the outputstream
      PdfWriter writer = PdfWriter.getInstance(document, outputStream);
      
      document.setMargins(36f, 36f, 54f, 72f);
      document.open();
      
      PdfContentByte cb = writer.getDirectContent(); // Holds the PDF
      // data

      PdfImportedPage page;
      int currentPageNumber = 0;
      int pageOfCurrentReaderPDF = 0;
      Iterator<PdfReader> iteratorPDFReader = readers.iterator();

      // Loop through the PDF files and add to the output.
      while (iteratorPDFReader.hasNext()) {
        PdfReader pdfReader = iteratorPDFReader.next();

        // Create a new page in the target for each source page.
        while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) {
                document.setMargins(36, 36, 54, 72);
                document.newPage();
            pageOfCurrentReaderPDF++;
          currentPageNumber++;
          page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF);
          cb.addTemplate(page, 0, 0);

          // Code for pagination.
          if (paginate) {
            cb.beginText();
            //cb.setFontAndSize(bf, 9);
            //cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "" +
currentPageNumber + " of " + totalPages, 520, 5, 0);
            cb.endText();
           
          }
        }
        pageOfCurrentReaderPDF = 0;
      }
      outputStream.flush();
      document.setMargins(0, 0, 0, 0);
      document.close();
      outputStream.close();
      System.out.println("Created");
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (document.isOpen())
        document.close();
      try {
        if (outputStream != null)
          outputStream.close();
      } catch (IOException ioe) {
        ioe.printStackTrace();
      }
    }
  }
}

--
View this message in context: 
http://itext-general.2136553.n4.nabble.com/How-to-merge-pdf-with-different-page-size-tp3697861p3697861.html
Sent from the iText - General mailing list archive at Nabble.com.

------------------------------------------------------------------------------
Got Input?   Slashdot Needs You.
Take our quick survey online.  Come on, we don't ask for help often.
Plus, you'll get a chance to win $100 to spend on ThinkGeek.
http://p.sf.net/sfu/slashdot-survey
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions

iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference 
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: 
http://itextpdf.com/themes/keywords.php

Reply via email to