如何获取pdf文件每一页的尺寸 [英] How to get dimensions of each page of a pdf file

查看:1066
本文介绍了如何获取pdf文件每一页的尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在pdf文件每页中心添加水印时遇到问题.

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

到目前为止,我已经尝试过:

      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);
                        }
                    }
                }

问题:

上面的代码对于所有页面都具有相同尺寸(高度和宽度)的pdf文件很好用. 但是,当我提供尺寸不同的pdf页面时,水印将放置在不同的位置而不是在中心.

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.

我所知道的:

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

调试并评估上述代码后,对于尺寸不同的pdf页面,我将获得相同的页面大小值.

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()

为pdf的所有页面提供相同的页面大小值. 并且inputPdfReader.getPageSize(pageNumber).getTop()为所有尺寸(宽度和宽度)不同的页面提供相同的值

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)

问题:

如何获取不同尺寸的pdf文件每页的页面大小

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

推荐答案

这不是iText问题.这是一个数学问题.

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

如果具有代表矩形左下角的坐标(x1,y1)和代表矩形右上角的坐标(x2,y2),则可以计算这样的矩形:

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:

矩形的宽度为(x2-x1).

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

宽度的一半等于(x2-x1)/2.

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

您需要获得中心的坐标是x1 +(x2-x1)/2

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

或:x1-x2/2-x1/2

Or: x1 - x2 / 2 - x1 / 2

或x1/2 + x2/2

Or x1 / 2 + x2 / 2

或(x1 + x2)/2

Or (x1 + x2) / 2

在代码示例中,您尝试过:

In your code sample, you have tried:

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

这对应于:

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;

您的问题不属于与iText相关的问题.这是基础数学.

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

很明显:如果crop等于null,则没有裁剪框,​​您需要使用媒体框的值.

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

这篇关于如何获取pdf文件每一页的尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆