You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by daijun <16...@qq.com> on 2013/07/16 04:12:15 UTC
Can I copy tokens from one PDF to another?
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");
Re: Can I copy tokens from one PDF to another?
Posted by Maruan Sahyoun <sa...@fileaffairs.de>.
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 <16...@qq.com>:
> 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");