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; string htmlWithLinksAndAttachMarkers = htmlStringTextBox.Text; string baseUrl = baseUrlTextBox.Text; // Convert a HTML string with markers for file links and attachments to a PDF document object Document pdfDocument = htmlToPdfConverter.ConvertHtmlToPdfDocumentObject(htmlWithLinksAndAttachMarkers, baseUrl); // Display the attachments panel when the PDF document is opened in a PDF viewer pdfDocument.ViewerPreferences.PageMode = ViewerPageMode.UseAttachments; // Create File Attachments // Create an attachment from a file without icon string fileAttachmentPath = Server.MapPath("~/DemoAppFiles/Input/Attach_Files/Attachment_File.txt"); pdfDocument.AddFileAttachment(fileAttachmentPath, "Attachment from File"); // Create an attachment from a stream without icon string fileStreamAttachmentPath = Server.MapPath("~/DemoAppFiles/Input/Attach_Files/Attachment_Stream.txt"); byte[] attachmentData = System.IO.File.ReadAllBytes(fileStreamAttachmentPath); pdfDocument.AddFileAttachment(attachmentData, "Attachment_Stream.txt", "Attachment from Stream"); // Create an attachment from file with paperclip icon in PDF HtmlElementMapping attachFromFileIconMapping = htmlToPdfConverter.HtmlElementsMappingOptions.HtmlElementsMappingResult.GetElementByMappingId("attach_from_file_icon"); if (attachFromFileIconMapping != null) { PdfPage attachFromFilePage = pdfDocument.GetPage(attachFromFileIconMapping.PdfRectangles[0].PageIndex); RectangleFloat attachFromFileIconRectangle = attachFromFileIconMapping.PdfRectangles[0].Rectangle; string fileAttachmentWithIconPath = Server.MapPath("~/DemoAppFiles/Input/Attach_Files/Attachment_File_Icon.txt"); // Create the attachment from file FileAttachmentElement attachFromFileElement = new FileAttachmentElement(attachFromFileIconRectangle, fileAttachmentWithIconPath); attachFromFileElement.IconType = FileAttachmentIcon.Paperclip; attachFromFileElement.Text = "Attachment from File with Paperclip Icon"; attachFromFileElement.IconColor = RgbColor.Blue; attachFromFilePage.AddElement(attachFromFileElement); } // Create an attachment from stream with pushpin icon in PDF byte[] attachmentDataWithIcon = null; HtmlElementMapping attachFromStreamIconMapping = htmlToPdfConverter.HtmlElementsMappingOptions.HtmlElementsMappingResult.GetElementByMappingId("attach_from_stream_icon"); if (attachFromStreamIconMapping != null) { PdfPage attachFromStreamPage = pdfDocument.GetPage(attachFromStreamIconMapping.PdfRectangles[0].PageIndex); RectangleFloat attachFromStreamIconRectangle = attachFromStreamIconMapping.PdfRectangles[0].Rectangle; string fileStreamAttachmentWithIconPath = Server.MapPath("~/DemoAppFiles/Input/Attach_Files/Attachment_Stream_Icon.txt"); attachmentDataWithIcon = System.IO.File.ReadAllBytes(fileStreamAttachmentWithIconPath); // Create the attachment from stream FileAttachmentElement attachFromStreamElement = new FileAttachmentElement(attachFromStreamIconRectangle, attachmentDataWithIcon, "Attachment_Stream_Icon.txt"); attachFromStreamElement.IconType = FileAttachmentIcon.PushPin; attachFromStreamElement.Text = "Attachment from Stream with Pushpin Icon"; attachFromStreamElement.IconColor = RgbColor.Green; attachFromStreamPage.AddElement(attachFromStreamElement); } // 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=File_Links_and_Attachments.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 createPdfButton_Click(object sender, EventArgs e) { // Get the server IP and port String serverIP = textBoxServerIP.Text; uint serverPort = uint.Parse(textBoxServerPort.Text); // Create a PDF document Document pdfDocument = new Document(serverIP, serverPort); // Set optional service password if (textBoxServicePassword.Text.Length > 0) { pdfDocument.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 pdfDocument.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c="; // Display the attachments panel when the PDF document is opened in a PDF viewer pdfDocument.ViewerPreferences.PageMode = ViewerPageMode.UseAttachments; // Add a page to PDF document PdfPage pdfPage = pdfDocument.AddPage(); // The titles font used to mark various sections of the PDF document PdfFont titleFont = new PdfFont("Times New Roman", 10, true); titleFont.Bold = true; PdfFont subtitleFont = new PdfFont("Times New Roman", 8, true); // Add document title TextElement titleTextElement = new TextElement(5, 5, "Attach Files and Streams to a PDF Document", titleFont); pdfPage.AddElement(titleTextElement); // Create an attachment from a file without icon string fileAttachmentPath = Server.MapPath("~/DemoAppFiles/Input/Attach_Files/Attachment_File.txt"); pdfDocument.AddFileAttachment(fileAttachmentPath, "Attachment from File"); // Create an attachment from a stream without icon string fileStreamAttachmentPath = Server.MapPath("~/DemoAppFiles/Input/Attach_Files/Attachment_Stream.txt"); byte[] attachmentData = System.IO.File.ReadAllBytes(fileStreamAttachmentPath); pdfDocument.AddFileAttachment(attachmentData, "Attachment_Stream.txt", "Attachment from Stream"); // Add the text element string text = "Click the next icon to open the attachment from a file:"; TextElement textElement = new TextElement(0, 0, 200, text, subtitleFont); pdfDocument.AddElement(textElement, 15); // Create an attachment from file with paperclip icon in PDF string fileAttachmentWithIconPath = Server.MapPath("~/DemoAppFiles/Input/Attach_Files/Attachment_File_Icon.txt"); // Create the attachment from file RectangleFloat attachFromFileIconRectangle = new RectangleFloat(0, 0, 6, 10); FileAttachmentElement attachFromFileElement = new FileAttachmentElement(attachFromFileIconRectangle, fileAttachmentWithIconPath); attachFromFileElement.IconType = FileAttachmentIcon.Paperclip; attachFromFileElement.Text = "Attachment from File with Paperclip Icon"; attachFromFileElement.IconColor = RgbColor.Blue; pdfDocument.AddElement(attachFromFileElement, 10, true, false, 0, true, false); // Add the text element text = "Click the next icon to open the attachment from a stream:"; textElement = new TextElement(0, 0, 200, text, subtitleFont); pdfDocument.AddElement(textElement, 5, false, 10, true); // Create an attachment from stream with pushpin icon in PDF string fileStreamAttachmentWithIconPath = Server.MapPath("~/DemoAppFiles/Input/Attach_Files/Attachment_Stream_Icon.txt"); byte[] attachmentDataWithIcon = System.IO.File.ReadAllBytes(fileStreamAttachmentWithIconPath); // Create the attachment from stream RectangleFloat attachFromStreamIconRectangle = new RectangleFloat(0, 0, 6, 10); FileAttachmentElement attachFromStreamElement = new FileAttachmentElement(attachFromStreamIconRectangle, attachmentDataWithIcon, "Attachment_Stream_Icon.txt"); attachFromStreamElement.IconType = FileAttachmentIcon.PushPin; attachFromStreamElement.Text = "Attachment from Stream with Pushpin Icon"; attachFromStreamElement.IconColor = RgbColor.Green; pdfDocument.AddElement(attachFromStreamElement, 10, true, false, 0, true, false); // 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=File_Attachments.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 createPdfButton_Click(object sender, EventArgs e) { // Create a PDF document Document pdfDocument = new Document(); // Set license key received after purchase to use the converter in licensed mode // Leave it not set to use the converter in demo mode pdfDocument.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c="; // Display the attachments panel when the PDF document is opened in a PDF viewer pdfDocument.ViewerPreferences.PageMode = ViewerPageMode.UseAttachments; // Add a page to PDF document PdfPage pdfPage = pdfDocument.AddPage(); try { // The titles font used to mark various sections of the PDF document PdfFont titleFont = pdfDocument.AddFont(new Font("Times New Roman", 10, FontStyle.Bold, GraphicsUnit.Point)); PdfFont subtitleFont = pdfDocument.AddFont(new Font("Times New Roman", 8, FontStyle.Regular, GraphicsUnit.Point)); float xLocation = 5; float yLocation = 5; // Add document title TextElement titleTextElement = new TextElement(xLocation, yLocation, "Attach Files and Streams to a PDF Document", titleFont); AddElementResult addElementResult = pdfPage.AddElement(titleTextElement); yLocation = addElementResult.EndPageBounds.Bottom + 15; // Create an attachment from a file without icon string fileAttachmentPath = Server.MapPath("~/DemoAppFiles/Input/Attach_Files/Attachment_File.txt"); pdfDocument.AddFileAttachment(fileAttachmentPath, "Attachment from File"); // Create an attachment from a stream without icon string fileStreamAttachmentPath = Server.MapPath("~/DemoAppFiles/Input/Attach_Files/Attachment_Stream.txt"); System.IO.FileStream attachmentStream = new System.IO.FileStream(fileStreamAttachmentPath, System.IO.FileMode.Open, System.IO.FileAccess.Read); pdfDocument.AddFileAttachment(attachmentStream, "Attachment_Stream.txt", "Attachment from Stream"); // Add the text element string text = "Click the next icon to open the attachment from a file:"; float textWidth = subtitleFont.GetTextWidth(text); TextElement textElement = new TextElement(xLocation, yLocation, text, subtitleFont); addElementResult = pdfPage.AddElement(textElement); // Create an attachment from file with paperclip icon in PDF string fileAttachmentWithIconPath = Server.MapPath("~/DemoAppFiles/Input/Attach_Files/Attachment_File_Icon.txt"); // Create the attachment from file RectangleF attachFromFileIconRectangle = new RectangleF(xLocation + textWidth + 3, yLocation, 6, 10); FileAttachmentElement attachFromFileElement = new FileAttachmentElement(attachFromFileIconRectangle, fileAttachmentWithIconPath); attachFromFileElement.IconType = FileAttachmentIcon.Paperclip; attachFromFileElement.Text = "Attachment from File with Paperclip Icon"; attachFromFileElement.IconColor = Color.Blue; pdfPage.AddElement(attachFromFileElement); yLocation = addElementResult.EndPageBounds.Bottom + 10; // Add the text element text = "Click the next icon to open the attachment from a stream:"; textWidth = subtitleFont.GetTextWidth(text); textElement = new TextElement(xLocation, yLocation, text, subtitleFont); addElementResult = pdfPage.AddElement(textElement); // Create an attachment from stream with pushpin icon in PDF string fileStreamAttachmentWithIconPath = Server.MapPath("~/DemoAppFiles/Input/Attach_Files/Attachment_Stream_Icon.txt"); System.IO.FileStream attachmentStreamWithIcon = new System.IO.FileStream(fileStreamAttachmentWithIconPath, System.IO.FileMode.Open, System.IO.FileAccess.Read); // Create the attachment from stream RectangleF attachFromStreamIconRectangle = new RectangleF(xLocation + textWidth + 3, yLocation, 6, 10); FileAttachmentElement attachFromStreamElement = new FileAttachmentElement(attachFromStreamIconRectangle, attachmentStreamWithIcon, "Attachment_Stream_Icon.txt"); attachFromStreamElement.IconType = FileAttachmentIcon.PushPin; attachFromStreamElement.Text = "Attachment from Stream with Pushpin Icon"; attachFromStreamElement.IconColor = Color.Green; pdfPage.AddElement(attachFromStreamElement); // 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=File_Attachments.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 pdfDocument.Close(); } }
public ActionResult CreatePdf(IFormCollection collection) { // Create a PDF document Document pdfDocument = new Document(); // Set license key received after purchase to use the converter in licensed mode // Leave it not set to use the converter in demo mode pdfDocument.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c="; // Display the attachments panel when the PDF document is opened in a PDF viewer pdfDocument.ViewerPreferences.PageMode = ViewerPageMode.UseAttachments; // Add a page to PDF document PdfPage pdfPage = pdfDocument.AddPage(); try { // The titles font used to mark various sections of the PDF document PdfFont titleFont = pdfDocument.AddFont(new Font("Times New Roman", 10, FontStyle.Bold, GraphicsUnit.Point)); PdfFont subtitleFont = pdfDocument.AddFont(new Font("Times New Roman", 8, FontStyle.Regular, GraphicsUnit.Point)); float xLocation = 5; float yLocation = 5; // Add document title TextElement titleTextElement = new TextElement(xLocation, yLocation, "Attach Files and Streams to a PDF Document", titleFont); AddElementResult addElementResult = pdfPage.AddElement(titleTextElement); yLocation = addElementResult.EndPageBounds.Bottom + 15; // Create an attachment from a file without icon string fileAttachmentPath = m_hostingEnvironment.ContentRootPath + "/wwwroot" + "/DemoAppFiles/Input/Attach_Files/Attachment_File.txt"; pdfDocument.AddFileAttachment(fileAttachmentPath, "Attachment from File"); // Create an attachment from a stream without icon string fileStreamAttachmentPath = m_hostingEnvironment.ContentRootPath + "/wwwroot" + "/DemoAppFiles/Input/Attach_Files/Attachment_Stream.txt"; System.IO.FileStream attachmentStream = new System.IO.FileStream(fileStreamAttachmentPath, System.IO.FileMode.Open, System.IO.FileAccess.Read); pdfDocument.AddFileAttachment(attachmentStream, "Attachment_Stream.txt", "Attachment from Stream"); // Add the text element string text = "Click the next icon to open the attachment from a file:"; float textWidth = subtitleFont.GetTextWidth(text); TextElement textElement = new TextElement(xLocation, yLocation, text, subtitleFont); addElementResult = pdfPage.AddElement(textElement); // Create an attachment from file with paperclip icon in PDF string fileAttachmentWithIconPath = m_hostingEnvironment.ContentRootPath + "/wwwroot" + "/DemoAppFiles/Input/Attach_Files/Attachment_File_Icon.txt"; // Create the attachment from file RectangleF attachFromFileIconRectangle = new RectangleF(xLocation + textWidth + 3, yLocation, 6, 10); FileAttachmentElement attachFromFileElement = new FileAttachmentElement(attachFromFileIconRectangle, fileAttachmentWithIconPath); attachFromFileElement.IconType = FileAttachmentIcon.Paperclip; attachFromFileElement.Text = "Attachment from File with Paperclip Icon"; attachFromFileElement.IconColor = Color.Blue; pdfPage.AddElement(attachFromFileElement); yLocation = addElementResult.EndPageBounds.Bottom + 10; // Add the text element text = "Click the next icon to open the attachment from a stream:"; textWidth = subtitleFont.GetTextWidth(text); textElement = new TextElement(xLocation, yLocation, text, subtitleFont); addElementResult = pdfPage.AddElement(textElement); // Create an attachment from stream with pushpin icon in PDF string fileStreamAttachmentWithIconPath = m_hostingEnvironment.ContentRootPath + "/wwwroot" + "/DemoAppFiles/Input/Attach_Files/Attachment_Stream_Icon.txt"; System.IO.FileStream attachmentStreamWithIcon = new System.IO.FileStream(fileStreamAttachmentWithIconPath, System.IO.FileMode.Open, System.IO.FileAccess.Read); // Create the attachment from stream RectangleF attachFromStreamIconRectangle = new RectangleF(xLocation + textWidth + 3, yLocation, 6, 10); FileAttachmentElement attachFromStreamElement = new FileAttachmentElement(attachFromStreamIconRectangle, attachmentStreamWithIcon, "Attachment_Stream_Icon.txt"); attachFromStreamElement.IconType = FileAttachmentIcon.PushPin; attachFromStreamElement.Text = "Attachment from Stream with Pushpin Icon"; attachFromStreamElement.IconColor = Color.Green; pdfPage.AddElement(attachFromStreamElement); // 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 = "File_Attachments.pdf"; return(fileResult); } finally { // Close the PDF document 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 { string htmlWithLinksAndAttachMarkers = collection["htmlStringTextBox"]; string baseUrl = collection["baseUrlTextBox"]; // Convert a HTML string with markers for file links and attachments to a PDF document object pdfDocument = htmlToPdfConverter.ConvertHtmlToPdfDocumentObject(htmlWithLinksAndAttachMarkers, baseUrl); // Display the attachments panel when the PDF document is opened in a PDF viewer pdfDocument.ViewerPreferences.PageMode = ViewerPageMode.UseAttachments; // Create File Attachments // Create an attachment from a file without icon string fileAttachmentPath = m_hostingEnvironment.ContentRootPath + "/wwwroot" + "/DemoAppFiles/Input/Attach_Files/Attachment_File.txt"; pdfDocument.AddFileAttachment(fileAttachmentPath, "Attachment from File"); // Create an attachment from a stream without icon string fileStreamAttachmentPath = m_hostingEnvironment.ContentRootPath + "/wwwroot" + "/DemoAppFiles/Input/Attach_Files/Attachment_Stream.txt"; System.IO.FileStream attachmentStream = new System.IO.FileStream(fileStreamAttachmentPath, System.IO.FileMode.Open, System.IO.FileAccess.Read); pdfDocument.AddFileAttachment(attachmentStream, "Attachment_Stream.txt", "Attachment from Stream"); // Create an attachment from file with paperclip icon in PDF HtmlElementMapping attachFromFileIconMapping = htmlToPdfConverter.HtmlElementsMappingOptions.HtmlElementsMappingResult.GetElementByMappingId("attach_from_file_icon"); if (attachFromFileIconMapping != null) { PdfPage attachFromFilePage = attachFromFileIconMapping.PdfRectangles[0].PdfPage; RectangleF attachFromFileIconRectangle = attachFromFileIconMapping.PdfRectangles[0].Rectangle; string fileAttachmentWithIconPath = m_hostingEnvironment.ContentRootPath + "/wwwroot" + "/DemoAppFiles/Input/Attach_Files/Attachment_File_Icon.txt"; // Create the attachment from file FileAttachmentElement attachFromFileElement = new FileAttachmentElement(attachFromFileIconRectangle, fileAttachmentWithIconPath); attachFromFileElement.IconType = FileAttachmentIcon.Paperclip; attachFromFileElement.Text = "Attachment from File with Paperclip Icon"; attachFromFileElement.IconColor = Color.Blue; attachFromFilePage.AddElement(attachFromFileElement); } // Create an attachment from stream with pushpin icon in PDF System.IO.FileStream attachmentStreamWithIcon = null; HtmlElementMapping attachFromStreamIconMapping = htmlToPdfConverter.HtmlElementsMappingOptions.HtmlElementsMappingResult.GetElementByMappingId("attach_from_stream_icon"); if (attachFromStreamIconMapping != null) { PdfPage attachFromStreamPage = attachFromStreamIconMapping.PdfRectangles[0].PdfPage; RectangleF attachFromStreamIconRectangle = attachFromStreamIconMapping.PdfRectangles[0].Rectangle; string fileStreamAttachmentWithIconPath = m_hostingEnvironment.ContentRootPath + "/wwwroot" + "/DemoAppFiles/Input/Attach_Files/Attachment_Stream_Icon.txt"; attachmentStreamWithIcon = new System.IO.FileStream(fileStreamAttachmentWithIconPath, System.IO.FileMode.Open, System.IO.FileAccess.Read); // Create the attachment from stream FileAttachmentElement attachFromStreamElement = new FileAttachmentElement(attachFromStreamIconRectangle, attachmentStreamWithIcon, "Attachment_Stream_Icon.txt"); attachFromStreamElement.IconType = FileAttachmentIcon.PushPin; attachFromStreamElement.Text = "Attachment from Stream with Pushpin Icon"; attachFromStreamElement.IconColor = Color.Green; attachFromStreamPage.AddElement(attachFromStreamElement); } // 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 = "File_Links_and_Attachments.pdf"; return(fileResult); } finally { // Close the PDF document if (pdfDocument != null) { pdfDocument.Close(); } } }