Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I want to export Asp.Net Page to PDF but I don't know how i do this Please help me
Posted
Comments
Rajesh Anuhya 30-Nov-10 9:08am    
No Effort

I got this many links with your subject line when i place in the google search

How to export Asp.Net page to Pdf[^]
 
Share this answer
 
I use the Winnovative.WnvHtmlConvert;

You can find it and download the wnvhtmlconvert.dll.
Make reference to the dll.
It is not free but it has demo version with a license. Get it from the site: http://www.winnovative-software.com/[^]
Use the following function:


/// <summary>
    /// Convert the HTML code from the specified URL to a PDF document and send the
    /// document as an attachment to the browser
    /// </summary>
    private void ConvertURLToPDF()
    {

        // Create the PDF converter. Optionally you can specify the virtual browser
        // width as parameter. 1024 pixels is default, 0 means autodetect
        PdfConverter pdfConverter = new PdfConverter();

        // set the license key - required
        pdfConverter.LicenseKey = "you license here";

        // set the converter options - optional
        pdfConverter.PdfDocumentOptions.PdfPageSize = PdfPageSize.A4;
        pdfConverter.PdfDocumentOptions.PdfCompressionLevel = PdfCompressionLevel.Normal;
        pdfConverter.PdfDocumentOptions.PdfPageOrientation = PDFPageOrientation.Portrait;


        // set if header and footer are shown in the PDF - optional - default is false
        pdfConverter.PdfDocumentOptions.ShowHeader = false;
        pdfConverter.PdfDocumentOptions.ShowFooter = false;
        // set to generate a pdf with selectable text or a pdf with embedded image - optional - default is true
        pdfConverter.PdfDocumentOptions.GenerateSelectablePdf = true;
        // set if the HTML content is resized if necessary to fit the PDF page width - optional - default is true
        pdfConverter.PdfDocumentOptions.FitWidth = true;
        //
        // set the embedded fonts option - optional - default is false
        pdfConverter.PdfDocumentOptions.EmbedFonts = false;
        // set the live HTTP links option - optional - default is true
        pdfConverter.PdfDocumentOptions.LiveUrlsEnabled = true;

        if (true) //(radioConvertToSelectablePDF.Checked)
        {
            // set if the JavaScript is enabled during conversion to a PDF with selectable text
            // - optional - default is false
            pdfConverter.ScriptsEnabled = false;
            // set if the ActiveX controls (like Flash player) are enabled during conversion
            // to a PDF with selectable text - optional - default is false
            pdfConverter.ActiveXEnabled = false;
        }
        else
        {
            // set if the JavaScript is enabled during conversion to a PDF with embedded image
            // - optional - default is true
            pdfConverter.ScriptsEnabledInImage = true;
            // set if the ActiveX controls (like Flash player) are enabled during conversion
            // to a PDF with embedded image - optional - default is true
            pdfConverter.ActiveXEnabledInImage = true;
        }

        // set if the images in PDF are compressed with JPEG to reduce the PDF document size - optional - default is true
        pdfConverter.PdfDocumentOptions.JpegCompressionEnabled = true;

        // enable auto-generated bookmarks for a specified list of tags (e.g. H1 and H2)
        if (false)
        {
            pdfConverter.PdfBookmarkOptions.TagNames = new string[] { "H1", "H2" };
        }

        // set PDF security options - optional
        //pdfConverter.PdfSecurityOptions.CanPrint = true;
        //pdfConverter.PdfSecurityOptions.CanEditContent = true;
        //pdfConverter.PdfSecurityOptions.UserPassword = "";

        //set PDF document description - optional
        //pdfConverter.PdfDocumentInfo.AuthorName = "Winnovative HTML to PDF Converter";

        //// add HTML header
        //if (cbAddHeader.Checked)
        //    AddHeader(pdfConverter);
        //// add HTML footer
        //if (cbAddFooter.Checked)
        //    AddFooter(pdfConverter);

        // Performs the conversion and get the pdf document bytes that you can further
        // save to a file or send as a browser response
        byte[] pdfBytes = pdfConverter.GetPdfBytesFromUrl(urlToConvert);

        // send the PDF document as a response to the browser for download
        System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
        response.Clear();
        response.AddHeader("Content-Type", "binary/octet-stream");
        response.AddHeader("Content-Disposition",
            "attachment; filename=ConversionResult.pdf; size=" + pdfBytes.Length.ToString());
        response.Flush();
        response.BinaryWrite(pdfBytes);
        response.Flush();
        response.End();
    }

Good luck.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900