0

I'm switching some parts of my program from itext5 to itext 7. How can I import pages from others pdf's to a single one?

    void merge(PdfDocument reader, PdfDocumentContentParser parser, int page) throws IOException {

        PdfImportedPage importedPage = writer.getImportedPage(reader, page);
        PdfContentByte directContent = writer.getDirectContent();

        PageVerticalAnalyzer finder = parser.processContent(page, new PageVerticalAnalyzer());

        if (finder.verticalFlips.size() < 2) {
            return;
        }
        Rectangle pageSizeToImport = reader.GetPageSize(page);

        int startFlip = finder.verticalFlips.size() - 1;
        boolean first = true;
        while (startFlip > 0) {
            if (!first) {
                newPage();
            }

            float freeSpace = yPosition - pageSize.getBottom();
            int endFlip = startFlip + 1;
            while ((endFlip > 1) && (finder.verticalFlips.get(startFlip) - finder.verticalFlips.get(endFlip - 2) < freeSpace)) {
                endFlip -= 2;
            }
            if (endFlip < startFlip) {
                float height = finder.verticalFlips.get(startFlip) - finder.verticalFlips.get(endFlip);

                directContent.saveState();
                directContent.rectangle(0, yPosition - height, pageSizeToImport.getWidth(), height);
                directContent.clip();
                directContent.newPath();

                directContent.addTemplate(importedPage, 0, yPosition - (finder.verticalFlips.get(startFlip) - pageSizeToImport.getBottom()));

                directContent.restoreState();
                yPosition -= height + gap;
                startFlip = endFlip - 1;
            } else if (!first) {
                throw new IllegalArgumentException(String.format("Page %s content sections too large.", page));
            }
            first = false;
        }
}

I have been searching the web, reading the api's for itext7 and still can't turn this to make it work. Can't find any replacement for PdfImportedPage and PdfContentByte.

I have listed my code up, my main question is what to use in replacement for PdfImportedPage and PdfContentByte

slamek10
  • 83
  • 1
  • 10
  • 2
    Did you see this page: https://developers.itextpdf.com/content/itext-7-jump-start-tutorial/chapter-6-reusing-existing-pdf-documents It's about reusing content from existing PDF's. `PdfImportedPage` is now just `PdfPage`; `PdfContentByte` is either `PdfCanvas` or `Canvas`. – Bruno Lowagie Jan 17 '18 at 08:22
  • Ah I see, was looking at that jump start tutorial, trying out some stuff but nothing worked out for now... will continue working on that then – slamek10 Jan 17 '18 at 08:29
  • 1
    Your code looks very much like the [`PdfVeryDenseMergeTool`](https://github.com/mkl-public/testarea-itext5/blob/master/src/main/java/mkl/testarea/itext5/merge/PdfVeryDenseMergeTool.java) from [this answer](https://stackoverflow.com/a/29078954/1729265). You'll find the corresponding code as [PdfDenseMerger](https://git.itextsupport.com/projects/I7JS/repos/samples/browse/samples/src/main/java/com/itextpdf/samples/sandbox/merge/PdfDenseMerger.java) in the old [iText 7 samples](https://git.itextsupport.com/projects/I7JS/repos/samples/browse/samples/src/main/java/com/itextpdf/samples/sandbox/merge) – mkl Jan 17 '18 at 11:51

0 Answers0