我有一个现有的 PDF,我需要从 PDF 的每一页上删除页脚。
我从 PDF 中获取所有页面,但无法从 PDF 中删除页脚。
我使用的是 iText 版本 5.5.0。
Document document = null;
PdfCopy writer = null;
PdfReader reader = new PdfReader("input.pdf");
int n = reader.getNumberOfPages();
outFile = "output.pdf";
document = new Document();
writer = new PdfCopy(document, new FileOutputStream(outFile));
document.open();
for (int j = 0; j < n; j++) {
PdfImportedPage page = writer.getImportedPage(reader, j);
writer.addPage(page);
}
document.close();
writer.close();
如何从此 input.pdf 文件中删除页脚并创建新的 output.pdf 文件?
您可以使用 pdfSweep 删除文本。参见示例 https://kb.itextpdf.com/home/it7kb/examples/removing-content-with-pdfsweep
或检查已经给出的解决方案
使用 iText PDF 删除 PDF 中的页眉和页脚时出现问题
我的建议是首先避免创建页脚,这样您就可以避免仅仅为了删除页脚而重新创建 pdf
package com.ashok;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Locale;
public class ClipPdf {
public static final String SRC = "/home/ashok/src-file.pdf";
public static final String DEST = "/home/ashok/dest_new.pdf";
public static final String imageFile = "/home/ashok/logo-1.jpeg";
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new ClipPdf().manipulatePdf(SRC, DEST);
}
/*
Parameters:
llx - lower left x // box size right
lly - lower left y // box height up side
urx - upper right x // box width
ury - upper right y // box height down side
*/
public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
PdfReader.unethicalreading = true;
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
int n = reader.getNumberOfPages();
PdfDictionary page;
PdfArray media;
//ImageData data = ImageDataFactory.create(imageFile);
// Image img = new Image(data);
com.itextpdf.text.Image image = com.itextpdf.text.Image.getInstance(imageFile);
/* Page wise */
for (int p = 1; p <= n; p++) {
page = reader.getPageN(p);
media = page.getAsArray(PdfName.CROPBOX);
if (media == null) {
media = page.getAsArray(PdfName.MEDIABOX);
}
/* CROP the area all 4 side */
float llx = media.getAsNumber(0).floatValue() + 1; // footer
float lly = media.getAsNumber(1).floatValue() + 100; // left side
float w = media.getAsNumber(2).floatValue() - media.getAsNumber(0).floatValue() - 5; // right side
float h = media.getAsNumber(3).floatValue() - media.getAsNumber(1).floatValue() - 210; // header
String command = String.format(Locale.ROOT,
"\nq %.2f %.2f %.2f %.2f re W n\nq\n",
llx, lly, w, h);
stamper.getUnderContent(p).setLiteral(command);
stamper.getOverContent(p).setLiteral("\nQ\nQ\n");
PdfContentByte content = stamper.getOverContent(p);
/* Add header image for example */
int indentation = 0;
float scaler = (( reader.getPageSize(p).getWidth() - reader.getPageSize(p).getBorderWidthLeft()
- reader.getPageSize(p).getBorderWidthRight() - indentation) / image.getWidth()) * 100;
image.scalePercent(scaler);
image.setAbsolutePosition(-5f, 760f);
content.addImage(image);
}
stamper.close();
reader.close();
}
}