Draw Line separator in PDF using iTextSharp in ASP.Net

Last Reply 5 months ago By pandeyism

Posted 5 months ago

Required same format as following while generating pdf report using itextsharp

https://imgur.com/a/wJZWIHO

You are viewing reply posted by: pandeyism 5 months ago.
Posted 5 months ago

Hi smile,

Please refer below sample and adjust the line gap as per your requirement.

Namespaces

C#

using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

VB.Net

Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.pdf

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    Document document = new Document(PageSize.A4, 88f, 88f, 10f, 10f);
    using (MemoryStream memoryStream = new MemoryStream())
    {
        PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
        document.Open();

        Paragraph lineSeparator = new Paragraph(new Chunk(new iTextSharp.text.pdf.draw.LineSeparator(0.0F, 100.0F, Color.BLACK, Element.ALIGN_LEFT, 1)));
        // Set gap between line paragraphs.
        lineSeparator.SetLeading(0.5F, 0.5F);
        Paragraph par = new Paragraph(" ");
        // Set gap between text paragraphs.
        par.SetLeading(0.7F, 0.7F);

        document.Add(new Paragraph("    i.     How Many words for colours are there in the poem?"));
        document.Add(par);
        document.Add(lineSeparator);
        document.Add(lineSeparator);
        document.Add(lineSeparator);
        document.Add(lineSeparator);
        document.Add(lineSeparator);

        document.Add(new Paragraph("    ii.     Where will the boatgo?"));
        document.Add(par);
        document.Add(lineSeparator);
        document.Add(lineSeparator);
        document.Add(lineSeparator);
        document.Add(lineSeparator);
        document.Add(lineSeparator);

        document.Add(new Paragraph("    iii.     Which son could see everythings?"));
        document.Add(par);
        document.Add(lineSeparator);
        document.Add(lineSeparator);
        document.Add(lineSeparator);
        document.Add(lineSeparator);
        document.Add(lineSeparator);

        document.Close();
        byte[] bytes = memoryStream.ToArray();
        memoryStream.Close();
        Response.Clear();
        Response.ContentType = "application/pdf";
        Response.AddHeader("Content-Disposition", "attachment; filename=Employee.pdf");
        Response.ContentType = "application/pdf";
        Response.Buffer = true;
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.BinaryWrite(bytes);
        Response.End();
        Response.Close();
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim document As Document = New Document(PageSize.A4, 88.0F, 88.0F, 10.0F, 10.0F)
    Using memoryStream As MemoryStream = New MemoryStream()
        Dim writer As PdfWriter = PdfWriter.GetInstance(document, memoryStream)
        document.Open()
        Dim lineSeparator As Paragraph = New Paragraph(New Chunk(New iTextSharp.text.pdf.draw.LineSeparator(0.0F, 100.0F, Color.BLACK, Element.ALIGN_LEFT, 1)))
        lineSeparator.SetLeading(0.5F, 0.5F)
        Dim par As Paragraph = New Paragraph(" ")
        par.SetLeading(0.7F, 0.7F)
        document.Add(New Paragraph("    i.     How Many words for colours are there in the poem?"))
        document.Add(par)
        document.Add(lineSeparator)
        document.Add(lineSeparator)
        document.Add(lineSeparator)
        document.Add(lineSeparator)
        document.Add(lineSeparator)
        document.Add(New Paragraph("    ii.     Where will the boatgo?"))
        document.Add(par)
        document.Add(lineSeparator)
        document.Add(lineSeparator)
        document.Add(lineSeparator)
        document.Add(lineSeparator)
        document.Add(lineSeparator)
        document.Add(New Paragraph("    iii.     Which son could see everythings?"))
        document.Add(par)
        document.Add(lineSeparator)
        document.Add(lineSeparator)
        document.Add(lineSeparator)
        document.Add(lineSeparator)
        document.Add(lineSeparator)
        document.Close()
        Dim bytes As Byte() = memoryStream.ToArray()
        memoryStream.Close()
        Response.Clear()
        Response.ContentType = "application/pdf"
        Response.AddHeader("Content-Disposition", "attachment; filename=Employee.pdf")
        Response.ContentType = "application/pdf"
        Response.Buffer = True
        Response.Cache.SetCacheability(HttpCacheability.NoCache)
        Response.BinaryWrite(bytes)
        Response.End()
        Response.Close()
    End Using
End Sub

Screenshot