在 Java 中删除 PDF 页眉和页脚
Contents
[
Hide
]
在 Java 中删除 PDF 第一页的页眉和页脚伪影。
删除页眉和页脚工件
当需要从页面中删除现有的页眉和页脚伪影时,请使用此方法。
- 使用 Document 打开源 PDF.
- 倒序遍历页面工件集合。
- 删除子类型为页眉或页脚的分页工件,然后保存文档。
public static void deleteHeaderFooterArtifact(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
for (int i = document.getPages().get_Item(1).getArtifacts().size(); i >= 1; i--) {
Artifact artifact = document.getPages().get_Item(1).getArtifacts().get_Item(i);
if (artifact.getType() == Artifact.ArtifactType.Pagination
&& (artifact.getSubtype() == Artifact.ArtifactSubtype.Header
|| artifact.getSubtype() == Artifact.ArtifactSubtype.Footer)) {
document.getPages().get_Item(1).getArtifacts().delete(artifact);
}
}
document.save(outputFile.toString());
}
}