underline portion of text using iTextSharp

廉价感情. 提交于 2019-11-29 12:26:53
Bruno Lowagie

Ordinary text fields do not support rich text. If you want the fields to remain interactive, you will need RichText fields. These are fields that are flagged in a way that they accept an RV value. This is explained here: Set different parts of a form field to have different fonts using iTextSharp (Note that I didn't succeed in getting this to work, but you may have better luck.)

If it is OK for you to flatten the form (i.e. remove all interactivity), please take a look at the FillWithUnderline example:

public void manipulatePdf(String src, String dest) throws DocumentException, IOException {
    PdfReader reader = new PdfReader(src);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    stamper.setFormFlattening(true);
    AcroFields form = stamper.getAcroFields();
    FieldPosition pos = form.getFieldPositions("Name").get(0);
    ColumnText ct = new ColumnText(stamper.getOverContent(pos.page));
    ct.setSimpleColumn(pos.position);
    ElementList elements = XMLWorkerHelper.parseToElementList("<div>Bruno <u>Lowagie</u></div>", null);
    for (Element element : elements) {
        ct.addElement(element);
    }
    ct.go();
    stamper.close();
}

In this example, we don't fill out the field, but we get the fields position (a page number and a rectangle). We then use ColumnText to add content at this position. As we are inputting HTML, we use XML Worker to parse the HTML into iText objects that we can add to the ColumnText object.

This is a Java example, but it should be easy to port this to C# if you know how to code in C# (which I don't).

You can trythis

            Chunk chunk = new Chunk("Underlined TExt", FontFactory.GetFont(FontFactory.TIMES_ROMAN, 12.0f, iTextSharp.text.Font.BOLD | iTextSharp.text.Font.UNDERLINE));
            Paragraph reportHeadline = new Paragraph(chunk);
            reportHeadline.SpacingBefore = 12.0f;
            pdfDoc.Add(reportHeadline);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!