2

I am creating a small application in which I open a existing PDF and then check whether the individual pages are in A4 or not. If pages are not in A4 then convert them to A4.

I am facing two problems now:

  1. How to check whether the page in PDF is in A4 or not?
  2. During the conversion of a non-A4 page to A4, the text outside the A4 page frame is omitted. Therefore it cause some loss of content.

How can I get around these two issues?

4

2 回答 2

1

PdfReader用类加载文件。

 PdfReader reader = new PdfReader("path to your file");
 Rectangle rect = reader.GetPageSizeWithRotation(i); // i page number, index starts at 1
 for (int i = 1; i <= reader.NumberOfPages; i++)
 {
      Rectangle rect = reader.GetPageSizeWithRotation(i);
      var a = rect.Width;
      var b = rect.Height;
 }

对于 A4 Portrait 格式变量应该是a == 595, b == 842. 谈到页面尺寸时,公差很小。更多信息。

我有这样的方法:

public static bool CheckFirstPageSizeA4Portret(Document doc)
    {

        var a = doc.PageSize.Width;
        var b = doc.PageSize.Height;
        pageFormat format = PageFormat.GetPageFormat(a, b);
        if (format == pageFormat.A4_Portret)
            return true;
        else
            return false;
    }

pageFormat是静态PageFormat类返回的枚举。 PageFormat课程很长,但是很简单,在这里发帖会很傻,差不多有 500 行。什么类,它检查给定的尺寸是否与任何官方的 A、B 或 C 页面格式相对应,并返回格式的名称。

第二个问题看这里。那里是java代码,但我认为你应该明白。

于 2013-01-15T06:16:50.973 回答
0

@masius 已经在他的回答中指出如何使用该GetPageSizeWithRotation方法检索页面的尺寸。

不过,还请注意,该方法返回的尺寸实际上可能由页面字典的/UserUnit条目进行缩放。返回的尺寸GetPageSizeWithRotation/UserUnit值乘以 1 / 72 英寸的形式给出,该值默认为 1.0。

@masius 提到的问题“iText:成功调整一页 pdf 的大小,但在 pdf 文档中有多个页面时失败”的答案中提出的解决方案有些欠佳,因为它放弃了所有交互功能(Acroform 字段和其他注释) .

我建议将页面媒体框扩展为 A4 格式的纵横比,然后使用/UserUnit页面字典条目缩放页面。

于 2013-01-15T08:33:55.653 回答