public ActionResult ConvertHtmlToPdf(IFormCollection collection) { // Create a HTML to PDF converter object with default settings HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter(); // Set license key received after purchase to use the converter in licensed mode // Leave it not set to use the converter in demo mode htmlToPdfConverter.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c="; // Set an adddional delay in seconds to wait for JavaScript or AJAX calls after page load completed // Set this property to 0 if you don't need to wait for such asynchcronous operations to finish htmlToPdfConverter.ConversionDelay = 2; Document pdfDocument = null; try { // Convert a HTML page to a PDF document object pdfDocument = htmlToPdfConverter.ConvertUrlToPdfDocumentObject(collection["urlTextBox"]); string javaScript = null; if (collection["JavaScriptAction"] == "alertMessageRadioButton") { // JavaScript to display an alert mesage javaScript = String.Format("app.alert(\"{0}\")", collection["alertMessageTextBox"]); } else if (collection["JavaScriptAction"] == "printDialogRadioButton") { // JavaScript to open the print dialog javaScript = "print()"; } else if (collection["JavaScriptAction"] == "zoomLevelRadioButton") { // JavaScript to set an initial zoom level javaScript = String.Format("zoom={0}", int.Parse(collection["zoomLevelTextBox"])); } // Set the JavaScript action pdfDocument.OpenAction.Action = new PdfActionJavaScript(javaScript); // Save the PDF document in a memory buffer byte[] outPdfBuffer = pdfDocument.Save(); // Send the PDF file to browser FileResult fileResult = new FileContentResult(outPdfBuffer, "application/pdf"); fileResult.FileDownloadName = "Execute_Acrobat_JavaScript.pdf"; return(fileResult); } finally { // Close the PDF document if (pdfDocument != null) { pdfDocument.Close(); } } }
public ActionResult ConvertHtmlToPdf(IFormCollection collection) { // Create a HTML to PDF converter object with default settings HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter(); // Set license key received after purchase to use the converter in licensed mode // Leave it not set to use the converter in demo mode htmlToPdfConverter.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c="; // Set an adddional delay in seconds to wait for JavaScript or AJAX calls after page load completed // Set this property to 0 if you don't need to wait for such asynchcronous operations to finish htmlToPdfConverter.ConversionDelay = 2; // Select all images from HTML page htmlToPdfConverter.HtmlElementsMappingOptions.HtmlElementSelectors = new string[] { "img" }; // Exclude the original images from rendering becuase they will be replaced by an image from local file system htmlToPdfConverter.HiddenHtmlElementsSelectors = new string[] { "img" }; Document pdfDocument = null; try { // Convert a HTML string with images to replace to a PDF document object pdfDocument = htmlToPdfConverter.ConvertUrlToPdfDocumentObject(collection["urlTextBox"]); // Replace the images selected in HTML using special attributes with images from local file system foreach (HtmlElementMapping imageElementInfo in htmlToPdfConverter.HtmlElementsMappingOptions.HtmlElementsMappingResult) { PdfPage imagePdfPage = imageElementInfo.PdfRectangles[0].PdfPage; RectangleF imageRectangle = imageElementInfo.PdfRectangles[0].Rectangle; ImageElement newImageElement = new ImageElement(imageRectangle.X, imageRectangle.Y, imageRectangle.Width, imageRectangle.Height, m_hostingEnvironment.ContentRootPath + "/wwwroot" + "/DemoAppFiles/Input/Images/box.jpg"); newImageElement.EnlargeEnabled = true; imagePdfPage.AddElement(newImageElement); } // Save the PDF document in a memory buffer byte[] outPdfBuffer = pdfDocument.Save(); // Send the PDF file to browser FileResult fileResult = new FileContentResult(outPdfBuffer, "application/pdf"); fileResult.FileDownloadName = "Replace_with_Higher_Quality_Images.pdf"; return(fileResult); } finally { // Close the PDF document if (pdfDocument != null) { pdfDocument.Close(); } } }
protected void convertToPdfButton_Click(object sender, EventArgs e) { // Create a HTML to PDF converter object with default settings HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter(); // Set license key received after purchase to use the converter in licensed mode // Leave it not set to use the converter in demo mode htmlToPdfConverter.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c="; // Set an adddional delay in seconds to wait for JavaScript or AJAX calls after page load completed // Set this property to 0 if you don't need to wait for such asynchcronous operations to finish htmlToPdfConverter.ConversionDelay = 2; Document pdfDocument = null; try { // Convert a HTML page to a PDF document object pdfDocument = htmlToPdfConverter.ConvertUrlToPdfDocumentObject(urlTextBox.Text); int goToPageNumber = int.Parse(pageNumberTextBox.Text); if (goToPageNumber > pdfDocument.Pages.Count) { return; } // Get destination PDF page PdfPage goToPage = pdfDocument.Pages[goToPageNumber - 1]; // Get the destination point in PDF page float goToX = float.Parse(xLocationTextBox.Text); float goToY = float.Parse(yLocationTextBox.Text); PointF goToLocation = new PointF(goToX, goToY); // Get the destination view mode DestinationViewMode viewMode = SelectedViewMode(); // Create the destination in PDF document ExplicitDestination goToDestination = new ExplicitDestination(goToPage, goToLocation, viewMode); // Set the zoom level when the destination is displayed if (viewMode == DestinationViewMode.XYZ) { goToDestination.ZoomPercentage = int.Parse(zoomLevelTextBox.Text); } // Set the document Go To open action pdfDocument.OpenAction.Action = new PdfActionGoTo(goToDestination); // Save the PDF document in a memory buffer byte[] outPdfBuffer = pdfDocument.Save(); // Send the PDF as response to browser // Set response content type Response.AddHeader("Content-Type", "application/pdf"); // Instruct the browser to open the PDF file as an attachment or inline Response.AddHeader("Content-Disposition", String.Format("attachment; filename=Go_To_Page_Open_Action.pdf; size={0}", outPdfBuffer.Length.ToString())); // Write the PDF document buffer to HTTP response Response.BinaryWrite(outPdfBuffer); // End the HTTP response and stop the current page processing Response.End(); } finally { // Close the PDF document if (pdfDocument != null) { pdfDocument.Close(); } } }
protected void convertToPdfButton_Click(object sender, EventArgs e) { // Create a HTML to PDF converter object with default settings HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter(); // Set license key received after purchase to use the converter in licensed mode // Leave it not set to use the converter in demo mode htmlToPdfConverter.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c="; // Set an adddional delay in seconds to wait for JavaScript or AJAX calls after page load completed // Set this property to 0 if you don't need to wait for such asynchcronous operations to finish htmlToPdfConverter.ConversionDelay = 2; Document pdfDocument = null; try { // Convert a HTML page to a PDF document object pdfDocument = htmlToPdfConverter.ConvertUrlToPdfDocumentObject(urlTextBox.Text); // Get the stamp width and height float stampWidth = float.Parse(stampWidthTextBox.Text); float stampHeight = float.Parse(stampHeightTextBox.Text); // Center the stamp at the top of PDF page float stampXLocation = (pdfDocument.Pages[0].ClientRectangle.Width - stampWidth) / 2; float stampYLocation = 0; RectangleF stampRectangle = new RectangleF(stampXLocation, stampYLocation, stampWidth, stampHeight); // Create the stamp template to be repeated in each PDF page Template stampTemplate = pdfDocument.AddTemplate(stampRectangle); // Create the HTML element to add in stamp template HtmlToPdfElement stampHtmlElement = new HtmlToPdfElement(htmlStringTextBox.Text, baseUrlTextBox.Text); // Set the HTML viewer width for the HTML added in stamp stampHtmlElement.HtmlViewerWidth = 600; // Fit the HTML content in stamp template stampHtmlElement.FitWidth = true; stampHtmlElement.FitHeight = true; // Add HTML to stamp template stampTemplate.AddElement(stampHtmlElement); // Save the PDF document in a memory buffer byte[] outPdfBuffer = pdfDocument.Save(); // Send the PDF as response to browser // Set response content type Response.AddHeader("Content-Type", "application/pdf"); // Instruct the browser to open the PDF file as an attachment or inline Response.AddHeader("Content-Disposition", String.Format("attachment; filename=Watermarks_and_Stamps.pdf; size={0}", outPdfBuffer.Length.ToString())); // Write the PDF document buffer to HTTP response Response.BinaryWrite(outPdfBuffer); // End the HTTP response and stop the current page processing Response.End(); } finally { // Close the PDF document if (pdfDocument != null) { pdfDocument.Close(); } } }
protected void convertToPdfButton_Click(object sender, EventArgs e) { // Get the server IP and port String serverIP = textBoxServerIP.Text; uint serverPort = uint.Parse(textBoxServerPort.Text); // Create a HTML to PDF converter object with default settings HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter(serverIP, serverPort); // Set optional service password if (textBoxServicePassword.Text.Length > 0) { htmlToPdfConverter.ServicePassword = textBoxServicePassword.Text; } // Set license key received after purchase to use the converter in licensed mode // Leave it not set to use the converter in demo mode htmlToPdfConverter.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c="; // Set an adddional delay in seconds to wait for JavaScript or AJAX calls after page load completed // Set this property to 0 if you don't need to wait for such asynchcronous operations to finish htmlToPdfConverter.ConversionDelay = 2; // Select the HTML elements for which to retrieve location and other information from HTML document htmlToPdfConverter.HtmlElementsMappingOptions.HtmlElementSelectors = new string[] { htmlElementsSelectorTextBox.Text }; // Convert HTML page to a PDF document object which can be further modified to highlight the selected elements Document pdfDocument = htmlToPdfConverter.ConvertUrlToPdfDocumentObject(urlTextBox.Text); // Highlight the selected elements in PDF with colored rectangles for (int i = 0; i < htmlToPdfConverter.HtmlElementsMappingOptions.HtmlElementsMappingResult.Count; i++) { HtmlElementMapping htmlElementInfo = htmlToPdfConverter.HtmlElementsMappingOptions.HtmlElementsMappingResult.GetByIndex(i); // Get other information about HTML element string htmlElementTagName = htmlElementInfo.HtmlElementTagName; string htmlElementID = htmlElementInfo.HtmlElementId; // Hightlight the HTML element in PDF // A HTML element can span over many PDF pages and therefore the mapping of the HTML element in PDF document consists // in a list of rectangles, one rectangle for each PDF page where this element was rendered foreach (PdfRectangle htmlElementLocationInPdf in htmlElementInfo.PdfRectangles) { // Get the HTML element location in PDF page PdfPage htmlElementPdfPage = pdfDocument.GetPage(htmlElementLocationInPdf.PageIndex); RectangleFloat htmlElementRectangleInPdfPage = htmlElementLocationInPdf.Rectangle; // Highlight the HTML element element with a colored rectangle in PDF RectangleElement highlightRectangle = new RectangleElement(htmlElementRectangleInPdfPage.X, htmlElementRectangleInPdfPage.Y, htmlElementRectangleInPdfPage.Width, htmlElementRectangleInPdfPage.Height); if (htmlElementTagName.ToLower() == "h1") { highlightRectangle.ForeColor = RgbColor.Blue; } else if (htmlElementTagName.ToLower() == "h2") { highlightRectangle.ForeColor = RgbColor.Green; } else if (htmlElementTagName.ToLower() == "h3") { highlightRectangle.ForeColor = RgbColor.Red; } else if (htmlElementTagName.ToLower() == "h4") { highlightRectangle.ForeColor = RgbColor.Yellow; } else if (htmlElementTagName.ToLower() == "h5") { highlightRectangle.ForeColor = RgbColor.Indigo; } else if (htmlElementTagName.ToLower() == "h6") { highlightRectangle.ForeColor = RgbColor.Orange; } else { highlightRectangle.ForeColor = RgbColor.Navy; } highlightRectangle.LineStyle.LineDashStyle = LineDashStyle.Solid; htmlElementPdfPage.AddElement(highlightRectangle); } } // Save the PDF document in a memory buffer byte[] outPdfBuffer = pdfDocument.Save(); // Send the PDF as response to browser // Set response content type Response.AddHeader("Content-Type", "application/pdf"); // Instruct the browser to open the PDF file as an attachment or inline Response.AddHeader("Content-Disposition", String.Format("attachment; filename=Select_in_API_HTML_Elements_to_Retrieve.pdf; size={0}", outPdfBuffer.Length.ToString())); // Write the PDF document buffer to HTTP response Response.BinaryWrite(outPdfBuffer); // End the HTTP response and stop the current page processing Response.End(); }
public ActionResult ConvertHtmlToPdf(IFormCollection collection) { // Create a HTML to PDF converter object with default settings htmlToPdfConverter = new HtmlToPdfConverter(); // Set license key received after purchase to use the converter in licensed mode // Leave it not set to use the converter in demo mode htmlToPdfConverter.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c="; // Set an adddional delay in seconds to wait for JavaScript or AJAX calls after page load completed // Set this property to 0 if you don't need to wait for such asynchcronous operations to finish htmlToPdfConverter.ConversionDelay = 2; // Enable header in the generated PDF document htmlToPdfConverter.PdfDocumentOptions.ShowHeader = true; string headerHtmlUrl = m_hostingEnvironment.ContentRootPath + "/wwwroot" + "/DemoAppFiles/Input/HTML_Files/Header_HTML.html"; Document documentObject = null; try { if (collection["AutoResize"] == "autoResizeHeaderRadioButton") { // Create a HTML element to be added in header HtmlToPdfElement headerHtml = new HtmlToPdfElement(headerHtmlUrl); // Install a handler where to set the automatically calculated header height headerHtml.NavigationCompletedEvent += new NavigationCompletedDelegate(headerHtml_NavigationCompletedEvent); // Add the HTML element to header // When the element is rendered in header by converter, the headerHtml_NavigationCompletedEvent handler // will be invoked and the header height will be automatically calculated htmlToPdfConverter.PdfHeaderOptions.AddElement(headerHtml); // Call the converter to produce a Document object documentObject = htmlToPdfConverter.ConvertUrlToPdfDocumentObject(collection["urlTextBox"]); // Uninstall the handler headerHtml.NavigationCompletedEvent -= new NavigationCompletedDelegate(headerHtml_NavigationCompletedEvent); // Draw a line at the header bottom if (drawHeaderLine) { float headerWidth = documentObject.Header.Width; float headerHeight = documentObject.Header.Height; // Create a line element for the bottom of the header LineElement headerLine = new LineElement(0, headerHeight - 1, headerWidth, headerHeight - 1); // Set line color headerLine.ForeColor = Color.Gray; // Add line element to the bottom of the header documentObject.Header.AddElement(headerLine); } // Save the PDF document in a memory buffer byte[] outPdfBuffer = documentObject.Save(); // Send the PDF file to browser FileResult fileResult = new FileContentResult(outPdfBuffer, "application/pdf"); fileResult.FileDownloadName = "Auto_Resize_Header_Footer.pdf"; return(fileResult); } else { // Create a HTML to PDF element to be added in header HtmlToPdfElement headerHtml = new HtmlToPdfElement(headerHtmlUrl); // Set a fixed header height in points htmlToPdfConverter.PdfHeaderOptions.HeaderHeight = float.Parse(collection["headerHeightTextBox"]); // Set the HTML element to fit the container height headerHtml.FitHeight = true; // Add HTML element to fit the fixed header height htmlToPdfConverter.PdfHeaderOptions.AddElement(headerHtml); // Draw a line at the header bottom if (drawHeaderLine) { // Calculate the header width based on PDF page size and margins float headerWidth = htmlToPdfConverter.PdfDocumentOptions.PdfPageSize.Width - htmlToPdfConverter.PdfDocumentOptions.LeftMargin - htmlToPdfConverter.PdfDocumentOptions.RightMargin; // Calculate header height float headerHeight = htmlToPdfConverter.PdfHeaderOptions.HeaderHeight; // Create a line element for the bottom of the header LineElement headerLine = new LineElement(0, headerHeight - 1, headerWidth, headerHeight - 1); // Set line color headerLine.ForeColor = Color.Gray; // Add line element to the bottom of the header htmlToPdfConverter.PdfHeaderOptions.AddElement(headerLine); } // Convert the HTML page to a PDF document in a memory buffer byte[] outPdfBuffer = htmlToPdfConverter.ConvertUrl(collection["urlTextBox"]); // Send the PDF file to browser FileResult fileResult = new FileContentResult(outPdfBuffer, "application/pdf"); fileResult.FileDownloadName = "Auto_Resize_Header_Footer.pdf"; return(fileResult); } } finally { // Close the PDF document if (documentObject != null) { documentObject.Close(); } } }
public ActionResult ConvertHtmlToPdf(IFormCollection collection) { // Create a HTML to PDF converter object with default settings HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter(); // Set license key received after purchase to use the converter in licensed mode // Leave it not set to use the converter in demo mode htmlToPdfConverter.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c="; // Set an adddional delay in seconds to wait for JavaScript or AJAX calls after page load completed // Set this property to 0 if you don't need to wait for such asynchcronous operations to finish htmlToPdfConverter.ConversionDelay = 2; Document pdfDocument = null; try { // Convert a HTML page to a PDF document object pdfDocument = htmlToPdfConverter.ConvertUrlToPdfDocumentObject(collection["urlTextBox"]); // Get the stamp width and height float stampWidth = float.Parse(collection["stampWidthTextBox"]); float stampHeight = float.Parse(collection["stampHeightTextBox"]); // Center the stamp at the top of PDF page float stampXLocation = (pdfDocument.Pages[0].ClientRectangle.Width - stampWidth) / 2; float stampYLocation = 0; RectangleF stampRectangle = new RectangleF(stampXLocation, stampYLocation, stampWidth, stampHeight); // Create the stamp template to be repeated in each PDF page Template stampTemplate = pdfDocument.AddTemplate(stampRectangle); // Create the HTML element to add in stamp template HtmlToPdfElement stampHtmlElement = new HtmlToPdfElement(collection["htmlStringTextBox"], collection["baseUrlTextBox"]); // Set the HTML viewer width for the HTML added in stamp stampHtmlElement.HtmlViewerWidth = 600; // Fit the HTML content in stamp template stampHtmlElement.FitWidth = true; stampHtmlElement.FitHeight = true; // Add HTML to stamp template stampTemplate.AddElement(stampHtmlElement); // Save the PDF document in a memory buffer byte[] outPdfBuffer = pdfDocument.Save(); // Send the PDF file to browser FileResult fileResult = new FileContentResult(outPdfBuffer, "application/pdf"); fileResult.FileDownloadName = "Watermarks_and_Stamps.pdf"; return(fileResult); } finally { // Close the PDF document if (pdfDocument != null) { pdfDocument.Close(); } } }
public ActionResult ConvertHtmlToPdf(IFormCollection collection) { // Create a HTML to PDF converter object with default settings HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter(); // Set license key received after purchase to use the converter in licensed mode // Leave it not set to use the converter in demo mode htmlToPdfConverter.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c="; // Set an adddional delay in seconds to wait for JavaScript or AJAX calls after page load completed // Set this property to 0 if you don't need to wait for such asynchcronous operations to finish htmlToPdfConverter.ConversionDelay = 2; Document pdfDocument = null; try { // Convert a HTML page to a PDF document object pdfDocument = htmlToPdfConverter.ConvertUrlToPdfDocumentObject(collection["urlTextBox"]); int goToPageNumber = int.Parse(collection["pageNumberTextBox"]); if (goToPageNumber > pdfDocument.Pages.Count) { goToPageNumber = 1; } // Get destination PDF page PdfPage goToPage = pdfDocument.Pages[goToPageNumber - 1]; // Get the destination point in PDF page float goToX = float.Parse(collection["xLocationTextBox"]); float goToY = float.Parse(collection["yLocationTextBox"]); PointF goToLocation = new PointF(goToX, goToY); // Get the destination view mode DestinationViewMode viewMode = SelectedViewMode(collection["viewModeComboBox"]); // Create the destination in PDF document ExplicitDestination goToDestination = new ExplicitDestination(goToPage, goToLocation, viewMode); // Set the zoom level when the destination is displayed if (viewMode == DestinationViewMode.XYZ) { goToDestination.ZoomPercentage = int.Parse(collection["zoomLevelTextBox"]); } // Set the document Go To open action pdfDocument.OpenAction.Action = new PdfActionGoTo(goToDestination); // Save the PDF document in a memory buffer byte[] outPdfBuffer = pdfDocument.Save(); // Send the PDF file to browser FileResult fileResult = new FileContentResult(outPdfBuffer, "application/pdf"); fileResult.FileDownloadName = "Go_To_Page_Open_Action.pdf"; return(fileResult); } finally { // Close the PDF document if (pdfDocument != null) { pdfDocument.Close(); } } }
protected void convertToPdfButton_Click(object sender, EventArgs e) { // Create a HTML to PDF converter object with default settings HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter(); // Set license key received after purchase to use the converter in licensed mode // Leave it not set to use the converter in demo mode htmlToPdfConverter.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c="; // Set an adddional delay in seconds to wait for JavaScript or AJAX calls after page load completed // Set this property to 0 if you don't need to wait for such asynchcronous operations to finish htmlToPdfConverter.ConversionDelay = 2; Document pdfDocument = null; try { // Convert a HTML page to a PDF document object pdfDocument = htmlToPdfConverter.ConvertUrlToPdfDocumentObject(urlTextBox.Text); string javaScript = null; if (alertMessageRadioButton.Checked) { // JavaScript to display an alert mesage javaScript = String.Format("app.alert(\"{0}\")", alertMessageTextBox.Text); } else if (printDialogRadioButton.Checked) { // JavaScript to open the print dialog javaScript = "print()"; } else if (zoomLevelRadioButton.Checked) { // JavaScript to set an initial zoom level javaScript = String.Format("zoom={0}", int.Parse(zoomLevelTextBox.Text)); } // Set the JavaScript action pdfDocument.OpenAction.Action = new PdfActionJavaScript(javaScript); // Save the PDF document in a memory buffer byte[] outPdfBuffer = pdfDocument.Save(); // Send the PDF as response to browser // Set response content type Response.AddHeader("Content-Type", "application/pdf"); // Instruct the browser to open the PDF file as an attachment or inline Response.AddHeader("Content-Disposition", String.Format("attachment; filename=Execute_Acrobat_JavaScript.pdf; size={0}", outPdfBuffer.Length.ToString())); // Write the PDF document buffer to HTTP response Response.BinaryWrite(outPdfBuffer); // End the HTTP response and stop the current page processing Response.End(); } finally { // Close the PDF document if (pdfDocument != null) { pdfDocument.Close(); } } }
public ActionResult ConvertHtmlToPdf(IFormCollection collection) { // Create a HTML to PDF converter object with default settings HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter(); // Set license key received after purchase to use the converter in licensed mode // Leave it not set to use the converter in demo mode htmlToPdfConverter.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c="; // Set an adddional delay in seconds to wait for JavaScript or AJAX calls after page load completed // Set this property to 0 if you don't need to wait for such asynchcronous operations to finish htmlToPdfConverter.ConversionDelay = 2; Document pdfDocument = null; try { // Convert HTML page or string with mapping attributes to a PDF document object // The document can be further modified to highlight the selected elements if (collection["HtmlPageSource"] == "convertHtmlRadioButton") { string htmlWithMappingAttributes = collection["htmlStringTextBox"]; string baseUrl = collection["baseUrlTextBox"]; // Convert a HTML string with mapping attributes to a PDF document object pdfDocument = htmlToPdfConverter.ConvertHtmlToPdfDocumentObject(htmlWithMappingAttributes, baseUrl); } else { string url = collection["urlTextBox"]; // Convert a HTML page with mapping attributes to a PDF document object pdfDocument = htmlToPdfConverter.ConvertUrlToPdfDocumentObject(url); } // Display detailed information about the selected elements StringBuilder htmlElementInfoBuilder = new StringBuilder(); foreach (HtmlElementMapping htmlElementInfo in htmlToPdfConverter.HtmlElementsMappingOptions.HtmlElementsMappingResult) { // Get other information about HTML element string htmlElementTagName = htmlElementInfo.HtmlElementTagName; string htmlElementID = htmlElementInfo.HtmlElementId; string htmlElementMappingID = htmlElementInfo.MappingId; string htmlElementCssClasssName = htmlElementInfo.HtmlElementCssClassName; string htmlElementHtmlCode = htmlElementInfo.HtmlElementOuterHtml; string htmlElementInnerHtml = htmlElementInfo.HtmlElementInnerHtml; string htmlElementText = htmlElementInfo.HtmlElementText; System.Collections.Specialized.NameValueCollection htmlElementAttributes = htmlElementInfo.HtmlElementAttributes; HtmlElementPdfRectangle[] htmlElementRectanglesInPdf = htmlElementInfo.PdfRectangles; htmlElementInfoBuilder.AppendFormat("<br/>---------------------------------------- HTML Element Info ----------------------------------------<br/><br/>"); htmlElementInfoBuilder.AppendFormat("<b>Tag Name:</b> {0}<br/>", htmlElementTagName); htmlElementInfoBuilder.AppendFormat("<b>Element ID:</b> {0}<br/>", htmlElementID); htmlElementInfoBuilder.AppendFormat("<b>Mapping ID:</b> {0}<br/>", htmlElementMappingID); htmlElementInfoBuilder.AppendFormat("<b>Text:</b> {0}<br/>", htmlElementText); htmlElementInfoBuilder.AppendFormat("<b>Attributes:</b><br/>"); for (int i = 0; i < htmlElementAttributes.Count; i++) { htmlElementInfoBuilder.AppendFormat(" {0} = \"{1}\"<br/>", htmlElementAttributes.GetKey(i), htmlElementAttributes.Get(i)); } htmlElementInfoBuilder.AppendFormat("<b>Location in PDF:</b><br/>"); for (int i = 0; i < htmlElementRectanglesInPdf.Length; i++) { PdfPage pdfPage = htmlElementRectanglesInPdf[i].PdfPage; int pdfPageIndex = htmlElementRectanglesInPdf[i].PageIndex; RectangleF rectangleInPdfPage = htmlElementRectanglesInPdf[i].Rectangle; htmlElementInfoBuilder.AppendFormat(" PDF Page Index: {0}<br>", pdfPageIndex); htmlElementInfoBuilder.AppendFormat(" Rectangle: X = {0:N2} pt , Y = {1:N2} pt , W = {2:N2} pt , H = {3:N2} pt<br/>", rectangleInPdfPage.X, rectangleInPdfPage.Y, rectangleInPdfPage.Width, rectangleInPdfPage.Height); } htmlElementInfoBuilder.AppendFormat("<br/>"); } PdfPage lastPdfPage = htmlToPdfConverter.ConversionSummary.LastPdfPage; RectangleF lastPageRectangle = htmlToPdfConverter.ConversionSummary.LastPageRectangle; HtmlToPdfElement htmlElementInfoHtml = new HtmlToPdfElement(0, lastPageRectangle.Bottom + 1, htmlElementInfoBuilder.ToString(), null); lastPdfPage.AddElement(htmlElementInfoHtml); // Save the PDF document in a memory buffer byte[] outPdfBuffer = pdfDocument.Save(); // Send the PDF file to browser FileResult fileResult = new FileContentResult(outPdfBuffer, "application/pdf"); fileResult.FileDownloadName = "Select_in_HTML_Elements_to_Retrieve.pdf"; return(fileResult); } finally { // Close the PDF document if (pdfDocument != null) { pdfDocument.Close(); } } }
public ActionResult ConvertHtmlToPdf(IFormCollection collection) { // Create a HTML to PDF converter object with default settings HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter(); // Set license key received after purchase to use the converter in licensed mode // Leave it not set to use the converter in demo mode htmlToPdfConverter.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c="; // Set an adddional delay in seconds to wait for JavaScript or AJAX calls after page load completed // Set this property to 0 if you don't need to wait for such asynchcronous operations to finish htmlToPdfConverter.ConversionDelay = 2; // Select the HTML elements for which to retrieve location and other information from HTML document htmlToPdfConverter.HtmlElementsMappingOptions.HtmlElementSelectors = new string[] { collection["htmlElementsSelectorTextBox"] }; Document pdfDocument = null; try { // Convert HTML page to a PDF document object which can be further modified to highlight the selected elements pdfDocument = htmlToPdfConverter.ConvertUrlToPdfDocumentObject(collection["urlTextBox"]); // Highlight the selected elements in PDF with colored rectangles foreach (HtmlElementMapping htmlElementInfo in htmlToPdfConverter.HtmlElementsMappingOptions.HtmlElementsMappingResult) { // Get other information about HTML element string htmlElementTagName = htmlElementInfo.HtmlElementTagName; string htmlElementID = htmlElementInfo.HtmlElementId; // Hightlight the HTML element in PDF // A HTML element can span over many PDF pages and therefore the mapping of the HTML element in PDF document consists // in a list of rectangles, one rectangle for each PDF page where this element was rendered foreach (HtmlElementPdfRectangle htmlElementLocationInPdf in htmlElementInfo.PdfRectangles) { // Get the HTML element location in PDF page PdfPage htmlElementPdfPage = htmlElementLocationInPdf.PdfPage; RectangleF htmlElementRectangleInPdfPage = htmlElementLocationInPdf.Rectangle; // Highlight the HTML element element with a colored rectangle in PDF RectangleElement highlightRectangle = new RectangleElement(htmlElementRectangleInPdfPage.X, htmlElementRectangleInPdfPage.Y, htmlElementRectangleInPdfPage.Width, htmlElementRectangleInPdfPage.Height); if (htmlElementTagName.ToLower() == "h1") { highlightRectangle.ForeColor = Color.Blue; } else if (htmlElementTagName.ToLower() == "h2") { highlightRectangle.ForeColor = Color.Green; } else if (htmlElementTagName.ToLower() == "h3") { highlightRectangle.ForeColor = Color.Red; } else if (htmlElementTagName.ToLower() == "h4") { highlightRectangle.ForeColor = Color.Yellow; } else if (htmlElementTagName.ToLower() == "h5") { highlightRectangle.ForeColor = Color.Indigo; } else if (htmlElementTagName.ToLower() == "h6") { highlightRectangle.ForeColor = Color.Orange; } else { highlightRectangle.ForeColor = Color.Navy; } highlightRectangle.LineStyle.LineDashStyle = LineDashStyle.Solid; htmlElementPdfPage.AddElement(highlightRectangle); } } // Save the PDF document in a memory buffer byte[] outPdfBuffer = pdfDocument.Save(); // Send the PDF file to browser FileResult fileResult = new FileContentResult(outPdfBuffer, "application/pdf"); fileResult.FileDownloadName = "Select_in_API_HTML_Elements_to_Retrieve.pdf"; return(fileResult); } finally { // Close the PDF document if (pdfDocument != null) { pdfDocument.Close(); } } }
protected void convertToPdfButton_Click(object sender, EventArgs e) { // Create a HTML to PDF converter object with default settings htmlToPdfConverter = new HtmlToPdfConverter(); // Set license key received after purchase to use the converter in licensed mode // Leave it not set to use the converter in demo mode htmlToPdfConverter.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c="; // Set an adddional delay in seconds to wait for JavaScript or AJAX calls after page load completed // Set this property to 0 if you don't need to wait for such asynchcronous operations to finish htmlToPdfConverter.ConversionDelay = 2; // Enable header in the generated PDF document htmlToPdfConverter.PdfDocumentOptions.ShowHeader = true; string headerHtmlUrl = Server.MapPath("~/DemoAppFiles/Input/HTML_Files/Header_HTML.html"); Document documentObject = null; try { if (autoResizeHeaderRadioButton.Checked) { // Create a HTML element to be added in header HtmlToPdfElement headerHtml = new HtmlToPdfElement(headerHtmlUrl); // Install a handler where to set the automatically calculated header height headerHtml.NavigationCompletedEvent += new NavigationCompletedDelegate(headerHtml_NavigationCompletedEvent); // Add the HTML element to header // When the element is rendered in header by converter, the headerHtml_NavigationCompletedEvent handler // will be invoked and the header height will be automatically calculated htmlToPdfConverter.PdfHeaderOptions.AddElement(headerHtml); // Call the converter to produce a Document object documentObject = htmlToPdfConverter.ConvertUrlToPdfDocumentObject(urlTextBox.Text); // Uninstall the handler headerHtml.NavigationCompletedEvent -= new NavigationCompletedDelegate(headerHtml_NavigationCompletedEvent); // Draw a line at the header bottom if (drawHeaderLine) { float headerWidth = documentObject.Header.Width; float headerHeight = documentObject.Header.Height; // Create a line element for the bottom of the header LineElement headerLine = new LineElement(0, headerHeight - 1, headerWidth, headerHeight - 1); // Set line color headerLine.ForeColor = Color.Gray; // Add line element to the bottom of the header documentObject.Header.AddElement(headerLine); } // Save the PDF document in a memory buffer byte[] outPdfBuffer = documentObject.Save(); // Send the PDF as response to browser // Set response content type Response.AddHeader("Content-Type", "application/pdf"); // Instruct the browser to open the PDF file as an attachment or inline Response.AddHeader("Content-Disposition", String.Format("attachment; filename=Auto_Resize_Header_Footer.pdf; size={0}", outPdfBuffer.Length.ToString())); // Write the PDF document buffer to HTTP response Response.BinaryWrite(outPdfBuffer); // End the HTTP response and stop the current page processing Response.End(); } else { // Create a HTML to PDF element to be added in header HtmlToPdfElement headerHtml = new HtmlToPdfElement(headerHtmlUrl); // Set a fixed header height in points htmlToPdfConverter.PdfHeaderOptions.HeaderHeight = float.Parse(headerHeightTextBox.Text); // Set the HTML element to fit the container height headerHtml.FitHeight = true; // Add HTML element to fit the fixed header height htmlToPdfConverter.PdfHeaderOptions.AddElement(headerHtml); // Draw a line at the header bottom if (drawHeaderLine) { // Calculate the header width based on PDF page size and margins float headerWidth = htmlToPdfConverter.PdfDocumentOptions.PdfPageSize.Width - htmlToPdfConverter.PdfDocumentOptions.LeftMargin - htmlToPdfConverter.PdfDocumentOptions.RightMargin; // Calculate header height float headerHeight = htmlToPdfConverter.PdfHeaderOptions.HeaderHeight; // Create a line element for the bottom of the header LineElement headerLine = new LineElement(0, headerHeight - 1, headerWidth, headerHeight - 1); // Set line color headerLine.ForeColor = Color.Gray; // Add line element to the bottom of the header htmlToPdfConverter.PdfHeaderOptions.AddElement(headerLine); } // Convert the HTML page to a PDF document in a memory buffer byte[] outPdfBuffer = htmlToPdfConverter.ConvertUrl(urlTextBox.Text); // Send the PDF as response to browser // Set response content type Response.AddHeader("Content-Type", "application/pdf"); // Instruct the browser to open the PDF file as an attachment or inline Response.AddHeader("Content-Disposition", String.Format("attachment; filename=Auto_Resize_Header_Footer.pdf; size={0}", outPdfBuffer.Length.ToString())); // Write the PDF document buffer to HTTP response Response.BinaryWrite(outPdfBuffer); // End the HTTP response and stop the current page processing Response.End(); } } finally { // Close the PDF document if (documentObject != null) { documentObject.Close(); } } }
protected void convertToPdfButton_Click(object sender, EventArgs e) { // Get the server IP and port String serverIP = textBoxServerIP.Text; uint serverPort = uint.Parse(textBoxServerPort.Text); // Create a HTML to PDF converter object with default settings HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter(serverIP, serverPort); // Set optional service password if (textBoxServicePassword.Text.Length > 0) { htmlToPdfConverter.ServicePassword = textBoxServicePassword.Text; } // Set license key received after purchase to use the converter in licensed mode // Leave it not set to use the converter in demo mode htmlToPdfConverter.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c="; // Set an adddional delay in seconds to wait for JavaScript or AJAX calls after page load completed // Set this property to 0 if you don't need to wait for such asynchcronous operations to finish htmlToPdfConverter.ConversionDelay = 2; // Select all images from HTML page htmlToPdfConverter.HtmlElementsMappingOptions.HtmlElementSelectors = new string[] { "img" }; // Exclude the original images from rendering becuase they will be replaced by an image from local file system htmlToPdfConverter.HiddenHtmlElementsSelectors = new string[] { "img" }; // Convert a HTML string with images to replace to a PDF document object Document pdfDocument = htmlToPdfConverter.ConvertUrlToPdfDocumentObject(urlTextBox.Text); // Replace the images selected in HTML using special attributes with images from local file system int mappingsCount = htmlToPdfConverter.HtmlElementsMappingOptions.HtmlElementsMappingResult.Count; for (int i = 0; i < mappingsCount; i++) { HtmlElementMapping imageElementInfo = htmlToPdfConverter.HtmlElementsMappingOptions.HtmlElementsMappingResult.GetByIndex(i); PdfPage imagePdfPage = pdfDocument.GetPage(imageElementInfo.PdfRectangles[0].PageIndex); RectangleFloat imageRectangle = imageElementInfo.PdfRectangles[0].Rectangle; ImageElement newImageElement = new ImageElement(imageRectangle.X, imageRectangle.Y, imageRectangle.Width, imageRectangle.Height, Server.MapPath("~/DemoAppFiles/Input/Images/box.jpg")); newImageElement.EnlargeEnabled = true; imagePdfPage.AddElement(newImageElement); } // Save the PDF document in a memory buffer byte[] outPdfBuffer = pdfDocument.Save(); // Send the PDF as response to browser // Set response content type Response.AddHeader("Content-Type", "application/pdf"); // Instruct the browser to open the PDF file as an attachment or inline Response.AddHeader("Content-Disposition", String.Format("attachment; filename=Replace_with_Higher_Quality_Images.pdf; size={0}", outPdfBuffer.Length.ToString())); // Write the PDF document buffer to HTTP response Response.BinaryWrite(outPdfBuffer); // End the HTTP response and stop the current page processing Response.End(); }
protected void convertToPdfButton_Click(object sender, EventArgs e) { // Get the server IP and port String serverIP = textBoxServerIP.Text; uint serverPort = uint.Parse(textBoxServerPort.Text); // Create a HTML to PDF converter object with default settings HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter(serverIP, serverPort); // Set optional service password if (textBoxServicePassword.Text.Length > 0) { htmlToPdfConverter.ServicePassword = textBoxServicePassword.Text; } // Set license key received after purchase to use the converter in licensed mode // Leave it not set to use the converter in demo mode htmlToPdfConverter.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c="; // Set an adddional delay in seconds to wait for JavaScript or AJAX calls after page load completed // Set this property to 0 if you don't need to wait for such asynchcronous operations to finish htmlToPdfConverter.ConversionDelay = 2; Document pdfDocument = null; // Convert HTML page or string with mapping attributes to a PDF document object // The document can be further modified to highlight the selected elements if (convertHtmlRadioButton.Checked) { string htmlWithMappingAttributes = htmlStringTextBox.Text; string baseUrl = baseUrlTextBox.Text; // Convert a HTML string with mapping attributes to a PDF document object pdfDocument = htmlToPdfConverter.ConvertHtmlToPdfDocumentObject(htmlWithMappingAttributes, baseUrl); } else { string url = urlTextBox.Text; // Convert a HTML page with mapping attributes to a PDF document object pdfDocument = htmlToPdfConverter.ConvertUrlToPdfDocumentObject(url); } // Display detailed information about the selected elements StringBuilder htmlElementInfoBuilder = new StringBuilder(); for (int mappingIndex = 0; mappingIndex < htmlToPdfConverter.HtmlElementsMappingOptions.HtmlElementsMappingResult.Count; mappingIndex++) { HtmlElementMapping htmlElementInfo = htmlToPdfConverter.HtmlElementsMappingOptions.HtmlElementsMappingResult.GetByIndex(mappingIndex); // Get other information about HTML element string htmlElementTagName = htmlElementInfo.HtmlElementTagName; string htmlElementID = htmlElementInfo.HtmlElementId; string htmlElementMappingID = htmlElementInfo.MappingId; string htmlElementCssClasssName = htmlElementInfo.HtmlElementCssClassName; string htmlElementHtmlCode = htmlElementInfo.HtmlElementOuterHtml; string htmlElementInnerHtml = htmlElementInfo.HtmlElementInnerHtml; string htmlElementText = htmlElementInfo.HtmlElementText; NameValuePairsCollection htmlElementAttributes = htmlElementInfo.HtmlElementAttributes; PdfRectangle[] htmlElementRectanglesInPdf = htmlElementInfo.PdfRectangles; htmlElementInfoBuilder.AppendFormat("<br/>---------------------------------------- HTML Element Info ----------------------------------------<br/><br/>"); htmlElementInfoBuilder.AppendFormat("<b>Tag Name:</b> {0}<br/>", htmlElementTagName); htmlElementInfoBuilder.AppendFormat("<b>Element ID:</b> {0}<br/>", htmlElementID); htmlElementInfoBuilder.AppendFormat("<b>Mapping ID:</b> {0}<br/>", htmlElementMappingID); htmlElementInfoBuilder.AppendFormat("<b>Text:</b> {0}<br/>", htmlElementText); htmlElementInfoBuilder.AppendFormat("<b>Attributes:</b><br/>"); for (int i = 0; i < htmlElementAttributes.Count; i++) { NameValuePair nameValuePair = htmlElementAttributes.GetByIndex(i); htmlElementInfoBuilder.AppendFormat(" {0} = \"{1}\"<br/>", nameValuePair.Name, nameValuePair.Value); } htmlElementInfoBuilder.AppendFormat("<b>Location in PDF:</b><br/>"); for (int i = 0; i < htmlElementRectanglesInPdf.Length; i++) { PdfPage pdfPage = pdfDocument.GetPage(htmlElementRectanglesInPdf[i].PageIndex); int pdfPageIndex = htmlElementRectanglesInPdf[i].PageIndex; RectangleFloat rectangleInPdfPage = htmlElementRectanglesInPdf[i].Rectangle; htmlElementInfoBuilder.AppendFormat(" PDF Page Index: {0}<br>", pdfPageIndex); htmlElementInfoBuilder.AppendFormat(" Rectangle: X = {0:N2} pt , Y = {1:N2} pt , W = {2:N2} pt , H = {3:N2} pt<br/>", rectangleInPdfPage.X, rectangleInPdfPage.Y, rectangleInPdfPage.Width, rectangleInPdfPage.Height); } htmlElementInfoBuilder.AppendFormat("<br/>"); } PdfPage lastPdfPage = pdfDocument.GetPage(htmlToPdfConverter.ConversionSummary.LastPageIndex); RectangleFloat lastPageRectangle = htmlToPdfConverter.ConversionSummary.LastPageRectangle; HtmlToPdfElement htmlElementInfoHtml = new HtmlToPdfElement(0, lastPageRectangle.Y + lastPageRectangle.Height + 1, htmlElementInfoBuilder.ToString(), null); lastPdfPage.AddElement(htmlElementInfoHtml); // Save the PDF document in a memory buffer byte[] outPdfBuffer = pdfDocument.Save(); // Send the PDF as response to browser // Set response content type Response.AddHeader("Content-Type", "application/pdf"); // Instruct the browser to open the PDF file as an attachment or inline Response.AddHeader("Content-Disposition", String.Format("attachment; filename=Select_in_HTML_Elements_to_Retrieve.pdf; size={0}", outPdfBuffer.Length.ToString())); // Write the PDF document buffer to HTTP response Response.BinaryWrite(outPdfBuffer); // End the HTTP response and stop the current page processing Response.End(); }