Category / Section
How to create a single page PDF for roll paper or fiscal printer using C# and VB.NET
7 mins read
Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can create a single page PDF document for roll paper or fiscal printer using C# and VB.NET.
Steps to create single page PDF programmatically:
- Create a new C# Windows Forms application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.
- Include the following namespaces in Form.cs file.
C#
using Syncfusion.Pdf; using Syncfusion.Pdf.Graphics; using Syncfusion.Pdf.Grid; using System.Drawing;
VB.NET
Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Graphics Imports Syncfusion.Pdf.Grid Imports System.Drawing
- Use the following code snippet in the click event of the button to create a single page PDF document.
C#
float pageHeight = 0; PdfDocument document; PdfPage page; private void button1_Click(object sender, EventArgs e) { //Initialize a new instance of PDF document document = new PdfDocument(); //Add a page to PDF page = document.Pages.Add(); //Draw invoice content to calculate page height DrawInvoice(page, true); //Reinitialize an instance of PDF document document = new PdfDocument(); PdfMargins margins = document.PageSettings.Margins; //Set height to PDF document.PageSettings.Height = margins.Top + margins.Bottom + pageHeight; //Add a page to PDF page = document.Pages.Add(); //Draw invoice content to PDF page DrawInvoice(page, false); //Save and close the document document.Save("SinglePagePDF.pdf"); document.Close(true); System.Diagnostics.Process.Start("SinglePagePDF.pdf"); } //Draw invoice content to PDF page and calculate the total height of the page after drawn private void DrawInvoice(PdfPage page, bool isLayout) { //Get page graphics PdfGraphics g = page.Graphics; //Draw invoice address PdfTextElement element = new PdfTextElement("Northwind Traders\n67, rue des Cinquante Otages,\nElgin,\nUnites States."); element.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 12); element.Brush = new PdfSolidBrush(new PdfColor(89, 89, 93)); PdfLayoutResult result = element.Draw(page, new RectangleF(0, 0, page.Graphics.ClientSize.Width / 2, 200)); //Draw an image PdfImage img = new PdfBitmap(@"..\..\inputs\logo.jpg"); page.Graphics.DrawImage(img, new RectangleF(g.ClientSize.Width - 200, result.Bounds.Y, 190, 45)); PdfFont subHeadingFont = new PdfStandardFont(PdfFontFamily.TimesRoman, 14); g.DrawRectangle(new PdfSolidBrush(new PdfColor(126, 151, 173)), new RectangleF(0, result.Bounds.Bottom + 40, g.ClientSize.Width, 30)); //Draw invoice ID and date element = new PdfTextElement("INVOICE 001", subHeadingFont); element.Brush = PdfBrushes.White; result = element.Draw(page, new PointF(10, result.Bounds.Bottom + 48)); string currentDate = "DATE " + DateTime.Now.ToString("MM/dd/yyyy"); SizeF textSize = subHeadingFont.MeasureString(currentDate); g.DrawString(currentDate, subHeadingFont, element.Brush, new PointF(g.ClientSize.Width - textSize.Width - 10, result.Bounds.Y)); //Draw shipping address element = new PdfTextElement("BILL TO ", new PdfStandardFont(PdfFontFamily.TimesRoman, 10)); element.Brush = new PdfSolidBrush(new PdfColor(126, 155, 203)); result = element.Draw(page, new PointF(10, result.Bounds.Bottom + 25)); g.DrawLine(new PdfPen(new PdfColor(126, 151, 173), 0.70f), new PointF(0, result.Bounds.Bottom + 3), new PointF(g.ClientSize.Width, result.Bounds.Bottom + 3)); element = new PdfTextElement("Vins et alcools Chevalier", new PdfStandardFont(PdfFontFamily.TimesRoman, 10)); element.Brush = PdfBrushes.Black; result = element.Draw(page, new PointF(20, result.Bounds.Bottom + 6)); element = new PdfTextElement("Abbaye, Reims, France", new PdfStandardFont(PdfFontFamily.TimesRoman, 10)); element.Brush = PdfBrushes.Black; result = element.Draw(page, new PointF(20, result.Bounds.Bottom + 1)); g.DrawLine(new PdfPen(new PdfColor(126, 151, 173), 0.70f), new PointF(0, result.Bounds.Bottom + 3), new PointF(g.ClientSize.Width, result.Bounds.Bottom + 3)); //Create PDF grid PdfGrid grid = new PdfGrid(); grid.DataSource = GetOrderDetails(); PdfStringFormat headerFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); PdfFont headerFont = new PdfStandardFont(PdfFontFamily.TimesRoman, 14f, PdfFontStyle.Regular); PdfBrush headerBrush = new PdfSolidBrush(new PdfColor(126, 151, 173)); PdfFont rowFont = new PdfStandardFont(PdfFontFamily.TimesRoman, 12f); PdfBrush rowBrush = new PdfSolidBrush(new PdfColor(131, 130, 136)); grid.Columns[0].Width = 70; grid.Columns[1].Width = 200; foreach (PdfGridRow row in grid.Headers) { row.Style.BackgroundBrush = headerBrush; row.Style.Font = headerFont; row.Style.TextBrush = PdfBrushes.White; foreach (PdfGridCell cell in row.Cells) { cell.StringFormat = headerFormat; } } foreach (PdfGridRow row in grid.Rows) { row.Style.Font = rowFont; row.Style.TextBrush = rowBrush; } //Bind event to calculate page height if(isLayout) grid.BeginPageLayout += BeginPageLayout; //Draw invoice data as table PdfGridLayoutResult gridLayoutResult = grid.Draw(result.Page, new PointF(10, result.Bounds.Bottom + 20)); //Calculate actual height of the client area in single page PDF if (isLayout && (document.Pages.Count > 1)) pageHeight += (gridLayoutResult.Bounds.Height - page.Graphics.ClientSize.Height); } //Event to calculate total height of the single page PDF private void BeginPageLayout(object sender, BeginPageLayoutEventArgs args) { pageHeight += args.Page.Graphics.ClientSize.Height; } //Get DataTable for invoice private DataTable GetOrderDetails() { DataTable dataTable = new DataTable(); dataTable.Columns.Add("ProductID"); dataTable.Columns.Add("ProductName"); dataTable.Columns.Add("Quantity"); dataTable.Columns.Add("UnitPrice"); dataTable.Columns.Add("Discount"); dataTable.Columns.Add("Price"); //Add rows to the DataTable for(int i = 20; i < 200; i++) { string id = i.ToString(); if(id[id.Length - 1] == '0') { dataTable.Rows.Add(new object[] { id, "Sir Rodney's Marmalade", "40", "$64.80", "$0.05", "$2,592.00" }); } else if (id[id.Length - 1] == '1') { dataTable.Rows.Add(new object[] { id, "Gorgonzola Telino", "20", "$10.00", "$0.00", "$200.00" }); } else if (id[id.Length - 1] == '2') { dataTable.Rows.Add(new object[] { id, "Chartreuse verte", "42", "$14.40", "$0.00", "$604.80" }); } else if (id[id.Length - 1] == '4') { dataTable.Rows.Add(new object[] { id, "Maxilaku", "40", "$16.00", "$0.00", "$640.00" }); } else if (id[id.Length - 1] == '6') { dataTable.Rows.Add(new object[] { id, "Longlife Tofu", "21", "$8.00", "$0.00", "$168.00" }); } else if (id[id.Length - 1] == '9') { dataTable.Rows.Add(new object[] { id, "Gudbrandsdalsost", "18", "$28.80", "$0.10", "$518.40" }); } } return dataTable; }
VB.NET
Private pageHeight As Single = 0 Private document As PdfDocument Private page As PdfPage Private Sub button1_Click(sender As Object, e As EventArgs) 'Initialize a new instance of PDF document document = New PdfDocument() 'Add a page to PDF page = document.Pages.Add() 'Draw invoice content to calculate page height DrawInvoice(page, True) 'Reinitialize an instance of PDF document document = New PdfDocument() Dim margins As PdfMargins = document.PageSettings.Margins 'Set height to PDF document.PageSettings.Height = margins.Top + margins.Bottom + pageHeight 'Add a page to PDF page = document.Pages.Add() 'Draw invoice content to PDF page DrawInvoice(page, False) 'Save and close the document document.Save("SinglePagePDF.pdf") document.Close(True) System.Diagnostics.Process.Start("SinglePagePDF.pdf") End Sub 'Draw invoice content to PDF page and calculate the total height of the page after drawn Private Sub DrawInvoice(ByVal page As PdfPage, ByVal isLayout As Boolean) 'Get page graphics Dim g As PdfGraphics = page.Graphics 'Draw invoice address Dim element As PdfTextElement = New PdfTextElement("Northwind Traders" + Environment.NewLine + "67, rue des Cinquante Otages," + Environment.NewLine + "Elgin," + Environment.NewLine + "Unites States.") element.Font = New PdfStandardFont(PdfFontFamily.TimesRoman, 12) element.Brush = New PdfSolidBrush(New PdfColor(89, 89, 93)) Dim result As PdfLayoutResult = element.Draw(page, New RectangleF(0, 0, page.Graphics.ClientSize.Width / 2, 200)) 'Draw an image Dim img As PdfImage = New PdfBitmap("..\..\inputs\logo.jpg") page.Graphics.DrawImage(img, New RectangleF(g.ClientSize.Width - 200, result.Bounds.Y, 190, 45)) Dim subHeadingFont As PdfFont = New PdfStandardFont(PdfFontFamily.TimesRoman, 14) g.DrawRectangle(New PdfSolidBrush(New PdfColor(126, 151, 173)), New RectangleF(0, result.Bounds.Bottom + 40, g.ClientSize.Width, 30)) 'Draw invoice ID and date element = New PdfTextElement("INVOICE 001", subHeadingFont) element.Brush = PdfBrushes.White result = element.Draw(page, New PointF(10, result.Bounds.Bottom + 48)) Dim currentDate As String = "DATE " & DateTime.Now.ToString("MM/dd/yyyy") Dim textSize As SizeF = subHeadingFont.MeasureString(currentDate) g.DrawString(currentDate, subHeadingFont, element.Brush, New PointF(g.ClientSize.Width - textSize.Width - 10, result.Bounds.Y)) 'Draw shipping address element = New PdfTextElement("BILL TO ", New PdfStandardFont(PdfFontFamily.TimesRoman, 10)) element.Brush = New PdfSolidBrush(New PdfColor(126, 155, 203)) result = element.Draw(page, New PointF(10, result.Bounds.Bottom + 25)) g.DrawLine(New PdfPen(New PdfColor(126, 151, 173), 0.7F), New PointF(0, result.Bounds.Bottom + 3), New PointF(g.ClientSize.Width, result.Bounds.Bottom + 3)) element = New PdfTextElement("Vins et alcools Chevalier", New PdfStandardFont(PdfFontFamily.TimesRoman, 10)) element.Brush = PdfBrushes.Black result = element.Draw(page, New PointF(20, result.Bounds.Bottom + 6)) element = New PdfTextElement("Abbaye, Reims, France", New PdfStandardFont(PdfFontFamily.TimesRoman, 10)) element.Brush = PdfBrushes.Black result = element.Draw(page, New PointF(20, result.Bounds.Bottom + 1)) g.DrawLine(New PdfPen(New PdfColor(126, 151, 173), 0.7F), New PointF(0, result.Bounds.Bottom + 3), New PointF(g.ClientSize.Width, result.Bounds.Bottom + 3)) 'Create PDF grid Dim grid As PdfGrid = New PdfGrid() grid.DataSource = GetOrderDetails() Dim headerFormat As PdfStringFormat = New PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle) Dim headerFont As PdfFont = New PdfStandardFont(PdfFontFamily.TimesRoman, 14.0F, PdfFontStyle.Regular) Dim headerBrush As PdfBrush = New PdfSolidBrush(New PdfColor(126, 151, 173)) Dim rowFont As PdfFont = New PdfStandardFont(PdfFontFamily.TimesRoman, 12.0F) Dim rowBrush As PdfBrush = New PdfSolidBrush(New PdfColor(131, 130, 136)) grid.Columns(0).Width = 70 grid.Columns(1).Width = 200 For Each row As PdfGridRow In grid.Headers row.Style.BackgroundBrush = headerBrush row.Style.Font = headerFont row.Style.TextBrush = PdfBrushes.White For Each cell As PdfGridCell In row.Cells cell.StringFormat = headerFormat Next Next For Each row As PdfGridRow In grid.Rows row.Style.Font = rowFont row.Style.TextBrush = rowBrush Next 'Bind event to calculate page height If isLayout Then AddHandler grid.BeginPageLayout, New BeginPageLayoutEventHandler(AddressOf BeginPageLayout) 'Draw invoice data as table Dim gridLayoutResult As PdfGridLayoutResult = grid.Draw(result.Page, New PointF(10, result.Bounds.Bottom + 20)) 'Calculate actual height of the client area in single page PDF If isLayout AndAlso (document.Pages.Count > 1) Then pageHeight += (gridLayoutResult.Bounds.Height - page.Graphics.ClientSize.Height) End Sub 'Event to calculate total height of the single page PDF Private Sub BeginPageLayout(ByVal sender As Object, ByVal args As BeginPageLayoutEventArgs) pageHeight += args.Page.Graphics.ClientSize.Height End Sub 'Get DataTable for invoice Private Function GetOrderDetails() As DataTable Dim dataTable As DataTable = New DataTable() dataTable.Columns.Add("ProductID") dataTable.Columns.Add("ProductName") dataTable.Columns.Add("Quantity") dataTable.Columns.Add("UnitPrice") dataTable.Columns.Add("Discount") dataTable.Columns.Add("Price") 'Add rows to the DataTable For i As Integer = 20 To 200 - 1 Dim id As String = i.ToString() If id(id.Length - 1) = "0"c Then dataTable.Rows.Add(New Object() {id, "Sir Rodney's Marmalade", "40", "$64.80", "$0.05", "$2,592.00"}) ElseIf id(id.Length - 1) = "1"c Then dataTable.Rows.Add(New Object() {id, "Gorgonzola Telino", "20", "$10.00", "$0.00", "$200.00"}) ElseIf id(id.Length - 1) = "2"c Then dataTable.Rows.Add(New Object() {id, "Chartreuse verte", "42", "$14.40", "$0.00", "$604.80"}) ElseIf id(id.Length - 1) = "4"c Then dataTable.Rows.Add(New Object() {id, "Maxilaku", "40", "$16.00", "$0.00", "$640.00"}) ElseIf id(id.Length - 1) = "6"c Then dataTable.Rows.Add(New Object() {id, "Longlife Tofu", "21", "$8.00", "$0.00", "$168.00"}) ElseIf id(id.Length - 1) = "9"c Then dataTable.Rows.Add(New Object() {id, "Gudbrandsdalsost", "18", "$28.80", "$0.10", "$518.40"}) End If Next Return dataTable End Function
A complete working sample can be downloaded from SinglePagePDF.zip.
By executing the program, you will get the PDF document as follows.
Refer here to explore the rich set of Syncfusion Essential PDF features.
Note:
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.