Add page as nth page

I have two pdf files:

  • Master.pdf: contains bookmarked pages, outline (table of contents).
  • Child.pdf: single page file.

Child.pdf pages should be added as page n (in my case as page 10) in Master.pdf.

As a result, the PDF file should have a new outline element (new bookmark) for the new page. Also existing bookmarks should work fine. In fact: the existing structure tree needs to be reorganized.

Can I achieve this through the iText API? Are there any useful samples?

public class ConcatenateBookmarks {
    public static final String SRC1 = "C:\\c\\spring-in-action.pdf";
    public static final String SRC2 = "C:\\c\\SPD-DUAL DS.pdf";
    public static final String DEST = "C:\\c\\final.pdf";

    /**
     * Manipulates a PDF file src with the file dest as result
     * 
     * @param src
     *            the original PDF
     * @param dest
     *            the resulting PDF
     * @throws IOException
     * @throws DocumentException
     */
    public void manipulatePdf(String[] src, String dest) throws IOException, DocumentException {
        int POINT = 3;
        Document document = new Document();
        PdfSmartCopy copy = new PdfSmartCopy(document, new FileOutputStream(dest));
        document.open();
        PdfReader reader;
        int page_offset = 0;
        int n;
        // Create a list for the bookmarks
        ArrayList<HashMap<String, Object>> bookmarks = new ArrayList<HashMap<String, Object>>();
        List<HashMap<String, Object>> tmp;
        for (int i = 0; i < 1/* src.length */; i++) {
            reader = new PdfReader(src[i]);
            PdfReader reader2 = new PdfReader(src[1]);
            int pagesCount = reader2.getNumberOfPages();
            page_offset = pagesCount;
            if (i == 0) {
                HashMap<String, String> map = SimpleNamedDestination.getNamedDestination(reader, false);
                SimpleNamedDestination.exportToXML(map, new FileOutputStream(dest), "ISO8859-1", false);
                copy.addNamedDestinations(map, 0);
            }
            tmp = SimpleBookmark.getBookmark(reader);
            // this level have to
            // separate up to n don't
            // change and after the
            // should shift
            SimpleBookmark.shiftPageNumbers(tmp, page_offset,
               new int[] { POINT, reader.getNumberOfPages() + pagesCount });
            bookmarks.addAll(tmp);
            // add the pages
            n = reader.getNumberOfPages();
            page_offset += n;
            for (int page = 0; page < n;) {
                copy.addPage(copy.getImportedPage(reader, ++page));
                if (page == POINT) // add child pages to nth point
                {
                    for (int page2 = 0; page2 < pagesCount;) {
                        copy.addPage(copy.getImportedPage(reader2, ++page2));
                    }
                }
            }
            copy.freeReader(reader);
            reader.close();
        }
        // Add the merged bookmarks
        copy.setOutlines(bookmarks);
        // step 5
        document.close();
    }
    /**
     * Main method.
     * 
     * @param args
     *            no arguments needed
     * @throws DocumentException
     * @throws IOException
     * @throws SQLException
     */
    public static void main(String[] args) throws IOException, DocumentException, SQLException {
        new ConcatenateBookmarks().manipulatePdf(new String[] { SRC1, SRC2 }, DEST);
    }
}
+4
source share
1 answer

, InsertAndAdaptOutlines. PdfStamper PdfCopy, , PdfOutline.

bookmarks.pdf:

enter image description here

"Hello World", 4 ( 4 10), bookmarks_hello.pdf

enter image description here

:

PdfReader insert = new PdfReader(INSERT);
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(DEST));
stamper.insertPage(4, insert.getPageSize(1));
PdfContentByte cb = stamper.getOverContent(4);
cb.addTemplate(stamper.getImportedPage(insert, 1), 0, 0);
...
stamper.close();

: ? PdfReader, entry, outlines, PdfStamper, setOutlines() . ( PdfCopy PdfSmartCopy):

List<HashMap<String, Object>> outlines = SimpleBookmark.getBookmark(reader);
HashMap<String, Object> entry = new HashMap<String, Object>();
entry.put("Title", "Hello");
entry.put("Action", "GoTo");
entry.put("Page", "4 Fit");
updateOutline(outlines, entry, 4);
stamper.setOutlines(outlines);

updateOutline() , GoTo:

public boolean updateOutline(List<HashMap<String, Object>> outlines, HashMap<String, Object> entry, int p) {
    int index = 0;
    for (HashMap<String, Object> outline : outlines) {
        Object kids = outline.get("Kids");
        if (kids != null) {
            updateOutline((List<HashMap<String, Object>>)kids, entry, p);
        }
        else {
            if (p < getPage(outline)) {
                outlines.add(index, entry);
                return true;
            }
            index++;
        }
    }
    return false;
}

public int getPage(HashMap<String, Object> outline) {
    Object page = outline.get("Page");
    if (page == null) return -1;
    String p = page.toString().substring(0, page.toString().indexOf(" "));
    return Integer.parseInt(p);
}

-, updateOutline(), , , . entry . , PDF .

+1

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


All Articles