使用itext生成pdf的文本重叠问题

时间:2020-12-07 22:22:48

hi everyone i have a header text(ARTIST Biography(Continue)) in my pdf file generated by using itext(Java).where 'ARTIST' and '(Continue)' are static text 'Biography' is a dynamic text it will be varied with respect to the content fetched from xml.My problem is while the dynamic text length is too long it will overlap the '(continue)' text.How can i set the dynamic text length with respect to the nearest static text'(Continue)'.please reply Thanks in advance

大家好我在使用itext(Java)生成的pdf文件中有一个标题文本(ARTIST传记(继续))。其中'ARTIST'和'(继续)'是静态文本'传记'是一个动态文本,它将变化关于从xml获取的内容。我的问题是,当动态文本长度太长时,它将重叠'(继续)'文本。如何设置相对于最近的静态文本的动态文本长度'(继续) '。请回复在此先感谢

My code for displaying the header text is (ARTIST Biography(Continue)) written bellow

我用于显示标题文本的代码是(ARTIST Biography(Continue))

PdfContentByte cb = writer.getDirectContent();
        BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        cb.setFontAndSize(bf, 18);
        cb.beginText();
        cb.setColorFill(Color.BLACK);
        cb.showTextAligned(PdfContentByte.ALIGN_LEFT,"ARTIST", document.left(), document.top() + 20  , 0);
        cb.resetRGBColorFill();
        cb.endText();
        cb.beginText();
        cb.setColorFill(Color.RED);
        cb.showTextAligned(PdfContentByte.ALIGN_LEFT, strBiography, document.left() + 70, document.top() + 20, 0);
        cb.resetRGBColorFill();
        cb.endText();
        cb.beginText();
        cb.setColorFill(Color.BLACK);
        cb.showTextAligned(PdfContentByte.ALIGN_LEFT,"(Continue)", document.left()+90, document.top() + 20  , 0);
        cb.resetRGBColorFill();
        cb.endText();
        cb.stroke();

1 个解决方案

#1


0  

You can create Chunk with the right font and content of strBiography and on the Chunk call getWidthPoint to now it's size in points, you'll have about the length of your strBiography when added to the doc and can calculate the position of "(Continue)"

您可以使用strBiography的正确字体和内容创建Chunk,并在Chunk上调用getWidthPoint现在它的大小为点数,当您添加到文档时,您将获得strBiography的长度,并且可以计算“(继续)”的位置“

#1


0  

You can create Chunk with the right font and content of strBiography and on the Chunk call getWidthPoint to now it's size in points, you'll have about the length of your strBiography when added to the doc and can calculate the position of "(Continue)"

您可以使用strBiography的正确字体和内容创建Chunk,并在Chunk上调用getWidthPoint现在它的大小为点数,当您添加到文档时,您将获得strBiography的长度,并且可以计算“(继续)”的位置“