|
1 | 1 | # How to Create a PDF Document in .NET Using the Syncfusion PDF Library |
| 2 | + |
| 3 | +## Introduction |
2 | 4 | A quick start .NET console project that shows how to create a PDF document and add text, image, and table to it using the Syncfusion PDF Library. |
| 5 | + |
| 6 | +## System requirement |
| 7 | +**Framework and SDKs** |
| 8 | +* .NET SDK (version 5.0 or later) |
| 9 | + |
| 10 | +**IDEs** |
| 11 | +* Visual Studio 2019/ Visual Studio 2022 |
| 12 | + |
| 13 | +## Code snippet for simple PDF document with text, images, and tables |
| 14 | +we will create a new .NET console application, add the Syncfusion PDF library package, and write the code |
| 15 | + |
| 16 | +```csharp |
| 17 | + PdfDocument pdfDocument = new PdfDocument(); |
| 18 | + PdfPage currentPage = pdfDocument.Pages.Add(); |
| 19 | + SizeF clientSize = currentPage.GetClientSize(); |
| 20 | + FileStream imageStream = new FileStream("../../../Data/icon.png", FileMode.Open, FileAccess.Read); |
| 21 | + PdfImage icon= new PdfBitmap(imageStream); |
| 22 | + SizeF iconSize = new SizeF(40, 40); |
| 23 | + PointF iconLocation = new PointF(14, 13); |
| 24 | + PdfGraphics graphics= currentPage.Graphics; |
| 25 | + graphics.DrawImage(icon,iconLocation,iconSize); |
| 26 | + PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20, PdfFontStyle.Bold); |
| 27 | + var text = new PdfTextElement("INVOICE", font, new PdfSolidBrush(Color.FromArgb(1, 53, 67, 168))); |
| 28 | + text.StringFormat= new PdfStringFormat(PdfTextAlignment.Right); |
| 29 | + PdfLayoutResult result = text.Draw(currentPage, new PointF(clientSize.Width - 25, iconLocation.Y + 10)); |
| 30 | + |
| 31 | + font = new PdfStandardFont(PdfFontFamily.Helvetica, 10); |
| 32 | + text = new PdfTextElement("To ", font); |
| 33 | + result = text.Draw(currentPage, new PointF(14, result.Bounds.Bottom + 30)); |
| 34 | + font = new PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold); |
| 35 | + text = new PdfTextElement("John Smith,", font); |
| 36 | + result = text.Draw(currentPage, new PointF(14, result.Bounds.Bottom + 3)); |
| 37 | + font = new PdfStandardFont(PdfFontFamily.Helvetica, 10); |
| 38 | + text = new PdfTextElement(string.Format("{0}, {1}", "398 W Broadway, Evanston Ave Fargo,", "\nNorth Dakota, 10012"), font); |
| 39 | + result = text.Draw(currentPage, new PointF(14, result.Bounds.Bottom + 3)); |
| 40 | + |
| 41 | + font = new PdfStandardFont(PdfFontFamily.Helvetica, 10, PdfFontStyle.Bold); |
| 42 | + text = new PdfTextElement("Invoice No.#23698720 ", font); |
| 43 | + text.StringFormat = new PdfStringFormat(PdfTextAlignment.Right); |
| 44 | + text.Draw(currentPage, new PointF(clientSize.Width - 25, result.Bounds.Y - 20)); |
| 45 | + |
| 46 | + PdfGrid grid=new PdfGrid(); |
| 47 | + font= new PdfStandardFont(PdfFontFamily.Helvetica, 10, PdfFontStyle.Regular); |
| 48 | + grid.Style.Font = font; |
| 49 | + grid.Columns.Add(4); |
| 50 | + grid.Columns[1].Width = 70; |
| 51 | + grid.Columns[2].Width = 70; |
| 52 | + grid.Columns[3].Width = 70; |
| 53 | + |
| 54 | + grid.Headers.Add(1); |
| 55 | + PdfStringFormat stringFormat = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle); |
| 56 | + PdfGridRow header = grid.Headers[0]; |
| 57 | + |
| 58 | + header.Cells[0].Value= "Item & description"; |
| 59 | + header.Cells[0].StringFormat.LineAlignment = PdfVerticalAlignment.Middle; |
| 60 | + header.Cells[1].Value = "Hrs/Qty"; |
| 61 | + header.Cells[1].StringFormat = stringFormat; |
| 62 | + header.Cells[2].Value = "Rate($)"; |
| 63 | + header.Cells[2].StringFormat = stringFormat; |
| 64 | + header.Cells[3].Value = "Amount($)"; |
| 65 | + header.Cells[3].StringFormat = stringFormat; |
| 66 | + |
| 67 | + PdfGridRow row = grid.Rows.Add(); |
| 68 | + row.Cells[0].Value = "API Development"; |
| 69 | + row.Cells[0].StringFormat.LineAlignment = PdfVerticalAlignment.Middle; |
| 70 | + |
| 71 | + row.Cells[1].Value = $"{25}"; |
| 72 | + row.Cells[1].StringFormat = stringFormat; |
| 73 | + |
| 74 | + row.Cells[2].Value = $"{24.46}"; |
| 75 | + row.Cells[2].StringFormat = stringFormat; |
| 76 | + |
| 77 | + decimal amount = (decimal)(25 * 24.46); |
| 78 | + row.Cells[3].Value = String.Format("{0:0.##}", amount); |
| 79 | + row.Cells[3].StringFormat = stringFormat; |
| 80 | + |
| 81 | + decimal sum = 0; |
| 82 | + sum += amount; |
| 83 | + |
| 84 | + row = grid.Rows.Add(); |
| 85 | + row.Cells[0].Value = "Desktop Software Development"; |
| 86 | + row.Cells[0].StringFormat.LineAlignment = PdfVerticalAlignment.Middle; |
| 87 | + row.Cells[1].Value = $"{25}"; |
| 88 | + row.Cells[1].StringFormat = stringFormat; |
| 89 | + row.Cells[2].Value = $"{47.83}"; |
| 90 | + row.Cells[2].StringFormat = stringFormat; |
| 91 | + amount = (decimal)(25 * 47.83); |
| 92 | + row.Cells[3].Value = String.Format("{0:0.##}", amount); |
| 93 | + row.Cells[3].StringFormat = stringFormat; |
| 94 | + |
| 95 | + sum += amount; |
| 96 | + |
| 97 | + row = grid.Rows.Add(); |
| 98 | + row.Cells[0].Value = "Site admin development"; |
| 99 | + row.Cells[0].StringFormat.LineAlignment = PdfVerticalAlignment.Middle; |
| 100 | + row.Cells[1].Value = $"{33}"; |
| 101 | + row.Cells[1].StringFormat = stringFormat; |
| 102 | + row.Cells[2].Value = $"{85.1}"; |
| 103 | + row.Cells[2].StringFormat = stringFormat; |
| 104 | + amount = (decimal)(33 * 85.1); |
| 105 | + row.Cells[3].Value = String.Format("{0:0.##}", amount); |
| 106 | + row.Cells[3].StringFormat = stringFormat; |
| 107 | + |
| 108 | + sum += amount; |
| 109 | + |
| 110 | + grid.ApplyBuiltinStyle(PdfGridBuiltinStyle.GridTable4Accent5); |
| 111 | + PdfGridStyle gridStyle = new PdfGridStyle(); |
| 112 | + gridStyle.CellPadding = new PdfPaddings(5, 5, 5, 5); |
| 113 | + grid.Style= gridStyle; |
| 114 | + |
| 115 | + PdfGridLayoutFormat layoutFormat = new PdfGridLayoutFormat(); |
| 116 | + layoutFormat.Layout = PdfLayoutType.Paginate; |
| 117 | + result = grid.Draw(currentPage, 14, result.Bounds.Bottom + 30, clientSize.Width - 35, layoutFormat); |
| 118 | + |
| 119 | + currentPage.Graphics.DrawRectangle(new PdfSolidBrush(Color.FromArgb(255, 239, 242, 255)), |
| 120 | + new RectangleF(result.Bounds.Right - 100, result.Bounds.Bottom + 20, 100, 20)); |
| 121 | + |
| 122 | + PdfTextElement element = new PdfTextElement("Total", font); |
| 123 | + element.Draw(currentPage, new RectangleF(result.Bounds.Right - 100, result.Bounds.Bottom + 22, result.Bounds.Width, result.Bounds.Height)); |
| 124 | + |
| 125 | + var totalPrice = $"$ {Math.Round(sum, 2)}"; |
| 126 | + element=new PdfTextElement(totalPrice, font); |
| 127 | + element.StringFormat = new PdfStringFormat(PdfTextAlignment.Right); |
| 128 | + element.Draw(currentPage, new RectangleF(15, result.Bounds.Bottom + 22, result.Bounds.Width, result.Bounds.Height)); |
| 129 | + |
| 130 | + MemoryStream stream = new MemoryStream(); |
| 131 | + pdfDocument.Save(stream); |
| 132 | + pdfDocument.Close(true); |
| 133 | + stream.Position = 0; |
| 134 | + File.WriteAllBytes("Output.pdf",stream.ToArray()); |
| 135 | +``` |
| 136 | + |
| 137 | +**Output Image** |
| 138 | +<img src="InvoicePDFSample/InvoicePDFSample/Output_images/Invoice.jpg" alt="output_image" width="100%" Height="Auto"/> |
| 139 | + |
| 140 | +## How to run the examples |
| 141 | +* Download this project to a location in your disk. |
| 142 | +* Open the solution file using Visual Studio. |
| 143 | +* Rebuild the solution to install the required NuGet package. |
| 144 | +* Run the application. |
| 145 | + |
| 146 | +## Resources |
| 147 | +* **Product page:** [Syncfusion PDF Framework](https://www.syncfusion.com/document-processing/pdf-framework/net) |
| 148 | +* **Documentation page:** [Syncfusion .NET PDF library](https://help.syncfusion.com/file-formats/pdf/overview) |
| 149 | +* **Online demo:** [Syncfusion .NET PDF library - Online demos](https://ej2.syncfusion.com/aspnetcore/PDF/CompressExistingPDF#/bootstrap5) |
| 150 | +* **Blog:** [Syncfusion .NET PDF library - Blog](https://www.syncfusion.com/blogs/category/pdf) |
| 151 | +* **Knowledge Base:** [Syncfusion .NET PDF library - Knowledge Base](https://www.syncfusion.com/kb/windowsforms/pdf) |
| 152 | +* **EBooks:** [Syncfusion .NET PDF library - EBooks](https://www.syncfusion.com/succinctly-free-ebooks) |
| 153 | +* **FAQ:** [Syncfusion .NET PDF library - FAQ](https://www.syncfusion.com/faq/) |
| 154 | + |
| 155 | +## Support and feedback |
| 156 | +* For any other queries, reach our [Syncfusion support team](https://www.syncfusion.com/support/directtrac/incidents/newincident?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples) or post the queries through the [community forums](https://www.syncfusion.com/forums?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples). |
| 157 | +* Request new feature through [Syncfusion feedback portal](https://www.syncfusion.com/feedback?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples). |
| 158 | + |
| 159 | +## License |
| 160 | +This is a commercial product and requires a paid license for possession or use. Syncfusion’s licensed software, including this component, is subject to the terms and conditions of [Syncfusion's EULA](https://www.syncfusion.com/eula/es/?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples). You can purchase a licnense [here](https://www.syncfusion.com/sales/products?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples) or start a free 30-day trial [here](https://www.syncfusion.com/account/manage-trials/start-trials?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples). |
| 161 | + |
| 162 | +## About Syncfusion |
| 163 | +Founded in 2001 and headquartered in Research Triangle Park, N.C., Syncfusion has more than 26,000+ customers and more than 1 million users, including large financial institutions, Fortune 500 companies, and global IT consultancies. |
| 164 | + |
| 165 | +Today, we provide 1600+ components and frameworks for web ([Blazor](https://www.syncfusion.com/blazor-components?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [ASP.NET Core](https://www.syncfusion.com/aspnet-core-ui-controls?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [ASP.NET MVC](https://www.syncfusion.com/aspnet-mvc-ui-controls?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [ASP.NET WebForms](https://www.syncfusion.com/jquery/aspnet-webforms-ui-controls?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [JavaScript](https://www.syncfusion.com/javascript-ui-controls?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [Angular](https://www.syncfusion.com/angular-ui-components?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [React](https://www.syncfusion.com/react-ui-components?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [Vue](https://www.syncfusion.com/vue-ui-components?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), and [Flutter](https://www.syncfusion.com/flutter-widgets?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples)), mobile ([Xamarin](https://www.syncfusion.com/xamarin-ui-controls?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [Flutter](https://www.syncfusion.com/flutter-widgets?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [UWP](https://www.syncfusion.com/uwp-ui-controls?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), and [JavaScript](https://www.syncfusion.com/javascript-ui-controls?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples)), and desktop development ([WinForms](https://www.syncfusion.com/winforms-ui-controls?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [WPF](https://www.syncfusion.com/wpf-ui-controls?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [WinUI(Preview)](https://www.syncfusion.com/winui-controls?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [Flutter](https://www.syncfusion.com/flutter-widgets?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples) and [UWP](https://www.syncfusion.com/uwp-ui-controls?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples)). We provide ready-to-deploy enterprise software for dashboards, reports, data integration, and big data processing. Many customers have saved millions in licensing fees by deploying our software. |
0 commit comments