Skip to content

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.

Notifications You must be signed in to change notification settings

SyncfusionExamples/create-a-pdf-document-in-net-C-Sharp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 

Repository files navigation

How to Create a PDF Document in .NET Using the Syncfusion® PDF Library

Introduction

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.

System requirement

Framework and SDKs

  • .NET SDK (version 5.0 or later)

IDEs

  • Visual Studio 2019/ Visual Studio 2022

Code snippet for simple PDF document with text, images, and tables

we will create a new .NET console application, add the Syncfusion® PDF library package, and write the code

    PdfDocument pdfDocument = new PdfDocument();
    PdfPage currentPage = pdfDocument.Pages.Add();
    SizeF clientSize = currentPage.GetClientSize();
    FileStream imageStream = new FileStream("../../../Data/icon.png", FileMode.Open, FileAccess.Read);
    PdfImage icon= new PdfBitmap(imageStream);
    SizeF iconSize = new SizeF(40, 40);
    PointF iconLocation = new PointF(14, 13);
    PdfGraphics graphics= currentPage.Graphics;
    graphics.DrawImage(icon,iconLocation,iconSize);
    PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20, PdfFontStyle.Bold);
    var text = new PdfTextElement("INVOICE", font, new PdfSolidBrush(Color.FromArgb(1, 53, 67, 168)));
    text.StringFormat= new PdfStringFormat(PdfTextAlignment.Right);
    PdfLayoutResult result = text.Draw(currentPage, new PointF(clientSize.Width - 25, iconLocation.Y + 10));

    font = new PdfStandardFont(PdfFontFamily.Helvetica, 10);
    text = new PdfTextElement("To ", font);
    result = text.Draw(currentPage, new PointF(14, result.Bounds.Bottom + 30));
    font = new PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold);
    text = new PdfTextElement("John Smith,", font);
    result = text.Draw(currentPage, new PointF(14, result.Bounds.Bottom + 3));
    font = new PdfStandardFont(PdfFontFamily.Helvetica, 10);
    text = new PdfTextElement(string.Format("{0}, {1}", "398 W Broadway, Evanston Ave Fargo,", "\nNorth Dakota, 10012"), font);
    result = text.Draw(currentPage, new PointF(14, result.Bounds.Bottom + 3));

    font = new PdfStandardFont(PdfFontFamily.Helvetica, 10, PdfFontStyle.Bold);
    text = new PdfTextElement("Invoice No.#23698720 ", font);
    text.StringFormat = new PdfStringFormat(PdfTextAlignment.Right);
    text.Draw(currentPage, new PointF(clientSize.Width - 25, result.Bounds.Y - 20));

    PdfGrid grid=new PdfGrid();
    font= new PdfStandardFont(PdfFontFamily.Helvetica, 10, PdfFontStyle.Regular);
    grid.Style.Font = font;
    grid.Columns.Add(4);
    grid.Columns[1].Width = 70;
    grid.Columns[2].Width = 70;
    grid.Columns[3].Width = 70;

    grid.Headers.Add(1);
    PdfStringFormat stringFormat = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle);
    PdfGridRow header = grid.Headers[0];

    header.Cells[0].Value= "Item & description";
    header.Cells[0].StringFormat.LineAlignment = PdfVerticalAlignment.Middle;
    header.Cells[1].Value = "Hrs/Qty";
    header.Cells[1].StringFormat = stringFormat;
    header.Cells[2].Value = "Rate($)";
    header.Cells[2].StringFormat = stringFormat;
    header.Cells[3].Value = "Amount($)";
    header.Cells[3].StringFormat = stringFormat;

    PdfGridRow row = grid.Rows.Add();
    row.Cells[0].Value = "API Development";
    row.Cells[0].StringFormat.LineAlignment = PdfVerticalAlignment.Middle;

    row.Cells[1].Value = $"{25}";
    row.Cells[1].StringFormat = stringFormat;

    row.Cells[2].Value = $"{24.46}";
    row.Cells[2].StringFormat = stringFormat;

    decimal amount = (decimal)(25 * 24.46);
    row.Cells[3].Value = String.Format("{0:0.##}", amount);
    row.Cells[3].StringFormat = stringFormat;

    decimal sum = 0;
    sum += amount;

    row = grid.Rows.Add();
    row.Cells[0].Value = "Desktop Software Development";
    row.Cells[0].StringFormat.LineAlignment = PdfVerticalAlignment.Middle;
    row.Cells[1].Value = $"{25}";
    row.Cells[1].StringFormat = stringFormat;
    row.Cells[2].Value = $"{47.83}";
    row.Cells[2].StringFormat = stringFormat;
    amount = (decimal)(25 * 47.83);
    row.Cells[3].Value = String.Format("{0:0.##}", amount);
    row.Cells[3].StringFormat = stringFormat;

    sum += amount;

    row = grid.Rows.Add();
    row.Cells[0].Value = "Site admin development";
    row.Cells[0].StringFormat.LineAlignment = PdfVerticalAlignment.Middle;
    row.Cells[1].Value = $"{33}";
    row.Cells[1].StringFormat = stringFormat;
    row.Cells[2].Value = $"{85.1}";
    row.Cells[2].StringFormat = stringFormat;
    amount = (decimal)(33 * 85.1);
    row.Cells[3].Value = String.Format("{0:0.##}", amount);
    row.Cells[3].StringFormat = stringFormat;

    sum += amount;

    grid.ApplyBuiltinStyle(PdfGridBuiltinStyle.GridTable4Accent5);
    PdfGridStyle gridStyle = new PdfGridStyle();
    gridStyle.CellPadding = new PdfPaddings(5, 5, 5, 5);
    grid.Style= gridStyle;

    PdfGridLayoutFormat layoutFormat = new PdfGridLayoutFormat();
    layoutFormat.Layout = PdfLayoutType.Paginate;
    result = grid.Draw(currentPage, 14, result.Bounds.Bottom + 30, clientSize.Width - 35, layoutFormat);

    currentPage.Graphics.DrawRectangle(new PdfSolidBrush(Color.FromArgb(255, 239, 242, 255)),
        new RectangleF(result.Bounds.Right - 100, result.Bounds.Bottom + 20, 100, 20));

    PdfTextElement element = new PdfTextElement("Total", font);
    element.Draw(currentPage, new RectangleF(result.Bounds.Right - 100, result.Bounds.Bottom + 22, result.Bounds.Width, result.Bounds.Height));

    var totalPrice = $"$ {Math.Round(sum, 2)}";
    element=new PdfTextElement(totalPrice, font);
    element.StringFormat = new PdfStringFormat(PdfTextAlignment.Right);
    element.Draw(currentPage, new RectangleF(15, result.Bounds.Bottom + 22, result.Bounds.Width, result.Bounds.Height));

    MemoryStream stream = new MemoryStream();
    pdfDocument.Save(stream);
    pdfDocument.Close(true);
    stream.Position = 0;
    File.WriteAllBytes("Output.pdf",stream.ToArray());

Output Image output_image

How to run the examples

  • Download this project to a location in your disk.
  • Open the solution file using Visual Studio.
  • Rebuild the solution to install the required NuGet package.
  • Run the application.

Resources

Support and feedback

License

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. You can purchase a licnense here or start a free 30-day trial here.

About Syncfusion®

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.

Today, we provide 1600+ components and frameworks for web (Blazor, ASP.NET Core, ASP.NET MVC, ASP.NET WebForms, JavaScript, Angular, React, Vue, and Flutter), mobile (Xamarin, Flutter, UWP, and JavaScript), and desktop development (WinForms, WPF, WinUI(Preview), Flutter and UWP). 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.

About

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.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 7

Languages