This guide explains how to create a program that uses GcPdf to generate and save to disk a PDF file with the "Hello, World!" text.

GrapeCity Documents for PDF assemblies are built for .NET Standard 2.0, and can be used with any target that supports it. In this short tutorial, we show how to build a .NET Core console application. The tutorial takes you through the steps required to do that in Visual Studio on Windows or MAC, or Visual Studio Code on Linux.

These videos cover installing Documents for PDF on Windows, Mac, and Linux.

Get started with Documents for PDF on Windows

Get started with Documents for PDF on Mac

Get started with Documents for PDF on Linux

Step 1: Create an empty console application

Create an app using Visual Studio on Windows

1. Open Visual Studio for Windows.

2. Create a new .NET Core Console Application.

3. Right click the project in Solution Explorer and choose Manage NuGet Packages.

4. In Package source in top right, select nuget.org.

5. Click Browse tab in top left and enter "GrapeCity.Documents" as the search string. You should see several GrapeCity.Documents packages listed.

6. Select GrapeCity.Documents.Pdf, and click Install. Accept the license agreement.

This will add the required references to your application.

Create an app using Visual Studio on MAC

1. Open Visual Studio for MAC.

2. Create a new .NET Core Console Application.

3. In the tree view on the left, right click Dependencies and choose Add Packages.

4. In the Search panel, type "GrapeCity.Documents".

5. From the list in the left panel, select GrapeCity.Documents.Pdf and click Add Packages.

6. Accept the license agreement.

This will add the required references to your application.

Create an app using Visual Studio Code on Linux

1. In a terminal window (you may use the Terminal in Visual Studio Code), type the following commands:

$ mkdir ~/MyApp # create a directory for the application
$ cd ~/MyApp
$ dotnet new console # create a .NET Core application with MyApp.csproj and Program.cs files

2. Open Visual Studio Code.

3. If you haven't already done so, from Extensions install Nuget Package Manager, and activate it.

4. In Visual Studio Code, press Ctrl+P to open the file command box, type > in it, find "Nuget Package Manager: Add Package" in the list that opens, and click it.

5. In the search box that opens, type "GrapeCity" and press Enter. This should bring up the list of available GrapeCity packages, GrapeCity.Documents.Pdf among them.

6. Select it. This will add a reference to that package to you .csproj file, which would now look like this:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>netcoreapp2.0</TargetFramework>
    </PropertyGroup>
    <ItemGroup>
        <PackageReference Include="GrapeCity.Documents.Pdf" Version="1.0.0.105" />
    </ItemGroup>
</Project>

7. In a terminal window, type the following commands to build and run the app:

$ cd ~/MyApp
$ dotnet restore # fetches referenced packages for MyApp.csproj
$ dotnet run # runs the default app

At this point you should see the "Hello, World!" printed in the terminal window (the default behavior of a new console app). Now modify the application so that it generates the PDF instead.

Step 2: Add code to your application

Open Program.cs in Visual Studio or Visual Studio Code, and modify it so that it looks like this:

using System;
using System.Drawing;
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Text;

namespace MyApp
{
        class Program
        {
            static void Main(string[] args)
            {
                // Create a new PDF document:
                GcPdfDocument doc = new GcPdfDocument();
                // Add a page, get its graphics:
                GcPdfGraphics g = doc.NewPage().Graphics;
                // Render a string into the page:
                g.DrawString("Hello, World!",
                        // Use a standard font (the 14 standard PDF fonts are built into GcPdf
                        // and are always available):
                        new TextFormat() { Font = StandardFonts.Times, FontSize = 12 },
                        // GcPdf page coordinates start at top left corner, using 72 dpi by default:
                        new PointF(72, 72));
                        // Save the PDF:
                doc.Save("HelloWorld.pdf");
            }
    }
}

Step 3: Run the application

  • On Windows or MAC, just click the "start debugging" button in Visual Studio.
  • On Linux, enter the following in a terminal window:
$ cd ~/MyApp
$ dotnet run # runs MyApp

That's all it takes to generate a PDF file using GcPdf. The HelloWorld.pdf should now be in your project directory. If you'd like to generate a PDF with more content, you can read "How to generate PDF documents in code with Documents for PDF".

Get the sample and demo

Try Documents for PDF 30-day trial