使用iText java为整个pdf文档选择文本和背景 [英] Select text and background for entire pdf document using iText java

查看:178
本文介绍了使用iText java为整个pdf文档选择文本和背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



最简单的方法是什么?最简单的方法是什么?我做这个?是否有一个API调用来选择每个页面的背景和文件中的所有文本?或者我必须以某种方式遍历每个页面?

解决方案

正如我在你的问题的最后一个评论中提到的,画一个白色的矩形在混合模式差异应该做的工作,只要反转所有的颜色是一个足够的解决方案为您的任务:

  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);
翻转(压模);
stamper.close();
os.close();
}

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


$ b 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();

code

$ c $ invert>
前面提到的混合模式Difference over 页面上的白色矩形。此外,它通常在页面下绘制一个白色矩形,至少在我手上的Acrobat Reader版本中是必需的。



您可能需要稍微调整代码以使结果更好地阅读。也许混合模式 Exclusion BM_EXCLUSION )更合适,或者其他图形状态调整可以改善您的阅读体验。只是有创意! ;)

对于PDF混合模式中的某些背景,您可能需要阅读混合模式 http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/PDF32000_2008.pdfrel =nofollow> PDF规范ISO 32000-1 ,研究透明度相关示例 of



PS:此代码仅反转页面内容。注释不受影响。如果事实证明是必要的,你可能会做一些类似于他们的外观流。


I'm looking to take a pdf file, set the background colour for every page to black, and all the text to white.

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? 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. 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.

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. 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.

PS: This code only inverts the page content. Annotations are not affected. If that turns out to be necessary, you might do something similar to their appearance streams.

这篇关于使用iText java为整个pdf文档选择文本和背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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