使用iText7

时间:2018-12-07 14:00:28

标签: java annotations highlight itext7 appearance

我有一个PDF查看器,如果没有外观流,则不显示突出显示。我正在尝试使用Java中的iText 7核心尝试将突出显示注释添加到PDF,但是这些注释没有外观流,因此我希望在将注释写入PDF文件时尝试自己添加它们。

我遇到了this的旧答案,但这是针对C#和iText 5的,我似乎无法弄清楚如何在iText 7中复制它并获得成功。

因此,我的问题是:如何在正在工作的iText 7核心中的突出显示注释上设置外观流?

我对代码最了解的内容如下所示。我正在使用RegexBasedLocationExtrationStrategy类来查找pdf中所有搜索词的位置。

RegexBasedLocationExtractionStrategy evntlstnr = new RegexBasedLocationExtractionStrategy(pattern);
for (int pIdx = 0; pIdx < pdfDoc.getNumberOfPages(); ++pIdx) {
    final PdfPage page = pdfDoc.getPage(pIdx + 1);
    new PdfCanvasProcessor(evntlstnr).processPageContent(page);
    Collection<IPdfTextLocation> locations =  evntlstnr.getResultantLocations();
    for (IPdfTextLocation location : locations) {
        Rectangle rect = location.getRectangle();
        // Specify quad points in Z-like order
        // [0,1] x1,y1   [2,3] x2,y2
        // [4,5] x3,y3   [6,7] x4,y4
        float[] quads = new float[8];
        quads[0] = rect.getX();
        quads[1] = rect.getY() + rect.getHeight();
        quads[2] = rect.getX() + rect.getWidth();
        quads[3] = quads[1];
        quads[4] = quads[0];
        quads[5] = rect.getY();
        quads[6] = quads[2];
        quads[7] = quads[5];

        Color highlightColor = new DeviceRgb(0f, 0f, 1f);
        PdfTextMarkupAnnotation highlight = PdfTextMarkupAnnotation.createHighLight(rect, quads);
        highlight.setColor(highlightColor);


        Rectangle appearRect = new Rectangle(0f, 0f, rect.getWidth(), rect.getHeight());                            
        PdfFormXObject appearObj = new PdfFormXObject(appearRect);                                                        
        final PdfResources appearRes = appearObj.getResources();

        PdfExtGState extGState = new PdfExtGState();
        extGState.setBlendMode(PdfExtGState.BM_MULTIPLY);
        appearRes.addExtGState(extGState);
        appearObj.setBBox(new PdfArray(new float[] {0f, 0f, rect.getWidth(), rect.getHeight()}));

        PdfShading appearShading = new PdfShading.Axial(highlightColor.getColorSpace(), 0f, 0f, highlightColor.getColorValue(), 1f, 1f, highlightColor.getColorValue());                                                     
        appearRes.addShading(appearShading);

        appearRes.addColorSpace(highlightColor.getColorSpace());                            

        PdfAnnotationAppearance appearance = new PdfAnnotationAppearance(appearObj.getPdfObject());
        highlight.setNormalAppearance(appearance);
        highlight.setFlag(PdfAnnotation.PRINT);
        page.addAnnotation(highlight);
    }
}

1 个答案:

答案 0 :(得分:1)

使用塞缪尔的答案,我迷失了方向。

我对PDF标准和这个框架(iText)没什么期望,但是基于下面的工作示例,我的假设是我试图为加亮显示的矩形是一种粗略的后备方法当查看器无法渲染注释时(因为它们没有外观流),“伪造”突出显示的矩形。意识到这两个操作没有链接,我来到下面显示的工作示例。希望这对将来有帮助。

RegexBasedLocationExtractionStrategy evntlstnr = new RegexBasedLocationExtractionStrategy(pattern);
for (int pIdx = 0; pIdx < pdfDoc.getNumberOfPages(); ++pIdx) {
    final PdfPage page = pdfDoc.getPage(pIdx + 1);
    new PdfCanvasProcessor(evntlstnr).processPageContent(page);
    Collection<IPdfTextLocation> locations =  evntlstnr.getResultantLocations();
    for (IPdfTextLocation location : locations) {
        Rectangle rect = location.getRectangle();
        // Specify quad points in Z-like order
        // [0,1] x1,y1   [2,3] x2,y2
        // [4,5] x3,y3   [6,7] x4,y4
        float[] quads = new float[8];
        quads[0] = rect.getX();
        quads[1] = rect.getY() + rect.getHeight();
        quads[2] = rect.getX() + rect.getWidth();
        quads[3] = quads[1];
        quads[4] = quads[0];
        quads[5] = rect.getY();
        quads[6] = quads[2];
        quads[7] = quads[5];

        Color highlightColor = new DeviceRgb(1f, 1f, 0f);                                                                                                                           

        PdfTextMarkupAnnotation highlight = PdfTextMarkupAnnotation.createHighLight(rect, quads);
        highlight.setColor(highlightColor); 
        highlight.setFlag(PdfAnnotation.PRINT);
        page.addAnnotation(highlight);                            

        PdfCanvas canvas = new PdfCanvas(page);
        PdfExtGState extGState = new PdfExtGState();
        extGState.setBlendMode(PdfExtGState.BM_MULTIPLY);
        canvas.setExtGState(extGState);
        canvas.rectangle(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
        canvas.setFillColor(highlightColor);
        canvas.fill();                            
        canvas.release();                                                        
    }
}