我使用itext-xtra-5.5.6 api来删除/清理页眉和页脚。
这是代码
//removes header and footer based on the configuration
public static void cleanUpContent(String inPDFFile,String targetPDFFile,PDFConfig pdfConfig) throws Exception{
PdfReader reader = new PdfReader(inPDFFile);
OutputStream outputStream = new FileOutputStream(targetPDFFile);
float upperY=pdfConfig.getPdfUpperY();
float lowerY=pdfConfig.getPdfLowerY();
boolean highLightColor=pdfConfig.isPdfHighLightClippedTextColor();
PdfStamper stamper = new PdfStamper(reader, outputStream);
List<PdfCleanUpLocation> cleanUpLocations = new ArrayList<PdfCleanUpLocation>();
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
Rectangle pageRect = reader.getCropBox(i);
Rectangle headerRect= new Rectangle(pageRect);
headerRect.setBottom(headerRect.getTop()-upperY);
Rectangle footerRect= new Rectangle(pageRect);
footerRect.setTop(footerRect.getBottom()+lowerY);
if(highLightColor){
cleanUpLocations.add(new PdfCleanUpLocation(i, headerRect,BaseColor.GREEN));
cleanUpLocations.add(new PdfCleanUpLocation(i, footerRect,BaseColor.GREEN));
}else{
cleanUpLocations.add(new PdfCleanUpLocation(i, headerRect));
cleanUpLocations.add(new PdfCleanUpLocation(i, footerRect));
}
}
PdfCleanUpProcessor cleaner = new PdfCleanUpProcessor(cleanUpLocations, stamper);
try{
cleaner.cleanUp();
}catch(Exception e){
e.printStackTrace();
}
stamper.close();
reader.close();
outputStream.flush();
outputStream.close();
}
当我运行代码以删除包含1440页,其中upperY = 65且lowerY = 65的PDF文件的页眉和页脚时,代码将删除页面中的所有内容,但是当upperY = 65且lowerY = 45时代码正在删除预期的页眉和页脚。
另一个问题是DefaultClipper类
中某些页面的Null指针异常private void fixupFirstLefts2( OutRec OldOutRec, OutRec NewOutRec ) {
for (final OutRec outRec : polyOuts) {
if (outRec.firstLeft.equals( OldOutRec )) {
outRec.firstLeft = NewOutRec;
}
}
}
polyOuts中的- &gt; outRec.firstLeft为null,因此outRec.firstLeft.equals方法抛出Null指针异常。
异常堆栈跟踪
java.lang.NullPointerException
at com.itextpdf.text.pdf.parser.clipper.DefaultClipper.fixupFirstLefts2(DefaultClipper.java:1463)
at com.itextpdf.text.pdf.parser.clipper.DefaultClipper.joinCommonEdges(DefaultClipper.java:2121)
at com.itextpdf.text.pdf.parser.clipper.DefaultClipper.executeInternal(DefaultClipper.java:1420)
at com.itextpdf.text.pdf.parser.clipper.DefaultClipper.execute(DefaultClipper.java:1362)
at com.itextpdf.text.pdf.pdfcleanup.PdfCleanUpRegionFilter.filterFillPath(PdfCleanUpRegionFilter.java:174)
at com.itextpdf.text.pdf.pdfcleanup.PdfCleanUpRenderListener.filterCurrentPath(PdfCleanUpRenderListener.java:402)
at com.itextpdf.text.pdf.pdfcleanup.PdfCleanUpRenderListener.renderPath(PdfCleanUpRenderListener.java:232)
at com.itextpdf.text.pdf.parser.PdfContentStreamProcessor.paintPath(PdfContentStreamProcessor.java:377)
at com.itextpdf.text.pdf.parser.PdfContentStreamProcessor.access$6300(PdfContentStreamProcessor.java:60)
at com.itextpdf.text.pdf.parser.PdfContentStreamProcessor$PaintPath.invoke(PdfContentStreamProcessor.java:1183)
at com.itextpdf.text.pdf.pdfcleanup.PdfCleanUpContentOperator.invoke(PdfCleanUpContentOperator.java:138)
at com.itextpdf.text.pdf.parser.PdfContentStreamProcessor.invokeOperator(PdfContentStreamProcessor.java:286)
at com.itextpdf.text.pdf.parser.PdfContentStreamProcessor.processContent(PdfContentStreamProcessor.java:429)
at com.itextpdf.text.pdf.parser.PdfContentStreamProcessor$FormXObjectDoHandler.handleXObject(PdfContentStreamProcessor.java:1252)
at com.itextpdf.text.pdf.parser.PdfContentStreamProcessor.displayXObject(PdfContentStreamProcessor.java:352)
at com.itextpdf.text.pdf.parser.PdfContentStreamProcessor.access$6100(PdfContentStreamProcessor.java:60)
at com.itextpdf.text.pdf.parser.PdfContentStreamProcessor$Do.invoke(PdfContentStreamProcessor.java:988)
at com.itextpdf.text.pdf.pdfcleanup.PdfCleanUpContentOperator.invoke(PdfCleanUpContentOperator.java:138)
at com.itextpdf.text.pdf.parser.PdfContentStreamProcessor.invokeOperator(PdfContentStreamProcessor.java:286)
at com.itextpdf.text.pdf.parser.PdfContentStreamProcessor.processContent(PdfContentStreamProcessor.java:429)
at com.itextpdf.text.pdf.pdfcleanup.PdfCleanUpProcessor.cleanUpPage(PdfCleanUpProcessor.java:160)
at com.itextpdf.text.pdf.pdfcleanup.PdfCleanUpProcessor.cleanUp(PdfCleanUpProcessor.java:135)
不确定我在哪里做错了。我甚至试图查看pdf页面是否包含图像或其他类型,但页面只是基于文本。请帮助解决2个问题。