PDFBox - coordinate system

I would like to do the following. 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 pdf is not in the upper left corner (usually it is the beginning on the left), I would like to create a resulting PDF with coordinates in the upper left corner. I am trying to do this using the PDFBox [code snippet below], however the resulting PDF is getting empty, which I am doing wrong. I am new to PDFBox, so 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();  
+3
source share
1 answer

You create an empty, transformed content stream.

page.getContents() . - :

contentStream.conactenate2CTM(...);
contentStream.magicFunctionThatSucksUpTheExistingPageContent( page.getContents() );
contentStream.close();

PS: saveGraphicsState() restoreGraphicsState() no-no.

, , "magicFunction..." appendRawCommands().

+2

Source: https://habr.com/ru/post/1789447/


All Articles