Using PdfSharp for Processing PDF from Native C++ : Hello World Sample

The whole package of PdfSharp comes with lots of sample applications, one of them is the obligatory hello world sample which shows how to create a PDF document with one page and the text “Hello, World!” written in its center.

You can see the resulting PDF as shown below.

image

The following is the original C# console application of Hello World. The C# source code was released under MIT and it was written by Thomas Hövel.

In the previous article, we talked about how to use xInterop C++ .NET Bridge with Native C++ to .NET Bridge to generate the native C++ bridge DLL for PDFsharp .NET library assembly. In this Hello World example, we will need to use the native C++ bridge DLL PdbSharpgidBridge.DLL.

The following is the complete source code of the example.

The C++ code is very similar to the C# counterpart since we can simulate the C# property in the C++ code. Let’s go over the important steps before we can start calling the functions.

1. Include the header file for the native C++ Bridge DLL.

image

2. Reference the native C++ Bridge lib files.

image

3. Define all the required namespace inclusion.

image

There are a few things worth mentioning in the C++ code.

1. Define an instance of the C++ bridge class on the stack.

image

In the preceding screen-shot, the variable of document is created on the stack. It is simple, you certainly can create a pointer of an instance of PdfDocument by calling “new” and then release it later on.

2. Using properties.

image

In the preceding screen-shot, both Info and Title are properties. We are using them just like using the properties in the C# code.

3. System.Diagnostics.Process class

We also created the native C++ bridge class for the .NET class of Process, the screen-shot below shows how to use them.

image

Translate »