0
votes

I've made a lot of research on this subject but everything I find is everytime "use the function getOverContent of the stamper". I made this and it still not working.

I made a programm which merge together the PDFs of a repertory, than it paginates this new document (I hope you can follow what I'm writting). The original PDFs are self made (direct saved in PDF) or not (scanned). That's with these last ones where there are trouble. The pagination shows on the firsts but not on the seconds (it exists probably, but it should be behind the image)!

Here is the code for the pagination, has someone THE idea, where I'm mistaken?

PdfReader reader = new PdfReader(source);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(destination));
    for (int i = start + 1; i <= reader.getNumberOfPages(); i++) {
        Phrase noPage = new Phrase((i - start) + "", new Font(FontFamily.COURIER, 14));
        float x = reader.getPageSize(i).getRight(20);
        float y = reader.getPageSize(i).getTop(20);

        PdfContentByte content = stamper.getOverContent(i);
        content.beginText();
        ColumnText.showTextAligned(content,Element.ALIGN_CENTER, noPage, x, y, 0);
        content.endText();
    }
    stamper.close();
    reader.close();

Thanks

After Answer from Bruno, I've made the following:

PdfReader reader = new PdfReader(source); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(destination));

for (int i = start + 1; i <= reader.getNumberOfPages(); i++) {
    Phrase noPage = new Phrase((i - start) + "", new Font(FontFamily.COURIER, 14));
    float x = reader.getCropBox(i).getRight(20);
        float y = reader.getCropBox(i).getTop(20);

        PdfContentByte content = stamper.getOverContent(i);
        ColumnText.showTextAligned(content,Element.ALIGN_CENTER, noPage, x, y, 0);
    }
    stamper.close();
    reader.close();

But it's still not working

For examples: https://www.transfernow.net/24axn1g4wq4l

1
You get the x and y value by examining the /MediaBox, but maybe there's also a /CropBox that is smaller than the /MediaBox. In that case, the text is added within the boundaries of the /MediaBox, but outside the boundaries of the /CropBox, and therefore outside the visible area of the page. See How to get the page width and height of a PDF document?Bruno Lowagie
You are also creating an invalid PDF. According to ISO-32000-1, it is forbidden to nest text objects. You are using content.beginText()/content.endText() in combination with ColumnText.showTextAligned(). Please remove the lines content.beginText(); and content.endText(); because the ColumnText.showTextAligned() takes care of adding the BT/ET operators.Bruno Lowagie

1 Answers

0
votes

The original PDFs are self made (direct saved in PDF) or not (scanned). That's with these last ones where there are trouble. The pagination shows on the firsts but not on the seconds (it exists probably, but it should be behind the image)!

The problem is not that the second type of PDFs is scanned but that it uses page rotation.

When a page is rotated, iText inserts a coordinate system rotation instruction at the start of the undercontent and overcontent which ensures that any text drawn without further transformation is displayed upright on the rotated page.

This rotation of the coordinate system obviously needs to be considered when choosing absolute coordinates.

Thus, instead of

reader.getPageSize(i)

you should use

reader.getPageSizeWithRotation(i)

Alternatively you can switch of this iText mechanism using

stamper.setRotateContents(false);

and then consider the presence of page rotation explicitly in all your following operations.