Export Entire User Input Form to PDF in C#

Malam Malam 266 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]

Developer technologies | ASP.NET | Other
{count} votes

Answer accepted by question author
  1. AgaveJoe 30,491 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 266 Reputation points
    2022-02-14T22:23:14.657+00:00

    Yes, understand iTextSharp is deprecated but VS 2017 shows that when I select "Show potential fixes".
    I am just not been able to resolve these issues for some reasons. The code "using iText.Html2pdf;" gives error "using directive is not necessary.

    Which .Net Framerwork is needed to use iText7? I googled but couldn't find the answer.


  2. Malam Malam 266 Reputation points
    2022-02-15T12:50:06.543+00:00

    Error: HtmlConverter does not exist in the current context
    HtmlConverter.ConvertToPdf(htmlStream, pdfStream);


  3. Malam Malam 266 Reputation points
    2022-02-15T15:59:33.947+00:00

    I did install the iText7.pdfhtml NuGet packages and also tried with using iText.Html2pdf; which errors "using directive is unnecessary". And the HtmlConverter still errors "The name HtmlConverter does not exist in the current context"

    .174449-error.png

    0 comments No comments

  4. Malam Malam 266 Reputation points
    2022-02-15T17:01:59.487+00:00

    You were right; install failed and I didn't catch it since the panel was small. It has been fixed and I see it in References.

    I am still getting errors when I click on button:

    Error loading entities resource: I/O error occurred.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: iText.StyledXmlParser.Jsoup.MissingResourceException: Error loading entities resource: I/O error occurred.

    Source Error:
    Line 41: MemoryStream htmlStream = new MemoryStream(Encoding.UTF8.GetBytes(Content));
    Line 42: HtmlConverter.ConvertToPdf(htmlStream, pdfStream);

    I have copied and pasted your code and haven't made any changes to it.

            public bool returnPdf;
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            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);
                }
            }
    

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.