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.
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.
using System.Diagnostics; using PdfSharp.Drawing; using PdfSharp.Pdf; namespace HelloWorld { // This sample is the obligatory Hello World program. class Program { static void Main() { // Create a new PDF document. var document = new PdfDocument(); document.Info.Title = "Created with PDFsharp"; // Create an empty page in this document. var page = document.AddPage(); // Get an XGraphics object for drawing on this page. var gfx = XGraphics.FromPdfPage(page); // Draw two lines with a red default pen. var width = page.Width; var height = page.Height; gfx.DrawLine(XPens.Red, 0, 0, width, height); gfx.DrawLine(XPens.Red, width, 0, 0, height); // Draw a circle with a red pen which is 1.5 point thick. var r = width / 5; gfx.DrawEllipse(new XPen(XColors.Red, 1.5), XBrushes.White, new XRect(width / 2 - r, height / 2 - r, 2 * r, 2 * r)); // Create a font. var font = new XFont("Times New Roman", 20, XFontStyle.BoldItalic); // Draw the text. gfx.DrawString("Hello, PDFsharp!", font, XBrushes.Black, new XRect(0, 0, page.Width, page.Height), XStringFormats.Center); // Save the document... const string filename = "HelloWorld_tempfile.pdf"; document.Save(filename); // ...and start a viewer. Process.Start(filename); } } }
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.
// ---------------------------------------------------------------------------- // <copyright file="TestApp.cpp" company="xInterop Software, LLC"> // Copyright (c) 2016 xInterop Software, LLC. All rights reserved. // http://www.xinterop.com // </copyright> // // NOTICE: All information contained herein is, and remains the property of // xInterop Software, LLC, if any. The intellectual and technical concepts // contained herein are proprietary to xInterop Software, LLC and may be // covered by U.S. and Foreign Patents, patents in process, and are protected // by trade secret or copyright law. Dissemination of this information or // reproduction of this material is strictly forbidden unless prior written // permission is obtained from xInterop Software, LLC. // // This copyright header must be kept intact without any change under any circumstances. // // The source code of this file can NOT be re-distributed to any third parties // unless certain OEM license has been arranged and written permission has been // authorized by xInterop Software, LLC. // // ---------------------------------------------------------------------------- #include "stdafx.h" #include <Ole2.h> #include "../Native/Include/PdfSharpgdiBridge.h" #ifdef _WIN64 #pragma comment(lib, "../Native/Lib/x64/PdfSharpgdiBridge.lib") #else #pragma comment(lib, "../Native/Lib/win32/PdfSharpgdiBridge.lib") #endif using namespace xinterop; using namespace System::Diagnostics; using namespace PdfSharp::Pdf; using namespace PdfSharp::Drawing; int _tmain(int argc, _TCHAR* argv[]) { /////////////////////////////////////////////////////////////////////////////////////////////// // // Notes: // // This file was created when "Load .NET bridge assembly automatically" option was turned on. // // The C# bridge assembly shall be loaded and initialized automatically when any c++ // class of the C++ bridge DLL is accessed. // // The C# bridge assembly can also be loaded and initialized by calling xiInitializeBridgeAssembly. // /////////////////////////////////////////////////////////////////////////////////////////////// CoInitializeEx(0, COINIT_APARTMENTTHREADED); /////////////////////////////////////////////////////////////////////////////////////////////// // Add your code here. /////////////////////////////////////////////////////////////////////////////////////////////// // Create a new PdfDocument instance. PdfDocument document; document.Info->Title = _T("Created with PDFsharp"); // Create an empty page in this document. auto page = document.AddPage(); // Get an XGraphics object for drawing on this page. auto gfx = XGraphics::FromPdfPage(page.get()); // Draw two lines with a red default pen. auto width = page->Width; auto height = page->Height; gfx->DrawLine(XPens::get_Red().get(), 0, 0, width->Value, height->Value); gfx->DrawLine(XPens::get_Red().get(), width->Value, 0, 0, height->Value); // Draw a circle with a red pen which is 1.5 point thick. auto r = width->Value / 5; XRect rectForCircle(width->Value / 2 - r, height->Value / 2 - r, 2 * r, 2 * r); XPen pen(XColors::get_Red().get(), 1.5); gfx->DrawEllipse(&pen, XBrushes::get_White().get(), &rectForCircle); // Create a font. XFont font(_T("Times New Roman"), 20, XFontStyle::BoldItalic); // Draw the text. XRect rectForText(0, 0, page->Width->Value, page->Height->Value); gfx->DrawString(_T("Hello, PDFsharp!"), &font, XBrushes::get_Black().get(), &rectForText, XStringFormats::get_Center().get()); // Save the document... auto filename = _T("HelloWorld_tempfile.pdf"); document.Save(filename); // ...and start a viewer. Process::Start(filename); /////////////////////////////////////////////////////////////////////////////////////////////// // End of your code. /////////////////////////////////////////////////////////////////////////////////////////////// CoUninitialize(); exit(0); return 0; }
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.
2. Reference the native C++ Bridge lib files.
3. Define all the required namespace inclusion.
There are a few things worth mentioning in the C++ code.
1. Define an instance of the C++ bridge class on the stack.
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.
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.
The post Using PdfSharp for Processing PDF from Native C++ : Hello World Sample appeared first on xInterop C++ .NET Bridge.