question

MalamMalam-4042 avatar image
0 Votes"
MalamMalam-4042 asked MalamMalam-4042 commented

Export Entire User Input Form to PDF in C#

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]

dotnet-aspnet-webforms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AgaveJoe avatar image
0 Votes"
AgaveJoe answered

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.




capture.png (9.5 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

sreejukg avatar image
0 Votes"
sreejukg answered

Yes, you can generate PDF from the data you have. iTextsharp is a pdf library that will help you generate pdf document from code. I published an article about generating the pdf from itextsharp, you can read this below.

https://weblogs.asp.net/sreejukg/create-pdf-document-using-itextsharp-in-asp-net-4-0-and-memorymappedfile

hope this helps

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MalamMalam-4042 avatar image
0 Votes"
MalamMalam-4042 answered sreejukg commented

Thanks but this is not what I am looking for; I already know how to export GridView data to PDF.
My question was "how to export the entire form that include 'captions' and 'user inputs' to PDF"

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

My question was "how to export the entire form that include 'captions' and 'user inputs' to PDF"

This should be the same a exporting a GridView. Override the page Render method.
https://docs.microsoft.com/en-us/answers/questions/433022/override-render-to-create-pdf-then-want-to-continu.html




0 Votes 0 ·
sreejukg avatar image
0 Votes"
sreejukg answered


The article was about how to use iTextsharp to generate pdf. it is possible to generate form. Refer the following sample.

https://kb.itextpdf.com/home/it7kb/ebooks/itext-7-converting-html-to-pdf-with-pdfhtml/chapter-7-frequently-asked-questions-about-pdfhtml/can-i-convert-an-html-form-to-a-pdf

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MalamMalam-4042 avatar image
0 Votes"
MalamMalam-4042 answered AgaveJoe commented

I've looked at that example and still not been able to see how it would convert asp page to pdf.
Also, I did have all references to iTextSharp but the sample code still generates errors everywhere in the code. For example.
ConverterProperties properties = new ConverterProperties(); Error on ConverterProperties
PdfReader pdfReader = new PdfReader(pdfStream);
Error on PdfReader

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

You did not share any code which makes it a bit hard to determine what you're doing exactly.

0 Votes 0 ·
MalamMalam-4042 avatar image
0 Votes"
MalamMalam-4042 answered

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);
     }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AgaveJoe avatar image
0 Votes"
AgaveJoe answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MalamMalam-4042 avatar image
0 Votes"
MalamMalam-4042 answered AgaveJoe commented

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);



· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

It's just a global variable used to switch between HTML and PDF content. It seems you are not taking the time to understand the code you are copying from the Internet.

     public partial class _default : System.Web.UI.Page
     {
         private bool returnPdf = false;
         protected void Page_Load(object sender, EventArgs e)
         {
    
         }


0 Votes 0 ·

Sorry, just saw your response. This was already done but had other errors that I posted above

0 Votes 0 ·
AgaveJoe avatar image AgaveJoe MalamMalam-4042 ·

I assume that you installed the iText7 Nuget packages because you are using classes in your sample code. Just add the using statements to the top of the file. Visual Studio should provide a help balloon that will fix these common coding patterns. If you did not install the Nuget packages then do so...

iText7
iText7.pdfhtml

Using statments

 using System;
 using System.IO;
 using System.Text;
 using System.Web.UI;
 using iText.Html2pdf;




0 Votes 0 ·
MalamMalam-4042 avatar image
0 Votes"
MalamMalam-4042 answered AgaveJoe commented

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.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

If you read the NuGet information, iTextSharp is deprecated. The iTextSharp NuGet content suggests installing iText7.

174251-capture.png

Also, I specified the NuGet packages I'm using in my post.

0 Votes 0 ·
capture.png (7.5 KiB)
MalamMalam-4042 avatar image
0 Votes"
MalamMalam-4042 answered YijingSun-MSFT commented

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Yes, understand iTextSharp is deprecated but VS 2017 shows that when I select "Show potential fixes".

I honestly have no idea what you're doing at this point.


Which .Net Framerwork is needed to use iText7?

According to information in Visual Studio when installing iText7 v 4.0.1 NuGet package, iText7 depends on at least .NET 4.6.1. My sample project is 4.8. You can always go back a few versions if need. iText7 v 3.0.5 needs at least .NET 4.5. v 3.0.0 needs at least .NET 4. All this information is available in Visual Studio.

The code "using iText.Html2pdf;" gives error "using directive is not necessary.

Did you install the itext7.pdfhtml NuGet Package as specified in my post?

0 Votes 0 ·

Hi @MalamMalam-4042
The prerequisites for installing iText7:

  • Visual Studio 2017 and above

  • .NET Framework, Version 4.0 and above

  • Installed iText 7 Library using NuGet Package Manager

According to your error,I think you could check if you installed the NuGet package correctly and quote it right.
Best regards,
Yijing Sun

0 Votes 0 ·