Export Page to PDF with CheckBox and Image at Buttom using iTextSharp in ASP.Net

Last Reply 12 days ago By pandeyism

Posted 13 days ago

How to move checkbox, text I agree and image to the bottom after it converted to pdf

The codes worked for the web and PDF.  But when I open PDF file, The check box and an image at the top of its converted PDF file.  How can I move check box to the bottom PDF page?

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <asp:Image ID="Image1" ImageUrl="~/Files/E-Sign.png" runat="server" Height="100px"
        Width="100px" />
    <br />
    <div runat="server" id="pnlDetails">
        <p>
            1. Get A Discount On A New Walk In Tub</p>
        <p>
            Walk in tubs are a perfect addition for seniors living with mobility challenges
            or anyone worried about fall related injuries. Statistics tell us that falls happen
            in your bathroom more than any other room in the home.</p>
        <p>
            In fact two-thirds of all bathroom injuries happen near the shower or bath-tub,
            according to The Center for Disease Control. Most falls typically happen when your
            stepping over the tub, slipping on a wet floor or when leaning on a glass door that
            wasn't designed to support your weight</p>
        <p>
            A Walk-in tub is an investment in your future a simple modification to your bathroom
            can change your life. Keeping your independence and dignity, plus peace of mind
            for your loved ones all ad up to a better quality of life. Right now, Walk-in tub
            manufacturers all over the country are offering big senior discounts. Visit the
            walk-in tub website to find discounts and get a free quote on a new Walk-in tub.</p>
    </div>
    <asp:CheckBox Text="I Agree" runat="server" ID="chkAgree" />
    <asp:Button ID="btnSave" Text="Save" runat="server" OnClick="Save" />
</asp:Content>

 

 

namespace checkboxToPDF
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Save(object sender, EventArgs e)
        {
            using (FileStream stream = new FileStream(Server.MapPath("~/Files/") + "ImageExport.pdf", FileMode.Create))
            {
                String[] texts = { chkAgree.Text };
                string filePath = "~/Files/" + Path.GetFileName("E-Sign.png");
                byte[] bytes = File.ReadAllBytes(Server.MapPath(filePath));
                Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
                PdfWriter.GetInstance(pdfDoc, stream);
                pdfDoc.Open();
                string FONT = "c:/windows/fonts/WINGDING.TTF";
                string checkBox = "\u00fe";
                string uncheckBox = "o";
                BaseFont bf = BaseFont.CreateFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
                iTextSharp.text.Font f = new iTextSharp.text.Font(bf, 12);
                Paragraph ptrue = new Paragraph(checkBox, f);
                Paragraph pfalse = new Paragraph(uncheckBox, f);
                if (chkAgree.Checked)
                {
                    pdfDoc.Add(ptrue);
                }
                else
                {
                    pdfDoc.Add(pfalse);
                }
                pdfDoc.Add(new Paragraph(chkAgree.Text));
                iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(bytes);
                pdfDoc.Add(img);
                iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(pdfDoc);
                hw.Parse(new StringReader(pnlDetails.InnerText));
              
               

                pdfDoc.Close();
                stream.Close();
            }
        }
    }
}

 

 

Posted 12 days ago

Hi KatieNgoc,

Refer below sample code.

HTML

<div>
    <asp:Image ID="Image1" ImageUrl="~/Files/E-Sign.png" runat="server" Height="100px"
        Width="100px" />
    <br />
    <div runat="server" id="pnlDetails">
        <p>
            1. Get A Discount On A New Walk In Tub</p>
        <p>
            Walk in tubs are a perfect addition for seniors living with mobility challenges
            or anyone worried about fall related injuries. Statistics tell us that falls happen
            in your bathroom more than any other room in the home.</p>
        <p>
            In fact two-thirds of all bathroom injuries happen near the shower or bath-tub,
            according to The Center for Disease Control. Most falls typically happen when your
            stepping over the tub, slipping on a wet floor or when leaning on a glass door that
            wasn't designed to support your weight</p>
        <p>
            A Walk-in tub is an investment in your future a simple modification to your bathroom
            can change your life. Keeping your independence and dignity, plus peace of mind
            for your loved ones all ad up to a better quality of life. Right now, Walk-in tub
            manufacturers all over the country are offering big senior discounts. Visit the
            walk-in tub website to find discounts and get a free quote on a new Walk-in tub.</p>
    </div>
    <asp:CheckBox Text="I Agree" runat="server" ID="chkAgree" />
    <asp:Button ID="btnSave" Text="Save" runat="server" OnClick="Save" />
</div>

Namespaces

C#

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

VB.Net

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

Code

C#

protected void Save(object sender, EventArgs e)
{
    using (FileStream stream = new FileStream(Server.MapPath("~/Files/") + "ImageExport.pdf", FileMode.Create))
    {
        String[] texts = { chkAgree.Text };
        string filePath = "~/Files/E-Sign.png";
        byte[] bytes = File.ReadAllBytes(Server.MapPath(filePath));
        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
        PdfWriter.GetInstance(pdfDoc, stream);
        pdfDoc.Open();
        string FONT = "c:/windows/fonts/WINGDING.TTF";
        string checkBox = "\u00fe";
        string uncheckBox = "o";
        BaseFont bf = BaseFont.CreateFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        iTextSharp.text.Font f = new iTextSharp.text.Font(bf, 12);
        Paragraph ptrue = new Paragraph(checkBox, f);
        Paragraph pfalse = new Paragraph(uncheckBox, f);
        iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(bytes);
        pdfDoc.Add(img);
        iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(pdfDoc);
        hw.Parse(new StringReader(pnlDetails.InnerText));
        if (chkAgree.Checked)
        {
            pdfDoc.Add(ptrue);
        }
        else
        {
            pdfDoc.Add(pfalse);
        }
        pdfDoc.Add(new Paragraph(chkAgree.Text));

        pdfDoc.Close();
        stream.Close();
    }
}

VB.Net

Protected Sub Save(ByVal sender As Object, ByVal e As EventArgs)
    Using stream As FileStream = New FileStream(Server.MapPath("~/Files/") & "ImageExport.pdf", FileMode.Create)
        Dim texts As String() = {chkAgree.Text}
        Dim filePath As String = "~/Files/E-Sign.png"
        Dim bytes As Byte() = File.ReadAllBytes(Server.MapPath(filePath))
        Dim pdfDoc As Document = New Document(PageSize.A4, 10F, 10F, 10F, 10F)
        PdfWriter.GetInstance(pdfDoc, stream)
        pdfDoc.Open()
        Dim FONT As String = "c:/windows/fonts/WINGDING.TTF"
        Dim checkBox As String = "þ"
        Dim uncheckBox As String = "o"
        Dim bf As BaseFont = BaseFont.CreateFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED)
        Dim f As iTextSharp.text.Font = New iTextSharp.text.Font(bf, 12)
        Dim ptrue As Paragraph = New Paragraph(checkBox, f)
        Dim pfalse As Paragraph = New Paragraph(uncheckBox, f)
        Dim img As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(bytes)
        pdfDoc.Add(img)
        Dim hw As iTextSharp.text.html.simpleparser.HTMLWorker = New iTextSharp.text.html.simpleparser.HTMLWorker(pdfDoc)
        hw.Parse(New StringReader(pnlDetails.InnerText))

        If chkAgree.Checked Then
            pdfDoc.Add(ptrue)
        Else
            pdfDoc.Add(pfalse)
        End If
        pdfDoc.Add(New Paragraph(chkAgree.Text))
        pdfDoc.Close()
        stream.Close()
    End Using
End Sub