Search code examples
htmlcssmodel-view-controlleritextwkhtmltopdf

Draw full line using itextsharp


For conversion from HTML to PDF I used wkhtmltopdfbecause of css support.. but I want to draw full line width of pdf page..I used HR and DIV for that but it left some margin before and after it may b because of I passed argument as args+= "--margin-top 10mm --margin-bottom 10mm --margin-right 3mm --margin-left 3mm ";..so I am using ITextsharp for this..but it doesn't draw line I tried below code:

        PdfContentByte cb;
        // DO PAGE NUMBER
        byte[] bytes1 = System.IO.File.ReadAllBytes(@"E:\temp\test_QA.pdf");
        var FontColour = new BaseColor(97, 123, 149);
        iTextSharp.text.Font baseFontNormal = FontFactory.GetFont("Calibri", 9, iTextSharp.text.Font.NORMAL, FontColour);
        using (MemoryStream stream = new MemoryStream())
        {
            PdfReader reader = new PdfReader(bytes1);
            var doc = new iTextSharp.text.Document();
            PdfWriter writer = PdfWriter.GetInstance(doc, stream);
            writer.PageEvent = new iTextEvents();
            //Open the document for writing                                                
            doc.Open();
            cb = writer.DirectContent;
            using (PdfStamper stamper = new PdfStamper(reader, stream))
            {
                cb.MoveTo(0, doc.PageSize.Height - 55);
                cb.LineTo(doc.PageSize.Width, doc.PageSize.Height - 55);
                cb.SetColorStroke(FontColour);
                cb.Stroke();

                //Move the pointer and draw line to separate footer section from rest of page
                cb.MoveTo(0, doc.PageSize.GetBottom(150));
                cb.LineTo(doc.PageSize.Width, doc.PageSize.GetBottom(55));
                cb.SetColorStroke(FontColour);
                cb.Stroke();
            }
            bytes1 = stream.ToArray();
            System.IO.File.WriteAllBytes(@"E:\download.pdf", bytes1);
            //doc.Close();
        }

any idea what is missing here?


Solution

  • You use both a Document/PdfWriter couple (creating a new PDF) and a PdfReader/PdfStamper couple (manipulating an existing PDF) writing to the same stream! You should not expect that to produce anything but garbage. Instead only use a PdfReader/PdfStamper couple and change its OverContent.

    Furthermore, your code is wrong in another way: You first create a path (cb.MoveTo, cb.LineTo), then you set the color (cb.SetColorStroke), then you try to use the path (cb.Stroke). This creates invalid content according to the PDF specification: There shall not be other instructions between the creation and the use of a path.

    Thus, use something like this (untested, merely changed your code in editor here):

    var FontColour = new BaseColor(97, 123, 149);
    using (MemoryStream stream = new MemoryStream())
    {
        using (PdfReader reader = new PdfReader(@"E:\temp\test_QA.pdf"))
        using (PdfStamper stamper = new PdfStamper(reader, stream))
        {
            Rectangle rect = reader.GetPageSize(1);
            PdfContentByte cb = stamper.GetOverContent(1);
            cb.SetColorStroke(FontColour);
            cb.MoveTo(0, rect.Height - 55);
            cb.LineTo(rect.Width, rect.Height - 55);
            cb.MoveTo(0, rect.GetBottom(150));
            cb.LineTo(rect.Width, rect.GetBottom(55));
            cb.Stroke();
        }
        System.IO.File.WriteAllBytes(@"E:\download.pdf", stream.ToArray());
    }