How to get dimensions of each page of a pdf file

2019-09-17 16:40发布

问题:

I am facing a problem while adding a watermark at the center of each page of a pdf file.

What i have tried so far :

      PdfStamper inputPdfStamper = null;
                try {
                    PdfReader inputPdfReader = new PdfReader(new FileInputStream(input));
                    inputPdfStamper = new PdfStamper(inputPdfReader, new FileOutputStream(input));
                    Font font = new Font(fontFamily, fontSize, fontStyle, color);
                    for (int pageNumber = 1 ;pageNumber <=inputPdfStamper.getReader().getNumberOfPages() ; pageNumber++){
                        if(isWatermarkAbove){                           
                      ColumnText.showTextAligned(inputPdfStamper.getOverContent(pageNumber), Element.ALIGN_CENTER, new Phrase(watermark, font),  inputPdfReader.getPageSize(pageNumber).getRight()/2, inputPdfReader.getPageSize(pageNumber).getTop()/2, 45);
     // Updated Code 
    // ColumnText.showTextAligned(inputPdfStamper.getUnderContent(pageNumber), Element.ALIGN_CENTER, new Phrase(watermark, font), inputPdfReader.getCropBox(pageNumber).getLeft()/2, inputPdfReader.getCropBox(pageNumber).getBottom()/2, 45);
                        }else{
                            ColumnText.showTextAligned(inputPdfStamper.getUnderContent(pageNumber), Element.ALIGN_CENTER, new Phrase(watermark, font), inputPdfReader.getPageSize(pageNumber).getRight()/2, inputPdfReader.getPageSize(pageNumber).getTop()/2, 45);
                        }
                    }
                    inputPdfStamper.close();
                } catch (Exception e){
                    throw new RuntimeException(e);
                }finally {
                    if (inputPdfStamper!=null) {
                        try {
                            inputPdfStamper.close();
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                    }
                }

Issue:

The above code works fine for pdf file which has same dimensions(height and width) for all the pages. But when I provide pdf pages with different dimensions , watermark is placing in different positions not in the center.

What I Known :

ColumnText.showTextAligned(inputPdfStamper.getUnderContent(pageNumber), Element.ALIGN_CENTER, new Phrase(watermark, font), inputPdfReader.getPageSize(pageNumber).getRight()/2, inputPdfReader.getPageSize(pageNumber).getTop()/2, 45)

After I debug and evaluate the above code I am getting the same page size value for all the pages of the pdf which are in different dimensions.

inputPdfReader.getPageSize(pageNumber).getRight()

gives the same page size value for all the pages of the pdf. and also inputPdfReader.getPageSize(pageNumber).getTop() gives the same value for all the pages which are in different dimensions(hieght and width)

Question :

How to get the page size of each pages of the pdf file which are in different dimensions

回答1:

This is not an iText problem. This is a Math problem.

If you have a coordinate (x1, y1) representing the lower-left corner of a rectangle, and a coordinate (x2, y2) representing the upper-right corner of a rectangle, you can calculate the coordinate of the middle of the rectangle like this:

((x1 + x2) / 2, (y1 + y2) / 2)

If you don't understand this formula, think about this:

The width of the rectangle is (x2 - x1).

Half the width equals (x2 - x1) / 2.

The coordinate you need to get the middle is x1 + (x2 - x1) / 2

Or: x1 - x2 / 2 - x1 / 2

Or x1 / 2 + x2 / 2

Or (x1 + x2) / 2

In your code sample, you have tried:

inputPdfReader.getCropBox(pageNumber).getLeft()/2
inputPdfReader.getPageSize(pageNumber).getRight()/2

This corresponds with:

x1 / 2
x2 / 2

This doesn't make any sense! This is what you need:

Rectangle crop = inpitPdfReader.getCropBox(pageNumber);
float x = (crop.getLeft() + crop.getRight()) / 2;
float y = (crop.getBottom() + crop.getTop()) / 2;

Your question doesn't qualify as an iText-related question. This is elementary Math.

Obviously: if crop equals null, then there is no crop box and you need to use the value of the media box.



标签: java pdf itext