marko,

Here is a sample of code I have used.  You can either return a
ByteArrayOutputStream (os) or you can convert this ByteArrayOutputStream to
ByteArray (os.toByteArray()).  

// begin sample code //

public ByteArrayOutputStream concatByteArrays (byte[] doc1, byte[] doc2){
// or public byte[] concatByteArrays (byte[] doc1, byte[] doc2){
    
    /**
      * used to return merged doc as ByteArrayOutputStream
      */
    java.io.ByteArrayOutputStream os = new java.io.ByteArrayOutputStream(); 
    
    try{
      Document document = null;
      byteArrayOutputStream = new ByteArrayOutputStream();
      PdfCopy Writer = null;

      /**
        * create a reader for doc1
        */
      PdfReader reader = new PdfReader(doc1);
      reader.consolidateNamedDestinations();

      /**
        * retrieve total number of pages
        */
      int n = reader.getNumberOfPages();

      /**
        * creation of document object
        */
      document = new Document(reader.getPageSizeWithRotation(1));

      /**
        * cr4eate a writer that listens to the document
        */
      writer = new PdfCopy(document, os);
      writer.setViewerPreferences(PdfWriter.PageModeUseOutlines);

      /**
        * now open the document
        */
      document.open();

      /**
        * add content
        */
      PdfImportedPage page;
      for (int i = 0; i < n; ){
        ++i;
        page = writer.getImportedPage(reader, i);
        writer.addPage(page);
      }// end for loop
      writer.freeReader(reader);

      /**
        * create a reader for doc2
        */
      reader = new PdfReader(doc2);
      reader.consolidateNamedDestinations();

      /**
        * retrieve the total number of pages
        */
      n = reader.getNumberOfPages();

      /**
        * add contents of doc2
        */
      for (int i = 0; i < n; ){
        ++i;
        page = writer.getImportedPage(reader, i);
        writer.addPage(page);
      }// end second for loop
      writer.freeReader(reader);

      /**
        * now close the document
        */      
      document.close();
    }// end try
    catch (Exception e) { e.printStackTrace(); }// end catch

    retrun os;  // or return os.toByteArray();
  }// end concatByteArrays

// end sample code //

Hope this helps.
Ed


marko[FIN] wrote:
> 
> Ho do i merge two pdfs from byte arrays with different page sizes? I have
> wrote the followin code, it merges them but the page sizes are the same. 
> 
> public static byte[] mergePdfs(byte[] firstPdf, byte[] lastPdf)
> 
>     ByteArrayOutputStream byteArrayOutputStream = null;
>     byte[] mergedPdf = null;
>     try{
>       Document document = new Document();
>       byteArrayOutputStream = new ByteArrayOutputStream();
>       PdfWriter pdfWriter = PdfWriter.getInstance(document,
> byteArrayOutputStream);
> 
>       document.open();
>       PdfContentByte pdfContentByte = pdfWriter.getDirectContent();
>       PdfImportedPage pdfImportedPage = null;
> 
>       PdfReader pdfReader = new PdfReader(firstPdf);
> 
>       for (int i = 0; i < pdfReader.getNumberOfPages(); i++) {
>           document.newPage();
>           pdfImportedPage = pdfWriter.getImportedPage(pdfReader, i + 1);
>           float width = pdfImportedPage.getWidth();
>           float height = pdfImportedPage.getHeight();
>           document.setPageSize(new Rectangle(width, height)); // doesent
> work
>           pdfContentByte.addTemplate(pdfImportedPage, 0, 0);
>       } // for
> 
>       pdfReader = new PdfReader(lastPdf);
>       for (int i = 0; i < pdfReader.getNumberOfPages(); i++) {
>           document.newPage();
>           pdfImportedPage = pdfWriter.getImportedPage(pdfReader, i + 1);
>           float width = pdfImportedPage.getWidth();
>           float height = pdfImportedPage.getHeight();
>           document.setPageSize(new Rectangle(width, height)); // doesent
> work
>           pdfContentByte.addTemplate(pdfImportedPage, 0, 0);
>       } // for
> 
>       document.close();
>       mergedPdf = byteArrayOutputStream.toByteArray();
>     } // try
> 

-- 
View this message in context: 
http://www.nabble.com/Mergin-pdfs-tp25058642p25068665.html
Sent from the iText - General mailing list archive at Nabble.com.


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with 
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.1t3xt.com/docs/book.php
Check the site with examples before you ask questions: 
http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/

Reply via email to