PDF Creation Made Easy with Syncfusion Document Processing Libraries
Tags: csharp, dotnet, pdf
Published: 2022-05-17 11:24:37

---
title: PDF Creation Made Easy with Syncfusion Document Processing Libraries
published: true
date: 2022-05-17 10:30:44 UTC
tags: csharp, dotnet, PDF, 
canonical_url: https://www.syncfusion.com/blogs/post/pdf-creation-document-processing-libraries.aspx
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ti8gfot93ocsdtm13l90.png
---

The Syncfusion [PDF Library](https://www.syncfusion.com/document-processing/pdf-framework/net ".NET 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%20PDF-file-from-scratch)
- [Create a PDF file from a Word template](#create-a-pdf-file-from-a-word-template)
- [Create a PDF file from an Excel file](#create-a-pdf-file-from-an-excel-file)
- [Create a PDF file from a PPTX file](#create-a-pdf-from-a-powerpoint)
- [Create a PDF file from web pages](#create-a-pdf-from-webpages)
- [Create a PDF file from images](#create-a-pdf-file-from-images)

## Create a PDF file from scratch

With the [Syncfusion .NET PDF Library](https://www.syncfusion.com/document-processing/pdf-framework/net "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](https://ej2.syncfusion.com/aspnetcore/PDF/HelloWorld#/fluent "Hello World PDF creation demo in ASP.NET Core").

First, install the [Syncfusion.Pdf.Net.Core](https://www.nuget.org/packages/Syncfusion.Pdf.Net.Core/ "Syncfusion.Pdf.Net.Core NuGet package") NuGet package as a reference to your .NET app from the [NuGet Gallery](https://www.nuget.org/ "NuGet Gallery").

Refer to the following image to install the NuGet package in the project.

![Install the Syncfusion.Pdf.Net.Core NuGet package](https://www.syncfusion.com/blogs/wp-content/uploads/2022/05/Install-the-Syncfusion.Pdf.Net_.Core-NuGet-package.png)Refer to the following code example to create a simple PDF document from scratch.

```csharp
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.

![Creating a PDF file from scratch](https://www.syncfusion.com/blogs/wp-content/uploads/2022/05/Creating-a-PDF-file-from-scratch.png)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](https://help.syncfusion.com/file-formats/pdf/overview "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](https://www.syncfusion.com/document-processing/word-framework/net ".NET Word 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.

**![Designing a Word document template for an envelope](https://www.syncfusion.com/blogs/wp-content/uploads/2022/05/Designing-a-Word-document-template-for-an-envelope.png)Step 2**: Then, install the free [Syncfusion.DocIORenderer.Net.Core](https://www.nuget.org/packages/Syncfusion.DocIORenderer.Net.Core/ "Syncfusion.DocIORenderer.Net.Core NuGet package") NuGet package as a reference to your .NET app from [NuGet Gallery](https://www.nuget.org "NuGet Gallery").

**![Install the Syncfusion.DocIORenderer.Net.Core NuGet package](https://www.syncfusion.com/blogs/wp-content/uploads/2022/05/Syncfusion.DocIORenderer.Net_.Core_.png)Step 3**: Create envelopes with data and convert them to PDF documents.

Refer to the following C# code example.

```csharp
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.

```csharp
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.

```csharp
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. ![Creating a PDF file from a Word template](https://www.syncfusion.com/blogs/wp-content/uploads/2022/05/Creating-a-PDF-file-from-a-Word-template.png)

**Note** : For more details, refer to the [Working with Mail merge](https://help.syncfusion.com/file-formats/docio/working-with-mail-merge "Working with Mail merge"), [Word to PDF conversion](https://help.syncfusion.com/file-formats/docio/word-to-pdf "Word to PDF conversion"), and [Overview of Word (DocIO) Library](https://help.syncfusion.com/file-formats/docio/overview "Overview of Word (DocIO) Library") documentation.

## **Create a** PDF file **from an Excel file**

With the help of Syncfusion [.NET PDF](https://www.syncfusion.com/document-processing/pdf-framework/net ".NET PDF Library") and [Excel](https://www.syncfusion.com/document-processing/excel-framework/net ".NET Excel Library") library, you can easily convert any Excel report to a PDF document using C#.

**Step 1** : Install the [Syncfusion.XlsIORenderer.Net.Core](https://www.nuget.org/packages/Syncfusion.XlsIORenderer.Net.Core/ "Syncfusion.XlsIORenderer.Net.Core") NuGet package as a reference in your .NET app from [NuGet Gallery](https://www.nuget.org "NuGet Gallery").

**![Install the Syncfusion.XlsIORenderer.Net.Core NuGet package](https://www.syncfusion.com/blogs/wp-content/uploads/2022/05/NuGet-Package-Manager-1.png)Step 2:** Then, create a PDF report from Excel using C#. Refer to the following code example.

```csharp
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.  
 ![Creating a PDF file from Excel](https://www.syncfusion.com/blogs/wp-content/uploads/2022/05/Creating-a-PDF-file-from-Excel.png)

**Note:** For more details, refer to the [Excel to PDF conversion](https://help.syncfusion.com/file-formats/xlsio/excel-to-pdf-conversion "Excel to PDF conversion") and [Overview of Syncfusion Excel (XlsIO) Library](https://help.syncfusion.com/file-formats/xlsio/overview "Overview of Syncfusion Excel (XlsIO) Library") documentation.

## Create a PDF from a PowerPoint

With the help of the Syncfusion .NET [PDF](https://www.syncfusion.com/document-processing/pdf-framework/net ".NET PDF Library") and [PowerPoint](https://www.syncfusion.com/document-processing/powerpoint-framework/net ".NET PowerPoint Library") Libraries, you can easily convert PowerPoint files into PDF documents using C#.

**Step 1** : Install the [Syncfusion.PresentationRenderer.Net.Core](https://www.nuget.org/packages/Syncfusion.PresentationRenderer.Net.Core/ "Syncfusion.PresentationRenderer.Net.Core NuGet package") NuGet package as a reference in your .NET app from [NuGet Gallery.](https://www.nuget.org "NuGet Gallery")

![Install the Syncfusion.PresentationRenderer.Net.Core NuGet package](https://www.syncfusion.com/blogs/wp-content/uploads/2022/05/Install-the-Syncfusion.PresentationRenderer.Net_.Core-NuGet-package.png)

**Step 2** : Then, convert the PowerPoint document into a PDF by referring to the following code example.

```csharp
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.  
 ![Creating a PDF from PowerPoint](https://www.syncfusion.com/blogs/wp-content/uploads/2022/05/Creating-a-PDF-from-PowerPoint.png)

**Note** : Refer to the [PowerPoint to PDF conversion](https://help.syncfusion.com/file-formats/presentation/presentation-to-pdf "PowerPoint to PDF conversion") and [Overview of PowerPoint Presentation](https://help.syncfusion.com/file-formats/presentation/overview "Overview of PowerPoint Presentation") documentation for more details.

## Create **a PDF** from **webpages**

The Syncfusion [HTML-to-PDF converter](https://www.syncfusion.com/document-processing/pdf-framework/net/html-to-pdf "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](https://www.nuget.org/packages/Syncfusion.HtmlToPdfConverter.Blink.Net.Core.Windows/ "Syncfusion.HtmlToPdfConverter.Blink.Net.Core.Windows NuGet package") NuGet package as a reference in your .NET app from [NuGet Gallery](https://www.nuget.org "NuGet Gallery").

![Install the Syncfusion.HtmlToPdfConverter.Blink.Net.Core.Windows NuGet package](https://www.syncfusion.com/blogs/wp-content/uploads/2022/05/Install-the-Syncfusion.HtmlToPdfConverter.Blink_.Net_.Core_.Windows-NuGet-package.png)

**Step 2** : Then, refer to the following code example to convert a webpage URL to a PDF document.

```csharp
//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.  
 ![Creating a PDF from Web pages](https://www.syncfusion.com/blogs/wp-content/uploads/2022/05/Creating-a-PDF-from-Web-pages.png)

**Note:** For more details, refer to the [conversion using Blink rendering](https://help.syncfusion.com/file-formats/pdf/converting-html-to-pdf#converting-html-to-pdf-using-blink-rendering-engine "HTML to PDF conversion using Blink rendering") and [Overview of HTML to PDF conversion](https://help.syncfusion.com/file-formats/pdf/converting-html-to-pdf "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](https://www.nuget.org/packages/Syncfusion.Pdf.Net.Core/ "Syncfusion.Pdf.Net.Core NuGet package") NuGet package as a reference in your .NET app from NuGet Gallery.

**![Install the Syncfusion.Pdf.Net.Core NuGet package](https://www.syncfusion.com/blogs/wp-content/uploads/2022/05/Create-a-PDF-file-from-images.png)**

**Step 2** : Then, refer to the following code example to convert the JPG and PNG images to PDF documents.

```csharp
// 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.

![Creating a PDF file from images](https://www.syncfusion.com/blogs/wp-content/uploads/2022/05/Creating-a-PDF-file-from-images.png)

**Note:** For more information, refer to the [Working with images using various options in .NET PDF Library documentation.](https://help.syncfusion.com/file-formats/pdf/working-with-images "Working with images using various options in .NET PDF Library")

## GitHub reference

You can find all the examples of [creating PDF documents in C# using the Syncfusion .NET PDF Library](https://github.com/SyncfusionExamples/create-pdf-in-csharp "Creating PDF documents in C# using the Syncfusion .NET PDF Library GitHub demo") 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](https://www.syncfusion.com/document-processing/pdf-framework/net ".NET PDF Library"). Take a moment to peruse our [documentation](https://help.syncfusion.com/file-formats/pdf/ "Overview of PDF Framework"), 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](https://www.syncfusion.com/account/downloads/studio/ "License and download page of Essential Studio") for existing customers of Essential Studio. If you aren’t a customer yet, you can try our 30-day [free trial](https://www.syncfusion.com/downloads "Free evaluation of the Syncfusion Essential Studio") to check out these features.

If you have questions, you can contact us through our [support forum](https://www.syncfusion.com/forums "Syncfusion support forum"), [support portal](https://support.syncfusion.com/ "Syncfusion support portal"), or [feedback portal](https://www.syncfusion.com/feedback/pdf "Syncfusion feedback portal"). We are happy to assist you!

## Related blogs

- [How to Find Corrupted PDF Files in C# Easily](https://dev.to/syncfusion/how-to-find-corrupted-pdf-files-in-c-easily-16hi "Blog: How to Find Corrupted PDF Files in C# Easily")
- [Easy Ways to Redact PDFs Using C#](https://www.syncfusion.com/blogs/post/easy-ways-to-redact-pdfs-using-c.aspx "Blog: Easy Ways to Redact PDFs Using C#")
- [7 Ways to Compress PDF Files in C#, VB.NET](https://www.syncfusion.com/blogs/post/7-ways-to-compress-pdf-files.aspx "Blog: 7 Ways to Compress PDF Files in C#, VB.NET")