none
How to export ASPX webpage to pdf with CSS included RRS feed

  • Question

  • I am trying to export ASPX webform to PDF. this page has fields to populate with style. I would like to know the options to make this work in ASP.net using C#. I have tried "iTextSharp" but it is not appending style to this fields. I would like to export the attached form to pdf document as close to it as possible. Thanks.
    Monday, March 23, 2020 12:30 PM

All replies

  • ASP.NET Web form as a forum in ASP.NET forums.

    http://forums.asp.net/

    Monday, March 23, 2020 12:49 PM
  • Hi vinisha9,

    Thank you for posting here.

    There are many nuget packages to convert web pages to pdf files, such as Spire.PDF, IronPDF, PDF Vision .Net, Syncfusion Essential PDF, and I will give you an example of Spire.PDF.

                PdfDocument doc = new PdfDocument();
                PdfPageSettings setting = new PdfPageSettings();
                setting.Size = new SizeF(1000, 1000);
                setting.Margins = new Spire.Pdf.Graphics.PdfMargins(20);
                PdfHtmlLayoutFormat htmlLayoutFormat = new PdfHtmlLayoutFormat();
                htmlLayoutFormat.IsWaiting = true;
                String url = "url";
                Thread thread = new Thread(() =>
                {
                    doc.LoadFromHTML(url, false, false, false, setting, htmlLayoutFormat);
                });
                thread.SetApartmentState(ApartmentState.STA);
                thread.Start();
                thread.Join();
                doc.SaveToFile(@"d:\test\testMSDN.pdf");
                doc.Close();

    It should be noted that both Spire.PDF and IronPDF convert web pages to entire images and then convert them to pdf files. The other two nuget packages need to pay, so I have not tested them.

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, March 24, 2020 2:25 AM
  • Hi. Thanks for your reply.

    I have tried your code. The PDF document that is exported is having a watermark of SpirePDF.

    Can that be removed?

    Also, Is there any way i can capture the textbox values to PDF document too.

    Wednesday, March 25, 2020 4:00 AM
  • Hi vinisha9,

    I also noticed the watermark issue, but unfortunately I cannot remove it, but I have an idea if we can take the page as an image and insert the image into a pdf. It seems Spire.PDF also uses this idea.

    As for getting the contents of the textbox, I don't have any good ideas yet, I will continue to look for possible solutions, and I will let you know if there is progress.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, March 25, 2020 9:42 AM
  • Hi vinisha9,

    An update.

    You can download install a nuget package: Selenium.Firefox.WebDriver.

    This package is for Firefox, but they also provide nuget packages for other browsers, you can download it according to your situation.

    Then you can use the following code to take a screenshot of a web page.

                IWebDriver driver = new FirefoxDriver();
                driver.Manage().Window.Maximize();
                driver.Navigate().GoToUrl("url");
                Screenshot image = ((ITakesScreenshot)driver).GetScreenshot();
                image.SaveAsFile("D:/test/Screenshot.png", ScreenshotImageFormat.Png);

    Now you can use iTestSharp or other tools to create a pdf file without watermark and insert the picture.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 26, 2020 2:11 AM