0

I want to add a dynamic footer in itext7 using C#. The footer can vary from 1 to 6 lines of text. I have been able to implement single line footers but the long text is being truncated.

Please help.

Code to add footer:

class CustomEventHandler : IEventHandler
{
    protected Document doc;
    private Table table;


    public CustomEventHandler(Document doc)
    {
        this.doc = doc;
    }

    public CustomEventHandler(Table table)
    {
        this.table = table;
    }


    public void HandleEvent(Event currentEvent)
    {
        PdfDocumentEvent docEvent = (PdfDocumentEvent)currentEvent;
        Rectangle pageSize = docEvent.GetPage().GetPageSize();
        PdfFont font = null;
        try
        {
            font = PdfFontFactory.CreateFont(StandardFonts.HELVETICA_OBLIQUE);
        }
        catch (IOException e)
        {
            Console.Error.WriteLine(e.Message);
        }

        float coordX = pageSize.GetLeft() + doc.GetLeftMargin();

        float headerY = pageSize.GetTop() - doc.GetTopMargin() + 10;
        float footerY = doc.GetBottomMargin();
        Canvas canvas = new Canvas(docEvent.GetPage(), pageSize);
        canvas
            .SetFont(font)
            .SetFontSize(5)
            .ShowTextAligned("this is a header", coordX, headerY, TextAlignment.CENTER)
            .ShowTextAligned("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.", coordX, footerY, TextAlignment.LEFT)
            .Close();

    }
}

Thanks Priyanka

2 Answers2

0

You will have to break down your text so that it fits without truncating. The canvas does not know how to do line breaks or text wrapping, so you have to do this manually.

If you need text wrapping to happen automatically, use a ColumnText instead of a Canvas. See this example

To keep using the canvas, break your text up first after a certain number of characters, you can do trial and error to figure out how many. Below is a method you can use to do that

static List<string> Split(string str, int chunkSize)
{
    return Enumerable.Range(0, str.Length / chunkSize)
        .Select(i => str.Substring(i * chunkSize, chunkSize)).ToList();
}

It is taken from this answer to a different question

Then from there, you insert each of the returned strings into your canvas

Canvas canvas = new Canvas(docEvent.GetPage(), pageSize)
        .SetFont(font)
        .SetFontSize(5);
List<string> chunks = Split("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.", 50);//splitting after every 50 characters, for example

for(int i=0; i<chunks.Count; i++)
{
    canvas = canvas.ShowTextAligned(new Paragraph(chunks[i]),
             coordX, footerY-(i*5), TextAlignment.LEFT, VerticalAlignment.MIDDLE);
    //where each line is separated by 5 units
}
canvas.Close();

You may want to check if all six lines will fit in your bottom margin, otherwise you'll need to play around with the initial value of footerX or the line spacing to get exactly what you need

peterpie
  • 103
  • 10
  • Thank you for the explanation. Is there a way to place the text at a dynamic position as my footer text will be dynamic? It can go from 2-6 lines. – Priyanka Kothari May 22 '20 at 13:59
  • Yes. Position is determined by your variables `coordX` and `footerY`. I didn't make changes to those from your code sample. Have you tried this solution? Is it now working? – peterpie May 22 '20 at 16:17
0

If you want itext does line breaks by itself, you should wrap the text by a Paragraph and add it to the canvas by the Canvas#add method:

Paragraph p = new Paragraph("");
//put the paragraph in the very bottom of the canvas
p.SetFixedPosition(0, 0, pageSize.getWidth()); 
canvas.Add(p);