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="; // Add a page to PDF document PdfPage pdfPage = pdfDocument.AddPage(); try { // The font used for titles in PDF document PdfFont titlesFont = pdfDocument.AddFont(new Font("Times New Roman", 10, FontStyle.Bold, GraphicsUnit.Point)); // The font used for field names in PDF document PdfFont fieldNameFont = pdfDocument.AddFont(new Font("Times New Roman", 10, FontStyle.Regular, GraphicsUnit.Point)); // The font used for buttons text in PDF document PdfFont buttonTextFont = pdfDocument.AddFont(new Font("Times New Roman", 10, FontStyle.Regular, GraphicsUnit.Point)); // The font used for PDF form text box fields PdfFont textFieldFont = pdfDocument.AddFont(StdFontBaseFamily.Helvetica); textFieldFont.Size = 8; // The font used for PDF form combo box fields PdfFont comboBoxFieldFont = pdfDocument.AddFont(StdFontBaseFamily.Helvetica); comboBoxFieldFont.Size = 8; float xLocation = 5; float yLocation = 5; // Add document title TextElement titleTextElement = new TextElement(xLocation, yLocation, "Create PDF Forms", titlesFont); AddElementResult addElementResult = pdfPage.AddElement(titleTextElement); yLocation = addElementResult.EndPageBounds.Bottom + 15; // Add a text box field to PDF form TextElement fieldNameTextElement = new TextElement(xLocation, yLocation, 60, "First name:", fieldNameFont); addElementResult = pdfPage.AddElement(fieldNameTextElement); RectangleF fieldNameRectangle = addElementResult.EndPageBounds; RectangleF fieldBoundingRectangle = new RectangleF(fieldNameRectangle.Right + 10, yLocation, 150, 15); // Create the form field PdfFormTextBox textBoxField = pdfDocument.Form.AddTextBox(pdfPage, fieldBoundingRectangle, "Enter First Name", textFieldFont); // Set unique form field name used when the form is submitted textBoxField.Name = "firstName"; // Set the form field default value textBoxField.DefaultValue = "A default first name"; // Set form field style textBoxField.Style.BackColor = Color.AliceBlue; yLocation = fieldNameRectangle.Bottom + 10; // Add a text box field to PDF form fieldNameTextElement = new TextElement(xLocation, yLocation, 60, "Last name:", fieldNameFont); addElementResult = pdfPage.AddElement(fieldNameTextElement); fieldNameRectangle = addElementResult.EndPageBounds; fieldBoundingRectangle = new RectangleF(fieldNameRectangle.Right + 10, yLocation, 150, 15); // Create the form field textBoxField = pdfDocument.Form.AddTextBox(pdfPage, fieldBoundingRectangle, "Enter Last Name", textFieldFont); // Set unique form field name used when the form is submitted textBoxField.Name = "lastName"; // Set the form field default value textBoxField.DefaultValue = "A default last name"; // Set form field style textBoxField.Style.BackColor = Color.MistyRose; yLocation = fieldNameRectangle.Bottom + 10; // Add a password text box field to PDF form fieldNameTextElement = new TextElement(xLocation, yLocation, 60, "Password:"******"", textFieldFont); // Set unique form field name used when the form is submitted passwordTextBoxField.Name = "password"; // Set form field style passwordTextBoxField.Style.BackColor = Color.AliceBlue; // Set the password mode for the text box passwordTextBoxField.IsPassword = true; yLocation = fieldNameRectangle.Bottom + 10; // Add a radio buttons group to PDF form fieldNameTextElement = new TextElement(xLocation, yLocation, 60, "Gender:", fieldNameFont); addElementResult = pdfPage.AddElement(fieldNameTextElement); fieldNameRectangle = addElementResult.EndPageBounds; // Create the radio buttons group PdfFormRadioButtonsGroup radioButtonsGroup = pdfDocument.Form.AddRadioButtonsGroup(pdfPage); // Set unique form field name used when the form is submitted radioButtonsGroup.Name = "gender"; // Set style of the radio buttons in this group radioButtonsGroup.Style.BackColor = Color.AntiqueWhite; // Add the first radio button to group RectangleF radioButtonRectangle = new RectangleF(fieldNameRectangle.Right + 10, yLocation, 50, 10); // Create the form field PdfFormRadioButton radioButtonField = radioButtonsGroup.AddRadioButton(radioButtonRectangle, "male", pdfPage); fieldNameTextElement = new TextElement(fieldNameRectangle.Right + 22, yLocation, 30, "Male", fieldNameFont); pdfPage.AddElement(fieldNameTextElement); // Add the second radio button to group radioButtonRectangle = new RectangleF(fieldNameRectangle.Right + 60, yLocation, 50, 10); // Create the form field radioButtonField = radioButtonsGroup.AddRadioButton(radioButtonRectangle, "female", pdfPage); fieldNameTextElement = new TextElement(fieldNameRectangle.Right + 72, yLocation, 30, "Female", fieldNameFont); pdfPage.AddElement(fieldNameTextElement); // Set the selected radio btton in group radioButtonsGroup.SetCheckedRadioButton("male"); yLocation = fieldNameRectangle.Bottom + 10; // Add a checkbox field to PDF form fieldNameTextElement = new TextElement(xLocation, yLocation, 60, "Vehicle:", fieldNameFont); addElementResult = pdfPage.AddElement(fieldNameTextElement); fieldNameRectangle = addElementResult.EndPageBounds; fieldBoundingRectangle = new RectangleF(fieldNameRectangle.Right + 10, yLocation, 10, 10); // Create the form field PdfFormCheckBox checkBoxField = pdfDocument.Form.AddCheckBox(pdfPage, fieldBoundingRectangle); // Set unique form field name used when the form is submitted checkBoxField.Name = "haveCar"; // Set form field style checkBoxField.Style.BackColor = Color.AntiqueWhite; // Set checkbox field checked state checkBoxField.Checked = true; fieldNameTextElement = new TextElement(fieldNameRectangle.Right + 22, yLocation, 50, "I have a car", fieldNameFont); pdfPage.AddElement(fieldNameTextElement); yLocation = fieldNameRectangle.Bottom + 10; // Add a combo box list field to PDF form fieldNameTextElement = new TextElement(xLocation, yLocation, 60, "Vehicle Type:", fieldNameFont); addElementResult = pdfPage.AddElement(fieldNameTextElement); fieldNameRectangle = addElementResult.EndPageBounds; fieldBoundingRectangle = new RectangleF(fieldNameRectangle.Right + 10, yLocation, 50, 15); string[] comboBoxItems = new string[] { "Volvo", "Saab", "Audi", "Opel" }; // Create the form field PdfFormComboBox comboBoxField = pdfDocument.Form.AddComboBox(pdfPage, fieldBoundingRectangle, comboBoxItems, comboBoxFieldFont); // Set unique form field name used when the form is submitted comboBoxField.Name = "vehicleType"; // Set the form field default value comboBoxField.DefaultValue = "Audi"; // Set form field style comboBoxField.Style.BackColor = Color.LightCyan; // Set selected item in combo box comboBoxField.Value = "Audi"; yLocation = fieldNameRectangle.Bottom + 10; // Add a multiline text box field to PDF form fieldNameTextElement = new TextElement(xLocation, yLocation + 20, 60, "Comments:", fieldNameFont); addElementResult = pdfPage.AddElement(fieldNameTextElement); fieldNameRectangle = addElementResult.EndPageBounds; fieldBoundingRectangle = new RectangleF(fieldNameRectangle.Right + 10, yLocation, 150, 60); // Create the form field PdfFormTextBox multilineTextBoxField = pdfDocument.Form.AddTextBox(pdfPage, fieldBoundingRectangle, "Enter your comments here:\r\nFirst comment line\r\nSecond comment line", textFieldFont); // Set unique form field name used when the form is submitted multilineTextBoxField.Name = "comments"; // Set form field style multilineTextBoxField.Style.BackColor = Color.AliceBlue; // Set the multiline mode for text box field multilineTextBoxField.IsMultiLine = true; yLocation = yLocation + 70; // Add a form submit button to PDF form fieldBoundingRectangle = new RectangleF(xLocation, yLocation, 75, 15); PdfFormButton submitFormButton = pdfDocument.Form.AddButton(pdfPage, fieldBoundingRectangle, "Submit", buttonTextFont); // Set unique form field name used when the form is submitted submitFormButton.Name = "submitFormButton"; // Set form field style submitFormButton.Style.BackColor = Color.Beige; // Create the form submit action PdfSubmitFormAction submitFormAction = new PdfSubmitFormAction(collection["submitUrlTextBox"]); // Form values will be submitted in HTML form format submitFormAction.Flags |= PdfFormSubmitFlags.ExportFormat; if (collection["HttpMethod"] == "getMethodRadioButton") { submitFormAction.Flags |= PdfFormSubmitFlags.GetMethod; } // Set the form submit button action submitFormButton.Action = submitFormAction; // Add a form reset button to PDF form fieldBoundingRectangle = new RectangleF(xLocation + 100, yLocation, 75, 15); PdfFormButton resetFormButton = pdfDocument.Form.AddButton(pdfPage, fieldBoundingRectangle, "Reset", buttonTextFont); // Set unique form field name used when the form is submitted resetFormButton.Name = "resetFormButton"; // Set form field style resetFormButton.Style.BackColor = Color.Beige; // Create the form reset action PdfResetFormAction resetFormAction = new PdfResetFormAction(); // Set the form reset button action resetFormButton.Action = resetFormAction; // 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 = "Create_PDF_Forms.pdf"; return(fileResult); } finally { // Close the PDF document pdfDocument.Close(); } }
protected void convertToPdfButton_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="; // Create a PDF page where to add the first HTML PdfPage firstPdfPage = pdfDocument.AddPage(); try { // The element location in PDF float xLocation = float.Parse(xLocationTextBox.Text); float yLocation = float.Parse(yLocationTextBox.Text); // The URL of the HTML page to convert to PDF string urlToConvert = urlTextBox.Text; // Create the HTML to PDF element HtmlToPdfElement htmlToPdfElement = new HtmlToPdfElement(xLocation, yLocation, urlToConvert); // Optionally set the HTML viewer width htmlToPdfElement.HtmlViewerWidth = int.Parse(htmlViewerWidthTextBox.Text); // Optionally set the HTML viewer height if (htmlViewerHeightTextBox.Text.Length > 0) { htmlToPdfElement.HtmlViewerHeight = int.Parse(htmlViewerHeightTextBox.Text); } // Optionally set the HTML content clipping option to force the HTML content width to be exactly HtmlViewerWidth pixels htmlToPdfElement.ClipHtmlView = clipContentCheckBox.Checked; // Optionally set the destination width in PDF if (contentWidthTextBox.Text.Length > 0) { htmlToPdfElement.Width = float.Parse(contentWidthTextBox.Text); } // Optionally set the destination height in PDF if (contentHeightTextBox.Text.Length > 0) { htmlToPdfElement.Height = float.Parse(contentHeightTextBox.Text); } // Optionally set a delay before conversion to allow asynchonous scripts to finish htmlToPdfElement.ConversionDelay = 2; // Add the HTML to PDF element to PDF document // The AddElementResult contains the bounds of the HTML to PDF Element in last rendered PDF page // such that you can start a new PDF element right under it AddElementResult result = firstPdfPage.AddElement(htmlToPdfElement); // 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=HTML_to_PDF_Elements.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="; try { // The result of adding Text Elements to PDF document AddElementResult addTextResult = null; // 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)); titleFont.IsUnderline = true; // The position on X anf Y axes where to add the next element float yLocation = 5; float xLocation = 5; // Create a PDF page in PDF document PdfPage pdfPage = pdfDocument.AddPage(); // Text Elements Using Fonts Installed in System // Add section title TextElement titleTextElement = new TextElement(xLocation, yLocation, "Text Elements Using Fonts Installed in System", titleFont); titleTextElement.ForeColor = Color.DarkGreen; addTextResult = pdfPage.AddElement(titleTextElement); yLocation = addTextResult.EndPageBounds.Bottom + 10; xLocation += 5; pdfPage = addTextResult.EndPdfPage; // Embed in PDF document a font with Normal style installed in system Font systemFontNormal = new Font("Times New Roman", 10, GraphicsUnit.Point); PdfFont embeddedSystemFontNormal = pdfDocument.AddFont(systemFontNormal); // Add a text element using a font with Normal style installed in system TextElement embeddedSystemFontNormalTextElement = new TextElement(xLocation, yLocation, "This text uses a font with Normal style installed in system", embeddedSystemFontNormal); addTextResult = pdfPage.AddElement(embeddedSystemFontNormalTextElement); yLocation = addTextResult.EndPageBounds.Bottom + 3; pdfPage = addTextResult.EndPdfPage; // Embed in PDF document a font with Bold style installed in system Font systemFontBold = new Font("Times New Roman", 10, FontStyle.Bold, GraphicsUnit.Point); PdfFont embeddedSystemFontBold = pdfDocument.AddFont(systemFontBold); // Add a text element using a font with Bold style installed in system TextElement embeddedSystemFontBoldTextElement = new TextElement(xLocation, yLocation, "This text uses a font with Bold style installed in system", embeddedSystemFontBold); addTextResult = pdfPage.AddElement(embeddedSystemFontBoldTextElement); yLocation = addTextResult.EndPageBounds.Bottom + 3; pdfPage = addTextResult.EndPdfPage; // Embed in PDF document a font with Italic style installed in system Font systemFontItalic = new Font("Times New Roman", 10, FontStyle.Italic, GraphicsUnit.Point); PdfFont embeddedSystemFontItalic = pdfDocument.AddFont(systemFontItalic); // Add a text element using a font with Italic style installed in system TextElement embeddedSystemFontItalicTextElement = new TextElement(xLocation, yLocation, "This text uses a font with Italic style installed in system", embeddedSystemFontItalic); addTextResult = pdfPage.AddElement(embeddedSystemFontItalicTextElement); yLocation = addTextResult.EndPageBounds.Bottom + 3; pdfPage = addTextResult.EndPdfPage; // Text Elements Using Fonts From Local Files // Add section title xLocation -= 5; yLocation += 10; titleTextElement = new TextElement(xLocation, yLocation, "Text Elements Using Fonts From Local Files", titleFont); titleTextElement.ForeColor = Color.Navy; addTextResult = pdfPage.AddElement(titleTextElement); yLocation = addTextResult.EndPageBounds.Bottom + 10; xLocation += 5; pdfPage = addTextResult.EndPdfPage; // Embed a True Type font from a local file in PDF document PdfFont localTrueTypeFont = pdfDocument.AddFont(m_hostingEnvironment.ContentRootPath + "/wwwroot" + "/DemoAppFiles/Input/Fonts/TrueType.ttf"); // Add a text element using the local True Type font to PDF document TextElement localFontTtfTextElement = new TextElement(xLocation, yLocation, "This text uses a True Type Font loaded from a local file", localTrueTypeFont); addTextResult = pdfPage.AddElement(localFontTtfTextElement); yLocation = addTextResult.EndPageBounds.Bottom; pdfPage = addTextResult.EndPdfPage; // Embed an OpenType font with TrueType Outlines in PDF document PdfFont localOpenTypeTrueTypeFont = pdfDocument.AddFont(m_hostingEnvironment.ContentRootPath + "/wwwroot" + "/DemoAppFiles/Input/Fonts/OpenTypeTrueType.otf"); // Add a text element using the local OpenType font with TrueType Outlines to PDF document TextElement localOpenTypeTrueTypeFontTextElement = new TextElement(xLocation, yLocation, "This text uses an Open Type Font with TrueType Outlines loaded from a local file", localOpenTypeTrueTypeFont); addTextResult = pdfPage.AddElement(localOpenTypeTrueTypeFontTextElement); yLocation = addTextResult.EndPageBounds.Bottom + 3; pdfPage = addTextResult.EndPdfPage; // Embed an OpenType font with PostScript Outlines in PDF document PdfFont localOpenTypePostScriptFont = pdfDocument.AddFont(m_hostingEnvironment.ContentRootPath + "/wwwroot" + "/DemoAppFiles/Input/Fonts/OpenTypePostScript.otf"); // Add a text element using the local OpenType font with PostScript Outlines to PDF document TextElement localOpenTypePostScriptFontTextElement = new TextElement(xLocation, yLocation, "This text uses an Open Type Font with PostScript Outlines loaded from a local file", localOpenTypePostScriptFont); addTextResult = pdfPage.AddElement(localOpenTypePostScriptFontTextElement); yLocation = addTextResult.EndPageBounds.Bottom + 3; pdfPage = addTextResult.EndPdfPage; // Text Elements Using Standard PDF Fonts // Add section title xLocation -= 5; yLocation += 10; titleTextElement = new TextElement(xLocation, yLocation, "Text Elements Using Standard PDF Fonts", titleFont); titleTextElement.ForeColor = Color.DarkGreen; addTextResult = pdfPage.AddElement(titleTextElement); yLocation = addTextResult.EndPageBounds.Bottom + 10; xLocation += 5; pdfPage = addTextResult.EndPdfPage; // Create a standard PDF font with Normal style PdfFont standardPdfFontNormal = pdfDocument.AddFont(StdFontBaseFamily.Helvetica); standardPdfFontNormal.Size = 10; // Add a text element using the standard PDF font with Normal style TextElement standardPdfFontNormalTextElement = new TextElement(xLocation, yLocation, "This text uses a standard PDF font with Normal style", standardPdfFontNormal); addTextResult = pdfPage.AddElement(standardPdfFontNormalTextElement); yLocation = addTextResult.EndPageBounds.Bottom + 3; pdfPage = addTextResult.EndPdfPage; // Create a standard PDF font with Bold style PdfFont standardPdfFontBold = pdfDocument.AddFont(StdFontBaseFamily.HelveticaBold); standardPdfFontBold.Size = 10; // Add a text element using the standard PDF font with Bold style TextElement standardPdfFontBoldTextElement = new TextElement(xLocation, yLocation, "This text uses a standard PDF font with Bold style", standardPdfFontBold); addTextResult = pdfPage.AddElement(standardPdfFontBoldTextElement); yLocation = addTextResult.EndPageBounds.Bottom + 3; pdfPage = addTextResult.EndPdfPage; // Create a standard PDF font with Italic style PdfFont standardPdfFontItalic = pdfDocument.AddFont(StdFontBaseFamily.HelveticaOblique); standardPdfFontItalic.Size = 10; // Add a text element using the standard PDF font with Italic style TextElement standardPdfFontItalicTextElement = new TextElement(xLocation, yLocation, "This text uses a standard PDF font with Italic style", standardPdfFontItalic); addTextResult = pdfPage.AddElement(standardPdfFontItalicTextElement); yLocation = addTextResult.EndPageBounds.Bottom + 3; pdfPage = addTextResult.EndPdfPage; // Text Elements with Vertical Text // Add section title xLocation -= 5; yLocation += 10; titleTextElement = new TextElement(xLocation, yLocation, "Vertical Text", titleFont); titleTextElement.ForeColor = Color.Navy; addTextResult = pdfPage.AddElement(titleTextElement); yLocation = addTextResult.EndPageBounds.Bottom + 10; xLocation += 5; pdfPage = addTextResult.EndPdfPage; // Add a top to bottom vertical text string topBottomText = "This is a Top to Bottom Vertical Text"; float topBottomTextWidth = embeddedSystemFontNormal.GetTextWidth(topBottomText); TextElement topBottomVerticalTextElement = new TextElement(0, 0, topBottomText, embeddedSystemFontNormal); topBottomVerticalTextElement.Translate(xLocation + 25, yLocation); topBottomVerticalTextElement.Rotate(90); pdfPage.AddElement(topBottomVerticalTextElement); // Add a bottom to top vertical text string bottomTopText = "This is a Bottom to Top Vertical Text"; float bottomTopTextWidth = embeddedSystemFontNormal.GetTextWidth(bottomTopText); TextElement bottomTopVerticalTextElement = new TextElement(0, 0, bottomTopText, embeddedSystemFontNormal); bottomTopVerticalTextElement.Translate(xLocation + 125, yLocation + bottomTopTextWidth); bottomTopVerticalTextElement.Rotate(-90); pdfPage.AddElement(bottomTopVerticalTextElement); yLocation += bottomTopTextWidth + 10; // Add a text element that flows freely in width and height string text = System.IO.File.ReadAllText(m_hostingEnvironment.ContentRootPath + "/wwwroot" + "/DemoAppFiles/Input/Text_Files/Text_File.txt"); // Add section title xLocation -= 5; yLocation += 10; titleTextElement = new TextElement(xLocation, yLocation, "Text Element that flows freely in width and height", titleFont); titleTextElement.ForeColor = Color.DarkGreen; addTextResult = pdfPage.AddElement(titleTextElement); yLocation = addTextResult.EndPageBounds.Bottom + 10; xLocation += 5; pdfPage = addTextResult.EndPdfPage; // Add the text element TextElement freeWidthAndHeightTextElement = new TextElement(xLocation, yLocation, text, embeddedSystemFontNormal); addTextResult = pdfPage.AddElement(freeWidthAndHeightTextElement); yLocation = addTextResult.EndPageBounds.Bottom + 3; pdfPage = addTextResult.EndPdfPage; // Add a text element with a given width that flows freely in height // Add section title xLocation -= 5; yLocation += 10; titleTextElement = new TextElement(xLocation, yLocation, "Text Element with a given width that flows freely in height", titleFont); titleTextElement.ForeColor = Color.Navy; addTextResult = pdfPage.AddElement(titleTextElement); yLocation = addTextResult.EndPageBounds.Bottom + 10; xLocation += 5; pdfPage = addTextResult.EndPdfPage; // Add the text element TextElement freeHeightTextElement = new TextElement(xLocation, yLocation, 400, text, embeddedSystemFontNormal); addTextResult = pdfPage.AddElement(freeHeightTextElement); yLocation = addTextResult.EndPageBounds.Bottom + 3; pdfPage = addTextResult.EndPdfPage; // Add a bounding rectangle for text element RectangleElement border = new RectangleElement(addTextResult.EndPageBounds.X, addTextResult.EndPageBounds.Y, addTextResult.EndPageBounds.Width, addTextResult.EndPageBounds.Height); pdfPage.AddElement(border); // Add a text element with a given width and height // Add section title xLocation -= 5; yLocation += 10; titleTextElement = new TextElement(xLocation, yLocation, "Text Element with a given width and height", titleFont); titleTextElement.ForeColor = Color.DarkGreen; addTextResult = pdfPage.AddElement(titleTextElement); yLocation = addTextResult.EndPageBounds.Bottom + 10; xLocation += 5; pdfPage = addTextResult.EndPdfPage; // Add the text element TextElement boundedTextElement = new TextElement(xLocation, yLocation, 400, 50, text, embeddedSystemFontNormal); addTextResult = pdfPage.AddElement(boundedTextElement); yLocation = addTextResult.EndPageBounds.Bottom + 3; pdfPage = addTextResult.EndPdfPage; // Add a bounding rectangle for text element border = new RectangleElement(addTextResult.EndPageBounds.X, addTextResult.EndPageBounds.Y, addTextResult.EndPageBounds.Width, addTextResult.EndPageBounds.Height); pdfPage.AddElement(border); // Add a text element that flows freely on next PDF page // Add section title xLocation -= 5; yLocation += 10; titleTextElement = new TextElement(xLocation, yLocation, "Text Element that flows freely on multiple PDF pages", titleFont); titleTextElement.ForeColor = Color.Navy; addTextResult = pdfPage.AddElement(titleTextElement); yLocation = addTextResult.EndPageBounds.Bottom + 10; xLocation += 5; pdfPage = addTextResult.EndPdfPage; // Add the text element string multiPageText = System.IO.File.ReadAllText(m_hostingEnvironment.ContentRootPath + "/wwwroot" + "/DemoAppFiles/Input/Text_Files/Large_Text_File.txt"); TextElement multiPageTextElement = new TextElement(xLocation, yLocation, 575, multiPageText, embeddedSystemFontNormal); multiPageTextElement.BackColor = Color.WhiteSmoke; addTextResult = pdfPage.AddElement(multiPageTextElement); yLocation = addTextResult.EndPageBounds.Bottom + 3; pdfPage = addTextResult.EndPdfPage; // Add a line at the bottom of the multipage text element LineElement bottomLine = new LineElement(addTextResult.EndPageBounds.X, addTextResult.EndPageBounds.Bottom + 1, addTextResult.EndPageBounds.X + addTextResult.EndPageBounds.Width, addTextResult.EndPageBounds.Bottom + 1); pdfPage.AddElement(bottomLine); // Add a text stamp to a PDF document // Create a .NET font Font timesNewRomanFont = new Font("Times New Roman", 24, GraphicsUnit.Point); // Create a PDF font PdfFont stampPdfFont = pdfDocument.AddFont(timesNewRomanFont, true); // The stamp text string stampText = String.Format("Text Stamp {0}", DateTime.Now.ToString("d")); // Measure the text float textWidth = stampPdfFont.GetTextWidth(stampText); foreach (PdfPage page in pdfDocument.Pages) { // Get the PDF page drawable area width and height float pdfPageWidth = page.ClientRectangle.Width; float pdfPageHeight = page.ClientRectangle.Height; // Calculate the PDF page diagonal float pdfPageDiagonal = (float)Math.Sqrt(pdfPageWidth * pdfPageWidth + pdfPageHeight * pdfPageHeight); // The text location on PDF page diagonal float xStampLocation = (pdfPageDiagonal - textWidth) / 2; // Create the stamp as a rotated text element TextElement stampTextElement = new TextElement(xStampLocation, 0, stampText, stampPdfFont); stampTextElement.ForeColor = Color.Coral; stampTextElement.Rotate((float)(Math.Atan(pdfPageHeight / pdfPageWidth) * (180 / Math.PI))); stampTextElement.Opacity = 75; // Add the stamp to PDF page page.AddElement(stampTextElement); } // 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 = "Text_Elements.pdf"; return(fileResult); } finally { // Close the PDF document pdfDocument.Close(); } }
protected void convertToPdfButton_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="; // Add a page to PDF document PdfPage firstPdfPage = pdfDocument.AddPage(); // Create Header if (addHeaderCheckBox.Checked) { CreateHeader(pdfDocument, drawHeaderLineCheckBox.Checked); } // Create Footer if (addFooterCheckBox.Checked) { CreateFooter(pdfDocument, drawFooterLineCheckBox.Checked, addPageNumbersInFooterCheckBox.Checked); } try { // Add First HTML // Create the first HTML to PDF element HtmlToPdfElement firstHtml = new HtmlToPdfElement(0, 0, firstUrlTextBox.Text); // Optionally set a delay before conversion to allow asynchonous scripts to finish firstHtml.ConversionDelay = 2; // Optionally add a space between header and the content generated by this HTML to PDF element // The spacing for first page and the subsequent pages can be set independently // Leave this option not set for no spacing firstHtml.Y = float.Parse(firstPageSpacingTextBox.Text); firstHtml.TopSpacing = float.Parse(headerSpacingTextBox.Text); // Optionally add a space between footer and the content generated by this HTML to PDF element // Leave this option not set for no spacing firstHtml.BottomSpacing = float.Parse(footerSpacingTextBox.Text); // Install a handler where to set header visibility in the pages where the HTML element is rendered firstHtml.PrepareRenderPdfPageEvent += new PrepareRenderPdfPageDelegate(htmlToPdfElement_PrepareRenderPdfPageEvent); // Add the first HTML to PDF element to PDF document // The PrepareRenderPdfPageEvent event handler will be invoked for each rendered PDF page AddElementResult firstAddResult = firstPdfPage.AddElement(firstHtml); // Uninstall the handler firstHtml.PrepareRenderPdfPageEvent -= new PrepareRenderPdfPageDelegate(htmlToPdfElement_PrepareRenderPdfPageEvent); // Add Second HTML PdfPage secondPdfPage = null; PointF secondHtmlLocation = Point.Empty; if (startNewPageCheckBox.Checked) { // Create a PDF page where to add the second HTML secondPdfPage = pdfDocument.AddPage(); secondHtmlLocation = PointF.Empty; } else { // Add the second HTML on the PDF page where the first HTML ended secondPdfPage = pdfDocument.Pages[firstAddResult.EndPageIndex]; secondHtmlLocation = new PointF(firstAddResult.EndPageBounds.Left, firstAddResult.EndPageBounds.Bottom); } // Create the second HTML to PDF element HtmlToPdfElement secondHtml = new HtmlToPdfElement(secondHtmlLocation.X, secondHtmlLocation.Y, secondUrlTextBox.Text); // Optionally set a delay before conversion to allow asynchonous scripts to finish secondHtml.ConversionDelay = 2; // Optionally add a space between header and the content generated by this HTML to PDF element // Leave this option not set for no spacing secondHtml.TopSpacing = float.Parse(headerSpacingTextBox.Text); // Optionally add a space between footer and the content generated by this HTML to PDF element // Leave this option not set for no spacing secondHtml.BottomSpacing = float.Parse(footerSpacingTextBox.Text); // Install a handler where to set header visibility in the pages where the HTML element is rendered secondHtml.PrepareRenderPdfPageEvent += new PrepareRenderPdfPageDelegate(htmlToPdfElement_PrepareRenderPdfPageEvent); // Add the second HTML to PDF element to PDF document // The PrepareRenderPdfPageEvent event handler will be invoked for each rendered PDF page secondPdfPage.AddElement(secondHtml); // Uninstall the handler secondHtml.PrepareRenderPdfPageEvent -= new PrepareRenderPdfPageDelegate(htmlToPdfElement_PrepareRenderPdfPageEvent); // 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=Header_Footer_in_Merge_Multipe_HTML.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 ConvertHtmlToPdf(IFormCollection collection) { formCollection = 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="; // Add a page to PDF document PdfPage firstPdfPage = pdfDocument.AddPage(); // Create Header if (collection["addHeaderCheckBox"].Count > 0) { CreateHeader(pdfDocument, collection["drawHeaderLineCheckBox"].Count > 0); } // Create Footer if (collection["addFooterCheckBox"].Count > 0) { CreateFooter(pdfDocument, collection["drawFooterLineCheckBox"].Count > 0, collection["addPageNumbersInFooterCheckBox"].Count > 0); } try { // Add First HTML // Create the first HTML to PDF element HtmlToPdfElement firstHtml = new HtmlToPdfElement(0, 0, collection["firstUrlTextBox"]); // Optionally set a delay before conversion to allow asynchonous scripts to finish firstHtml.ConversionDelay = 2; // Optionally add a space between header and the content generated by this HTML to PDF element // The spacing for first page and the subsequent pages can be set independently // Leave this option not set for no spacing firstHtml.Y = float.Parse(collection["firstPageSpacingTextBox"]); firstHtml.TopSpacing = float.Parse(collection["headerSpacingTextBox"]); // Optionally add a space between footer and the content generated by this HTML to PDF element // Leave this option not set for no spacing firstHtml.BottomSpacing = float.Parse(collection["footerSpacingTextBox"]); // Install a handler where to set header visibility in the pages where the HTML element is rendered firstHtml.PrepareRenderPdfPageEvent += new PrepareRenderPdfPageDelegate(htmlToPdfElement_PrepareRenderPdfPageEvent); // Add the first HTML to PDF element to PDF document // The PrepareRenderPdfPageEvent event handler will be invoked for each rendered PDF page AddElementResult firstAddResult = firstPdfPage.AddElement(firstHtml); // Uninstall the handler firstHtml.PrepareRenderPdfPageEvent -= new PrepareRenderPdfPageDelegate(htmlToPdfElement_PrepareRenderPdfPageEvent); // Add Second HTML PdfPage secondPdfPage = null; PointF secondHtmlLocation = Point.Empty; if (collection["startNewPageCheckBox"].Count > 0) { // Create a PDF page where to add the second HTML secondPdfPage = pdfDocument.AddPage(); secondHtmlLocation = PointF.Empty; } else { // Add the second HTML on the PDF page where the first HTML ended secondPdfPage = pdfDocument.Pages[firstAddResult.EndPageIndex]; secondHtmlLocation = new PointF(firstAddResult.EndPageBounds.Left, firstAddResult.EndPageBounds.Bottom); } // Create the second HTML to PDF element HtmlToPdfElement secondHtml = new HtmlToPdfElement(secondHtmlLocation.X, secondHtmlLocation.Y, collection["secondUrlTextBox"]); // Optionally set a delay before conversion to allow asynchonous scripts to finish secondHtml.ConversionDelay = 2; // Optionally add a space between header and the content generated by this HTML to PDF element // Leave this option not set for no spacing secondHtml.TopSpacing = float.Parse(collection["headerSpacingTextBox"]); // Optionally add a space between footer and the content generated by this HTML to PDF element // Leave this option not set for no spacing secondHtml.BottomSpacing = float.Parse(collection["footerSpacingTextBox"]); // Install a handler where to set header visibility in the pages where the HTML element is rendered secondHtml.PrepareRenderPdfPageEvent += new PrepareRenderPdfPageDelegate(htmlToPdfElement_PrepareRenderPdfPageEvent); // Add the second HTML to PDF element to PDF document // The PrepareRenderPdfPageEvent event handler will be invoked for each rendered PDF page secondPdfPage.AddElement(secondHtml); // Uninstall the handler secondHtml.PrepareRenderPdfPageEvent -= new PrepareRenderPdfPageDelegate(htmlToPdfElement_PrepareRenderPdfPageEvent); // 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 = "Header_Footer_in_Merge_Multipe_HTML.pdf"; return(fileResult); } finally { // Close the PDF document pdfDocument.Close(); } }
public ActionResult ConvertHtmlToPdf(IFormCollection collection) { // Create the PDF document where to add the HTML documents 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="; // Create a PDF page where to add the first HTML PdfPage firstPdfPage = pdfDocument.AddPage(); try { // Create the first HTML to PDF element HtmlToPdfElement firstHtml = new HtmlToPdfElement(0, 0, collection["firstUrlTextBox"]); // Optionally set a delay before conversion to allow asynchonous scripts to finish firstHtml.ConversionDelay = 2; // Add the first HTML to PDF document AddElementResult firstAddResult = firstPdfPage.AddElement(firstHtml); PdfPage secondPdfPage = null; PointF secondHtmlLocation = Point.Empty; if (collection["startNewPageCheckBox"].Count > 0) { // Create a PDF page where to add the second HTML secondPdfPage = pdfDocument.AddPage(); secondHtmlLocation = PointF.Empty; } else { // Add the second HTML on the PDF page where the first HTML ended secondPdfPage = firstAddResult.EndPdfPage; secondHtmlLocation = new PointF(firstAddResult.EndPageBounds.Left, firstAddResult.EndPageBounds.Bottom); } // Create the second HTML to PDF element HtmlToPdfElement secondHtml = new HtmlToPdfElement(secondHtmlLocation.X, secondHtmlLocation.Y, collection["secondUrlTextBox"]); // Optionally set a delay before conversion to allow asynchonous scripts to finish secondHtml.ConversionDelay = 2; // Add the second HTML to PDF document secondPdfPage.AddElement(secondHtml); // 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 = "Merge_Multipe_HTML.pdf"; return(fileResult); } finally { // Close the PDF document pdfDocument.Close(); } }
public ActionResult ConvertHtmlToPdf(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="; // Create a PDF page where to add the first HTML PdfPage firstPdfPage = pdfDocument.AddPage(); try { // The image location in PDF float xLocation = float.Parse(collection["xLocationTextBox"]); float yLocation = float.Parse(collection["yLocationTextBox"]); // The URL of the HTML page to convert to an image in PDF string urlToConvert = collection["urlTextBox"]; // Create the HTML to Image element HtmlToImageElement htmlToImageElement = new HtmlToImageElement(xLocation, yLocation, urlToConvert); // Optionally set the HTML viewer width htmlToImageElement.HtmlViewerWidth = int.Parse(collection["htmlViewerWidthTextBox"]); // Optionally set the HTML viewer height if (collection["htmlViewerHeightTextBox"][0].Length > 0) { htmlToImageElement.HtmlViewerHeight = int.Parse(collection["htmlViewerHeightTextBox"]); } // Optionally set the HTML content clipping option to force the HTML content width to be exactly HtmlViewerWidth pixels htmlToImageElement.ClipHtmlView = collection["clipContentCheckBox"].Count > 0; // Optionally set the destination width in PDF if (collection["contentWidthTextBox"][0].Length > 0) { htmlToImageElement.Width = float.Parse(collection["contentWidthTextBox"]); } // Optionally set the destination height in PDF if (collection["contentHeightTextBox"][0].Length > 0) { htmlToImageElement.Height = float.Parse(collection["contentHeightTextBox"]); } // Optionally set a delay before conversion to allow asynchonous scripts to finish htmlToImageElement.ConversionDelay = 2; // Add the HTML to Image element to PDF document // The AddElementResult contains the bounds of the HTML to Image Element in last rendered PDF page // such that you can start a new PDF element right under it AddElementResult result = firstPdfPage.AddElement(htmlToImageElement); // 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 = "Add_HTML_to_Image_Elements_to_PDF.pdf"; return(fileResult); } finally { // Close the PDF document pdfDocument.Close(); } }
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="; // 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, "Add Text Notes to a PDF Document", titleFont); AddElementResult addElementResult = pdfPage.AddElement(titleTextElement); yLocation = addElementResult.EndPageBounds.Bottom + 15; // Add the text element string text = "Click the next icon to open the the text note:"; float textWidth = subtitleFont.GetTextWidth(text); TextElement textElement = new TextElement(xLocation, yLocation, text, subtitleFont); addElementResult = pdfPage.AddElement(textElement); RectangleF textNoteRectangle = new RectangleF(xLocation + textWidth + 1, yLocation, 10, 10); // Create the text note TextNoteElement textNoteElement = new TextNoteElement(textNoteRectangle, "This is an initially closed text note"); textNoteElement.NoteIcon = TextNoteIcon.Note; textNoteElement.Open = false; pdfPage.AddElement(textNoteElement); yLocation = addElementResult.EndPageBounds.Bottom + 10; // Add the text element text = "This is an already opened text note:"; textWidth = subtitleFont.GetTextWidth(text); textElement = new TextElement(xLocation, yLocation, text, subtitleFont); addElementResult = pdfPage.AddElement(textElement); textNoteRectangle = new RectangleF(xLocation + textWidth + 1, yLocation, 10, 10); // Create the text note TextNoteElement textNoteOpenedElement = new TextNoteElement(textNoteRectangle, "This is an initially opened text note"); textNoteOpenedElement.NoteIcon = TextNoteIcon.Note; textNoteOpenedElement.Open = true; pdfPage.AddElement(textNoteOpenedElement); yLocation = addElementResult.EndPageBounds.Bottom + 10; // Add the text element text = "Click the next icon to open the the help note:"; textWidth = subtitleFont.GetTextWidth(text); textElement = new TextElement(xLocation, yLocation, text, subtitleFont); addElementResult = pdfPage.AddElement(textElement); textNoteRectangle = new RectangleF(xLocation + textWidth + 1, yLocation, 10, 10); // Create the text note TextNoteElement helpNoteElement = new TextNoteElement(textNoteRectangle, "This is an initially closed help note"); helpNoteElement.NoteIcon = TextNoteIcon.Help; helpNoteElement.Open = false; pdfPage.AddElement(helpNoteElement); yLocation = addElementResult.EndPageBounds.Bottom + 10; // Add the text element text = "This is an already opened help note:"; textWidth = subtitleFont.GetTextWidth(text); textElement = new TextElement(xLocation, yLocation, text, subtitleFont); addElementResult = pdfPage.AddElement(textElement); textNoteRectangle = new RectangleF(xLocation + textWidth + 1, yLocation, 10, 10); // Create the text note TextNoteElement helpNoteOpenedElement = new TextNoteElement(textNoteRectangle, "This is an initially opened help note"); helpNoteOpenedElement.NoteIcon = TextNoteIcon.Help; helpNoteOpenedElement.Open = true; pdfPage.AddElement(helpNoteOpenedElement); yLocation = addElementResult.EndPageBounds.Bottom + 10; // Add the text element text = "Click the next icon to open the comment:"; textWidth = subtitleFont.GetTextWidth(text); textElement = new TextElement(xLocation, yLocation, text, subtitleFont); addElementResult = pdfPage.AddElement(textElement); textNoteRectangle = new RectangleF(xLocation + textWidth + 1, yLocation, 10, 10); // Create the text note TextNoteElement commentNoteElement = new TextNoteElement(textNoteRectangle, "This is an initially closed comment note"); commentNoteElement.NoteIcon = TextNoteIcon.Comment; commentNoteElement.Open = false; pdfPage.AddElement(commentNoteElement); yLocation = addElementResult.EndPageBounds.Bottom + 10; // Add the text element text = "This is an already opened comment:"; textWidth = subtitleFont.GetTextWidth(text); textElement = new TextElement(xLocation, yLocation, text, subtitleFont); addElementResult = pdfPage.AddElement(textElement); textNoteRectangle = new RectangleF(xLocation + textWidth + 1, yLocation, 10, 10); // Create the text note TextNoteElement commentNoteOpenedElement = new TextNoteElement(textNoteRectangle, "This is an initially opened comment note"); commentNoteOpenedElement.NoteIcon = TextNoteIcon.Comment; commentNoteOpenedElement.Open = true; pdfPage.AddElement(commentNoteOpenedElement); yLocation = addElementResult.EndPageBounds.Bottom + 10; // Add the text element text = "Click the next icon to open the paragraph note: "; textWidth = subtitleFont.GetTextWidth(text); textElement = new TextElement(xLocation, yLocation, text, subtitleFont); addElementResult = pdfPage.AddElement(textElement); textNoteRectangle = new RectangleF(xLocation + textWidth + 1, yLocation, 10, 10); // Create the text note TextNoteElement paragraphNoteElement = new TextNoteElement(textNoteRectangle, "This is an initially closed paragraph note"); paragraphNoteElement.NoteIcon = TextNoteIcon.Paragraph; paragraphNoteElement.Open = false; pdfPage.AddElement(paragraphNoteElement); yLocation = addElementResult.EndPageBounds.Bottom + 10; // Add the text element text = "Click the next icon to open the new paragraph note:"; textWidth = subtitleFont.GetTextWidth(text); textElement = new TextElement(xLocation, yLocation, text, subtitleFont); addElementResult = pdfPage.AddElement(textElement); textNoteRectangle = new RectangleF(xLocation + textWidth + 1, yLocation, 10, 10); // Create the text note TextNoteElement newParagraphNoteElement = new TextNoteElement(textNoteRectangle, "This is an initially closed new paragraph note"); newParagraphNoteElement.NoteIcon = TextNoteIcon.NewParagraph; newParagraphNoteElement.Open = false; pdfPage.AddElement(newParagraphNoteElement); yLocation = addElementResult.EndPageBounds.Bottom + 10; // Add the text element text = "Click the next icon to open the key note:"; textWidth = subtitleFont.GetTextWidth(text); textElement = new TextElement(xLocation, yLocation, text, subtitleFont); addElementResult = pdfPage.AddElement(textElement); textNoteRectangle = new RectangleF(xLocation + textWidth + 1, yLocation, 10, 10); // Create the text note TextNoteElement keyNoteElement = new TextNoteElement(textNoteRectangle, "This is an initially closed key note"); keyNoteElement.NoteIcon = TextNoteIcon.Key; keyNoteElement.Open = false; pdfPage.AddElement(keyNoteElement); // 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=Text_Notes.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(); } }
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="; // Add a page to PDF document PdfPage pdfPage = pdfDocument.AddPage(); // Add second page to PDF document PdfPage secondPdfPage = pdfDocument.AddPage(); // Add third page to PDF document PdfPage thirdPdfPage = 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)); // The links text font PdfFont linkTextFont = pdfDocument.AddFont(new Font("Times New Roman", 8, FontStyle.Bold, GraphicsUnit.Point)); linkTextFont.IsUnderline = true; float xLocation = 5; float yLocation = 5; // Add document title TextElement titleTextElement = new TextElement(xLocation, yLocation, "Create Internal Links in PDF Document", titleFont); AddElementResult addElementResult = pdfPage.AddElement(titleTextElement); yLocation = addElementResult.EndPageBounds.Bottom + 15; // Add a text in second page TextElement secondPageTextElement = new TextElement(5, 5, "This text is the target of an internal text link", subtitleFont); secondPdfPage.AddElement(secondPageTextElement); // Add a text in third page TextElement thirdPageTextElement = new TextElement(5, 5, "This text is the target of an internal image link", subtitleFont); thirdPdfPage.AddElement(thirdPageTextElement); // Make a text in PDF an internal link to the second page of the PDF document // Add the text element string text = "Click this text to go to the second page of this document!"; float textWidth = linkTextFont.GetTextWidth(text); TextElement linkTextElement = new TextElement(xLocation, yLocation, text, linkTextFont); linkTextElement.ForeColor = Color.Navy; addElementResult = pdfPage.AddElement(linkTextElement); // Make the text element an internal link to the second page of this document RectangleF linkRectangle = new RectangleF(xLocation, yLocation, textWidth, addElementResult.EndPageBounds.Height); // Create the destination in second page ExplicitDestination secondPageDestination = new ExplicitDestination(secondPdfPage, new PointF(5, 5)); // Create the internal link from text element to second page InternalLinkElement internalLink = new InternalLinkElement(linkRectangle, secondPageDestination); // Add the internal link to PDF document pdfPage.AddElement(internalLink); yLocation = addElementResult.EndPageBounds.Bottom + 10; // Make an image in PDF an internal link to the third page of the PDF document TextElement subtitleTextElement = new TextElement(xLocation, yLocation, "Click the image below to go to the third page of this document:", subtitleFont); addElementResult = pdfPage.AddElement(subtitleTextElement); yLocation = addElementResult.EndPageBounds.Bottom + 5; // Add the image element ImageElement linkImageElement = new ImageElement(xLocation, yLocation, 120, Server.MapPath("~/DemoAppFiles/Input/Images/logo.jpg")); addElementResult = pdfPage.AddElement(linkImageElement); // Make the image element an internal link to the third page of this document linkRectangle = addElementResult.EndPageBounds; // Create the destination in third page ExplicitDestination thirdPageDestination = new ExplicitDestination(thirdPdfPage, new PointF(5, 5)); // Create the internal link from image element to third page internalLink = new InternalLinkElement(linkRectangle, thirdPageDestination); // Add the internal link to PDF document pdfPage.AddElement(internalLink); // 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=Internal_Links.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(); } }
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="; try { // The result of adding elements to PDF document AddElementResult addElementResult = null; // The titles font used to mark various sections of the PDF document PdfFont titleFont = pdfDocument.AddFont(new Font("Times New Roman", 12, FontStyle.Bold, GraphicsUnit.Point)); // The position on X anf Y axes where to add the next element float yLocation = 5; float xLocation = 5; // Create a PDF page in PDF document PdfPage pdfPage = pdfDocument.AddPage(); // Line Elements // Add section title TextElement titleTextElement = new TextElement(xLocation, yLocation, "Line Elements", titleFont); titleTextElement.ForeColor = Color.Black; addElementResult = pdfPage.AddElement(titleTextElement); yLocation = addElementResult.EndPageBounds.Bottom + 10; xLocation += 5; pdfPage = addElementResult.EndPdfPage; // Add a line with default properties LineElement lineElement = new LineElement(xLocation, yLocation, xLocation + 50, yLocation); addElementResult = pdfPage.AddElement(lineElement); // Add a bold line LineElement boldLineElement = new LineElement(xLocation + 60, yLocation, xLocation + 110, yLocation); boldLineElement.LineStyle.LineWidth = 3; addElementResult = pdfPage.AddElement(boldLineElement); // Add dotted line LineElement dottedLineElement = new LineElement(xLocation + 120, yLocation, xLocation + 170, yLocation); dottedLineElement.LineStyle.LineDashStyle = LineDashStyle.Dot; dottedLineElement.ForeColor = Color.Green; addElementResult = pdfPage.AddElement(dottedLineElement); // Add a dashed line LineElement dashedLineElement = new LineElement(xLocation + 180, yLocation, xLocation + 230, yLocation); dashedLineElement.LineStyle.LineDashStyle = LineDashStyle.Dash; dashedLineElement.ForeColor = Color.Green; addElementResult = pdfPage.AddElement(dashedLineElement); // Add a dash-dot-dot line LineElement dashDotDotLineElement = new LineElement(xLocation + 240, yLocation, xLocation + 290, yLocation); dashDotDotLineElement.LineStyle.LineDashStyle = LineDashStyle.DashDotDot; dashDotDotLineElement.ForeColor = Color.Green; addElementResult = pdfPage.AddElement(dashDotDotLineElement); // Add a bold line with rounded cap style LineElement roundCapBoldLine = new LineElement(xLocation + 300, yLocation, xLocation + 350, yLocation); roundCapBoldLine.LineStyle.LineWidth = 5; roundCapBoldLine.LineStyle.LineCapStyle = LineCapStyle.RoundCap; roundCapBoldLine.ForeColor = Color.Blue; addElementResult = pdfPage.AddElement(roundCapBoldLine); // Add a bold line with projecting square cap style LineElement projectingSquareCapBoldLine = new LineElement(xLocation + 360, yLocation, xLocation + 410, yLocation); projectingSquareCapBoldLine.LineStyle.LineWidth = 5; projectingSquareCapBoldLine.LineStyle.LineCapStyle = LineCapStyle.ProjectingSquareCap; projectingSquareCapBoldLine.ForeColor = Color.Blue; addElementResult = pdfPage.AddElement(projectingSquareCapBoldLine); // Add a bold line with projecting butt cap style LineElement buttCapBoldLine = new LineElement(xLocation + 420, yLocation, xLocation + 470, yLocation); buttCapBoldLine.LineStyle.LineWidth = 5; buttCapBoldLine.LineStyle.LineCapStyle = LineCapStyle.ButtCap; buttCapBoldLine.ForeColor = Color.Blue; addElementResult = pdfPage.AddElement(buttCapBoldLine); yLocation = addElementResult.EndPageBounds.Bottom + 3; pdfPage = addElementResult.EndPdfPage; // Line Join Styles // Add section title xLocation -= 5; yLocation += 10; titleTextElement = new TextElement(xLocation, yLocation, "Line Join and Cap Styles", titleFont); titleTextElement.ForeColor = Color.Black; addElementResult = pdfPage.AddElement(titleTextElement); yLocation = addElementResult.EndPageBounds.Bottom + 10; xLocation += 5; pdfPage = addElementResult.EndPdfPage; // Add graphic path with miter join line style PathElement miterJoinPath = new PathElement(new PointF(xLocation, yLocation + 50)); // Add path lines miterJoinPath.AddLineSegment(new PointF(xLocation + 25, yLocation)); miterJoinPath.AddLineSegment(new PointF(xLocation + 50, yLocation + 50)); // Set path style miterJoinPath.LineStyle.LineWidth = 5; miterJoinPath.LineStyle.LineCapStyle = LineCapStyle.ProjectingSquareCap; miterJoinPath.LineStyle.LineJoinStyle = LineJoinStyle.MiterJoin; miterJoinPath.ForeColor = Color.Coral; addElementResult = pdfPage.AddElement(miterJoinPath); // Add graphic path with round join line style PathElement roundJoinPath = new PathElement(new PointF(xLocation + 70, yLocation + 50)); // Add path lines roundJoinPath.AddLineSegment(new PointF(xLocation + 95, yLocation)); roundJoinPath.AddLineSegment(new PointF(xLocation + 120, yLocation + 50)); // Set path style roundJoinPath.LineStyle.LineWidth = 5; roundJoinPath.LineStyle.LineCapStyle = LineCapStyle.RoundCap; roundJoinPath.LineStyle.LineJoinStyle = LineJoinStyle.RoundJoin; roundJoinPath.ForeColor = Color.Coral; addElementResult = pdfPage.AddElement(roundJoinPath); // Add graphic path with bevel join line style PathElement bevelJoinPath = new PathElement(new PointF(xLocation + 140, yLocation + 50)); // Add lines to path bevelJoinPath.AddLineSegment(new PointF(xLocation + 165, yLocation)); bevelJoinPath.AddLineSegment(new PointF(xLocation + 190, yLocation + 50)); // Set path style bevelJoinPath.LineStyle.LineWidth = 5; bevelJoinPath.LineStyle.LineCapStyle = LineCapStyle.ButtCap; bevelJoinPath.LineStyle.LineJoinStyle = LineJoinStyle.BevelJoin; bevelJoinPath.ForeColor = Color.Coral; // Add element to document addElementResult = pdfPage.AddElement(bevelJoinPath); // Add a polygon with miter join line style PointF[] polygonPoints = new PointF[] { new PointF(xLocation + 210, yLocation + 50), new PointF(xLocation + 235, yLocation), new PointF(xLocation + 260, yLocation + 50) }; PolygonElement miterJoinPolygon = new PolygonElement(polygonPoints); // Set polygon style miterJoinPolygon.LineStyle.LineWidth = 5; miterJoinPolygon.LineStyle.LineJoinStyle = LineJoinStyle.MiterJoin; miterJoinPolygon.ForeColor = Color.Green; miterJoinPolygon.BackColor = Color.AliceBlue; addElementResult = pdfPage.AddElement(miterJoinPolygon); // Add a polygon with round join line style polygonPoints = new PointF[] { new PointF(xLocation + 280, yLocation + 50), new PointF(xLocation + 305, yLocation), new PointF(xLocation + 330, yLocation + 50) }; PolygonElement roundJoinPolygon = new PolygonElement(polygonPoints); // Set polygon style roundJoinPolygon.LineStyle.LineWidth = 5; roundJoinPolygon.LineStyle.LineJoinStyle = LineJoinStyle.RoundJoin; roundJoinPolygon.ForeColor = Color.Green; roundJoinPolygon.BackColor = Color.Blue; addElementResult = pdfPage.AddElement(roundJoinPolygon); // Add a polygon with bevel join line style polygonPoints = new PointF[] { new PointF(xLocation + 350, yLocation + 50), new PointF(xLocation + 375, yLocation), new PointF(xLocation + 400, yLocation + 50) }; PolygonElement bevelJoinPolygon = new PolygonElement(polygonPoints); // Set polygon style bevelJoinPolygon.LineStyle.LineWidth = 5; bevelJoinPolygon.LineStyle.LineJoinStyle = LineJoinStyle.BevelJoin; bevelJoinPolygon.ForeColor = Color.Green; bevelJoinPolygon.BackColor = Color.Blue; addElementResult = pdfPage.AddElement(bevelJoinPolygon); yLocation = addElementResult.EndPageBounds.Bottom + 3; pdfPage = addElementResult.EndPdfPage; // Add a Graphics Path Element // Add section title xLocation -= 5; yLocation += 10; titleTextElement = new TextElement(xLocation, yLocation, "Path Elements", titleFont); titleTextElement.ForeColor = Color.Black; addElementResult = pdfPage.AddElement(titleTextElement); yLocation = addElementResult.EndPageBounds.Bottom + 10; xLocation += 5; pdfPage = addElementResult.EndPdfPage; // Create the path PathElement graphicsPath = new PathElement(new PointF(xLocation, yLocation)); // Add line and Bezier curve segments graphicsPath.AddLineSegment(new PointF(xLocation + 50, yLocation + 50)); graphicsPath.AddBezierCurveSegment(new PointF(xLocation + 100, yLocation), new PointF(xLocation + 200, yLocation + 100), new PointF(xLocation + 250, yLocation + 50)); graphicsPath.AddLineSegment(new PointF(xLocation + 300, yLocation)); // Close path graphicsPath.ClosePath = true; // Set path style graphicsPath.LineStyle.LineWidth = 3; graphicsPath.LineStyle.LineJoinStyle = LineJoinStyle.MiterJoin; graphicsPath.LineStyle.LineCapStyle = LineCapStyle.RoundCap; graphicsPath.ForeColor = Color.Green; //graphicsPath.BackColor = Color.Green; graphicsPath.Gradient = new GradientColor(GradientDirection.Vertical, System.Drawing.Color.LightGreen, System.Drawing.Color.Blue); // Add element to document addElementResult = pdfPage.AddElement(graphicsPath); yLocation = addElementResult.EndPageBounds.Bottom + 3; pdfPage = addElementResult.EndPdfPage; // Add Circle Elements // Add section title xLocation -= 5; yLocation -= 10; titleTextElement = new TextElement(xLocation, yLocation, "Circle Elements", titleFont); titleTextElement.ForeColor = Color.Black; addElementResult = pdfPage.AddElement(titleTextElement); yLocation = addElementResult.EndPageBounds.Bottom + 10; xLocation += 5; pdfPage = addElementResult.EndPdfPage; // Add a Circle Element with default settings CircleElement circleElement = new CircleElement(xLocation + 30, yLocation + 30, 30); addElementResult = pdfPage.AddElement(circleElement); // Add dotted circle element CircleElement dottedCircleElement = new CircleElement(xLocation + 100, yLocation + 30, 30); dottedCircleElement.ForeColor = Color.Green; dottedCircleElement.LineStyle.LineDashStyle = LineDashStyle.Dot; addElementResult = pdfPage.AddElement(dottedCircleElement); // Add a disc CircleElement discElement = new CircleElement(xLocation + 170, yLocation + 30, 30); discElement.ForeColor = Color.Green; discElement.BackColor = Color.LightGray; addElementResult = pdfPage.AddElement(discElement); // Add disc with bold border CircleElement discWithBoldBorder = new CircleElement(xLocation + 240, yLocation + 30, 30); discWithBoldBorder.LineStyle.LineWidth = 5; discWithBoldBorder.BackColor = Color.Coral; discWithBoldBorder.ForeColor = Color.Blue; addElementResult = pdfPage.AddElement(discWithBoldBorder); // Add colored disc with bold border for (int i = 30; i >= 0; i = i - 3) { CircleElement coloredDisc = new CircleElement(xLocation + 310, yLocation + 30, i == 0 ? 1 : i); coloredDisc.LineStyle.LineWidth = 3; switch ((i / 3) % 7) { case 0: coloredDisc.BackColor = Color.Red; break; case 1: coloredDisc.BackColor = Color.Orange; break; case 2: coloredDisc.BackColor = Color.Yellow; break; case 3: coloredDisc.BackColor = Color.Green; break; case 4: coloredDisc.BackColor = Color.Blue; break; case 5: coloredDisc.BackColor = Color.Indigo; break; case 6: coloredDisc.BackColor = Color.Violet; break; default: break; } addElementResult = pdfPage.AddElement(coloredDisc); } // Add a doughnut CircleElement exteriorNoBorderDisc = new CircleElement(xLocation + 380, yLocation + 30, 30); exteriorNoBorderDisc.BackColor = Color.Coral; addElementResult = pdfPage.AddElement(exteriorNoBorderDisc); CircleElement interiorNoBorderDisc = new CircleElement(xLocation + 380, yLocation + 30, 15); interiorNoBorderDisc.BackColor = Color.White; pdfPage.AddElement(interiorNoBorderDisc); // Add a simple disc CircleElement simpleDisc = new CircleElement(xLocation + 450, yLocation + 30, 30); simpleDisc.BackColor = Color.Green; addElementResult = pdfPage.AddElement(simpleDisc); yLocation = addElementResult.EndPageBounds.Bottom + 3; pdfPage = addElementResult.EndPdfPage; // Add Ellipse Elements // Add section title xLocation -= 5; yLocation += 10; titleTextElement = new TextElement(xLocation, yLocation, "Ellipse Elements", titleFont); titleTextElement.ForeColor = Color.Black; addElementResult = pdfPage.AddElement(titleTextElement); yLocation = addElementResult.EndPageBounds.Bottom + 10; xLocation += 5; pdfPage = addElementResult.EndPdfPage; // Add an Ellipse Element with default settings EllipseElement ellipseElement = new EllipseElement(xLocation + 50, yLocation + 30, 50, 30); addElementResult = pdfPage.AddElement(ellipseElement); // Add an Ellipse Element with background color and line color EllipseElement ellipseWithBackgroundAndBorder = new EllipseElement(xLocation + 160, yLocation + 30, 50, 30); ellipseWithBackgroundAndBorder.BackColor = Color.LightGray; ellipseWithBackgroundAndBorder.ForeColor = Color.Green; addElementResult = pdfPage.AddElement(ellipseWithBackgroundAndBorder); // Create an ellipse from multiple Ellipse Arc Elements EllipseArcElement ellipseArcElement1 = new EllipseArcElement(xLocation + 220, yLocation, 100, 60, 0, 100); ellipseArcElement1.ForeColor = Color.Coral; ellipseArcElement1.LineStyle.LineWidth = 3; addElementResult = pdfPage.AddElement(ellipseArcElement1); EllipseArcElement ellipseArcElement2 = new EllipseArcElement(xLocation + 220, yLocation, 100, 60, 100, 100); ellipseArcElement2.ForeColor = Color.Blue; ellipseArcElement2.LineStyle.LineWidth = 3; addElementResult = pdfPage.AddElement(ellipseArcElement2); EllipseArcElement ellipseArcElement3 = new EllipseArcElement(xLocation + 220, yLocation, 100, 60, 180, 100); ellipseArcElement3.ForeColor = Color.Green; ellipseArcElement3.LineStyle.LineWidth = 3; addElementResult = pdfPage.AddElement(ellipseArcElement3); EllipseArcElement ellipseArcElement4 = new EllipseArcElement(xLocation + 220, yLocation, 100, 60, 270, 100); ellipseArcElement4.ForeColor = Color.Violet; ellipseArcElement4.LineStyle.LineWidth = 3; addElementResult = pdfPage.AddElement(ellipseArcElement4); // Create an ellipse from multiple Ellipse Slice Elements EllipseSliceElement ellipseSliceElement1 = new EllipseSliceElement(xLocation + 330, yLocation, 100, 60, 0, 90); ellipseSliceElement1.BackColor = Color.Coral; addElementResult = pdfPage.AddElement(ellipseSliceElement1); EllipseSliceElement ellipseSliceElement2 = new EllipseSliceElement(xLocation + 330, yLocation, 100, 60, 90, 90); ellipseSliceElement2.BackColor = Color.Blue; addElementResult = pdfPage.AddElement(ellipseSliceElement2); EllipseSliceElement ellipseSliceElement3 = new EllipseSliceElement(xLocation + 330, yLocation, 100, 60, 180, 90); ellipseSliceElement3.BackColor = Color.Green; addElementResult = pdfPage.AddElement(ellipseSliceElement3); EllipseSliceElement ellipseSliceElement4 = new EllipseSliceElement(xLocation + 330, yLocation, 100, 60, 270, 90); ellipseSliceElement4.BackColor = Color.Violet; addElementResult = pdfPage.AddElement(ellipseSliceElement4); // Add an Ellipse Element with background EllipseElement ellipseWithBackground = new EllipseElement(xLocation + 490, yLocation + 30, 50, 30); ellipseWithBackground.BackColor = Color.Green; addElementResult = pdfPage.AddElement(ellipseWithBackground); yLocation = addElementResult.EndPageBounds.Bottom + 3; pdfPage = addElementResult.EndPdfPage; // Add Rectangle Elements // Add section title xLocation -= 5; yLocation += 10; titleTextElement = new TextElement(xLocation, yLocation, "Rectangle Elements", titleFont); titleTextElement.ForeColor = Color.Black; addElementResult = pdfPage.AddElement(titleTextElement); yLocation = addElementResult.EndPageBounds.Bottom + 10; xLocation += 5; pdfPage = addElementResult.EndPdfPage; // Add a Rectangle Element with default settings RectangleElement rectangleElement = new RectangleElement(xLocation, yLocation, 100, 60); addElementResult = pdfPage.AddElement(rectangleElement); // Add a Rectangle Element with background color and dotted line RectangleElement rectangleElementWithDottedLine = new RectangleElement(xLocation + 110, yLocation, 100, 60); rectangleElementWithDottedLine.BackColor = Color.LightGray; rectangleElementWithDottedLine.ForeColor = Color.Green; rectangleElementWithDottedLine.LineStyle.LineDashStyle = LineDashStyle.Dot; addElementResult = pdfPage.AddElement(rectangleElementWithDottedLine); // Add a Rectangle Element with background color without border RectangleElement rectangleElementWithoutBorder = new RectangleElement(xLocation + 220, yLocation, 100, 60); rectangleElementWithoutBorder.BackColor = Color.Green; addElementResult = pdfPage.AddElement(rectangleElementWithoutBorder); // Add a Rectangle Element with background color, bold border line and rounded corners RectangleElement rectangleElementWithRoundedCorners = new RectangleElement(xLocation + 330, yLocation, 100, 60); rectangleElementWithRoundedCorners.BackColor = Color.Coral; rectangleElementWithRoundedCorners.ForeColor = Color.Blue; rectangleElementWithRoundedCorners.LineStyle.LineWidth = 5; rectangleElementWithRoundedCorners.LineStyle.LineJoinStyle = LineJoinStyle.RoundJoin; addElementResult = pdfPage.AddElement(rectangleElementWithRoundedCorners); yLocation = addElementResult.EndPageBounds.Bottom + 3; pdfPage = addElementResult.EndPdfPage; // Add Polygon Elements // Add section title xLocation -= 5; yLocation += 10; titleTextElement = new TextElement(xLocation, yLocation, "Polygon Elements", titleFont); titleTextElement.ForeColor = Color.Black; addElementResult = pdfPage.AddElement(titleTextElement); yLocation = addElementResult.EndPageBounds.Bottom + 10; xLocation += 5; pdfPage = addElementResult.EndPdfPage; PointF[] polygonElementPoints = new PointF[] { new PointF(xLocation, yLocation + 50), new PointF(xLocation + 50, yLocation), new PointF(xLocation + 100, yLocation + 50), new PointF(xLocation + 50, yLocation + 100) }; // Add a Polygon Element with default settings PolygonElement polygonElement = new PolygonElement(polygonElementPoints); addElementResult = pdfPage.AddElement(polygonElement); polygonElementPoints = new PointF[] { new PointF(xLocation + 110, yLocation + 50), new PointF(xLocation + 160, yLocation), new PointF(xLocation + 210, yLocation + 50), new PointF(xLocation + 160, yLocation + 100) }; // Add a Polygon Element with background color and border polygonElement = new PolygonElement(polygonElementPoints); polygonElement.BackColor = Color.LightGray; polygonElement.ForeColor = Color.Green; polygonElement.LineStyle.LineDashStyle = LineDashStyle.Dot; addElementResult = pdfPage.AddElement(polygonElement); polygonElementPoints = new PointF[] { new PointF(xLocation + 220, yLocation + 50), new PointF(xLocation + 270, yLocation), new PointF(xLocation + 320, yLocation + 50), new PointF(xLocation + 270, yLocation + 100) }; // Add a Polygon Element with background color polygonElement = new PolygonElement(polygonElementPoints); polygonElement.BackColor = Color.Green; addElementResult = pdfPage.AddElement(polygonElement); PointF[] polyFillPoints = new PointF[] { new PointF(xLocation + 330, yLocation + 50), new PointF(xLocation + 380, yLocation), new PointF(xLocation + 430, yLocation + 50), new PointF(xLocation + 380, yLocation + 100) }; // Add a Polygon Element with background color and rounded line joins PolygonElement polygonElementWithBackgruondColorAndBorder = new PolygonElement(polyFillPoints); polygonElementWithBackgruondColorAndBorder.ForeColor = Color.Blue; polygonElementWithBackgruondColorAndBorder.BackColor = Color.Coral; polygonElementWithBackgruondColorAndBorder.LineStyle.LineWidth = 5; polygonElementWithBackgruondColorAndBorder.LineStyle.LineCapStyle = LineCapStyle.RoundCap; polygonElementWithBackgruondColorAndBorder.LineStyle.LineJoinStyle = LineJoinStyle.RoundJoin; addElementResult = pdfPage.AddElement(polygonElementWithBackgruondColorAndBorder); yLocation = addElementResult.EndPageBounds.Bottom + 3; pdfPage = addElementResult.EndPdfPage; // Add Bezier Curve Elements // Add section title xLocation -= 5; yLocation += 10; titleTextElement = new TextElement(xLocation, yLocation, "Bezier Curve Elements", titleFont); titleTextElement.ForeColor = Color.Black; addElementResult = pdfPage.AddElement(titleTextElement); yLocation = addElementResult.EndPageBounds.Bottom + 10; xLocation += 5; pdfPage = addElementResult.EndPdfPage; // Add a Bezier Curve Element with normal style BezierCurveElement bezierCurveElement = new BezierCurveElement(xLocation, yLocation + 50, xLocation + 50, yLocation, xLocation + 100, yLocation + 100, xLocation + 150, yLocation + 50); bezierCurveElement.ForeColor = Color.Blue; bezierCurveElement.LineStyle.LineWidth = 3; addElementResult = pdfPage.AddElement(bezierCurveElement); // Mark the points controlling the Bezier curve CircleElement controlPoint1 = new CircleElement(xLocation + 200, yLocation + 50, 2); controlPoint1.BackColor = Color.Violet; pdfPage.AddElement(controlPoint1); CircleElement controlPoint2 = new CircleElement(xLocation + 250, yLocation, 2); controlPoint2.BackColor = Color.Violet; pdfPage.AddElement(controlPoint2); CircleElement controlPoint3 = new CircleElement(xLocation + 300, yLocation + 100, 2); controlPoint3.BackColor = Color.Violet; pdfPage.AddElement(controlPoint3); CircleElement controlPoint4 = new CircleElement(xLocation + 350, yLocation + 50, 2); controlPoint4.BackColor = Color.Violet; pdfPage.AddElement(controlPoint4); // Add a Bezier Curve Element with dotted line using the controlling points above bezierCurveElement = new BezierCurveElement(controlPoint1.X, controlPoint1.Y, controlPoint2.X, controlPoint2.Y, controlPoint3.X, controlPoint3.Y, controlPoint4.X, controlPoint4.Y); bezierCurveElement.ForeColor = Color.Green; bezierCurveElement.LineStyle.LineDashStyle = LineDashStyle.Dot; bezierCurveElement.LineStyle.LineWidth = 1; addElementResult = pdfPage.AddElement(bezierCurveElement); // 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=Graphic_Elements.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="; // Add a page to PDF document PdfPage pdfPage = pdfDocument.AddPage(); // Add second page to PDF document PdfPage secondPdfPage = pdfDocument.AddPage(); // Add third page to PDF document PdfPage thirdPdfPage = 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)); // The links text font PdfFont linkTextFont = pdfDocument.AddFont(new Font("Times New Roman", 8, FontStyle.Bold, GraphicsUnit.Point)); linkTextFont.IsUnderline = true; float xLocation = 5; float yLocation = 5; // Add document title TextElement titleTextElement = new TextElement(xLocation, yLocation, "Create Internal Links in PDF Document", titleFont); AddElementResult addElementResult = pdfPage.AddElement(titleTextElement); yLocation = addElementResult.EndPageBounds.Bottom + 15; // Add a text in second page TextElement secondPageTextElement = new TextElement(5, 5, "This text is the target of an internal text link", subtitleFont); secondPdfPage.AddElement(secondPageTextElement); // Add a text in third page TextElement thirdPageTextElement = new TextElement(5, 5, "This text is the target of an internal image link", subtitleFont); thirdPdfPage.AddElement(thirdPageTextElement); // Make a text in PDF an internal link to the second page of the PDF document // Add the text element string text = "Click this text to go to the second page of this document!"; float textWidth = linkTextFont.GetTextWidth(text); TextElement linkTextElement = new TextElement(xLocation, yLocation, text, linkTextFont); linkTextElement.ForeColor = Color.Navy; addElementResult = pdfPage.AddElement(linkTextElement); // Make the text element an internal link to the second page of this document RectangleF linkRectangle = new RectangleF(xLocation, yLocation, textWidth, addElementResult.EndPageBounds.Height); // Create the destination in second page ExplicitDestination secondPageDestination = new ExplicitDestination(secondPdfPage, new PointF(5, 5)); // Create the internal link from text element to second page InternalLinkElement internalLink = new InternalLinkElement(linkRectangle, secondPageDestination); // Add the internal link to PDF document pdfPage.AddElement(internalLink); yLocation = addElementResult.EndPageBounds.Bottom + 10; // Make an image in PDF an internal link to the third page of the PDF document TextElement subtitleTextElement = new TextElement(xLocation, yLocation, "Click the image below to go to the third page of this document:", subtitleFont); addElementResult = pdfPage.AddElement(subtitleTextElement); yLocation = addElementResult.EndPageBounds.Bottom + 5; // Add the image element ImageElement linkImageElement = new ImageElement(xLocation, yLocation, 120, m_hostingEnvironment.ContentRootPath + "/wwwroot" + "/DemoAppFiles/Input/Images/logo.jpg"); addElementResult = pdfPage.AddElement(linkImageElement); // Make the image element an internal link to the third page of this document linkRectangle = addElementResult.EndPageBounds; // Create the destination in third page ExplicitDestination thirdPageDestination = new ExplicitDestination(thirdPdfPage, new PointF(5, 5)); // Create the internal link from image element to third page internalLink = new InternalLinkElement(linkRectangle, thirdPageDestination); // Add the internal link to PDF document pdfPage.AddElement(internalLink); // 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 = "Internal_Links.pdf"; return(fileResult); } 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="; // 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)); // The links text font PdfFont linkTextFont = pdfDocument.AddFont(new Font("Times New Roman", 8, FontStyle.Bold, GraphicsUnit.Point)); linkTextFont.IsUnderline = true; float xLocation = 5; float yLocation = 5; // Add document title TextElement titleTextElement = new TextElement(xLocation, yLocation, "Create URI Links in PDF Document", titleFont); AddElementResult addElementResult = pdfPage.AddElement(titleTextElement); yLocation = addElementResult.EndPageBounds.Bottom + 15; // Make a text in PDF a link to a web page // Add the text element string text = "Click this text to open a web page!"; float textWidth = linkTextFont.GetTextWidth(text); TextElement linkTextElement = new TextElement(xLocation, yLocation, text, linkTextFont); linkTextElement.ForeColor = Color.Navy; addElementResult = pdfPage.AddElement(linkTextElement); // Create the URI link element having the size of the text element RectangleF linkRectangle = new RectangleF(xLocation, yLocation, textWidth, addElementResult.EndPageBounds.Height); string url = "http://www.evopdf.com"; LinkUrlElement uriLink = new LinkUrlElement(linkRectangle, url); // Add the URI link to PDF document pdfPage.AddElement(uriLink); yLocation = addElementResult.EndPageBounds.Bottom + 10; // Make an image in PDF a link to a web page TextElement subtitleTextElement = new TextElement(xLocation, yLocation, "Click the image below to open a web page:", subtitleFont); addElementResult = pdfPage.AddElement(subtitleTextElement); yLocation = addElementResult.EndPageBounds.Bottom + 5; // Add the image element ImageElement linkImageElement = new ImageElement(xLocation, yLocation, 120, m_hostingEnvironment.ContentRootPath + "/wwwroot" + "/DemoAppFiles/Input/Images/logo.jpg"); addElementResult = pdfPage.AddElement(linkImageElement); // Create the URI link element having the size of the image element linkRectangle = addElementResult.EndPageBounds; uriLink = new LinkUrlElement(linkRectangle, url); // Add the URI link to PDF document pdfPage.AddElement(uriLink); // 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 = "URI_Links.pdf"; return(fileResult); } 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(); } }
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(); } }
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="; try { // The result of adding elements to PDF document AddElementResult addElementResult = null; // The titles font used to mark various sections of the PDF document PdfFont titleFont = pdfDocument.AddFont(new Font("Times New Roman", 12, FontStyle.Bold, GraphicsUnit.Point)); PdfFont subtitleFont = pdfDocument.AddFont(new Font("Times New Roman", 8, FontStyle.Bold, GraphicsUnit.Point)); // The position on X anf Y axes where to add the next element float yLocation = 5; float xLocation = 5; // Create a PDF page in PDF document PdfPage pdfPage = pdfDocument.AddPage(); // Add section title TextElement titleTextElement = new TextElement(xLocation, yLocation, "Images Scaling", titleFont); titleTextElement.ForeColor = Color.Black; addElementResult = pdfPage.AddElement(titleTextElement); yLocation = addElementResult.EndPageBounds.Bottom + 10; pdfPage = addElementResult.EndPdfPage; float titlesYLocation = yLocation; // Add an unscaled image // Add section title TextElement subtitleTextElement = new TextElement(xLocation, titlesYLocation, "Unscaled small image with normal resolution", subtitleFont); subtitleTextElement.ForeColor = Color.Navy; addElementResult = pdfPage.AddElement(subtitleTextElement); pdfPage = addElementResult.EndPdfPage; float imagesYLocation = addElementResult.EndPageBounds.Bottom + 10; string imagePath = Server.MapPath("~/DemoAppFiles/Input/Images/picture_small.jpg"); ImageElement unscaledImageElement = new ImageElement(xLocation, imagesYLocation, imagePath); addElementResult = pdfPage.AddElement(unscaledImageElement); RectangleF scaledDownImageRectangle = new RectangleF(addElementResult.EndPageBounds.Right + 30, addElementResult.EndPageBounds.Y, addElementResult.EndPageBounds.Width, addElementResult.EndPageBounds.Height); // Add a large image scaled down to same size in PDF // Add section title subtitleTextElement = new TextElement(scaledDownImageRectangle.X, titlesYLocation, "Scaled down large image has higher resolution", subtitleFont); subtitleTextElement.ForeColor = Color.Navy; pdfPage.AddElement(subtitleTextElement); imagePath = Server.MapPath("~/DemoAppFiles/Input/Images/picture_large.jpg"); ImageElement scaledDownImageElement = new ImageElement(scaledDownImageRectangle.X, scaledDownImageRectangle.Y, scaledDownImageRectangle.Width, imagePath); AddElementResult scaledDownImageResult = pdfPage.AddElement(scaledDownImageElement); // Add a border around the scaled down image RectangleElement borderElement = new RectangleElement(scaledDownImageRectangle); pdfPage.AddElement(borderElement); // Add an unscaled small image float columnX = scaledDownImageResult.EndPageBounds.Right + 30; // Add section title subtitleTextElement = new TextElement(columnX, titlesYLocation, "Unscaled small image", subtitleFont); subtitleTextElement.ForeColor = Color.Navy; pdfPage.AddElement(subtitleTextElement); imagePath = Server.MapPath("~/DemoAppFiles/Input/Images/picture_smaller.jpg"); unscaledImageElement = new ImageElement(columnX, imagesYLocation, imagePath); AddElementResult unscaledImageResult = pdfPage.AddElement(unscaledImageElement); RectangleF unscaledImageRectangle = unscaledImageResult.EndPageBounds; // Add an enlarged image // Add section title subtitleTextElement = new TextElement(columnX, unscaledImageRectangle.Bottom + 10, "Enlarged small image has lower resolution", subtitleFont); subtitleTextElement.ForeColor = Color.Navy; AddElementResult enlargedImageTitle = pdfPage.AddElement(subtitleTextElement); float enlargedImageWidth = unscaledImageRectangle.Width + 35; imagePath = Server.MapPath("~/DemoAppFiles/Input/Images/picture_smaller.jpg"); ImageElement enlargedImageElement = new ImageElement(columnX, enlargedImageTitle.EndPageBounds.Bottom + 10, enlargedImageWidth, imagePath); // Allow the image to be enlarged enlargedImageElement.EnlargeEnabled = true; AddElementResult enalargedImageResult = pdfPage.AddElement(enlargedImageElement); yLocation = addElementResult.EndPageBounds.Bottom + 10; // Scale an image preserving the aspect ratio titlesYLocation = yLocation; // Add section title subtitleTextElement = new TextElement(xLocation, titlesYLocation, "Scaled down image preserving aspect ratio", subtitleFont); subtitleTextElement.ForeColor = Color.Navy; addElementResult = pdfPage.AddElement(subtitleTextElement); pdfPage = addElementResult.EndPdfPage; yLocation = addElementResult.EndPageBounds.Bottom + 10; RectangleF boundingRectangle = new RectangleF(xLocation, yLocation, scaledDownImageRectangle.Width, scaledDownImageRectangle.Width); imagesYLocation = boundingRectangle.Y; imagePath = Server.MapPath("~/DemoAppFiles/Input/Images/landscape.jpg"); ImageElement keepAspectImageElement = new ImageElement(boundingRectangle.X, imagesYLocation, boundingRectangle.Width, boundingRectangle.Width, true, imagePath); addElementResult = pdfPage.AddElement(keepAspectImageElement); borderElement = new RectangleElement(boundingRectangle); borderElement.ForeColor = Color.Black; pdfPage.AddElement(borderElement); // Scale an image without preserving aspect ratio // This can produce a distorted image boundingRectangle = new RectangleF(addElementResult.EndPageBounds.Right + 30, addElementResult.EndPageBounds.Y, scaledDownImageRectangle.Width, scaledDownImageRectangle.Width); // Add section title subtitleTextElement = new TextElement(boundingRectangle.X, titlesYLocation, "Scaled down image without preserving aspect ratio", subtitleFont); subtitleTextElement.ForeColor = Color.Navy; pdfPage.AddElement(subtitleTextElement); imagePath = Server.MapPath("~/DemoAppFiles/Input/Images/landscape.jpg"); ImageElement notKeepAspectImageElement = new ImageElement(boundingRectangle.X, imagesYLocation, boundingRectangle.Width, boundingRectangle.Width, false, imagePath); addElementResult = pdfPage.AddElement(notKeepAspectImageElement); borderElement = new RectangleElement(boundingRectangle); borderElement.ForeColor = Color.Black; pdfPage.AddElement(borderElement); pdfPage = addElementResult.EndPdfPage; yLocation = addElementResult.EndPageBounds.Bottom + 20; // Add transparent images // Add section title titleTextElement = new TextElement(xLocation, yLocation, "Transparent Images", titleFont); titleTextElement.ForeColor = Color.Black; addElementResult = pdfPage.AddElement(titleTextElement); yLocation = addElementResult.EndPageBounds.Bottom + 10; pdfPage = addElementResult.EndPdfPage; imagePath = Server.MapPath("~/DemoAppFiles/Input/Images/transparent.png"); ImageElement trasparentImageElement = new ImageElement(xLocation, yLocation, 150, imagePath); addElementResult = pdfPage.AddElement(trasparentImageElement); imagePath = Server.MapPath("~/DemoAppFiles/Input/Images/rose.png"); trasparentImageElement = new ImageElement(addElementResult.EndPageBounds.Right + 60, yLocation + 20, 150, imagePath); pdfPage.AddElement(trasparentImageElement); pdfPage = addElementResult.EndPdfPage; yLocation = addElementResult.EndPageBounds.Bottom + 20; // Rotate images // Add section title titleTextElement = new TextElement(xLocation, yLocation, "Rotated Images", titleFont); titleTextElement.ForeColor = Color.Black; addElementResult = pdfPage.AddElement(titleTextElement); yLocation = addElementResult.EndPageBounds.Bottom + 10; pdfPage = addElementResult.EndPdfPage; // Add a not rotated image imagePath = Server.MapPath("~/DemoAppFiles/Input/Images/compass.png"); ImageElement noRotationImageElement = new ImageElement(xLocation, yLocation, 125, imagePath); addElementResult = pdfPage.AddElement(noRotationImageElement); float imageXLocation = addElementResult.EndPageBounds.X; float imageYLocation = addElementResult.EndPageBounds.Y; float imageWidth = addElementResult.EndPageBounds.Width; float imageHeight = addElementResult.EndPageBounds.Height; // The rotated coordinates system location float rotatedImageXLocation = imageXLocation + imageWidth + 20 + imageHeight; float rotatedImageYLocation = imageYLocation; // Add the image rotated 90 degrees ImageElement rotate90ImageElement = new ImageElement(0, 0, 125, imagePath); rotate90ImageElement.Translate(rotatedImageXLocation, rotatedImageYLocation); rotate90ImageElement.Rotate(90); pdfPage.AddElement(rotate90ImageElement); rotatedImageXLocation += 20 + imageWidth; rotatedImageYLocation = imageYLocation + imageHeight; // Add the image rotated 180 degrees ImageElement rotate180ImageElement = new ImageElement(0, 0, 125, imagePath); rotate180ImageElement.Translate(rotatedImageXLocation, rotatedImageYLocation); rotate180ImageElement.Rotate(180); pdfPage.AddElement(rotate180ImageElement); rotatedImageXLocation += 20; rotatedImageYLocation = imageYLocation + imageWidth; // Add the image rotated 270 degrees ImageElement rotate270ImageElement = new ImageElement(0, 0, 125, imagePath); rotate270ImageElement.Translate(rotatedImageXLocation, rotatedImageYLocation); rotate270ImageElement.Rotate(270); pdfPage.AddElement(rotate270ImageElement); pdfPage = addElementResult.EndPdfPage; yLocation = addElementResult.EndPageBounds.Bottom + 20; // 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=Image_Elements.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(); } }
protected void convertToPdfButton_Click(object sender, EventArgs e) { // Create the PDF document where to add the HTML documents 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="; // Create a PDF page where to add the first HTML PdfPage firstPdfPage = pdfDocument.AddPage(); try { // Create the first HTML to PDF element HtmlToPdfElement firstHtml = new HtmlToPdfElement(0, 0, firstUrlTextBox.Text); // Optionally set a delay before conversion to allow asynchonous scripts to finish firstHtml.ConversionDelay = 2; // Add the first HTML to PDF document AddElementResult firstAddResult = firstPdfPage.AddElement(firstHtml); PdfPage secondPdfPage = null; PointF secondHtmlLocation = Point.Empty; if (startNewPageCheckBox.Checked) { // Create a PDF page where to add the second HTML secondPdfPage = pdfDocument.AddPage(); secondHtmlLocation = PointF.Empty; } else { // Add the second HTML on the PDF page where the first HTML ended secondPdfPage = firstAddResult.EndPdfPage; secondHtmlLocation = new PointF(firstAddResult.EndPageBounds.Left, firstAddResult.EndPageBounds.Bottom); } // Create the second HTML to PDF element HtmlToPdfElement secondHtml = new HtmlToPdfElement(secondHtmlLocation.X, secondHtmlLocation.Y, secondUrlTextBox.Text); // Optionally set a delay before conversion to allow asynchonous scripts to finish secondHtml.ConversionDelay = 2; // Add the second HTML to PDF document secondPdfPage.AddElement(secondHtml); // 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=Merge_Multipe_HTML.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 ConvertHtmlToPdf(IFormCollection collection) { // Create the PDF document where to add the HTML documents 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="; // Create a PDF page where to add the first HTML PdfPage firstPdfPage = pdfDocument.AddPage(); // Enable the creation of a table of contents from H1 to H6 tags found in HTML pdfDocument.TableOfContents.AutoTocItemsEnabled = collection["autoTableOfContentsCheckBox"].Count > 0; // Optionally set the table of contents title pdfDocument.TableOfContents.Title = "Table of Contents"; // Optionally set the title style using CSS sttributes pdfDocument.TableOfContents.TitleStyle = "color:navy; font-family:'Times New Roman'; font-size:28px; font-weight:normal"; // Optionally set the style of level 1 items in table of contents string level1TextStyle = "color:black; font-family:'Times New Roman'; font-size:20px; font-weight:normal; font-style:normal; background-color:#F0F0F0"; pdfDocument.TableOfContents.SetItemStyle(1, level1TextStyle); // Optionally set the page numbers style of level 1 items in table of contents string level1PageNumberStyle = "color:black; padding-right:3px; background-color:#F0F0F0; font-size:14px; font-weight:bold"; pdfDocument.TableOfContents.SetPageNumberStyle(1, level1PageNumberStyle); try { // Create the first HTML to PDF element HtmlToPdfElement firstHtml = new HtmlToPdfElement(0, 0, collection["firstUrlTextBox"]); // Optionally set a delay before conversion to allow asynchonous scripts to finish firstHtml.ConversionDelay = 2; // Enable or disable the table of contents for the first HTML document firstHtml.TableOfContentsEnabled = collection["includeFirstHtmlTocCheckBox"].Count > 0; // Add the first HTML to PDF document AddElementResult firstAddResult = firstPdfPage.AddElement(firstHtml); PdfPage secondPdfPage = null; PointF secondHtmlLocation = Point.Empty; if (collection["startNewPageCheckBox"].Count > 0) { // Create a PDF page where to add the second HTML secondPdfPage = pdfDocument.AddPage(); secondHtmlLocation = PointF.Empty; } else { // Add the second HTML on the PDF page where the first HTML ended secondPdfPage = firstAddResult.EndPdfPage; secondHtmlLocation = new PointF(firstAddResult.EndPageBounds.Left, firstAddResult.EndPageBounds.Bottom); } // Create the second HTML to PDF element HtmlToPdfElement secondHtml = new HtmlToPdfElement(secondHtmlLocation.X, secondHtmlLocation.Y, collection["secondUrlTextBox"]); // Optionally set a delay before conversion to allow asynchonous scripts to finish secondHtml.ConversionDelay = 2; // Enable or disable the table of contents for the second HTML document secondHtml.TableOfContentsEnabled = collection["includeSecondHtmlTocCheckBox"].Count > 0; // Add the second HTML to PDF document secondPdfPage.AddElement(secondHtml); // 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 = "Merge_Table_of_Contents.pdf"; return(fileResult); } 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="; // 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, "Add Text Notes to a PDF Document", titleFont); AddElementResult addElementResult = pdfPage.AddElement(titleTextElement); yLocation = addElementResult.EndPageBounds.Bottom + 15; // Add the text element string text = "Click the next icon to open the the text note:"; float textWidth = subtitleFont.GetTextWidth(text); TextElement textElement = new TextElement(xLocation, yLocation, text, subtitleFont); addElementResult = pdfPage.AddElement(textElement); RectangleF textNoteRectangle = new RectangleF(xLocation + textWidth + 1, yLocation, 10, 10); // Create the text note TextNoteElement textNoteElement = new TextNoteElement(textNoteRectangle, "This is an initially closed text note"); textNoteElement.NoteIcon = TextNoteIcon.Note; textNoteElement.Open = false; pdfPage.AddElement(textNoteElement); yLocation = addElementResult.EndPageBounds.Bottom + 10; // Add the text element text = "This is an already opened text note:"; textWidth = subtitleFont.GetTextWidth(text); textElement = new TextElement(xLocation, yLocation, text, subtitleFont); addElementResult = pdfPage.AddElement(textElement); textNoteRectangle = new RectangleF(xLocation + textWidth + 1, yLocation, 10, 10); // Create the text note TextNoteElement textNoteOpenedElement = new TextNoteElement(textNoteRectangle, "This is an initially opened text note"); textNoteOpenedElement.NoteIcon = TextNoteIcon.Note; textNoteOpenedElement.Open = true; pdfPage.AddElement(textNoteOpenedElement); yLocation = addElementResult.EndPageBounds.Bottom + 10; // Add the text element text = "Click the next icon to open the the help note:"; textWidth = subtitleFont.GetTextWidth(text); textElement = new TextElement(xLocation, yLocation, text, subtitleFont); addElementResult = pdfPage.AddElement(textElement); textNoteRectangle = new RectangleF(xLocation + textWidth + 1, yLocation, 10, 10); // Create the text note TextNoteElement helpNoteElement = new TextNoteElement(textNoteRectangle, "This is an initially closed help note"); helpNoteElement.NoteIcon = TextNoteIcon.Help; helpNoteElement.Open = false; pdfPage.AddElement(helpNoteElement); yLocation = addElementResult.EndPageBounds.Bottom + 10; // Add the text element text = "This is an already opened help note:"; textWidth = subtitleFont.GetTextWidth(text); textElement = new TextElement(xLocation, yLocation, text, subtitleFont); addElementResult = pdfPage.AddElement(textElement); textNoteRectangle = new RectangleF(xLocation + textWidth + 1, yLocation, 10, 10); // Create the text note TextNoteElement helpNoteOpenedElement = new TextNoteElement(textNoteRectangle, "This is an initially opened help note"); helpNoteOpenedElement.NoteIcon = TextNoteIcon.Help; helpNoteOpenedElement.Open = true; pdfPage.AddElement(helpNoteOpenedElement); yLocation = addElementResult.EndPageBounds.Bottom + 10; // Add the text element text = "Click the next icon to open the comment:"; textWidth = subtitleFont.GetTextWidth(text); textElement = new TextElement(xLocation, yLocation, text, subtitleFont); addElementResult = pdfPage.AddElement(textElement); textNoteRectangle = new RectangleF(xLocation + textWidth + 1, yLocation, 10, 10); // Create the text note TextNoteElement commentNoteElement = new TextNoteElement(textNoteRectangle, "This is an initially closed comment note"); commentNoteElement.NoteIcon = TextNoteIcon.Comment; commentNoteElement.Open = false; pdfPage.AddElement(commentNoteElement); yLocation = addElementResult.EndPageBounds.Bottom + 10; // Add the text element text = "This is an already opened comment:"; textWidth = subtitleFont.GetTextWidth(text); textElement = new TextElement(xLocation, yLocation, text, subtitleFont); addElementResult = pdfPage.AddElement(textElement); textNoteRectangle = new RectangleF(xLocation + textWidth + 1, yLocation, 10, 10); // Create the text note TextNoteElement commentNoteOpenedElement = new TextNoteElement(textNoteRectangle, "This is an initially opened comment note"); commentNoteOpenedElement.NoteIcon = TextNoteIcon.Comment; commentNoteOpenedElement.Open = true; pdfPage.AddElement(commentNoteOpenedElement); yLocation = addElementResult.EndPageBounds.Bottom + 10; // Add the text element text = "Click the next icon to open the paragraph note: "; textWidth = subtitleFont.GetTextWidth(text); textElement = new TextElement(xLocation, yLocation, text, subtitleFont); addElementResult = pdfPage.AddElement(textElement); textNoteRectangle = new RectangleF(xLocation + textWidth + 1, yLocation, 10, 10); // Create the text note TextNoteElement paragraphNoteElement = new TextNoteElement(textNoteRectangle, "This is an initially closed paragraph note"); paragraphNoteElement.NoteIcon = TextNoteIcon.Paragraph; paragraphNoteElement.Open = false; pdfPage.AddElement(paragraphNoteElement); yLocation = addElementResult.EndPageBounds.Bottom + 10; // Add the text element text = "Click the next icon to open the new paragraph note:"; textWidth = subtitleFont.GetTextWidth(text); textElement = new TextElement(xLocation, yLocation, text, subtitleFont); addElementResult = pdfPage.AddElement(textElement); textNoteRectangle = new RectangleF(xLocation + textWidth + 1, yLocation, 10, 10); // Create the text note TextNoteElement newParagraphNoteElement = new TextNoteElement(textNoteRectangle, "This is an initially closed new paragraph note"); newParagraphNoteElement.NoteIcon = TextNoteIcon.NewParagraph; newParagraphNoteElement.Open = false; pdfPage.AddElement(newParagraphNoteElement); yLocation = addElementResult.EndPageBounds.Bottom + 10; // Add the text element text = "Click the next icon to open the key note:"; textWidth = subtitleFont.GetTextWidth(text); textElement = new TextElement(xLocation, yLocation, text, subtitleFont); addElementResult = pdfPage.AddElement(textElement); textNoteRectangle = new RectangleF(xLocation + textWidth + 1, yLocation, 10, 10); // Create the text note TextNoteElement keyNoteElement = new TextNoteElement(textNoteRectangle, "This is an initially closed key note"); keyNoteElement.NoteIcon = TextNoteIcon.Key; keyNoteElement.Open = false; pdfPage.AddElement(keyNoteElement); // 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 = "Text_Notes.pdf"; return(fileResult); } finally { // Close the PDF document pdfDocument.Close(); } }