ColumnText.ShowTextAligned() cuts off text after linebreak

前端 未结 2 1678
孤街浪徒
孤街浪徒 2021-01-29 03:55

I have the following code to print a text onto a PDF document using iTextSharp:

canvas = stamper.GetOverContent(i)
watermarkFont = iTextSharp.text.pdf.BaseFont.C         


        
2条回答
  •  醉梦人生
    2021-01-29 04:08

    As documented, the ShowTextAligned() method can only be used to draw a single line. If you want to draw two lines, you have two options:

    Option 1: use the method twice:

    ColumnText.ShowTextAligned(canvas, Element.ALIGN_TOP, new Phrase("line 1"), 0, 50, 0)
    ColumnText.ShowTextAligned(canvas, Element.ALIGN_TOP, new Phrase("line 2"), 0, 25, 0)
    

    Option 2: use ColumnText in a different way:

    ColumnText ct = new ColumnText(canvas);
    ct.SetSimpleColumn(rect);
    ct.AddElement(new Paragraph("line 1"));
    ct.AddElement(new Paragraph("line 2"));
    ct.Go();
    

    In this code snippet, rect is of type Rectangle. It defines the area where you want to add the text. See How to add text in PdfContentByte rectangle using itextsharp?

提交回复
热议问题