Highlight words in a pdf using itextsharp, not displaying highlighted word in browser

前端 未结 2 1576
星月不相逢
星月不相逢 2020-11-30 13:19

Highlighted words are not displaying in browser using itextsharp.

Adobe

Browser

CODE

2条回答
  •  佛祖请我去吃肉
    2020-11-30 13:52

    You are using a Markup annotation to highlight text. That's great! There's nothing wrong with your code, nor with iText. However: not all PDF viewers support that functionality.

    If you want to see highlighted text in every PDF viewer, a (sub-optimal) workaround could be to add a yellow rectangle to the content stream under the existing content (assuming that the existing content isn't opaque).

    This is demonstrated in the HighLightByAddingContent example:

    public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
        PdfReader reader = new PdfReader(src);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
        PdfContentByte canvas = stamper.getUnderContent(1);
        canvas.saveState();
        canvas.setColorFill(BaseColor.YELLOW);
        canvas.rectangle(36, 786, 66, 16);
        canvas.fill();
        canvas.restoreState();
        stamper.close();
        reader.close();
    }
    

    In this example, we take a file named hello.pdf and we add a yellow rectangle, with the file hello_highlighted.pdf as result.

    Note that you won't see the yellow rectangle if you add it under an opaque shape (e.g. under an image). In that case, you could add a transparent rectangle on top of the existing content.

    Update: my example was written in Java. It shouldn't be a problem for a developer to port this to C#. It's only a matter of changing some lower-cases into upper-cases. E.g. stamper.GetUnderContent(1) instead of stamper.getUnderContent(1), canvas.SaveState() instead of canvas.saveState(), and so on.

提交回复
热议问题