Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>According to your comments to the original question, you think in PDFBox</p> <blockquote> <p>for adding a new page &amp; content, you need to create a new pdf add new content and then merge the existing pdf. I wanted to avoid the merging step. Renaming is not the issue</p> </blockquote> <p>You might want to try something like this:</p> <pre><code>PDDocument doc = PDDocument.load(new FileInputStream(new File("original.pdf"))); PDPage page = new PDPage(); // fill page doc.addPage(page); doc.save("original-plus-page.pdf"); </code></pre> <p><strong>EDIT:</strong> In a comment to the answer the question arose how to <em>insert a new page at specific index(page number).</em> To do this, obviously the <code>doc.addPage(page)</code> has to be changed somehow.</p> <p>Originally this <code>PDDocument</code> method is defined like this:</p> <pre><code>/** * This will add a page to the document. This is a convenience method, that * will add the page to the root of the hierarchy and set the parent of the * page to the root. * * @param page The page to add to the document. */ public void addPage( PDPage page ) { PDPageNode rootPages = getDocumentCatalog().getPages(); rootPages.getKids().add( page ); page.setParent( rootPages ); rootPages.updateCount(); } </code></pre> <p>We merely need a similar function which merely does not simply <code>add</code> the page to the kids but instead adds it at a given index. Thus a helper method like the following in our code will do:</p> <pre><code>public static void addPage(PDDocument doc, int index, PDPage page) { PDPageNode rootPages = doc.getDocumentCatalog().getPages(); rootPages.getKids().add(index, page); page.setParent(rootPages); rootPages.updateCount(); } </code></pre> <p>If you now replace the line</p> <pre><code>doc.addPage(page); </code></pre> <p>in the code of the original answer by</p> <pre><code>addPage(doc, page, 0); </code></pre> <p>the empty page is added up front.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload