Create or Generate PDF files in C# and VB.NET

While handling office files, it's sometimes necessary to generate PDF files that offer portability, editability, compatibility, and quality.

With GemBox.Document you can create PDF files from .NET applications simply and efficiently without using Microsoft Word on either developer or client machines.

This article will guide you through the process of generating PDFs using GemBox.Document in C# and VB.NET. With short code samples, you will see how you can insert text, images, and tables into your PDF files and use encryption and digital signatures on them.

The article will cover the following topics:

Install and configure the GemBox.Document library

The first step before generating a PDF file is creating a new .NET project. If you are still getting familiar with Visual Studio or need a reminder, refer to the official Microsoft tutorial. Also, although GemBox.Document supports a wide range of .NET versions (from .NET Framework 3.5), we recommend using the newest version.

Then you need to install the GemBox.Document component, and the fastest way of doing that is via NuGet Package Manager.

  1. In the Solution Explorer window, right-click on the solution and select 'Manage NuGet Packages for Solution'.
    Manage NuGet Packages
  2. Search for GemBox.Document and click on 'Install'.
    Scan of printed PDF file

Alternatively, you can open the NuGet Package Manager Console (Tools -> NuGet Package Manager -> Package Manager Console) and run the following command:

Install-Package GemBox.Document

Now that you have installed the GemBox.Document library, all you have to do is make sure you call the ComponentInfo.SetLicense method before using any other member of the library.

ComponentInfo.SetLicense("FREE-LIMITED-KEY");

Since we are working with a console application, we suggest putting this line at the beginning of the main() method.

In this tutorial, we will work in the free mode, which allows you to use the library without purchasing the license, but with limitations. You can read more about working modes and limitations on the Evaluation and Licensing documentation page.

How to easily generate a PDF file in C#

The following code snippets show how you can create a simple document file from scratch and save it as a PDF using GemBox.Document.

  1. First, create a new empty document.
    var document = new DocumentModel();
    Dim document = New DocumentModel()
  2. Next, you will add the text “Hello Word!”.
    document.Sections.Add(
        new Section(document,
            new Paragraph(document, "Hello world!")));
    document.Sections.Add(
        New Section(document,
            New Paragraph(document, "Hello world!")))
  3. Finally, save the generated document as a PDF file.
    document.Save("Document.pdf");
    document.Save("Document.pdf")

Refer to the following image for the result of the previous code.

how to generate a pdf file in C# and VB.NET
Screenshot of a PDF file generated from a C# application

How to insert images and tables into a PDF file

This section will teach you how to create PDF files with more advanced content, such as images and tables.

  1. First, create a new empty document, and an empty section.
    var document = new DocumentModel();
    var section = new Section(document);
    
    document.Sections.Add(section);
    Dim document = New DocumentModel()
    Dim Section = New Section(document)
    
    document.Sections.Add(Section)
  2. Then insert an image using the new picture() method. In this case, you will set its size to 100x100 points and add it to the section.
    var picture = new Picture(document, "picture path");
    picture.Layout = Layout.Inline(new Size(100, 100));
    section.Blocks.Add(new Paragraph(document, picture));
    Dim Picture = New Picture(document, "picture path")
    Picture.Layout = Layout.Inline(New Size(100, 100))
    Section.Blocks.Add(New Paragraph(document, Picture))
  3. Next, you will create a 3x5 table, format it, and add it to the section.
    var table = new Table(document, 3, 5);
    table.TableFormat.Borders.SetBorders(MultipleBorderTypes.All, BorderStyle.Dashed, Color.Blue, 1);
    table.TableFormat.PreferredWidth = new TableWidth(100, TableWidthUnit.Percentage);
    section.Blocks.Add(table);
    Dim Table = New Table(document, 3, 5)
    Table.TableFormat.Borders.SetBorders(MultipleBorderTypes.All, BorderStyle.Dashed, Color.Blue, 1)
    Table.TableFormat.PreferredWidth = New TableWidth(100, TableWidthUnit.Percentage)
    Section.Blocks.Add(Table)
  4. Finally, save the generated document as a PDF file.
    document.Save("Document.pdf");
    document.Save("Document.pdf")

Refer to the following image for the result of the previous code.

how to create a table in a PDF document in C# and VB.NET
Screenshot of a table generated in a PDF file

Conclusion

In this article, you learned how to create PDF files from scratch using C# and VB.NET. You can read more about support for PDF with the GemBox.Document API on the documentation page.

You can explore theGemBox.PDF component, which focuses on executing low-level object manipulations, reading, writing, merging, and splitting PDF files from .NET applications.

See also


Next steps

GemBox.Document is a .NET component that enables you to read, write, edit, convert, and print document files from your .NET applications using one simple API. How about testing it today?

Download Buy