We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Unable to exclude HTML element when converting aspx page to pdf

            <div id="exportButton">
                <asp:Button Width="150px" Style="margin-left: 3px;" data-exclude="true" ID="Button1" Height="27px" runat="server" 
                    OnClick="Button1_Click" Text="Download Report" />

 protected void Button1_Click(object sender, EventArgs e)
            //Initialize HTML to PDF converter 
            HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.WebKit);
            WebKitConverterSettings settings = new WebKitConverterSettings();
            //Set WebKit path
            settings.WebKitPath = Server.MapPath("~/QtBinaries");
            //Assign WebKit settings to HTML converter
            htmlConverter.ConverterSettings = settings;
            //Get the current URL
            string url = HttpContext.Current.Request.Url.AbsoluteUri;
            //Convert URL to PDF
            PdfDocument document = htmlConverter.Convert(url);
            //Save the document
            string filename = "Risk_Report" + DateTime.Now.ToString();
            Session["filename"] = filename;
            document.Save(filename+".pdf", HttpContext.Current.Response, HttpReadType.Save);


Need to remove the Div from the pdf that gets downloaded. Kindly assist.

1 Reply

PV Prakash Viswanathan Syncfusion Team February 10, 2020 08:53 AM UTC

Hi Roshandeep, 
Thank you for contacting Syncfusion support.  
You can exclude the HTML element while printing/HTML conversion with print media type CSS. We have attached a simple sample, where the button only visible on screen media type. We need to set the button hide styles in print media type and convert it to PDF with Print media type in the converter settings. Please find the code snippet and styles below, 
Kindly copy the QtBinaries folder from HTML converter NuGet package folder to project folder before performing the conversion. 
Print media: 
//Set print media type 
settings.MediaType = MediaType.Print; 
<style type="text/css"> 
     @media print { 
          #exportButton { 
                display: none; 
<div id="exportButton"> 
     <asp:Button Width="150px" Style="margin-left: 3px;" data-exclude="true" ID="Button1" Height="27px" runat="server" OnClick="Button1_Click" Text="Download Report" /> 
Also, you can convert the particular HTML element by providing the HTML element ID in the Partial HTML conversion. Please refer below link for more information. 
Please let us know if you need any further assistance on this.  
Prakash V 

Live Chat Icon For mobile
Up arrow icon