java - function that can use iText to concatenate / merge pdfs together - causing some issues -


i'm using following code merge pdfs using itext:

public static void concatenatepdfs(list<file> listofpdffiles, file outputfile) throws documentexception, ioexception {         document document = new document();         fileoutputstream outputstream = new fileoutputstream(outputfile);         pdfwriter writer = pdfwriter.getinstance(document, outputstream);         document.open();         pdfcontentbyte cb = writer.getdirectcontent();         (file infile : listofpdffiles) {             pdfreader reader = new pdfreader(infile.getabsolutepath());             (int = 1; <= reader.getnumberofpages(); i++) {                 document.newpage();                 pdfimportedpage page = writer.getimportedpage(reader, i);                 cb.addtemplate(page, 0, 0);             }         }         outputstream.flush();         document.close();         outputstream.close();     } 

this works great! once , while, it's rotating of pages 90 degrees? ever have happen?

i looking pdfs see special ones being flipped.

there errors once in while because using wrong method concatenate documents. please read chapter 6 of book , you'll notice using pdfwriter concatenate (or merge) pdf documents wrong:

  • you ignore page size of pages in original document (you assume of size a4),
  • you ignore page boundaries such crop box (if present),
  • you ignore rotation value stored in page dictionary,
  • you throw away interactivity present in original document, , on.

concatenating pdfs done using pdfcopy, see instance fillflattenmerge2 example:

document document = new document(); pdfcopy copy = new pdfsmartcopy(document, new fileoutputstream(dest)); document.open(); pdfreader reader; string line = br.readline(); // loop on readers     // add pdf pdfcopy     reader = new pdfreader(baos.tobytearray());     copy.adddocument(reader);     reader.close(); // end loop document.close(); 

there other examples in the book.


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -