使用iText PDF删除PDF中的页眉和页脚的问题 [英] Issue in Removing Header and Footer in PDF using iText PDF

查看:227
本文介绍了使用iText PDF删除PDF中的页眉和页脚的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用itext-xtra-5.5.6 api删除/清理页眉和页脚.

I am using itext-xtra-5.5.6 api to remove/cleanup the header and footer.

这是代码

//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时,则代码正在只删除预期的页眉和页脚.

When I run the code to remove header and footer for a PDF file with 1440 pages with upperY=65 and lowerY=65 then the code is deleting all the content from the page but when upperY=65 and lowerY=45 then code is deleting just the header and footer which is expected.

另一个问题是DefaultClipper类中某些页面的空指针异常

Also another issue is Null pointer exception for some pages in the DefaultClipper class

private void fixupFirstLefts2( OutRec OldOutRec, OutRec NewOutRec ) {
    for (final OutRec outRec : polyOuts) {
        if (outRec.firstLeft.equals( OldOutRec )) {
            outRec.firstLeft = NewOutRec;
        }
    }
}

在polyOuts中-> outRec.firstLeft为null,因此outRec.firstLeft.equals方法将引发Null指针异常.

in polyOuts -> outRec.firstLeft is null so outRec.firstLeft.equals method throws the Null pointer exception.

异常堆栈跟踪

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个问题.

not sure where i am doing mistake. I even tried to see if the pdf pages contain images or other types but the pages are just text based. Please help resolve 2 issues.

推荐答案

关于OP的观察,当运行带有OP的代码时,确实抛出了OP提出的异常样本文件以及iText和iText-xtra 5.5.6.此外,结果PDF上的页面为空.

Concerning the observations by the OP, an exception like the one presented by the OP indeed is thrown when running the OP's code with his sample file and iText and iText-xtra 5.5.6. Furthermore, the page on which this happens is empty in the result PDF.

导致异常的原因确实是一些错误,而导致空白页的原因是,每个处理过的页的清理代码首先删除了先前的内容,然后开始构建新的内容.如果像处理当前情况一样在处理页面的早期发生异常,则结果可能是空白页面.

The cause for the exception indeed is some bug, and the cause for the empty page is that the cleanup code for each processed page first removes the former content and then starts building the new content; if an exception occurs early while processing the page as in the case at hand, the result can be an empty page.

尽管与此同时,该错误已修复,但在当前的5.5.7开发快照中,该异常不再发生.

Meanwhile, though, the bug has been fixed, in a current 5.5.7 development snapshot the exception does not occur anymore.

发生了另一种不想要的效果:OP的示例文档包含一些旋转的页面,例如第18页:

A different unwanted effect occurs, though: the OP's sample document contains some rotated pages, e.g. page 18:

按原样应用代码,将得到:

Applying the code as is to it, one gets:

这样做的原因是 PdfStamper 通常会尝试将旋转的纵向页面视为真实的横向页面.由于 PdfCleanUpProcessor 尝试的是旋转感知,因此导致混乱

The reason for this is that the PdfStamper usually tries to treat rotated portrait pages as if they were true landscape pages.As the PdfCleanUpProcessor tries is rotation-unaware, this results in mayhem

但是,可以使用 setRotateContents 设置器告诉它不要这样做:

One can tell it not to do so, though, using the setRotateContents setter:

    ...
    PdfStamper stamper = new PdfStamper(reader, outputStream);
    stamper.setRotateContents(false);
    List<PdfCleanUpLocation> cleanUpLocations = new ArrayList<PdfCleanUpLocation>();
    ...

此更新的代码现在产生:

This updated code now produces:

这篇关于使用iText PDF删除PDF中的页眉和页脚的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆