裁剪一英寸的 PDF 文件

Crop one inch of PDF file

我需要将单页 PDF 从 8X11 英寸调整为 8X9 英寸(或任何尺寸),而根本不调整内容的大小。

如何在 C# 中执行此操作?

谢谢

这应该可以使用任何像样的通用 PDF 库。

例如,使用 iText 7 从右边裁剪 1 英寸:

using (PdfReader reader = new PdfReader(SOURCE_PDF))
using (PdfWriter writer = new PdfWriter(TARGET_PDF))
using (PdfDocument document = new PdfDocument(reader, writer))
{
    for (int i = 1; i <= document.GetNumberOfPages(); i++)
    {
        PdfPage page = document.GetPage(i);
        Rectangle cropBox = page.GetCropBox();
        cropBox.SetWidth(cropBox.GetWidth() - 72);
        page.SetCropBox(cropBox);
    }
}

如果正如您在评论中提到的那样,您实际上想要将 PDF 的右侧部分剪切成宽度为 88 毫米的 PDF,并保持相同的高度,请替换

        cropBox.SetWidth(cropBox.GetWidth() - 72);

        cropBox.SetWidth(88f * 72f / 25.4f);

裁剪框尺寸以默认用户 space 单位给出,而默认为 1⁄72 英寸。因此,要设置以毫米为单位的尺寸,必须先将该数字乘以 (72/25.4)。


两个备注:

  • 实际上 默认用户 space 单位 可能会因页面 UserUnit 属性 被设置为

    a positive number that shall give the size of default user space units, in multiples of 1⁄72 inch. The range of supported values shall be implementation-dependent.

    Default value: 1.0 (user space unit is 1⁄72 inch).

    (ISO 32000-1,Table 30 – 页面对象中的条目)

    虽然这个属性很少用到,特别是因为"The range of supported values shall be implementation-dependent"位,所以我在上面忽略了它。

  • 如果您不想裁剪而是放大页面区域,您可能会不仅需要放大 CropBox 还要放大 MediaBox.

    The crop, bleed, trim, and art boxes shall not ordinarily extend beyond the boundaries of the media box. If they do, they are effectively reduced to their intersection with the media box.

    (ISO 32000-1,第 14.11.2.1 节页面边界/一般)

    PdfPage 有类似的方法 GetMediaBoxSetMediaBox 可用于扩大 MediaBox.