简体   繁体   English

使用iText java为整个pdf文档选择文本和背景

[英]Select text and background for entire pdf document using iText java

I'm looking to take a pdf file, set the background colour for every page to black, and all the text to white. 我想要一个pdf文件,将每个页面的背景颜色设置为黑色,将所有文本设置为白色。

What's the easiest way for me to do this? 对我来说最简单的方法是什么? Is there an api call to select every page's background and all the text in the file? 是否有api调用选择每个页面的背景和文件中的所有文本? Or do I have to iterate through each page somehow? 或者我必须以某种方式遍历每个页面?

As mentioned in my last comment to your question, painting a white rectangle in blend mode Difference should do the job as long as inverting all colors is a sufficient solution for your task: 正如我在上一篇评论中提到的那样,在混合模式下绘制一个白色矩形只要反转所有颜色是您的任务的充分解决方案, 差异就可以完成工作:

void invert(File source, File target) throws IOException, DocumentException
{
    PdfReader reader = new PdfReader(source.getPath());
    OutputStream os = new FileOutputStream(target);
    PdfStamper stamper = new PdfStamper(reader, os);
    invert(stamper);
    stamper.close();
    os.close();
}

void invert(PdfStamper stamper)
{
    for (int i = stamper.getReader().getNumberOfPages(); i>0; i--)
    {
        invertPage(stamper, i);
    }
}

void invertPage(PdfStamper stamper, int page)
{
    Rectangle rect = stamper.getReader().getPageSize(page);

    PdfContentByte cb = stamper.getOverContent(page);
    PdfGState gs = new PdfGState();
    gs.setBlendMode(PdfGState.BM_DIFFERENCE);
    cb.setGState(gs);
    cb.setColorFill(new GrayColor(1.0f));
    cb.rectangle(rect.getLeft(), rect.getBottom(), rect.getWidth(), rect.getHeight());
    cb.fill();

    cb = stamper.getUnderContent(page);
    cb.setColorFill(new GrayColor(1.0f));
    cb.rectangle(rect.getLeft(), rect.getBottom(), rect.getWidth(), rect.getHeight());
    cb.fill();
}

invertPage does draw the afore mentioned white reactangle in blend mode Difference over the page. invertPage确实绘制混合模式的差异页面上述提到的白reactangle。 Furthermore it paints a white rectangle normally under the page; 此外,它通常页面下方绘制一个白色矩形; that turned out to be necessary at least for the Acrobat Reader version I have at hands here. 至少对于我手边的Acrobat Reader版本来说,这是必要的。

You might want to tweak the code somewhat to make the result better to read. 您可能需要稍微调整代码以使结果更好地阅读。 Maybe the blend mode Exclusion ( BM_EXCLUSION ) is more appropriate, or maybe some other graphic state tweaks improve your reading experience. 也许混合模式排除BM_EXCLUSION )更合适,或者其他一些图形状态调整可能会改善您的阅读体验。 Just be creative! 要有创意! ;) ;)

For some backgrounds on the PDF blending mode you might want to read section 11.3.5 Blend Mode in the PDF specification ISO 32000-1 and study the transparency related examples of iText in Action — 2nd Edition . 对于PDF混合模式中的某些背景,您可能需要阅读PDF规范ISO 32000-1中的第11.3.5节混合模式 ,并研究iText in Action - 2nd Edition中透明度相关的示例

PS: This code only inverts the page content. PS:此代码仅反转页面内容。 Annotations are not affected. 注释不受影响。 If that turns out to be necessary, you might do something similar to their appearance streams. 如果事实证明是必要的,你可能会做一些类似于他们的外观流的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM