Hi, you can take a look at org.apache.pdfbox.util.PDFMergerUtility how to copy content from one PDF to the other. Copying the content stream token by token missed a lot of information.
BR Maruan Sahyoun Am 16.07.2013 um 04:12 schrieb daijun <[email protected]>: > Dears, > I just try to directly copy tokens from one pdf to another. The program run > with no error. But when I open the generated pdf, it shows error. > Could anyone be kind help me? Thank you. > > > Code like: > PDDocument document2 = new PDDocument(); // target > PDPage newPage = new PDPage(); // new page in the target > document2.addPage( newPage ); > > > > PDDocument document1 = PDDocument.load("c:\\a.pdf"); //source > PDDocumentCatalog catalog = document.getDocumentCatalog(); > PDPage page = (PDPage) catalog.getAllPages().get(0); > PDFStreamParser parser = new PDFStreamParser(page.getContents()); > parser.parse(); > List tokens = parser.getTokens(); > > List newTokens = new ArrayList(); > for(int j=0; j<tokens.size(); j++) { > Object token = tokens.get( j ); > newTokens.add( token ); > } > > > > PDStream newContents = new PDStream(document2 ); > ContentStreamWriter writer = new ContentStreamWriter( > newContents.createOutputStream() ); > writer.writeTokens( newTokens ); > newContents.addCompression(); > > newPage.setContents(newContents); > > > document2.save("c:\\new.pdf");

