Export Entire User Input Form to PDF in C#

Malam Malam 121 Reputation points
2022-02-14T15:59:15.65+00:00

I have a form that captures general user information like FirstName, LastName etc.
When user is done entering all information and clicks on the button Button1; I need to export the entire form to PDF.

Is there a way to do it?

First Name: <user Input> City: <user Input>
Last Name: <user Input> State: <user Input>
Address: <user Input> Zip: <user Input>

[Button1]

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,254 questions
{count} votes

Accepted answer
  1. AgaveJoe 26,201 Reputation points
    2022-02-15T16:25:39.3+00:00

    I do not see iText7.pdfhtml anywhere in your references screenshot.

    174390-capture.png

    As far as I can tell you still have not installed the NuGet package. It's possible that there are build errors which stops the package installation. Usually build errors are very clear. Anyway, fix the build errors first. Once the project builds successfully then install iText7.pdfhtml.

    Pay closer attention to the details.

    0 comments No comments

12 additional answers

Sort by: Most helpful
  1. Malam Malam 121 Reputation points
    2022-02-14T20:00:08.857+00:00

    Here is the code; copied form the link

        public System.Collections.ICollection GetFormFields(Stream pdfStream)
        {
            iTextSharp.text.pdf.PdfReader reader = null;
            try
            {
                PdfReader pdfReader = new PdfReader(pdfStream);
                AcroFields acroFields = pdfReader.AcroFields;
                return acroFields.Fields.Keys;
            }
            finally
            {
                reader?.Close();
            }
        }
    
        public void CreatePdf(String src, String dest)
        {
            ConverterProperties properties = new ConverterProperties();
            properties.SetCreateAcroForm(true);
            HtmlConverter.ConvertToPdf(new FileStream(src, FileMode.Open, FileAccess.Read),
                new FileStream(dest, FileMode.Create, FileAccess.Write), properties);
        }
    
    0 comments No comments

  2. AgaveJoe 26,201 Reputation points
    2022-02-14T21:21:39.83+00:00

    I have no idea what link you are referring to and there is no indication how you are calling the two methods.

    Below is an example that overrides the Web Form's Render() method.

    protected void Button1_Click(object sender, EventArgs e)
    {
        returnPdf = true;
    }
    
    protected override void Render(HtmlTextWriter Writer)
    {
        string Content = string.Empty;
        MemoryStream pdfStream = new MemoryStream();
        if (returnPdf)
        {
            using (StringWriter stringWriter = new StringWriter())
            {
                using (HtmlTextWriter HtmlTextWriter = new HtmlTextWriter(stringWriter))
                { 
                    base.Render(HtmlTextWriter); 
                    HtmlTextWriter.Close();
    
                    // get the page content  
                    Content = stringWriter.ToString();
    
                    //Get a PDF stream
                    MemoryStream htmlStream = new MemoryStream(Encoding.UTF8.GetBytes(Content));
                    HtmlConverter.ConvertToPdf(htmlStream, pdfStream);
                }
            }
    
            // render the PDF content
            Response.ContentType = "application/pdf";
            Response.BinaryWrite(pdfStream.ToArray());
            Response.End();
            Response.Flush();
            Response.Clear();
        }
        else
        {
            //Render HTML content
            using (StringWriter stringWriter = new StringWriter())
            {
                using (HtmlTextWriter HtmlTextWriter = new HtmlTextWriter(stringWriter))
                { 
                    base.Render(HtmlTextWriter); 
                    HtmlTextWriter.Close();
                    Content = stringWriter.ToString();
                }
            }
            Writer.Write(Content);
        }
    }
    

    If the button is clicked a PDF is returned otherwise HTML is returned. I'm not sure if this is what you are trying to do exaclty.

    0 comments No comments

  3. Malam Malam 121 Reputation points
    2022-02-14T21:29:27.823+00:00

    Where is "returnPdf " defined?

    Error: The name returnpdf does not exist in the current context.

    I defined a public bool bool returnPdf to get rid of this error but still get the error on HTMLConverter and Encoding

    MemoryStream htmlStream = new MemoryStream(Encoding.UTF8.GetBytes(Content));
    HtmlConverter.ConvertToPdf(htmlStream, pdfStream);


  4. Malam Malam 121 Reputation points
    2022-02-14T22:01:29.29+00:00

    Yes, I have both installed and added to the project but I get an error in the code:
    using iText.Html2pdf

    Error: The type or namespace 'iText' could not be found. I use the balloon and it give an option to change it to
    using iTextSharp.Html2pdf but it gives a different error when selected.

    Which .Net Framework do I need to have for iText7? 4.5 could be an issue.