I have a 300 page PDF file. I want to create a new PDF file from my chosen pages the existing pdf file.

I have created a blank PDF File (using PDFBox ) like this:

 // Create a new empty document
PDDocument document = new PDDocument();

// Create a new blank page and add it to the document
PDPage blankPage = new PDPage();
document.addPage( blankPage );

// Save the newly created document
document.save("BlankPage.pdf");

and this is how I am reading a page from pdf File.

PDDocument doc = PDDocument.load("Hello World.pdf");
PDPage firstPage = (PDPage) doc.getDocumentCatalog().getAllPages().get(67);

My question is, how do I get the contents of 'firstpage' into "blankPage.pdf". If I can choose the x,y coordinates of the firstpage(where it is to be overlayed), it would be even better.

P.S. The page sizes of my Hello World file are not A4.Each page is more like a thumbnail with text and shapes.So, overlaying is possible on A4. Also, I do not want to convert the files to image and then onverlay, I want the whole pdf file to be pasted as is(without converting to image first)

有帮助吗?

解决方案

it turns out that iText can do the same thing.Here is the code for that :

PdfReader reader = new PdfReader("Hello World.pdf");

            Document document = new Document(PageSize.A4);
            PdfWriter writer = PdfWriter.getInstance(document,
                    new FileOutputStream("RESULT.PDF"));
            document.open();
            PdfContentByte canvas = writer.getDirectContent();
            PdfImportedPage page;








            for (int i = 3; i <=6; i++) {
                page = writer.getImportedPage(reader, i);
                canvas.addTemplate(page, 1f, 0, 0, 1, 0, i*30-250);

            }
            document.close();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top