PDFBox - Coordinate System
I would like to accomplish the following thing. I have a set of PDF files, first I would like to check the origin of the coordinate system. If the origin of the coordinate system for the pdf is not up开发者_如何学Cper left [usually the origin is lower left], I would like to create a resultant PDF with coordinates on the upper left. I am trying to do this using PDFBox [the code snippet is below], however the resultant PDF is coming blank, what I am I doing wrong. I am new to PDFBox, hence any help in this regard is greatly appreciated.
// loading the existing doc
PDDocument doc = PDDocument.load("C:\\Users\\test\\workspace\\example1.pdf");
List allPages = doc.getDocumentCatalog().getAllPages();
PDPageContentStream contentStream = null;
for( int i=0; i<allPages.size(); i++ )
{
PDPage page = (PDPage)allPages.get( i );
contentStream = new PDPageContentStream(doc, page);
contentStream.concatenate2CTM(1f, 0f, 0f, -1f, 0f, page.findMediaBox().getHeight());
contentStream.saveGraphicsState();
contentStream.close();
}
doc.save("C:\\Users\\test\\workspace\\example2.pdf");
doc.close();
You're creating an empty, transformed, content stream.
You need to get the existing content via page.getContents()
, and wrap that in your transformation. Something like:
contentStream.conactenate2CTM(...);
contentStream.magicFunctionThatSucksUpTheExistingPageContent( page.getContents() );
contentStream.close();
PS: a call to saveGraphicsState()
without a corresponding call to restoreGraphicsState()
is a no-no.
Oh and it looks like "magicFunction..." is called appendRawCommands()
.
精彩评论