RSS

Blog posts of '2021' 'January'

Formating PDF Printouts

You can create nested tables

See https://www.c-sharpcorner.com/UploadFile/f2e803/basic-pdf-creation-using-itextsharp-part-i/

You can add a page number in a footer

// In the main program

            var doc = new Document(pageSize);
            var pdfWriter = PdfWriter.GetInstance(doc, stream);
            doc.Open();
            
            pdfWriter.PageEvent = new PageHeaderFooter();

// In the PDF Class

        public class PageHeaderFooter : PdfPageEventHelper
        {
            private readonly Font _pageNumberFont = new Font(Font.HELVETICA, 8f, Font.NORMAL);

            public override void OnEndPage(PdfWriter writer, Document document)
            {
                AddPageNumber(writer, document);
            }

            private void AddPageNumber(PdfWriter writer, Document document)
            {

                var text = writer.PageNumber.ToString();

                var numberTable = new PdfPTable(2) { WidthPercentage = 100f };
                numberTable.SetTotalWidth(new float[] { 250, 250 });

                var textCell = new PdfPCell(new Phrase("Report Name", _pageNumberFont)) 
                    { HorizontalAlignment = Element.ALIGN_LEFT, Border = Rectangle.TOP_BORDER, BorderWidthTop = 0.5f };               

                var numberCell = new PdfPCell(new Phrase(text, _pageNumberFont))  
                    { HorizontalAlignment = Element.ALIGN_RIGHT, Border = Rectangle.TOP_BORDER, BorderWidthTop = 0.5f };

                numberTable.AddCell(textCell);

                numberTable.AddCell(numberCell);

                numberTable.WriteSelectedRows(0, -1, document.LeftMargin, document.Bottom + 20, writer.DirectContent);
            }
        }