Java中的PDF文档操作

Aspose.PDF FOSS用于Java,提供了一套丰富的API,用于操作已有的PDF文档——拆分、合并、提取页面、插入页面范围以及调整页面内容的大小。这些功能集中在PdfFileEditor及其配套类ContentsResizeParametersContentsResizeValue中。

PdfFileEditor:核心页面操作

PdfFileEditor类是进行高层次PDF文件操作的核心设施。它提供了合并多个PDF文件、提取页面子集、将文档拆分为单独页面以及在指定位置将一个文档的页面插入到另一个文档中的方法。

PdfFileEditor editor = new PdfFileEditor();
// Concatenate two documents into one
editor.concatenate("first.pdf", "second.pdf", "combined.pdf");
// Extract pages 2 through 5 to a new file
editor.extract("source.pdf", 2, 5, "extracted.pdf");

关键方法包括:

  • concatenate(inputFiles, outputFile) — 按顺序合并多个文档
  • extract(inputFile, startPage, endPage, outputFile) — 导出连续范围
  • extract(inputFile, pageNumbers, outputFile) — 导出非连续集合
  • splitToPages(inputFile) — 将每页拆分为一个 ByteArrayOutputStream
  • insert(inputFile, insertPosition, portFile, startPage, endPage, outputFile) — 插入一个范围
  • delete(inputFile, pageNumbers, outputFile) — 删除特定页面
  • append(inputFile, portFile, startPage, endPage, outputFile) — 追加页面范围

对于大型文件,setUseDiskBuffer(true) 将内部存储从堆切换到磁盘。

使用 ContentsResizeParameters 调整内容大小

PdfFileEditor.resizeContents() 在现有页面边界内调整内容。每个边距 — 左、右、上、下 — 以及内容的宽度和高度均以 ContentsResizeValue 实例指定,这些实例通过工厂方法 ContentsResizeValue.percents(value)ContentsResizeValue.units(value) 构造。

try (Document doc = new Document("input.pdf")) {
    PdfFileEditor editor = new PdfFileEditor();
    ContentsResizeParameters params = new ContentsResizeParameters(
        ContentsResizeValue.percents(10),   // left margin
        ContentsResizeValue.percents(80),   // content width
        ContentsResizeValue.percents(10),   // right margin
        ContentsResizeValue.percents(10),   // top margin
        ContentsResizeValue.percents(80),   // content height
        ContentsResizeValue.percents(10)    // bottom margin
    );
    editor.resizeContents(doc, params);
    doc.save("resized.pdf");
}

针对 ContentsResizeParameters 的六参数构造函数接受的值顺序为:leftMargin、contentsWidth、rightMargin、topMargin、contentsHeight、bottomMargin。使用百分比值时,六个值的总和必须为 100%,使用绝对点值时则必须与页面尺寸相匹配。

调整特定页面的大小

当仅有部分页面需要调整大小时,接受整数数组页码的重载会针对这些页面进行处理,而保持其余页面不变:

PdfFileEditor editor = new PdfFileEditor();
ContentsResizeParameters params = new ContentsResizeParameters(
    ContentsResizeValue.units(28),   // left margin in points
    ContentsResizeValue.units(540),  // content width
    ContentsResizeValue.units(28),   // right margin
    ContentsResizeValue.units(28),   // top margin
    ContentsResizeValue.units(786),  // content height
    ContentsResizeValue.units(28)    // bottom margin
);
// Only resize pages 1 and 3
editor.resizeContents(doc, new int[]{1, 3}, params);

ContentsResizeValue.getValue()isPercent() 允许检查存储的值,并判断它是百分比还是绝对点数。

小册子和 N-Up 布局

PdfFileEditor 包含 makeBooklet() 用于以双面小册子顺序排版页面,以及 makeNUp() 用于在每张纸上平铺多个页面。两种方法均接受文件路径或流:

PdfFileEditor editor = new PdfFileEditor();
// Print as a folded booklet
editor.makeBooklet("input.pdf", "booklet.pdf");
// Tile 2 columns × 3 rows per sheet
editor.makeNUp("input.pdf", "nup.pdf", 2, 3);

PdfPageEditor:每页缩放控制

PdfPageEditor 作用于单个页面属性,例如缩放级别。setZoom() 接受一个浮点乘数,用于控制文档在查看器中打开时的初始放大倍率:

PdfPageEditor pageEditor = new PdfPageEditor();
pageEditor.bindPdf("input.pdf");
pageEditor.setZoom(1.5f);
pageEditor.save("zoomed.pdf");

0.0 为负数时会被拒绝——在这些情况下,getZoom() 返回 1.0。这使得 PdfPageEditor 适用于准备必须以特定放大倍率打开的文档,以满足可访问性或自助终端显示的使用场景。

快速入门

添加 Maven 依赖并导入 facades 包:

<dependency>
  <groupId>org.aspose</groupId>
  <artifactId>aspose-pdf-foss</artifactId>
  <version>26.8.0</version>
</dependency>
import org.aspose.pdf.*;
import org.aspose.pdf.facades.*;

Aspose.PDF FOSS 用于 Java 是 MIT 许可证的,并可在 https://github.com/aspose-pdf-foss/Aspose.PDF-FOSS-for-Java 获取。

相关资源