iText检测文本溢出或复制匹配

iText检测文本溢出或复制匹配,itext,itext7,Itext,Itext7,我需要把一些动态文本放到pdf上。我需要验证文本是否没有溢出允许用于放置文本的边界框 有没有办法检测是否发生了这种情况 当它发生时,我是否可以使用任何适合复制的规则来处理它 谢谢iText5处于维护模式,我建议您使用iText7启动项目iText7目前没有提供现成的机制来进行复制装配,但它可以手动完成,因为布局引擎在iText7中非常灵活。从技术上讲,它也可以在iText5中完成,但我将为iText7for Java提供一个答案,并且转换为C#对您来说应该不是问题 基本思想是利用渲染器的概念

我需要把一些动态文本放到pdf上。我需要验证文本是否没有溢出允许用于放置文本的边界框

  • 有没有办法检测是否发生了这种情况
  • 当它发生时,我是否可以使用任何适合复制的规则来处理它

谢谢

iText5
处于维护模式,我建议您使用
iText7
启动项目
iText7
目前没有提供现成的机制来进行复制装配,但它可以手动完成,因为布局引擎在
iText7
中非常灵活。从技术上讲,它也可以在
iText5
中完成,但我将为
iText7
for Java提供一个答案,并且转换为C#对您来说应该不是问题

基本思想是利用渲染器的概念,尝试在给定区域中使用不同的字体大小来布局段落,直到找到适合您的大小。二进制搜索方法非常适合这里

PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outFileName));
Document doc = new Document(pdfDoc);

String text = "...";
Rectangle area = new Rectangle(100, 100, 200, 200);
// Just draw a black box around to verify the result visually
new PdfCanvas(pdfDoc.addNewPage()).rectangle(area).setStrokeColor(Color.BLACK).stroke();

Paragraph p = new Paragraph(text);
IRenderer renderer = p.createRendererSubTree().setParent(doc.getRenderer());
LayoutArea layoutArea = new LayoutArea(1, area);

// lFontSize means the font size which will be definitely small enough to fit all the text into the box.
// rFontSize is the maximum bound for the font size you want to use. The only constraint is that it should be greater than lFontSize
// Set rFontSize to smaller value if you don't want the font size to scale upwards
float lFontSize = 0.0001f, rFontSize = 10000;

// Binary search. Can be replaced with while (Math.abs(lFontSize - rFontSize) < eps). It is a matter of implementation/taste
for (int i = 0; i < 100; i++) {
    float mFontSize = (lFontSize + rFontSize) / 2;
    p.setFontSize(mFontSize);
    LayoutResult result = renderer.layout(new LayoutContext(layoutArea));
    if (result.getStatus() == LayoutResult.FULL) {
        lFontSize = mFontSize;
    } else {
        rFontSize = mFontSize;
    }
}

// lFontSize is guaranteed to be small enough to fit all the text. Using it.
float finalFontSize = lFontSize;
System.out.println("Final font size: " + finalFontSize);
p.setFontSize(finalFontSize);
// We need to layout the final time with the final font size set.
renderer.layout(new LayoutContext(layoutArea));
renderer.draw(new DrawContext(pdfDoc, new PdfCanvas(pdfDoc.getPage(1))));

doc.close();
视觉效果:


iText5
处于维护模式,我建议您使用
iText7
启动项目
iText7
目前没有提供现成的机制来进行复制装配,但它可以手动完成,因为布局引擎在
iText7
中非常灵活。从技术上讲,它也可以在
iText5
中完成,但我将为
iText7
for Java提供一个答案,并且转换为C#对您来说应该不是问题

基本思想是利用渲染器的概念,尝试在给定区域中使用不同的字体大小来布局段落,直到找到适合您的大小。二进制搜索方法非常适合这里

PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outFileName));
Document doc = new Document(pdfDoc);

String text = "...";
Rectangle area = new Rectangle(100, 100, 200, 200);
// Just draw a black box around to verify the result visually
new PdfCanvas(pdfDoc.addNewPage()).rectangle(area).setStrokeColor(Color.BLACK).stroke();

Paragraph p = new Paragraph(text);
IRenderer renderer = p.createRendererSubTree().setParent(doc.getRenderer());
LayoutArea layoutArea = new LayoutArea(1, area);

// lFontSize means the font size which will be definitely small enough to fit all the text into the box.
// rFontSize is the maximum bound for the font size you want to use. The only constraint is that it should be greater than lFontSize
// Set rFontSize to smaller value if you don't want the font size to scale upwards
float lFontSize = 0.0001f, rFontSize = 10000;

// Binary search. Can be replaced with while (Math.abs(lFontSize - rFontSize) < eps). It is a matter of implementation/taste
for (int i = 0; i < 100; i++) {
    float mFontSize = (lFontSize + rFontSize) / 2;
    p.setFontSize(mFontSize);
    LayoutResult result = renderer.layout(new LayoutContext(layoutArea));
    if (result.getStatus() == LayoutResult.FULL) {
        lFontSize = mFontSize;
    } else {
        rFontSize = mFontSize;
    }
}

// lFontSize is guaranteed to be small enough to fit all the text. Using it.
float finalFontSize = lFontSize;
System.out.println("Final font size: " + finalFontSize);
p.setFontSize(finalFontSize);
// We need to layout the final time with the final font size set.
renderer.layout(new LayoutContext(layoutArea));
renderer.draw(new DrawContext(pdfDoc, new PdfCanvas(pdfDoc.getPage(1))));

doc.close();
视觉效果:


您使用的是iText5还是iText7?标签itext通常指前者。还有,java还是.Net?这两个版本都有一些模拟内容放置的方法,但答案取决于版本:)“我需要将一些动态文本放到pdf上”-如何做到这一点?是否要填写AcroForm文本字段?还是要添加FreeText(FreeTextTypeWriter)批注?还是要添加到页面内容流?定义“动态文本”。是运行程序前不知道长度的文本吗?还是作为mkl帖子?@SamuelHuylebroeck我还没有选择一个版本,我想既然这是一个新项目,我可以使用iText7。它将是C#@mkl,实际上我不确定。我自己创建的PDF,所以我可以做任何工作最好的。最终结果将是页面上的文本,而不是批注。您使用的是iText5还是iText7?标签itext通常指前者。还有,java还是.Net?这两个版本都有一些模拟内容放置的方法,但答案取决于版本:)“我需要将一些动态文本放到pdf上”-如何做到这一点?是否要填写AcroForm文本字段?还是要添加FreeText(FreeTextTypeWriter)批注?还是要添加到页面内容流?定义“动态文本”。是运行程序前不知道长度的文本吗?还是作为mkl帖子?@SamuelHuylebroeck我还没有选择一个版本,我想既然这是一个新项目,我可以使用iText7。它将是C#@mkl,实际上我不确定。我自己创建的PDF,所以我可以做任何工作最好的。最终结果将是页面上的文本,而不是注释。