The Syncfusion PDF Library is a .NET library that allows you to create PDF files programmatically using C#. You can easily create PDF documents from scratch or files in other formats such as Word, Excel, PowerPoint (PPTX), web pages, and images.
In this blog post, we are going to see various methods of PDF creation with code examples:
- Create a PDF file from scratch
- Create a PDF file from a Word template
- Create a PDF file from an Excel file
- Create a PDF file from a PPTX file
- Create a PDF file from web pages
- Create a PDF file from images
Create a PDF file from scratch
With the Syncfusion .NET PDF Library, you can easily create PDF documents from scratch. It has developer-friendly APIs to create any PDF document for your specific requirements.
Note: Check out the live Example of Hello World in ASP.NET Core PDF Library.
First, install the Syncfusion.Pdf.Net.Core NuGet package as a reference to your .NET app from the NuGet Gallery.
Refer to the following image to install the NuGet package in the project.
Refer to the following code example to create a simple PDF document from scratch.
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
//Create a new PDF document.
using (PdfDocument document = new PdfDocument())
{
// Set the page size.
document.PageSettings.Size = PdfPageSize.A4;
//Add a page to the document.
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
//create the PDF font.
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20);
//Draw the text.
graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, new Syncfusion.Drawing.PointF(0, 0));
// Write the PDF document to file
FileStream stream = new FileStream("output.pdf", FileMode.Create);
document.Save(stream);
stream.Close();
//Close the document.
document.Close(true);
}
You will get a PDF document similar to the following screenshot when executing the previous code example.
The Syncfusion PDF Library also supports including various PDF elements such as tables, images, shapes, barcodes, and annotations. For more details, refer to the Overview of PDF Framework.
Create a PDF file from a Word template
Creating a PDF document from scratch is a time-consuming process. To avoid this, we can convert an existing Word template into a PDF document with populated data.
The Syncfusion .NET Word (DocIO) Library is a .NET library that allows you to easily convert a Word document into a PDF without interoperability in C#.
Let’s see how to create a Word template for a personalized envelope with data and convert it to a PDF.
Step 1 : First, design your Word document template for an envelope with the required layout, formatting, graphics, and merge fields for personalized information using Microsoft Word.
Step 2: Then, install the free Syncfusion.DocIORenderer.Net.Core NuGet package as a reference to your .NET app from NuGet Gallery.
Step 3: Create envelopes with data and convert them to PDF documents.
Refer to the following C# code example.
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIORenderer;
using Syncfusion.Pdf;
static void Main(string[] args)
{
using (WordDocument document = new WordDocument())
{
//Opens the template Word document.
Stream docStream = File.OpenRead(Path.GetFullPath(@"../../../Template.docx"));
document.Open(docStream, FormatType.Docx);
docStream.Dispose();
//Gets the recipient details as an "IEnumerable" collection of .NET objects.
List<Recipient> recipients = GetRecipients();
//Performs the mail merge.
document.MailMerge.Execute(recipients);
using (DocIORenderer render = new DocIORenderer())
{
//Converts Word document into PDF document.
PdfDocument pdfDocument = render.ConvertToPDF(document);
//Write the PDF document to file.
FileStream outputStream = new FileStream("output.pdf", FileMode.CreateNew);
pdfDocument.Save(outputStream);
//Closes the instance of PDF document object.
pdfDocument.Close(true);
}
}
}
Refer to the following C# code definition for the GetRecipients method, which provides data for mail merge.
private static List<Recipient> GetRecipients()
{
List<Recipient> recipients = new List<Recipient>();
//Initializes the recipient details.
recipients.Add(new Recipient("Nancy", "Davolio", "507 - 20th Ave. E.Apt. 2A", "Seattle", "WA", "98122", "USA"));
recipients.Add(new Recipient("Andrew", "Fuller", "908 W. Capital Way", "Tacoma", "WA", "98401", "USA"));
recipients.Add(new Recipient("Janet", "Leverling", "722 Moss Bay Blvd.", "Kirkland", "WA", "98033", "USA"));
recipients.Add(new Recipient("Margaret", "Peacock", "4110 Old Redmond Rd.", "Redmond", "WA", "98052", "USA"));
recipients.Add(new Recipient("Steven", "Buchanan", "14 Garrett Hil", "London", "", "SW1 8JR", "UK"));
return recipients;
}
The following C# code defines the Recipient class.
class Recipient
{
#region Properties
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
public string Country { get; set; }
#endregion
#region Constructor
public Recipient(string firstName, string lastName, string address, string city, string state, string zipCode, string country)
{
FirstName = firstName;
LastName = lastName;
Address = address;
City = city;
State = state;
ZipCode = zipCode;
Country = country;
}
#endregion
}
You will get a PDF document similar to the following screenshot by executing the previous code examples.
Note : For more details, refer to the Working with Mail merge, Word to PDF conversion, and Overview of Word (DocIO) Library documentation.
Create a PDF file from an Excel file
With the help of Syncfusion .NET PDF and Excel library, you can easily convert any Excel report to a PDF document using C#.
Step 1 : Install the Syncfusion.XlsIORenderer.Net.Core NuGet package as a reference in your .NET app from NuGet Gallery.
Step 2: Then, create a PDF report from Excel using C#. Refer to the following code example.
using Syncfusion.Pdf;
using Syncfusion.XlsIO;
using Syncfusion.XlsIORenderer;
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
FileStream excelStream = new FileStream("../../../Expense.xlsx", FileMode.Open, FileAccess.Read);
IWorkbook workbook = application.Workbooks.Open(excelStream);
//Initialize XlsIO renderer.
XlsIORenderer renderer = new XlsIORenderer();
//Convert Excel document into PDF document
PdfDocument pdfDocument = renderer.ConvertToPDF(workbook);
Stream stream = new FileStream("ExcelToPDF.pdf", FileMode.Create, FileAccess.ReadWrite);
pdfDocument.Save(stream);
excelStream.Dispose();
stream.Dispose();
}
By executing the previous code example, you will get a PDF document similar to the following screenshot.
Note: For more details, refer to the Excel to PDF conversion and Overview of Syncfusion Excel (XlsIO) Library documentation.
Create a PDF from a PowerPoint
With the help of the Syncfusion .NET PDF and PowerPoint Libraries, you can easily convert PowerPoint files into PDF documents using C#.
Step 1 : Install the Syncfusion.PresentationRenderer.Net.Core NuGet package as a reference in your .NET app from NuGet Gallery.
Step 2 : Then, convert the PowerPoint document into a PDF by referring to the following code example.
using Syncfusion.Pdf;
using Syncfusion.Presentation;
using Syncfusion.PresentationToPdfConverter;
using System.IO;
static void Main(string[] args)
{
//Load the PowerPoint presentation into stream.
using (FileStream fileStreamInput = new FileStream(@"../../../InputTemplate.pptx", FileMode.Open, FileAccess.Read))
{
//Open the existing PowerPoint presentation with loaded stream.
using (IPresentation pptxDoc = Presentation.Open(fileStreamInput))
{
//Create the MemoryStream to save the converted PDF.
using (MemoryStream pdfStream = new MemoryStream())
{
//Convert the PowerPoint document to PDF document.
using (PdfDocument pdfDocument = PresentationToPdfConverter.Convert(pptxDoc))
{
//Save the converted PDF document to MemoryStream.
pdfDocument.Save(pdfStream);
pdfStream.Position = 0;
}
//Create the output PDF file stream
using (FileStream fileStreamOutput = File.Create("Output.pdf"))
{
//Copy the converted PDF stream into created output PDF stream
pdfStream.CopyTo(fileStreamOutput);
}
}
}
}
}
By executing the previous code example, you will get a PDF document similar to the following screenshot.
Note : Refer to the PowerPoint to PDF conversion and Overview of PowerPoint Presentation documentation for more details.
Create a PDF from webpages
The Syncfusion HTML-to-PDF converter converts any webpage or HTML template to a PDF document using C#. This conversion uses the Blink rendering engine to convert web pages to PDF. The output PDF looks exactly like what we see in a web browser.
Step 1 : Install the Syncfusion.HtmlToPdfConverter.Blink.Net.Core.Windows NuGet package as a reference in your .NET app from NuGet Gallery.
Step 2 : Then, refer to the following code example to convert a webpage URL to a PDF document.
//Initialize HTML-to-PDF converter with Blink rendering engine.
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.Blink);
BlinkConverterSettings blinkConverterSettings = new BlinkConverterSettings();
blinkConverterSettings.ViewPortSize = new Syncfusion.Drawing.Size(1440, 0);
//Assign Blink converter settings to HTML converter.
htmlConverter.ConverterSettings = blinkConverterSettings;
//Convert URL to PDF.
PdfDocument document = htmlConverter.Convert("https://www.syncfusion.com");
FileStream fileStream = new FileStream("Sample.pdf", FileMode.CreateNew, FileAccess.ReadWrite);
//Save and close the PDF document.
document.Save(fileStream);
document.Close(true);
You will get a PDF document similar to the following screenshot by executing the previous code example.
Note: For more details, refer to the conversion using Blink rendering and Overview of HTML to PDF conversion documentation.
Create a PDF file from images
The Syncfusion PDF Library allows you to create PDF documents from JPG and PNG images. You can even customize the orientation, margin, and other page settings.
Step 1 : Install the Syncfusion.Pdf.Net.Core NuGet package as a reference in your .NET app from NuGet Gallery.
Step 2 : Then, refer to the following code example to convert the JPG and PNG images to PDF documents.
// Create a new instance of PdfDocument class.
using (PdfDocument document = new PdfDocument())
{
// Add a new page to the newly created document.
PdfPage page = document.Pages.Add();
PdfStandardFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12, PdfFontStyle.Bold);
PdfGraphics g = page.Graphics;
g.DrawString("JPEG Image", font, PdfBrushes.Blue, new Syncfusion.Drawing.PointF(0, 40));
//Load JPEG image to stream.
FileStream jpgImageStream = new FileStream("../../../jpeg_image.jpg", FileMode.Open);
//Load the JPEG image.
PdfImage jpgImage = new PdfBitmap(jpgImageStream);
//Draw the JPEG image.
g.DrawImage(jpgImage, new Syncfusion.Drawing.RectangleF(0, 70, 515, 215));
g.DrawString("PNG Image", font, PdfBrushes.Blue, new Syncfusion.Drawing.PointF(0, 355));
//Load PNG image to stream.
FileStream pngImageStream = new FileStream("../../../png_image.png", FileMode.Open);
//Load the PNG image.
PdfImage pngImage = new PdfBitmap(pngImageStream);
g.DrawImage(pngImage, new Syncfusion.Drawing.RectangleF(0, 365, 199, 300));
FileStream fileStream = new FileStream("Sample.pdf", FileMode.CreateNew, FileAccess.ReadWrite);
//Save and close the PDF document.
document.Save(fileStream);
document.Close(true);
}
You will get a PDF document similar to the following screenshot by executing the previous code example.
Note: For more information, refer to the Working with images using various options in .NET PDF Library documentation.
GitHub reference
You can find all the examples of creating PDF documents in C# using the Syncfusion .NET PDF Library in the GitHub repository.
Conclusion
Thanks for reading! In this blog post, we have walked through how to create PDF documents from Word, Excel, PPTX, web, and image files using the Syncfusion PDF Library. Take a moment to peruse our documentation, where you’ll find other options and features, all with accompanying code examples.
Try out the steps given in this blog post and enjoy hassle-free PDF creation. Don’t forget to leave your feedback in the comments section below!
The new version is available for download from the License and Downloads page for existing customers of Essential Studio. If you aren’t a customer yet, you can try our 30-day free trial to check out these features.
If you have questions, you can contact us through our support forum, support portal, or feedback portal. We are happy to assist you!