Export multiline TextBox content with formatting to PDF in ASP.Net

Last Reply 10 months ago By pandeyism

Posted 10 months ago

I WANT TO CONVERT TEXTBOX CONTENT (MULTI LINE)TO PDF.

I CONVERTED

BUT PDF FILE NOT IN THE FORMAT AS IN THE TEXTBOX.

i AM USING itextsharp.dll

AND A TEMPLATE FILE   <td colspan="2" align="center">[NAME]</td>

AND USING CODE

 contents = contents.Replace("[NAME]", TextBox3.Text  );

I WANT TO SHOW CONTENTS LIKE IN TEXTBOX IN PDF.

I AM ATTACHING SCREENSHOT OF PDF & TEXTBOX CONTENT   HERE

PDF SCREEN SHOT

TEXTBOX SCREEN SHOT

 

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

Hi charles,

Please refer below sample.

If you want to more alignment and design so for that refer below article -

How to generate and download PDF Report from database in ASP.Net using iTextSharp C# and VB.Net

HTML

<div>
    <asp:TextBox ID="txtTextBox1" runat="server" TextMode="MultiLine" Height="100px"
        Width="250px"></asp:TextBox>
    <asp:Button ID="btnMakePDF" runat="server" OnClick="MakePDF" Text="Submit" />
</div>

Namespaces

C#

using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Text.RegularExpressions;

Vb.Net

Imports iTextSharp.text.pdf
Imports iTextSharp.text

Code

C#

protected void MakePDF(object sender, EventArgs e)
{
    Phrase phrase = new Phrase();
    string[] lines = Regex.Split(txtTextBox1.Text, "\r\n");
    foreach (string item in lines)
    {

        phrase.Add(new Chunk("" + item.ToString() + "\n"));
    }

    Document document = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
    using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
    {
        PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
        document.Open();
        document.Add(phrase);
        document.Close();
        byte[] bytes = memoryStream.ToArray();
        memoryStream.Close();
        Response.Clear();
        Response.ContentType = "application/pdf";
        Response.AddHeader("Content-Disposition", "attachment; filename=Test.pdf");
        Response.ContentType = "application/pdf";
        Response.Buffer = true;
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.BinaryWrite(bytes);
        Response.End();
        Response.Close();
    }
}

public override void VerifyRenderingInServerForm(Control control)
{
    /* Verifies that the control is rendered */
}

Vb.Net

Protected Sub MakePDF(ByVal sender As Object, ByVal e As EventArgs)
    Dim phrase As Phrase = New Phrase()
    Dim lines As String() = Regex.Split(txtTextBox1.Text, vbCrLf)
    For Each item As String In lines
        phrase.Add(New Chunk("" & item.ToString() & vbLf))
    Next
    Dim document As iTextSharp.text.Document = New iTextSharp.text.Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 10.0F)
    Using memoryStream As System.IO.MemoryStream = New System.IO.MemoryStream()
        Dim writer As PdfWriter = PdfWriter.GetInstance(document, memoryStream)
        document.Open()
        document.Add(phrase)
        document.Close()
        Dim bytes As Byte() = memoryStream.ToArray()
        memoryStream.Close()
        Response.Clear()
        Response.ContentType = "application/pdf"
        Response.AddHeader("Content-Disposition", "attachment; filename=Test.pdf")
        Response.ContentType = "application/pdf"
        Response.Buffer = True
        Response.Cache.SetCacheability(HttpCacheability.NoCache)
        Response.BinaryWrite(bytes)
        Response.[End]()
        Response.Close()
    End Using
End Sub

Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
End Sub

Screenshot