TextMarginFinder验证可印刷性 [英] TextMarginFinder to verify printability

查看:489
本文介绍了TextMarginFinder验证可印刷性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用TextMarginFinder来证明打印时奇数页和偶数页正确备份。我的代码基于:
http://itextpdf.com/examples/ iia.php?id = 280

I am attempting to use TextMarginFinder to prove that odd and even pages back up correctly when printing. I have based my code on: http://itextpdf.com/examples/iia.php?id=280

我遇到的问题是在奇数页面上我正在寻找左边对齐的方框显示1CM例如,后边距,在偶数页上,我希望页面框与右边对齐,同时显示1CM后边距。即使在上面的例子中也不是这样,但是在打印时,文本会完全备份,因为Trim Box符合。

The issue I have is that on odd pages I am looking for the box to be aligned to the left showing a 1CM back margin for example, and on an even page I would expect the page box to be aligned to the right also showing a 1CM back margin. Even in the example above this is not the case, but when printed the text does back up perfectly because the Trim Box conforms.

总之,我相信某些PDF文件TextMarginFinder错误地定位文本宽度,通常在偶数页面上。宽度大于实际文本就可以看出这一点。如果Media Box区域外有slug标记,通常会出现这种情况。

In summary I believe on certain PDF files the TextMarginFinder is incorrectly locating the text width, usually on Even pages. This is evident by the width being greater than the actual text. This is usually the case if there are slug marks outside of the Media Box area.

推荐答案

在OP指出的PDF中(从iText样本中 margins.pdf 确实框不与文本齐平

In the PDF the OP pointed to (margins.pdf from the iText samples themselves) indeed the box is not flush with the text:

但是,如果查看PDF内容,您会看到许多行都有尾随空格字符,例如第一行:

If you look into the PDF Content, though, you'll see that many of the lines have a trailing space character, e.g. the first line:

(s I have worn out since I started my ) Tj

这些尾随空格字符是文本的一部分,因此,该框不会与可见文本齐平,但它与包含此类空格的文本一起字符。

These trailing space characters are part of the text and, therefore, the box does not flush with the visible text but it does with the text including such space characters.

如果你想忽略这些空格字符,你可以尝试通过过滤这些尾随空格(或者为了简单起见所有空格)来获取它们进入 TextMarginFinder 。要做到这一点,我会以字符方式爆炸 TextRenderInfo 实例,然后过滤那些修剪为空字符串的实例。

If you want to ignore such space characters, you can try doing so by filtering such trailing spaces (or for the sake of simplicity all spaces) before they get fed into the TextMarginFinder. To do this I'd explode the TextRenderInfo instances character-wise and then filter those which trim to empty strings.

一个用于分解渲染信息对象的辅助类:

A helper class to explode the render info objects:

import com.itextpdf.text.pdf.parser.ImageRenderInfo;
import com.itextpdf.text.pdf.parser.RenderListener;
import com.itextpdf.text.pdf.parser.TextRenderInfo;

public class TextRenderInfoSplitter implements RenderListener
{
    public TextRenderInfoSplitter(RenderListener strategy) {
        this.strategy = strategy;
    }

    public void renderText(TextRenderInfo renderInfo) {
        for (TextRenderInfo info : renderInfo.getCharacterRenderInfos()) {
            strategy.renderText(info);
        }
    }

    public void beginTextBlock() {
        strategy.beginTextBlock();
    }

    public void endTextBlock() {
        strategy.endTextBlock();
    }

    public void renderImage(ImageRenderInfo renderInfo) {
        strategy.renderImage(renderInfo);
    }

    final RenderListener strategy;
}

使用此帮助程序,您可以像这样更新iText示例:

Using this helper you can update the iText sample like this:

RenderFilter spaceFilter = new RenderFilter() {
    public boolean allowText(TextRenderInfo renderInfo) {
        return renderInfo != null && renderInfo.getText().trim().length() > 0;
    }
};

PdfReader reader = new PdfReader(src);
PdfReaderContentParser parser = new PdfReaderContentParser(reader);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
    TextMarginFinder finder = new TextMarginFinder();
    FilteredRenderListener filtered = new FilteredRenderListener(finder, spaceFilter);
    parser.processContent(i, new TextRenderInfoSplitter(filtered));
    PdfContentByte cb = stamper.getOverContent(i);
    cb.rectangle(finder.getLlx(), finder.getLly(), finder.getWidth(), finder.getHeight());
    cb.stroke();
}
stamper.close();
reader.close();

结果:

如果是slug区域文本等你可能想要过滤更多,例如裁剪框外的任何东西。

In case of slug area text etc you might want to filter more, e.g. anything outside the crop box.

但要注意,可能存在空格字符不可见的字体,例如盒装字符的字体。在这种情况下取​​出等式中的空格是错误的。

Beware, though, there might be fonts in which the space character is not invisible, e.g. a font of boxed characters. Taking the spaces out of the equation in that case would be wrong.

这篇关于TextMarginFinder验证可印刷性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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