简体   繁体   English

使用iText在Java中生成PDF。 支持自由文本

[英]Generating PDFs in Java using iText. Support for Free flowing text

We have pdf templates of the following nature that need to be generated by a web application: 我们具有以下性质的pdf模板,这些模板需要由Web应用程序生成:

Sample Paragraph: 示例段落:

Dear {customer.name}, 尊敬的{customer.name},

Your lawyer, {customer.lawyer.name} has contacted us about your account, {customer.account.number} requesting immediate closure of the account. 您的律师{customer.lawyer.name}已就您的帐户{customer.account.number}与我们联系,要求立即关闭该帐户。

... ...

The {...} fields mentioned above are to accommodate the various acro fields that would be put in as placeholders, so that they can be populated with data. 上面提到的{...}字段用于容纳将作为占位符放入的各种acro字段,以便可以使用数据填充它们。

But the problem is that the {customer.lawyer.name} field can be of varying lengths, 10 characters to 50 characters, 但是问题是{customer.lawyer.name}字段的长度可以变化,从10个字符到50个字符,

Using iText, how can we generate a pdf for the above template such that varying lengths of the variable can be accommodated? 使用iText,我们如何为上述模板生成pdf,以便可以适应变量的不同长度? Maybe even wrap the text around appropriately? 甚至还可以适当地环绕文字?

I used iText columns to layout letters. 我使用iText列来布局字母。 Here is an example from my work. 这是我工作的一个例子。 Note that I've only just typed this into Stack Overflow, I've not compiled or tested it. 请注意,我只是将其键入Stack Overflow中,没有进行编译或测试。

Imagine a file where two newlines in a row indicate a paragraph, the text is otherwise wrapped by the text editor. 想象一个文件,其中连续两个换行符指示一个段落,否则文本将由文本编辑器包装。 The following class has a method named generate that reads the file and emits a US Letter sized PDF with a 1 inch margin at top and bottom and 1 1/2 inch margins on each side. 下列类具有一个名为generate的方法,该方法读取文件并发出US Letter大小的PDF,顶部和底部的边距为1英寸,每边的边距为1 1/2英寸。

/** Generates a letter. */
public class LetterGenerator
{
    /** One inch is 72 points. */
    private final static int INCH = 72;

    /** Generate a letter. */
    public void generate() throws DocumentException, IOException
    {
        BufferedReader in = new BufferedReader(new FileReader("letter.txt"));
        Document document = new Document(PageSize.LETTER);
        PdfWriter.getInstance(document, new FileOutputStream("letter.pdf"));
        PdfContentByte cb = writer.getDirectContent();
        ColumnText ct = new ColumnText(cb);
        String para;
        int spacingBefore = 0;
        while ((para = in.readLine()) != null)
        {
            line = line.trim();
            if (line.length() != 0)
            {
                // Shekhar, do your place-holder replacement here.
                Paragraph p = new Paragraph(line);
                p.setSpacingBefore(spacingBefore);
                ct.addElement(p);
                spacingBefore = 8;
            }
        }
        ct.setSimpleColumn(INCH, INCH * 1.5f, 7.5f * INCH, 9.5f * INCH);
        int status = ColumnText.START_COLUMN;
        while ((status & ColumnText.NO_MORE_TEXT) == 0)
        {   
            status = ct.go();
            ct.setYLine(PageSize.LETTER.getHeight() - INCH);
            ct.setSimpleColumn(INCH, INCH * 1.5f, 7.5f * INCH, 9.5f * INCH);
            document.newPage();
        }
        document.close();
    }
}

For the life of me, I can't remember why I reset the Y line. 为了我的一生,我不记得为什么重置Y线。

iText is a nice library. iText是一个不错的库。 I learned most of what I know about it form reading through the tutorials from the iText in Action book. 通过阅读《 iText in Action》一书中的教程,我学到了大部分关于它的知识。

http://developers.itextpdf.com/examples/ http://developers.itextpdf.com/examples/

Unfortunately, I never did get around to buying the book. 不幸的是,我从来没有去买这本书。

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

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