public ActionResult ConvertToPdf(IFormCollection collection) { m_formCollection = collection; // create an empty PDF document PdfDocument document = new PdfDocument(); // set a demo serial number document.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ=="; // add a page to document PdfPage page1 = document.AddPage(PdfPageSize.A4, new PdfDocumentMargins(5), PdfPageOrientation.Portrait); try { // set the document header and footer before adding any objects to document SetHeader(document); SetFooter(document); // layout the HTML from URL 1 PdfHtml html1 = new PdfHtml(collection["textBoxUrl1"]); html1.WaitBeforeConvert = 2; PdfLayoutInfo html1LayoutInfo = page1.Layout(html1); // determine the PDF page where to add URL 2 PdfPage page2 = null; System.Drawing.PointF location2 = System.Drawing.PointF.Empty; if (collection["checkBoxNewPage"].Count > 0) { // URL 2 is laid out on a new page with the selected orientation page2 = document.AddPage(PdfPageSize.A4, new PdfDocumentMargins(5), GetSelectedPageOrientation()); location2 = System.Drawing.PointF.Empty; } else { // URL 2 is laid out immediately after URL 1 and html1LayoutInfo // gives the location where the URL 1 layout finished page2 = document.Pages[html1LayoutInfo.LastPageIndex]; location2 = new System.Drawing.PointF(html1LayoutInfo.LastPageRectangle.X, html1LayoutInfo.LastPageRectangle.Bottom); } // layout the HTML from URL 2 PdfHtml html2 = new PdfHtml(location2.X, location2.Y, collection["textBoxUrl2"]); html2.WaitBeforeConvert = 2; page2.Layout(html2); // write the PDF document to a memory buffer byte[] pdfBuffer = document.WriteToMemory(); FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf"); fileResult.FileDownloadName = "LayoutMultipleHtml.pdf"; return(fileResult); } finally { document.Close(); } }
public ActionResult CreatePdf(IFormCollection collection) { // create a PDF document PdfDocument document = new PdfDocument(); // set a demo serial number document.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ=="; // create a page in document PdfPage page1 = document.AddPage(); // create the true type fonts that can be used in document text Font sysFont = new Font("Times New Roman", 10, System.Drawing.GraphicsUnit.Point); PdfFont pdfFont = document.CreateFont(sysFont); PdfFont pdfFontEmbed = document.CreateFont(sysFont, true); float crtYPos = 20; float crtXPos = 5; // add a title to PDF document PdfText titleTextTransImage = new PdfText(crtXPos, crtYPos, "Click the image below to open the digital signature", pdfFontEmbed); titleTextTransImage.ForeColor = Color.Navy; PdfLayoutInfo textLayoutInfo = page1.Layout(titleTextTransImage); crtYPos += textLayoutInfo.LastPageRectangle.Height + 10; // layout a PNG image with alpha transparency PdfImage transparentPdfImage = new PdfImage(crtXPos, crtYPos, m_hostingEnvironment.ContentRootPath + @"\wwwroot" + @"\DemoFiles\Images\HiQPdfLogo_small.png"); PdfLayoutInfo imageLayoutInfo = page1.Layout(transparentPdfImage); // apply a digital sgnature over the image PdfCertificatesCollection pdfCertificates = PdfCertificatesCollection.FromFile(m_hostingEnvironment.ContentRootPath + @"\wwwroot" + @"\DemoFiles\Pfx\hiqpdf.pfx", "hiqpdf"); PdfDigitalSignature digitalSignature = new PdfDigitalSignature(pdfCertificates[0]); digitalSignature.SigningReason = "My signing reason"; digitalSignature.SigningLocation = "My signing location"; digitalSignature.SignerContactInfo = "My contact info"; document.AddDigitalSignature(digitalSignature, imageLayoutInfo.LastPdfPage, imageLayoutInfo.LastPageRectangle); try { // write the PDF document to a memory buffer byte[] pdfBuffer = document.WriteToMemory(); FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf"); fileResult.FileDownloadName = "DigitalSignatures.pdf"; return(fileResult); } finally { document.Close(); } }
protected void buttonCreatePdf_Click(object sender, EventArgs e) { // create a PDF document PdfDocument document = new PdfDocument(); // set a demo serial number document.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ=="; // create a page in document PdfPage page1 = document.AddPage(); // create the true type fonts that can be used in document text Font sysFont = new Font("Times New Roman", 10, System.Drawing.GraphicsUnit.Point); PdfFont pdfFont = document.CreateFont(sysFont); PdfFont pdfFontEmbed = document.CreateFont(sysFont, true); float crtYPos = 20; float crtXPos = 5; // add a title to PDF document PdfText titleTextTransImage = new PdfText(crtXPos, crtYPos, "Click the image below to open the digital signature", pdfFontEmbed); titleTextTransImage.ForeColor = Color.Navy; PdfLayoutInfo textLayoutInfo = page1.Layout(titleTextTransImage); crtYPos += textLayoutInfo.LastPageRectangle.Height + 10; // layout a PNG image with alpha transparency PdfImage transparentPdfImage = new PdfImage(crtXPos, crtYPos, Server.MapPath("~") + @"\DemoFiles\Images\HiQPdfLogo_small.png"); PdfLayoutInfo imageLayoutInfo = page1.Layout(transparentPdfImage); // apply a digital sgnature over the image PdfCertificatesCollection pdfCertificates = PdfCertificatesCollection.FromFile(Server.MapPath("~") + @"\DemoFiles\Pfx\hiqpdf.pfx", "hiqpdf"); PdfDigitalSignature digitalSignature = new PdfDigitalSignature(pdfCertificates[0]); digitalSignature.SigningReason = "My signing reason"; digitalSignature.SigningLocation = "My signing location"; digitalSignature.SignerContactInfo = "My contact info"; document.AddDigitalSignature(digitalSignature, imageLayoutInfo.LastPdfPage, imageLayoutInfo.LastPageRectangle); try { // write the PDF document to a memory buffer byte[] pdfBuffer = document.WriteToMemory(); // inform the browser about the binary data format HttpContext.Current.Response.AddHeader("Content-Type", "application/pdf"); // let the browser know how to open the PDF document and the file name HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("attachment; filename=DigitalSignatures.pdf; size={0}", pdfBuffer.Length.ToString())); // write the PDF buffer to HTTP response HttpContext.Current.Response.BinaryWrite(pdfBuffer); // call End() method of HTTP response to stop ASP.NET page processing HttpContext.Current.Response.End(); } finally { document.Close(); } }
public ActionResult CreatePdf(IFormCollection collection) { // create a PDF document PdfDocument document = new PdfDocument(); // set a demo serial number document.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ=="; // create a page in document PdfPage page1 = document.AddPage(); // create the true type fonts that can be used in document text System.Drawing.Font sysFont = new System.Drawing.Font("Times New Roman", 10, System.Drawing.GraphicsUnit.Point); PdfFont pdfFont = document.CreateFont(sysFont); PdfFont pdfFontEmbed = document.CreateFont(sysFont, true); System.Drawing.Font sysFontBold = new System.Drawing.Font("Times New Roman", 10, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point); PdfFont pdfFontBold = document.CreateFont(sysFontBold); PdfFont pdfFontBoldEmbed = document.CreateFont(sysFontBold, true); // create a standard Helvetica Type 1 font that can be used in document text PdfFont helveticaStdFont = document.CreateStandardFont(PdfStandardFont.Helvetica); helveticaStdFont.Size = 10; float crtYPos = 20; float crtXPos = 5; PdfLayoutInfo textLayoutInfo = null; string dummyText = @"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."; #region Layout a text that expands to the right edge of the PDF page PdfText titleTextAtLocation = new PdfText(crtXPos, crtYPos, "The text below extends from the layout position to the right edge of the PDF page:", pdfFontBoldEmbed); titleTextAtLocation.ForeColor = System.Drawing.Color.Navy; textLayoutInfo = page1.Layout(titleTextAtLocation); // advance the Y position in the PDF page crtYPos += textLayoutInfo.LastPageRectangle.Height + 10; PdfText textExpandsToRightEdge = new PdfText(crtXPos + 50, crtYPos, dummyText, pdfFont); textExpandsToRightEdge.BackColor = System.Drawing.Color.WhiteSmoke; textLayoutInfo = page1.Layout(textExpandsToRightEdge); // draw a rectangle around the text PdfRectangle borderPdfRectangle = new PdfRectangle(textLayoutInfo.LastPageRectangle); borderPdfRectangle.LineStyle.LineWidth = 0.5f; page1.Layout(borderPdfRectangle); // advance the Y position in the PDF page crtYPos += textLayoutInfo.LastPageRectangle.Height + 10; #endregion #region Layout a text with width limit PdfText titleTextWithWidth = new PdfText(crtXPos, crtYPos, "The text below is limited by a given width and has a free height:", pdfFontBoldEmbed); titleTextWithWidth.ForeColor = System.Drawing.Color.Navy; textLayoutInfo = page1.Layout(titleTextWithWidth); // advance the Y position in the PDF page crtYPos += textLayoutInfo.LastPageRectangle.Height + 10; PdfText textWithWidthLimit = new PdfText(crtXPos + 50, crtYPos, 300, dummyText, pdfFont); textWithWidthLimit.BackColor = System.Drawing.Color.WhiteSmoke; textLayoutInfo = page1.Layout(textWithWidthLimit); // draw a rectangle around the text borderPdfRectangle = new PdfRectangle(textLayoutInfo.LastPageRectangle); borderPdfRectangle.LineStyle.LineWidth = 0.5f; page1.Layout(borderPdfRectangle); // advance the Y position in the PDF page crtYPos += textLayoutInfo.LastPageRectangle.Height + 10; #endregion #region Layout a text with width and height limits PdfText titleTextWithWidthAndHeight = new PdfText(crtXPos, crtYPos, "The text below is limited by a given width and height and is trimmed:", pdfFontBoldEmbed); titleTextWithWidthAndHeight.ForeColor = System.Drawing.Color.Navy; textLayoutInfo = page1.Layout(titleTextWithWidthAndHeight); // advance the Y position in the PDF page crtYPos += textLayoutInfo.LastPageRectangle.Height + 10; PdfText textWithWidthAndHeightLimit = new PdfText(crtXPos + 50, crtYPos, 300, 50, dummyText, pdfFont); textWithWidthAndHeightLimit.BackColor = System.Drawing.Color.WhiteSmoke; textLayoutInfo = page1.Layout(textWithWidthAndHeightLimit); // draw a rectangle around the text borderPdfRectangle = new PdfRectangle(textLayoutInfo.LastPageRectangle); borderPdfRectangle.LineStyle.LineWidth = 0.5f; page1.Layout(borderPdfRectangle); // advance the Y position in the PDF page crtYPos += textLayoutInfo.LastPageRectangle.Height + 10; #endregion #region Layout a text with standard font PdfText textWithStandardFont = new PdfText(crtXPos, crtYPos, "This green text is written with a Helvetica Standard Type 1 font", helveticaStdFont); textWithStandardFont.BackColor = System.Drawing.Color.WhiteSmoke; textWithStandardFont.ForeColor = System.Drawing.Color.Green; textLayoutInfo = page1.Layout(textWithStandardFont); // advance the Y position in the PDF page crtYPos += textLayoutInfo.LastPageRectangle.Height + 10; #endregion #region Layout a rotated text PdfText titleRotatedText = new PdfText(crtXPos, crtYPos, "The text below is rotated:", pdfFontBoldEmbed); titleRotatedText.ForeColor = System.Drawing.Color.Navy; textLayoutInfo = page1.Layout(titleRotatedText); // advance the Y position in the PDF page crtYPos += textLayoutInfo.LastPageRectangle.Height + 10; // create a reference Graphics used for measuring System.Drawing.Bitmap refBmp = new System.Drawing.Bitmap(1, 1); System.Drawing.Graphics refGraphics = System.Drawing.Graphics.FromImage(refBmp); refGraphics.PageUnit = System.Drawing.GraphicsUnit.Point; string counterRotatedText = "This text is rotated 45 degrees counter clockwise"; // measure the rotated text size System.Drawing.SizeF counterRotatedTextSize = refGraphics.MeasureString(counterRotatedText, sysFont); // advance the Y position in the PDF page crtYPos += counterRotatedTextSize.Width / (float)Math.Sqrt(2) + 10; string clockwiseRotatedText = "This text is rotated 45 degrees clockwise"; PdfText rotatedCounterClockwiseText = new PdfText(crtXPos + 100, crtYPos, counterRotatedText, pdfFontEmbed); rotatedCounterClockwiseText.RotationAngle = 45; textLayoutInfo = page1.Layout(rotatedCounterClockwiseText); PdfText rotatedClockwiseText = new PdfText(crtXPos + 100, crtYPos, clockwiseRotatedText, pdfFontEmbed); rotatedClockwiseText.RotationAngle = -45; textLayoutInfo = page1.Layout(rotatedClockwiseText); // measure the rotated text size System.Drawing.SizeF clockwiseRotatedTextSize = refGraphics.MeasureString(clockwiseRotatedText, sysFont); // advance the Y position in the PDF page crtYPos += clockwiseRotatedTextSize.Width / (float)Math.Sqrt(2) + 10; // dispose the graphics used for measuring refGraphics.Dispose(); refBmp.Dispose(); #endregion #region Layout an automatically paginated text string dummyBigText = System.IO.File.ReadAllText(m_hostingEnvironment.ContentRootPath + @"\wwwroot" + @"\DemoFiles\Text\DummyBigText.txt"); PdfText titleTextPaginated = new PdfText(crtXPos, crtYPos, "The text below is automatically paginated when it gets to the bottom of this page:", pdfFontBoldEmbed); titleTextPaginated.ForeColor = System.Drawing.Color.Navy; textLayoutInfo = page1.Layout(titleTextPaginated); // advance the Y position in the PDF page crtYPos += textLayoutInfo.LastPageRectangle.Height + 10; PdfText paginatedText = new PdfText(crtXPos + 50, crtYPos, 300, dummyBigText, pdfFont); paginatedText.BackColor = System.Drawing.Color.WhiteSmoke; paginatedText.Cropping = false; textLayoutInfo = page1.Layout(paginatedText); // get the last page where the text was rendered PdfPage crtPage = document.Pages[textLayoutInfo.LastPageIndex]; // draw a line at the bottom of the text on the second page System.Drawing.PointF leftPoint = new System.Drawing.PointF(textLayoutInfo.LastPageRectangle.Left, textLayoutInfo.LastPageRectangle.Bottom); System.Drawing.PointF rightPoint = new System.Drawing.PointF(textLayoutInfo.LastPageRectangle.Right, textLayoutInfo.LastPageRectangle.Bottom); PdfLine borderLine = new PdfLine(leftPoint, rightPoint); borderLine.LineStyle.LineWidth = 0.5f; crtPage.Layout(borderLine); // advance the Y position in the second PDF page crtYPos += textLayoutInfo.LastPageRectangle.Height + 10; #endregion try { // write the PDF document to a memory buffer byte[] pdfBuffer = document.WriteToMemory(); FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf"); fileResult.FileDownloadName = "PdfText.pdf"; return(fileResult); } finally { document.Close(); } }
public ActionResult CreatePdf(FormCollection collection) { // the path PDF document to edit string pdfDocumentToEdit = Server.MapPath("~") + @"\DemoFiles\Pdf\WikiPdf.pdf"; // load the PDF document to edit PdfDocument document = PdfDocument.FromFile(pdfDocumentToEdit); // set a demo serial number document.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ=="; #region Add an orange border to each page of the loaded PDF document // add an orange border to each PDF page in the loaded PDF document foreach (PdfPage pdfPage in document.Pages) { float crtPdfPageWidth = pdfPage.Size.Width; float crtPdfPageHeight = pdfPage.Size.Height; // create a PdfRectangle object PdfRectangle pdfRectangle = new PdfRectangle(2, 2, crtPdfPageWidth - 4, crtPdfPageHeight - 4); pdfRectangle.LineStyle.LineWidth = 2; pdfRectangle.ForeColor = System.Drawing.Color.OrangeRed; // layout the rectangle in PDF page pdfPage.Layout(pdfRectangle); } #endregion Add an orange border to each page of the loaded PDF document #region Layout HTML in a canvas to be repeated on each page of the loaded PDF document PdfPage pdfPage1 = document.Pages[0]; float pdfPageWidth = pdfPage1.Size.Width; float pdfPageHeight = pdfPage1.Size.Height; // the width of the HTML logo in pixels int htmlLogoWidthPx = 400; // the width of the HTML logo in points float htmlLogoWidthPt = PdfDpiTransform.FromPixelsToPoints(htmlLogoWidthPx); float htmlLogoHeightPt = 100; // create a canvas to be repeated in the center of each PDF page // the canvas is a PDF container that can contain PDF objects ( HTML, text, images, etc ) PdfRepeatCanvas repeatedCanvas = document.CreateRepeatedCanvas(new System.Drawing.RectangleF((pdfPageWidth - htmlLogoWidthPt) / 2, (pdfPageHeight - htmlLogoHeightPt) / 2, htmlLogoWidthPt, htmlLogoHeightPt)); // the HTML file giving the content to be placed in the repeated canvas string htmlFile = Server.MapPath("~") + @"\DemoFiles\Html\Logo.Html"; // the HTML object to be laid out in repeated canvas PdfHtml htmlLogo = new PdfHtml(0, 0, repeatedCanvas.Width, repeatedCanvas.Height, htmlFile); // the browser width when rendering the HTML htmlLogo.BrowserWidth = htmlLogoWidthPx; // the HTML content will fit the destination hight which is the same with repeated canvas height htmlLogo.FitDestHeight = true; // layout the HTML object in the repeated canvas PdfLayoutInfo htmlLogLayoutInfo = repeatedCanvas.Layout(htmlLogo); #endregion Layout HTML in a canvas to be repeated on each page of the loaded PDF document try { // write the PDF document to a memory buffer byte[] pdfBuffer = document.WriteToMemory(); FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf"); fileResult.FileDownloadName = "EditPdf.pdf"; return(fileResult); } finally { document.Close(); } }
public ActionResult CreatePdf(FormCollection collection) { // create an empty PDF document PdfDocument document = new PdfDocument(); // set a demo serial number document.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ=="; // add a page to document PdfPage page1 = document.AddPage(PdfPageSize.A4, new PdfDocumentMargins(0), PdfPageOrientation.Portrait); // an object to be set with HTML layout info after conversion PdfLayoutInfo htmlLayoutInfo = null; try { // create the HTML object from URL or HTML code PdfHtml htmlObject = null; if (collection["UrlOrHtmlCode"] == "radioButtonConvertUrl") { // create from URL htmlObject = new PdfHtml(collection["textBoxUrl"]); } else { // create from HTML code string htmlCode = collection["textBoxHtmlCode"]; string baseUrl = collection["textBoxBaseUrl"]; htmlObject = new PdfHtml(htmlCode, baseUrl); } // set the HTML object start location in PDF page htmlObject.DestX = float.Parse(collection["textBoxDestX"]); htmlObject.DestY = float.Parse(collection["textBoxDestY"]); // set the HTML object width in PDF if (collection["textBoxDestWidth"].Length > 0) { htmlObject.DestWidth = float.Parse(collection["textBoxDestWidth"]); } // set the HTML object height in PDF if (collection["textBoxDestHeight"].Length > 0) { htmlObject.DestHeight = float.Parse(collection["textBoxDestHeight"]); } // optionally wait an additional time before starting the conversion htmlObject.WaitBeforeConvert = 2; // set browser width htmlObject.BrowserWidth = int.Parse(collection["textBoxBrowserWidth"]); // set browser height if specified, otherwise use the default if (collection["textBoxBrowserHeight"].Length > 0) { htmlObject.BrowserHeight = int.Parse(collection["textBoxBrowserHeight"]); } // set HTML load timeout htmlObject.HtmlLoadedTimeout = int.Parse(collection["textBoxLoadHtmlTimeout"]); // layout the HTML object in PDF htmlLayoutInfo = page1.Layout(htmlObject); // write the PDF document to a memory buffer byte[] pdfBuffer = document.WriteToMemory(); FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf"); fileResult.FileDownloadName = "PdfHtmlObjects.pdf"; return(fileResult); } finally { document.Close(); } }
protected void buttonCreatePdf_Click(object sender, EventArgs e) { // create an empty PDF document PdfDocument document = new PdfDocument(); // set a demo serial number document.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ=="; // add a page to document PdfPage page1 = document.AddPage(PdfPageSize.A4, new PdfDocumentMargins(5), PdfPageOrientation.Portrait); try { // set the document header and footer before adding any objects to document SetHeader(document); SetFooter(document); // layout the HTML from URL 1 PdfHtml html1 = new PdfHtml(textBoxUrl1.Text); html1.WaitBeforeConvert = 2; PdfLayoutInfo html1LayoutInfo = page1.Layout(html1); // determine the PDF page where to add URL 2 PdfPage page2 = null; System.Drawing.PointF location2 = System.Drawing.PointF.Empty; if (checkBoxNewPage.Checked) { // URL 2 is laid out on a new page with the selected orientation page2 = document.AddPage(PdfPageSize.A4, new PdfDocumentMargins(5), GetSelectedPageOrientation()); location2 = System.Drawing.PointF.Empty; } else { // URL 2 is laid out immediately after URL 1 and html1LayoutInfo // gives the location where the URL 1 layout finished page2 = document.Pages[html1LayoutInfo.LastPageIndex]; location2 = new System.Drawing.PointF(html1LayoutInfo.LastPageRectangle.X, html1LayoutInfo.LastPageRectangle.Bottom); } // layout the HTML from URL 2 PdfHtml html2 = new PdfHtml(location2.X, location2.Y, textBoxUrl2.Text); html2.WaitBeforeConvert = 2; page2.Layout(html2); // write the PDF document to a memory buffer byte[] pdfBuffer = document.WriteToMemory(); // inform the browser about the binary data format HttpContext.Current.Response.AddHeader("Content-Type", "application/pdf"); // let the browser know how to open the PDF document and the file name HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("attachment; filename=LayoutMultipleHtml.pdf; size={0}", pdfBuffer.Length.ToString())); // write the PDF buffer to HTTP response HttpContext.Current.Response.BinaryWrite(pdfBuffer); // call End() method of HTTP response to stop ASP.NET page processing HttpContext.Current.Response.End(); } finally { document.Close(); } }
protected void buttonCreatePdf_Click(object sender, EventArgs e) { // create a PDF document PdfDocument document = new PdfDocument(); // set a demo serial number document.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ=="; // create a page in document PdfPage page1 = document.AddPage(); // create the true type fonts that can be used in document text System.Drawing.Font sysFont = new System.Drawing.Font("Times New Roman", 10, System.Drawing.GraphicsUnit.Point); PdfFont pdfFont = document.CreateFont(sysFont); PdfFont pdfFontEmbed = document.CreateFont(sysFont, true); float crtYPos = 20; float crtXPos = 5; PdfLayoutInfo textLayoutInfo = null; PdfLayoutInfo graphicsLayoutInfo = null; #region Layout lines PdfText titleTextLines = new PdfText(crtXPos, crtYPos, "Lines:", pdfFontEmbed); titleTextLines.ForeColor = System.Drawing.Color.Navy; textLayoutInfo = page1.Layout(titleTextLines); // advance the Y position in the PDF page crtYPos = textLayoutInfo.LastPageRectangle.Bottom + 10; // layout a simple line PdfLine pdfLine = new PdfLine(new System.Drawing.PointF(crtXPos, crtYPos), new System.Drawing.PointF(crtXPos + 60, crtYPos)); graphicsLayoutInfo = page1.Layout(pdfLine); // layout a thick line PdfLine pdfThickLine = new PdfLine(new System.Drawing.PointF(crtXPos + 70, crtYPos), new System.Drawing.PointF(crtXPos + 130, crtYPos)); pdfThickLine.LineStyle.LineWidth = 3; graphicsLayoutInfo = page1.Layout(pdfThickLine); // layout a dotted colored line PdfLine pdfDottedLine = new PdfLine(new System.Drawing.PointF(crtXPos + 140, crtYPos), new System.Drawing.PointF(crtXPos + 200, crtYPos)); pdfDottedLine.LineStyle.LineDashPattern = PdfLineDashPattern.Dot; pdfDottedLine.ForeColor = System.Drawing.Color.Green; graphicsLayoutInfo = page1.Layout(pdfDottedLine); // layout a dashed colored line PdfLine pdfDashedLine = new PdfLine(new System.Drawing.PointF(crtXPos + 210, crtYPos), new System.Drawing.PointF(crtXPos + 270, crtYPos)); pdfDashedLine.LineStyle.LineDashPattern = PdfLineDashPattern.Dash; pdfDashedLine.ForeColor = System.Drawing.Color.Green; graphicsLayoutInfo = page1.Layout(pdfDashedLine); // layout a dash-dot-dot colored line PdfLine pdfDashDotDotLine = new PdfLine(new System.Drawing.PointF(crtXPos + 280, crtYPos), new System.Drawing.PointF(crtXPos + 340, crtYPos)); pdfDashDotDotLine.LineStyle.LineDashPattern = PdfLineDashPattern.DashDotDot; pdfDashDotDotLine.ForeColor = System.Drawing.Color.Green; graphicsLayoutInfo = page1.Layout(pdfDashDotDotLine); // layout a thick line with rounded cap style PdfLine pdfRoundedLine = new PdfLine(new System.Drawing.PointF(crtXPos + 350, crtYPos), new System.Drawing.PointF(crtXPos + 410, crtYPos)); pdfRoundedLine.LineStyle.LineWidth = 5; pdfRoundedLine.LineStyle.LineCapStyle = PdfLineCapStyle.RoundCap; pdfRoundedLine.ForeColor = System.Drawing.Color.Blue; graphicsLayoutInfo = page1.Layout(pdfRoundedLine); // advance the Y position in the PDF page crtYPos += 10; #endregion #region Layout circles PdfText titleTextCircles = new PdfText(crtXPos, crtYPos, "Circles:", pdfFontEmbed); titleTextCircles.ForeColor = System.Drawing.Color.Navy; textLayoutInfo = page1.Layout(titleTextCircles); // advance the Y position in the PDF page crtYPos = textLayoutInfo.LastPageRectangle.Bottom + 10; // draw a simple circle with solid line PdfCircle pdfCircle = new PdfCircle(new System.Drawing.PointF(crtXPos + 30, crtYPos + 30), 30); page1.Layout(pdfCircle); // draw a simple circle with dotted border line PdfCircle pdfDottedCircle = new PdfCircle(new System.Drawing.PointF(crtXPos + 100, crtYPos + 30), 30); pdfDottedCircle.ForeColor = System.Drawing.Color.Green; pdfDottedCircle.LineStyle.LineDashPattern = PdfLineDashPattern.Dot; graphicsLayoutInfo = page1.Layout(pdfDottedCircle); // draw a circle with colored border line and fill color PdfCircle pdfDisc = new PdfCircle(new System.Drawing.PointF(crtXPos + 170, crtYPos + 30), 30); pdfDisc.ForeColor = System.Drawing.Color.Navy; pdfDisc.BackColor = System.Drawing.Color.WhiteSmoke; graphicsLayoutInfo = page1.Layout(pdfDisc); // draw a circle with thick border line PdfCircle pdfThickBorderDisc = new PdfCircle(new System.Drawing.PointF(crtXPos + 240, crtYPos + 30), 30); pdfThickBorderDisc.LineStyle.LineWidth = 5; pdfThickBorderDisc.BackColor = System.Drawing.Color.LightSalmon; pdfThickBorderDisc.ForeColor = System.Drawing.Color.LightBlue; graphicsLayoutInfo = page1.Layout(pdfThickBorderDisc); crtYPos = graphicsLayoutInfo.LastPageRectangle.Bottom + 10; #endregion #region Layout ellipses and arcs PdfText titleTextEllipses = new PdfText(crtXPos, crtYPos, "Ellipses, arcs and slices:", pdfFontEmbed); titleTextEllipses.ForeColor = System.Drawing.Color.Navy; textLayoutInfo = page1.Layout(titleTextEllipses); // advance the Y position in the PDF page crtYPos = textLayoutInfo.LastPageRectangle.Bottom + 10; // draw a simple ellipse with solid line PdfEllipse pdfEllipse = new PdfEllipse(new System.Drawing.PointF(crtXPos + 50, crtYPos + 30), 50, 30); graphicsLayoutInfo = page1.Layout(pdfEllipse); // draw an ellipse with fill color and colored border line PdfEllipse pdfFilledEllipse = new PdfEllipse(new System.Drawing.PointF(crtXPos + 160, crtYPos + 30), 50, 30); pdfFilledEllipse.BackColor = System.Drawing.Color.WhiteSmoke; pdfFilledEllipse.ForeColor = System.Drawing.Color.Green; graphicsLayoutInfo = page1.Layout(pdfFilledEllipse); // draw an ellipse arc with colored border line PdfEllipseArc pdfEllipseArc1 = new PdfEllipseArc(new System.Drawing.RectangleF(crtXPos + 220, crtYPos, 100, 60), 0, 90); pdfEllipseArc1.ForeColor = System.Drawing.Color.OrangeRed; pdfEllipseArc1.LineStyle.LineWidth = 3; graphicsLayoutInfo = page1.Layout(pdfEllipseArc1); // draw an ellipse arc with fill color and colored border line PdfEllipseArc pdfEllipseArc2 = new PdfEllipseArc(new System.Drawing.RectangleF(crtXPos + 220, crtYPos, 100, 60), 90, 90); pdfEllipseArc2.ForeColor = System.Drawing.Color.Navy; graphicsLayoutInfo = page1.Layout(pdfEllipseArc2); // draw an ellipse arc with fill color and colored border line PdfEllipseArc pdfEllipseArc3 = new PdfEllipseArc(new System.Drawing.RectangleF(crtXPos + 220, crtYPos, 100, 60), 180, 90); pdfEllipseArc3.ForeColor = System.Drawing.Color.Green; pdfEllipseArc3.LineStyle.LineWidth = 3; graphicsLayoutInfo = page1.Layout(pdfEllipseArc3); // draw an ellipse arc with fill color and colored border line PdfEllipseArc pdfEllipseArc4 = new PdfEllipseArc(new System.Drawing.RectangleF(crtXPos + 220, crtYPos, 100, 60), 270, 90); pdfEllipseArc4.ForeColor = System.Drawing.Color.Yellow; graphicsLayoutInfo = page1.Layout(pdfEllipseArc4); // draw an ellipse slice PdfEllipseSlice pdfEllipseSlice1 = new PdfEllipseSlice(new System.Drawing.RectangleF(crtXPos + 330, crtYPos, 100, 60), 0, 90); pdfEllipseSlice1.BackColor = System.Drawing.Color.Coral; graphicsLayoutInfo = page1.Layout(pdfEllipseSlice1); // draw an ellipse slice PdfEllipseSlice pdfEllipseSlice2 = new PdfEllipseSlice(new System.Drawing.RectangleF(crtXPos + 330, crtYPos, 100, 60), 90, 90); pdfEllipseSlice2.BackColor = System.Drawing.Color.LightBlue; graphicsLayoutInfo = page1.Layout(pdfEllipseSlice2); // draw an ellipse slice PdfEllipseSlice pdfEllipseSlice3 = new PdfEllipseSlice(new System.Drawing.RectangleF(crtXPos + 330, crtYPos, 100, 60), 180, 90); pdfEllipseSlice3.BackColor = System.Drawing.Color.LightGreen; graphicsLayoutInfo = page1.Layout(pdfEllipseSlice3); // draw an ellipse slice PdfEllipseSlice pdfEllipseSlice4 = new PdfEllipseSlice(new System.Drawing.RectangleF(crtXPos + 330, crtYPos, 100, 60), 270, 90); pdfEllipseSlice4.BackColor = System.Drawing.Color.Yellow; graphicsLayoutInfo = page1.Layout(pdfEllipseSlice4); crtYPos = graphicsLayoutInfo.LastPageRectangle.Bottom + 10; #endregion #region Layout rectangles PdfText titleTextRectangles = new PdfText(crtXPos, crtYPos, "Rectangles:", pdfFontEmbed); titleTextRectangles.ForeColor = System.Drawing.Color.Navy; textLayoutInfo = page1.Layout(titleTextRectangles); // advance the Y position in the PDF page crtYPos = textLayoutInfo.LastPageRectangle.Bottom + 10; // draw a simple rectangle with solid line PdfRectangle pdfRectangle = new PdfRectangle(crtXPos, crtYPos, 100, 60); graphicsLayoutInfo = page1.Layout(pdfRectangle); // draw a rectangle with fill color and colored dotted border line PdfRectangle pdfFilledRectangle = new PdfRectangle(crtXPos + 110, crtYPos, 100, 60); pdfFilledRectangle.BackColor = System.Drawing.Color.WhiteSmoke; pdfFilledRectangle.ForeColor = System.Drawing.Color.Green; pdfFilledRectangle.LineStyle.LineDashPattern = PdfLineDashPattern.Dot; graphicsLayoutInfo = page1.Layout(pdfFilledRectangle); // draw a rectangle with fill color and without border line PdfRectangle pdfFilledNoBorderRectangle = new PdfRectangle(crtXPos + 220, crtYPos, 100, 60); pdfFilledNoBorderRectangle.BackColor = System.Drawing.Color.LightGreen; graphicsLayoutInfo = page1.Layout(pdfFilledNoBorderRectangle); // draw a rectangle filled with a thick border line and reounded corners PdfRectangle pdfThickBorderRectangle = new PdfRectangle(crtXPos + 330, crtYPos, 100, 60); pdfThickBorderRectangle.BackColor = System.Drawing.Color.LightSalmon; pdfThickBorderRectangle.ForeColor = System.Drawing.Color.LightBlue; pdfThickBorderRectangle.LineStyle.LineWidth = 5; pdfThickBorderRectangle.LineStyle.LineJoinStyle = PdfLineJoinStyle.RoundJoin; graphicsLayoutInfo = page1.Layout(pdfThickBorderRectangle); crtYPos = graphicsLayoutInfo.LastPageRectangle.Bottom + 10; #endregion #region Layout Bezier curves PdfText titleTextBezier = new PdfText(crtXPos, crtYPos, "Bezier curves and polygons:", pdfFontEmbed); titleTextBezier.ForeColor = System.Drawing.Color.Navy; textLayoutInfo = page1.Layout(titleTextBezier); // advance the Y position in the PDF page crtYPos = textLayoutInfo.LastPageRectangle.Bottom + 10; // the points controlling the Bezier curve System.Drawing.PointF pt1 = new System.Drawing.PointF(crtXPos, crtYPos + 50); System.Drawing.PointF pt2 = new System.Drawing.PointF(crtXPos + 50, crtYPos); System.Drawing.PointF pt3 = new System.Drawing.PointF(crtXPos + 100, crtYPos + 100); System.Drawing.PointF pt4 = new System.Drawing.PointF(crtXPos + 150, crtYPos + 50); // draw controlling points PdfCircle pt1Circle = new PdfCircle(pt1, 2); pt1Circle.BackColor = System.Drawing.Color.LightSalmon; page1.Layout(pt1Circle); PdfCircle pt2Circle = new PdfCircle(pt2, 2); pt2Circle.BackColor = System.Drawing.Color.LightSalmon; page1.Layout(pt2Circle); PdfCircle pt3Circle = new PdfCircle(pt3, 2); pt3Circle.BackColor = System.Drawing.Color.LightSalmon; page1.Layout(pt3Circle); PdfCircle pt4Circle = new PdfCircle(pt4, 2); pt4Circle.BackColor = System.Drawing.Color.LightSalmon; page1.Layout(pt4Circle); // draw the Bezier curve PdfBezierCurve bezierCurve = new PdfBezierCurve(pt1, pt2, pt3, pt4); bezierCurve.ForeColor = System.Drawing.Color.LightBlue; bezierCurve.LineStyle.LineWidth = 2; graphicsLayoutInfo = page1.Layout(bezierCurve); #endregion #region Layout a polygons // layout a polygon without fill color // the polygon points System.Drawing.PointF[] polyPoints = new System.Drawing.PointF[] { new System.Drawing.PointF(crtXPos + 200, crtYPos + 50), new System.Drawing.PointF(crtXPos + 250, crtYPos), new System.Drawing.PointF(crtXPos + 300, crtYPos + 50), new System.Drawing.PointF(crtXPos + 250, crtYPos + 100) }; // draw polygon points foreach (System.Drawing.PointF polyPoint in polyPoints) { PdfCircle pointCircle = new PdfCircle(polyPoint, 2); pointCircle.BackColor = System.Drawing.Color.LightSalmon; page1.Layout(pointCircle); } // draw the polygon line PdfPolygon pdfPolygon = new PdfPolygon(polyPoints); pdfPolygon.ForeColor = System.Drawing.Color.LightBlue; pdfPolygon.LineStyle.LineWidth = 2; pdfPolygon.LineStyle.LineCapStyle = PdfLineCapStyle.ProjectingSquareCap; graphicsLayoutInfo = page1.Layout(pdfPolygon); // layout a polygon with fill color // the polygon points System.Drawing.PointF[] polyFillPoints = new System.Drawing.PointF[] { new System.Drawing.PointF(crtXPos + 330, crtYPos + 50), new System.Drawing.PointF(crtXPos + 380, crtYPos), new System.Drawing.PointF(crtXPos + 430, crtYPos + 50), new System.Drawing.PointF(crtXPos + 380, crtYPos + 100) }; // draw a polygon with fill color and thick rounded border PdfPolygon pdfFillPolygon = new PdfPolygon(polyFillPoints); pdfFillPolygon.ForeColor = System.Drawing.Color.LightBlue; pdfFillPolygon.BackColor = System.Drawing.Color.LightSalmon; pdfFillPolygon.LineStyle.LineWidth = 5; pdfFillPolygon.LineStyle.LineCapStyle = PdfLineCapStyle.RoundCap; pdfFillPolygon.LineStyle.LineJoinStyle = PdfLineJoinStyle.RoundJoin; graphicsLayoutInfo = page1.Layout(pdfFillPolygon); #endregion try { // write the PDF document to a memory buffer byte[] pdfBuffer = document.WriteToMemory(); // inform the browser about the binary data format HttpContext.Current.Response.AddHeader("Content-Type", "application/pdf"); // let the browser know how to open the PDF document and the file name HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("attachment; filename=PdfGraphics.pdf; size={0}", pdfBuffer.Length.ToString())); // write the PDF buffer to HTTP response HttpContext.Current.Response.BinaryWrite(pdfBuffer); // call End() method of HTTP response to stop ASP.NET page processing HttpContext.Current.Response.End(); } finally { document.Close(); } }
protected void buttonCreatePdf_Click(object sender, EventArgs e) { // create a PDF document PdfDocument document = new PdfDocument(); // set a demo serial number document.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ=="; // create a page in document PdfPage page1 = document.AddPage(); // set a background color for the page PdfRectangle backgroundRectangle = new PdfRectangle(page1.DrawableRectangle); backgroundRectangle.BackColor = System.Drawing.Color.WhiteSmoke; page1.Layout(backgroundRectangle); // create the true type fonts that can be used in document text System.Drawing.Font sysFont = new System.Drawing.Font("Times New Roman", 10, System.Drawing.GraphicsUnit.Point); PdfFont pdfFont = document.CreateFont(sysFont); PdfFont pdfFontEmbed = document.CreateFont(sysFont, true); float crtYPos = 20; float crtXPos = 5; #region Layout transparent image PdfText titleTextTransImage = new PdfText(crtXPos, crtYPos, "PNG image with alpha transparency:", pdfFontEmbed); titleTextTransImage.ForeColor = System.Drawing.Color.Navy; PdfLayoutInfo textLayoutInfo = page1.Layout(titleTextTransImage); // advance the Y position in the PDF page crtYPos += textLayoutInfo.LastPageRectangle.Height + 10; // layout a PNG image with alpha transparency PdfImage transparentPdfImage = new PdfImage(crtXPos, crtYPos, Server.MapPath("~") + @"\DemoFiles\Images\HiQPdfLogo_small.png"); PdfLayoutInfo imageLayoutInfo = page1.Layout(transparentPdfImage); // advance the Y position in the PDF page crtYPos += imageLayoutInfo.LastPageRectangle.Height + 10; #endregion #region Layout resized transparent image PdfText titleTextTransImageResized = new PdfText(crtXPos, crtYPos, "The transparent PNG image below is resized:", pdfFontEmbed); titleTextTransImageResized.ForeColor = System.Drawing.Color.Navy; textLayoutInfo = page1.Layout(titleTextTransImageResized); // advance the Y position in the PDF page crtYPos += textLayoutInfo.LastPageRectangle.Height + 10; // layout a PNG image with alpha transparency PdfImage transparentResizedPdfImage = new PdfImage(crtXPos, crtYPos, 50, Server.MapPath("~") + @"\DemoFiles\Images\HiQPdfLogo_small.png"); imageLayoutInfo = page1.Layout(transparentResizedPdfImage); // advance the Y position in the PDF page crtYPos += imageLayoutInfo.LastPageRectangle.Height + 10; #endregion #region Layout rotated transparent image PdfText titleTextTransImageRotated = new PdfText(crtXPos, crtYPos, "The transparent PNG image below is rotated 180 degrees counter clockwise:", pdfFontEmbed); titleTextTransImageRotated.ForeColor = System.Drawing.Color.Navy; textLayoutInfo = page1.Layout(titleTextTransImageRotated); // advance the Y position in the PDF page crtYPos += textLayoutInfo.LastPageRectangle.Height + 10; // rotate the PNG image with alpha transparency 180 degrees counter clockwise PdfImage transparentRotatedPdfImage = new PdfImage(0, 0, 50, Server.MapPath("~") + @"\DemoFiles\Images\HiQPdfLogo_small.png"); // translate the coordinates system to image location transparentRotatedPdfImage.SetTranslation(crtXPos, crtYPos); // rotate the coordinates system counter clockwise transparentRotatedPdfImage.SetRotationAngle(180); // translate back the coordinates system transparentRotatedPdfImage.SetTranslation(-50, -50); imageLayoutInfo = page1.Layout(transparentRotatedPdfImage); // advance the Y position in the PDF page crtYPos += 50 + 10; #endregion #region Layout clipped transparent image PdfText titleTextTransClippedImage = new PdfText(crtXPos, crtYPos, "The transparent PNG image below is clipped:", pdfFontEmbed); titleTextTransClippedImage.ForeColor = System.Drawing.Color.Navy; textLayoutInfo = page1.Layout(titleTextTransClippedImage); // advance the Y position in the PDF page crtYPos += textLayoutInfo.LastPageRectangle.Height + 10; // layout a clipped PNG image with alpha transparency PdfImage transparentClippedPdfImage = new PdfImage(crtXPos, crtYPos, 50, Server.MapPath("~") + @"\DemoFiles\Images\HiQPdfLogo_small.png"); transparentClippedPdfImage.ClipRectangle = new System.Drawing.RectangleF(crtXPos, crtYPos, 50, 25); imageLayoutInfo = page1.Layout(transparentClippedPdfImage); // advance the Y position in the PDF page crtYPos += transparentClippedPdfImage.ClipRectangle.Height + 10; #endregion #region Layout JPEG image PdfText titleTextOpaqueImage = new PdfText(crtXPos, crtYPos, "The JPG image below is opaque:", pdfFontEmbed); titleTextOpaqueImage.ForeColor = System.Drawing.Color.Navy; textLayoutInfo = page1.Layout(titleTextOpaqueImage); // advance the Y position in the PDF page crtYPos += textLayoutInfo.LastPageRectangle.Height + 10; // layout an opaque JPG image PdfImage opaquePdfImage = new PdfImage(crtXPos, crtYPos, Server.MapPath("~") + @"\DemoFiles\Images\HiQPdfLogo_small.jpg"); imageLayoutInfo = page1.Layout(opaquePdfImage); // advance the Y position in the PDF page crtYPos += imageLayoutInfo.LastPageRectangle.Height + 10; #endregion #region Layout clipped JPEG image PdfText titleTextClippedImage = new PdfText(crtXPos, crtYPos, "The JPG image below is clipped:", pdfFontEmbed); titleTextClippedImage.ForeColor = System.Drawing.Color.Navy; textLayoutInfo = page1.Layout(titleTextClippedImage); // advance the Y position in the PDF page crtYPos += textLayoutInfo.LastPageRectangle.Height + 10; // layout a clipped image PdfImage clippedPdfImage = new PdfImage(crtXPos, crtYPos, 50, Server.MapPath("~") + @"\DemoFiles\Images\HiQPdfLogo_small.jpg"); clippedPdfImage.ClipRectangle = new System.Drawing.RectangleF(crtXPos, crtYPos, 50, 25); imageLayoutInfo = page1.Layout(clippedPdfImage); // advance the Y position in the PDF page crtYPos += clippedPdfImage.ClipRectangle.Height + 10; #endregion #region Layout a vectorial SVG image PdfText titleTextSvgImage = new PdfText(crtXPos, crtYPos, "Vectorial SVG image:", pdfFontEmbed); titleTextSvgImage.ForeColor = System.Drawing.Color.Navy; textLayoutInfo = page1.Layout(titleTextSvgImage); // advance the Y position in the PDF page crtYPos += textLayoutInfo.LastPageRectangle.Height + 10; string svgImageCode = System.IO.File.ReadAllText(Server.MapPath("~") + @"\DemoFiles\Svg\SvgImage.svg"); PdfHtml svgImage = new PdfHtml(crtXPos, crtYPos, svgImageCode, null); PdfLayoutInfo svgLayoutInfo = page1.Layout(svgImage); // advance the Y position in the PDF page crtYPos += svgImage.ConversionInfo.PdfRegions[0].Rectangle.Height + 10; #endregion #region Layout JPEG image on multiple pages PdfText titleTexMultiPageImage = new PdfText(crtXPos, crtYPos, "The JPG image below is laid out on 2 pages:", pdfFontEmbed); titleTexMultiPageImage.ForeColor = System.Drawing.Color.Navy; textLayoutInfo = page1.Layout(titleTexMultiPageImage); // advance the Y position in the PDF page crtYPos += textLayoutInfo.LastPageRectangle.Height + 10; // layout an opaque JPG image on 2 pages PdfImage paginatedPdfImage = new PdfImage(crtXPos, crtYPos, Server.MapPath("~") + @"\DemoFiles\Images\HiQPdfLogo_big.jpg"); imageLayoutInfo = page1.Layout(paginatedPdfImage); #endregion // get the last page PdfPage crtPage = document.Pages[imageLayoutInfo.LastPageIndex]; crtYPos = imageLayoutInfo.LastPageRectangle.Bottom + 10; #region Layout the screenshot of a HTML document PdfText titleTextHtmlImage = new PdfText(crtXPos, crtYPos, "HTML document screenshot:", pdfFontEmbed); titleTextHtmlImage.ForeColor = System.Drawing.Color.Navy; textLayoutInfo = crtPage.Layout(titleTextHtmlImage); // advance the Y position in the PDF page crtYPos += textLayoutInfo.LastPageRectangle.Height + 10; string htmlFile = Server.MapPath("~") + @"\DemoFiles\Html\Logo.Html"; PdfHtmlImage htmlRasterImage = new PdfHtmlImage(crtXPos, crtYPos, htmlFile); htmlRasterImage.BrowserWidth = 400; PdfLayoutInfo htmlLayoutInfo = crtPage.Layout(htmlRasterImage); // advance the Y position in the PDF page crtYPos += htmlRasterImage.ConversionInfo.PdfRegions[0].Rectangle.Height + 10; #endregion try { // write the PDF document to a memory buffer byte[] pdfBuffer = document.WriteToMemory(); // inform the browser about the binary data format HttpContext.Current.Response.AddHeader("Content-Type", "application/pdf"); // let the browser know how to open the PDF document and the file name HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("attachment; filename=PdfImages.pdf; size={0}", pdfBuffer.Length.ToString())); // write the PDF buffer to HTTP response HttpContext.Current.Response.BinaryWrite(pdfBuffer); // call End() method of HTTP response to stop ASP.NET page processing HttpContext.Current.Response.End(); } finally { document.Close(); } }
protected void buttonCreatePdf_Click(object sender, EventArgs e) { // the path PDF document to edit string pdfDocumentToEdit = Server.MapPath("~") + @"\DemoFiles\Pdf\WikiPdf.pdf"; // load the PDF document to edit PdfDocument document = PdfDocument.FromFile(pdfDocumentToEdit); // set a demo serial number document.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ=="; #region Add an orange border to each page of the loaded PDF document // add an orange border to each PDF page in the loaded PDF document foreach (PdfPage pdfPage in document.Pages) { float crtPdfPageWidth = pdfPage.Size.Width; float crtPdfPageHeight = pdfPage.Size.Height; // create a PdfRectangle object PdfRectangle pdfRectangle = new PdfRectangle(2, 2, crtPdfPageWidth - 4, crtPdfPageHeight - 4); pdfRectangle.LineStyle.LineWidth = 2; pdfRectangle.ForeColor = System.Drawing.Color.OrangeRed; // layout the rectangle in PDF page pdfPage.Layout(pdfRectangle); } #endregion Add an orange border to each page of the loaded PDF document #region Layout HTML in a canvas to be repeated on each page of the loaded PDF document PdfPage pdfPage1 = document.Pages[0]; float pdfPageWidth = pdfPage1.Size.Width; float pdfPageHeight = pdfPage1.Size.Height; // the width of the HTML logo in pixels int htmlLogoWidthPx = 400; // the width of the HTML logo in points float htmlLogoWidthPt = PdfDpiTransform.FromPixelsToPoints(htmlLogoWidthPx); float htmlLogoHeightPt = 100; // create a canvas to be repeated in the center of each PDF page // the canvas is a PDF container that can contain PDF objects ( HTML, text, images, etc ) PdfRepeatCanvas repeatedCanvas = document.CreateRepeatedCanvas(new System.Drawing.RectangleF((pdfPageWidth - htmlLogoWidthPt) / 2, (pdfPageHeight - htmlLogoHeightPt) / 2, htmlLogoWidthPt, htmlLogoHeightPt)); // the HTML file giving the content to be placed in the repeated canvas string htmlFile = Server.MapPath("~") + @"\DemoFiles\Html\Logo.Html"; // the HTML object to be laid out in repeated canvas PdfHtml htmlLogo = new PdfHtml(0, 0, repeatedCanvas.Width, repeatedCanvas.Height, htmlFile); // the browser width when rendering the HTML htmlLogo.BrowserWidth = htmlLogoWidthPx; // the HTML content will fit the destination hight which is the same with repeated canvas height htmlLogo.FitDestHeight = true; // layout the HTML object in the repeated canvas PdfLayoutInfo htmlLogLayoutInfo = repeatedCanvas.Layout(htmlLogo); #endregion Layout HTML in a canvas to be repeated on each page of the loaded PDF document try { // write the PDF document to a memory buffer byte[] pdfBuffer = document.WriteToMemory(); // inform the browser about the binary data format HttpContext.Current.Response.AddHeader("Content-Type", "application/pdf"); // let the browser know how to open the PDF document and the file name HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("attachment; filename=EditPdf.pdf; size={0}", pdfBuffer.Length.ToString())); // write the PDF buffer to HTTP response HttpContext.Current.Response.BinaryWrite(pdfBuffer); // call End() method of HTTP response to stop ASP.NET page processing HttpContext.Current.Response.End(); } finally { document.Close(); } }
public ActionResult CreatePdf(IFormCollection collection) { // create a PDF document PdfDocument document = new PdfDocument(); // set a demo serial number document.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ=="; // create a page in document PdfPage page1 = document.AddPage(); // set a background color for the page PdfRectangle backgroundRectangle = new PdfRectangle(page1.DrawableRectangle); backgroundRectangle.BackColor = System.Drawing.Color.WhiteSmoke; page1.Layout(backgroundRectangle); // create the true type fonts that can be used in document text System.Drawing.Font sysFont = new System.Drawing.Font("Times New Roman", 10, System.Drawing.GraphicsUnit.Point); PdfFont pdfFont = document.CreateFont(sysFont); PdfFont pdfFontEmbed = document.CreateFont(sysFont, true); float crtYPos = 20; float crtXPos = 5; #region Layout transparent image PdfText titleTextTransImage = new PdfText(crtXPos, crtYPos, "PNG image with alpha transparency:", pdfFontEmbed); titleTextTransImage.ForeColor = System.Drawing.Color.Navy; PdfLayoutInfo textLayoutInfo = page1.Layout(titleTextTransImage); // advance the Y position in the PDF page crtYPos += textLayoutInfo.LastPageRectangle.Height + 10; // layout a PNG image with alpha transparency PdfImage transparentPdfImage = new PdfImage(crtXPos, crtYPos, m_hostingEnvironment.ContentRootPath + @"\wwwroot" + @"\DemoFiles\Images\HiQPdfLogo_small.png"); PdfLayoutInfo imageLayoutInfo = page1.Layout(transparentPdfImage); // advance the Y position in the PDF page crtYPos += imageLayoutInfo.LastPageRectangle.Height + 10; #endregion #region Layout resized transparent image PdfText titleTextTransImageResized = new PdfText(crtXPos, crtYPos, "The transparent PNG image below is resized:", pdfFontEmbed); titleTextTransImageResized.ForeColor = System.Drawing.Color.Navy; textLayoutInfo = page1.Layout(titleTextTransImageResized); // advance the Y position in the PDF page crtYPos += textLayoutInfo.LastPageRectangle.Height + 10; // layout a PNG image with alpha transparency PdfImage transparentResizedPdfImage = new PdfImage(crtXPos, crtYPos, 50, m_hostingEnvironment.ContentRootPath + @"\wwwroot" + @"\DemoFiles\Images\HiQPdfLogo_small.png"); imageLayoutInfo = page1.Layout(transparentResizedPdfImage); // advance the Y position in the PDF page crtYPos += imageLayoutInfo.LastPageRectangle.Height + 10; #endregion #region Layout rotated transparent image PdfText titleTextTransImageRotated = new PdfText(crtXPos, crtYPos, "The transparent PNG image below is rotated 180 degrees counter clockwise:", pdfFontEmbed); titleTextTransImageRotated.ForeColor = System.Drawing.Color.Navy; textLayoutInfo = page1.Layout(titleTextTransImageRotated); // advance the Y position in the PDF page crtYPos += textLayoutInfo.LastPageRectangle.Height + 10; // rotate the PNG image with alpha transparency 180 degrees counter clockwise PdfImage transparentRotatedPdfImage = new PdfImage(0, 0, 50, m_hostingEnvironment.ContentRootPath + @"\wwwroot" + @"\DemoFiles\Images\HiQPdfLogo_small.png"); // translate the coordinates system to image location transparentRotatedPdfImage.SetTranslation(crtXPos, crtYPos); // rotate the coordinates system counter clockwise transparentRotatedPdfImage.SetRotationAngle(180); // translate back the coordinates system transparentRotatedPdfImage.SetTranslation(-50, -50); imageLayoutInfo = page1.Layout(transparentRotatedPdfImage); // advance the Y position in the PDF page crtYPos += 50 + 10; #endregion #region Layout clipped transparent image PdfText titleTextTransClippedImage = new PdfText(crtXPos, crtYPos, "The transparent PNG image below is clipped:", pdfFontEmbed); titleTextTransClippedImage.ForeColor = System.Drawing.Color.Navy; textLayoutInfo = page1.Layout(titleTextTransClippedImage); // advance the Y position in the PDF page crtYPos += textLayoutInfo.LastPageRectangle.Height + 10; // layout a clipped PNG image with alpha transparency PdfImage transparentClippedPdfImage = new PdfImage(crtXPos, crtYPos, 50, m_hostingEnvironment.ContentRootPath + @"\wwwroot" + @"\DemoFiles\Images\HiQPdfLogo_small.png"); transparentClippedPdfImage.ClipRectangle = new System.Drawing.RectangleF(crtXPos, crtYPos, 50, 25); imageLayoutInfo = page1.Layout(transparentClippedPdfImage); // advance the Y position in the PDF page crtYPos += transparentClippedPdfImage.ClipRectangle.Height + 10; #endregion #region Layout JPEG image PdfText titleTextOpaqueImage = new PdfText(crtXPos, crtYPos, "The JPG image below is opaque:", pdfFontEmbed); titleTextOpaqueImage.ForeColor = System.Drawing.Color.Navy; textLayoutInfo = page1.Layout(titleTextOpaqueImage); // advance the Y position in the PDF page crtYPos += textLayoutInfo.LastPageRectangle.Height + 10; // layout an opaque JPG image PdfImage opaquePdfImage = new PdfImage(crtXPos, crtYPos, m_hostingEnvironment.ContentRootPath + @"\wwwroot" + @"\DemoFiles\Images\HiQPdfLogo_small.jpg"); imageLayoutInfo = page1.Layout(opaquePdfImage); // advance the Y position in the PDF page crtYPos += imageLayoutInfo.LastPageRectangle.Height + 10; #endregion #region Layout clipped JPEG image PdfText titleTextClippedImage = new PdfText(crtXPos, crtYPos, "The JPG image below is clipped:", pdfFontEmbed); titleTextClippedImage.ForeColor = System.Drawing.Color.Navy; textLayoutInfo = page1.Layout(titleTextClippedImage); // advance the Y position in the PDF page crtYPos += textLayoutInfo.LastPageRectangle.Height + 10; // layout a clipped image PdfImage clippedPdfImage = new PdfImage(crtXPos, crtYPos, 50, m_hostingEnvironment.ContentRootPath + @"\wwwroot" + @"\DemoFiles\Images\HiQPdfLogo_small.jpg"); clippedPdfImage.ClipRectangle = new System.Drawing.RectangleF(crtXPos, crtYPos, 50, 25); imageLayoutInfo = page1.Layout(clippedPdfImage); // advance the Y position in the PDF page crtYPos += clippedPdfImage.ClipRectangle.Height + 10; #endregion #region Layout a vectorial SVG image PdfText titleTextSvgImage = new PdfText(crtXPos, crtYPos, "Vectorial SVG image:", pdfFontEmbed); titleTextSvgImage.ForeColor = System.Drawing.Color.Navy; textLayoutInfo = page1.Layout(titleTextSvgImage); // advance the Y position in the PDF page crtYPos += textLayoutInfo.LastPageRectangle.Height + 10; string svgImageCode = System.IO.File.ReadAllText(m_hostingEnvironment.ContentRootPath + @"\wwwroot" + @"\DemoFiles\Svg\SvgImage.svg"); PdfHtml svgImage = new PdfHtml(crtXPos, crtYPos, svgImageCode, null); PdfLayoutInfo svgLayoutInfo = page1.Layout(svgImage); // advance the Y position in the PDF page crtYPos += svgImage.ConversionInfo.PdfRegions[0].Rectangle.Height + 10; #endregion #region Layout JPEG image on multiple pages PdfText titleTexMultiPageImage = new PdfText(crtXPos, crtYPos, "The JPG image below is laid out on 2 pages:", pdfFontEmbed); titleTexMultiPageImage.ForeColor = System.Drawing.Color.Navy; textLayoutInfo = page1.Layout(titleTexMultiPageImage); // advance the Y position in the PDF page crtYPos += textLayoutInfo.LastPageRectangle.Height + 10; // layout an opaque JPG image on 2 pages PdfImage paginatedPdfImage = new PdfImage(crtXPos, crtYPos, m_hostingEnvironment.ContentRootPath + @"\wwwroot" + @"\DemoFiles\Images\HiQPdfLogo_big.jpg"); imageLayoutInfo = page1.Layout(paginatedPdfImage); #endregion // get the last page PdfPage crtPage = document.Pages[imageLayoutInfo.LastPageIndex]; crtYPos = imageLayoutInfo.LastPageRectangle.Bottom + 10; #region Layout the screenshot of a HTML document PdfText titleTextHtmlImage = new PdfText(crtXPos, crtYPos, "HTML document screenshot:", pdfFontEmbed); titleTextHtmlImage.ForeColor = System.Drawing.Color.Navy; textLayoutInfo = crtPage.Layout(titleTextHtmlImage); // advance the Y position in the PDF page crtYPos += textLayoutInfo.LastPageRectangle.Height + 10; string htmlFile = m_hostingEnvironment.ContentRootPath + @"\wwwroot" + @"\DemoFiles\Html\Logo.Html"; PdfHtmlImage htmlRasterImage = new PdfHtmlImage(crtXPos, crtYPos, htmlFile); htmlRasterImage.BrowserWidth = 400; PdfLayoutInfo htmlLayoutInfo = crtPage.Layout(htmlRasterImage); // advance the Y position in the PDF page crtYPos += htmlRasterImage.ConversionInfo.PdfRegions[0].Rectangle.Height + 10; #endregion try { // write the PDF document to a memory buffer byte[] pdfBuffer = document.WriteToMemory(); FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf"); fileResult.FileDownloadName = "PdfImages.pdf"; return(fileResult); } finally { document.Close(); } }
public ActionResult CreatePdf(IFormCollection collection) { m_formCollection = collection; // create an empty PDF document PdfDocument document = new PdfDocument(); // set a demo serial number document.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ=="; // set encryption mode document.Security.EncryptionMode = GetSelectedEncryptionMode(); // set encryption level document.Security.EncryptionLevel = GetSelectedEncryptionLevel(); // set open password document.Security.OpenPassword = collection["textBoxOpenPassword"]; // set permissions password document.Security.PermissionsPassword = collection["textBoxPermissionsPassword"]; // set PDF document permissions document.Security.AllowPrinting = collection["checkBoxAllowPrint"].Count > 0; document.Security.AllowCopyContent = collection["checkBoxAllowCopy"].Count > 0; document.Security.AllowEditContent = collection["checkBoxAllowEdit"].Count > 0; document.Security.AllowEditAnnotations = collection["checkBoxAllowEditAnnotations"].Count > 0; document.Security.AllowFormFilling = collection["checkBoxAllowFillForms"].Count > 0; // set a default permissions password if an open password was set without settings a permissions password // or if any of the permissions does not have the default value if (document.Security.PermissionsPassword == String.Empty && (document.Security.OpenPassword != String.Empty || !IsDefaultPermission(document.Security))) { document.Security.PermissionsPassword = "******"; } // add a page to document with the default size and orientation PdfPage page1 = document.AddPage(PdfPageSize.A4, new PdfDocumentMargins(0), PdfPageOrientation.Portrait); // an object to be set with HTML layout info after conversion PdfLayoutInfo htmlLayoutInfo = null; try { // create the HTML object from URL PdfHtml htmlObject = new PdfHtml(collection["textBoxUrl"]); // optionally wait an additional time before starting the conversion htmlObject.WaitBeforeConvert = 2; // layout the HTML object in PDF htmlLayoutInfo = page1.Layout(htmlObject); // write the PDF document to a memory buffer byte[] pdfBuffer = document.WriteToMemory(); FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf"); fileResult.FileDownloadName = "PdfDocumentSecuritySettings.pdf"; return(fileResult); } finally { document.Close(); } }