Larry Finson
Larry Finson

Reputation: 1

unable to create document border using itext 7

Using the code below I try to set a border around my document, but nothing shows up:

           PdfWriter pdfWriter = new PdfWriter(toFile);
            PdfDocument pdfDoc = new PdfDocument(pdfWriter);        //Initialize PDF document
            var pageSize = PageSize.LETTER;
            var document = new iText.Layout.Document(pdfDoc, pageSize);       // Initialize document

            var fontTimesRoman = PdfFontFactory.CreateFont(iText.IO.Font.Constants.StandardFonts.TIMES_ROMAN);
            var fontTimesRomanBold = PdfFontFactory.CreateFont(iText.IO.Font.Constants.StandardFonts.TIMES_BOLD);
            var navy = new DeviceRgb(0, 0, 128);
            var red = new DeviceRgb(139, 0, 0);

            document.SetBorder(new SolidBorder(red, 18));

            document.Add( new Paragraph(DateTime.Now.ToString("MMMM dd, yyyy"))
                            .SetFont(fontTimesRoman)
                            .SetTextAlignment(iText.Layout.Properties.TextAlignment.RIGHT)
                            //.SetHorizontalAlignment(iText.Layout.Properties.HorizontalAlignment.RIGHT)
                            .SetFontSize(11)
                        );

Upvotes: 0

Views: 267

Answers (1)

rossfrank
rossfrank

Reputation: 95

This won't work since the document is the size of the page and the Border will be outside of the page. If you are trying to add a border to the edges of a Document you would need to do it in a PdfCanvas object

pdfDoc.AddNewPage();
var canvas = new PdfCanvas(pdfDoc, 1);
canvas.SetStokeColor(red);
canvas.SetLineWidth(18f);
canvas.Rectangle(0,1,pageSize.GetWidth()-1,pageSize.GetHeight()-1);
canvas.Stroke();

also don't forget to run

pdfDoc.Close();

Upvotes: 2

Related Questions