PDFDC - PDF Device Context - C++ Sample Code

Sample C#, C++, and VB code for using Apryse SDK to create and use PDFDC (i.e. a PDF Device Context). Windows developers can use standard GDI or GDI+ API-s to write on PDFDC and to generate PDF documents based on their existing drawing functions. PDFDC can also be used to implement file conversion from any printable file format to PDF.

Learn more about our Server SDK for Windows and Windows PDF Library.

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2021 by PDFTron Systems Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6#include <iostream>
7#include <sstream>
8#include <PDF/PDFNet.h>
9#include <PDF/PDFDC.h>
10#include <PDF/PDFDCEX.h>
11#include "../../LicenseKey/CPP/LicenseKey.h"
12
13//-----------------------------------------------------------------------------------
14// This sample shows how to create and use the PDFDCEX which allows
15// printer-like multipage translations with GDI resources used across pages.
16// PDFDCEX allows you to create page after page of GDI to PDF conversion using existing
17// printing functions.
18//-----------------------------------------------------------------------------------
19
20using namespace pdftron;
21using namespace PDF;
22
23// Relative path to the folder containing test files.
24std::string inputPath = "../../TestFiles/";
25std::string outputPath = "../../TestFiles/Output/";
26
27// Define a font
28static const LOGFONTW ArialFont =
29{
30 12, // height (points)
31 0, // width (use default)
32 0, // rotation of baseline for text strings
33 0, // rotation of each character
34 FW_NORMAL, // weight
35 FALSE, // italic
36 FALSE, // underline
37 FALSE, // strike out
38 ANSI_CHARSET, // character set
39 OUT_DEFAULT_PRECIS, // output precision
40 CLIP_DEFAULT_PRECIS, // clip precision
41 DEFAULT_QUALITY, // quality
42 VARIABLE_PITCH | FF_SWISS, // pitch and family
43 L"Arial" // font name
44};
45
46
47int main()
48{
49 int ret = 0;
50#if defined(_WIN32) && !defined(WINCE)
51
52 PDFNet::Initialize(LicenseKey);
53
54 try
55 {
56 ////////////////////////////////////////////////////////////////////////////////
57 // Add two pages to a new PDFDoc.
58 // Fonts and resources are shared across pages. No need to work with each PDFPage.
59 // We work in page coordinates which are page dimensions (inches) times
60 // DPI. Origin is at upper left corner of the page, positive down and to right.
61
62 // Start with a PDFDoc to put the translation into, and a PDFDCEX to translate GDI to PDF
63 PDFDoc pdfdoc2;
64 PDFDCEX pdfDcEx;
65 HDC hDC2;
66 HFONT hArialFont2;
67 HGDIOBJ hOldFont2;
68 HGDIOBJ hPen2, hOldPen2;
69
70 // Begin the translation from GDI to PDF -- provide the PDFDoc to append to.
71 // Get back a GDI Device Context, we've added "::" the start of all of the
72 // GDI calls to emphasize that we only use the PDFDCEX at the beginning and end.
73 PDF::Point paperDimensions(8.5, 11);
74 hDC2 = pdfDcEx.Begin( pdfdoc2, paperDimensions );
75
76 UInt32 dpi = pdfDcEx.GetDPI();
77 PDF::Point paperSize( paperDimensions.x * dpi, paperDimensions.y * dpi);
78
79 ::StartPage(hDC2);
80
81 // We like to think of font height in "points" -- convert this to page pixels
82 LOGFONTW tmpFont = ArialFont;
83 tmpFont.lfHeight *= dpi / 72;
84 hArialFont2 = ::CreateFontIndirectW( &tmpFont );
85 hOldFont2 = ::SelectObject(hDC2, hArialFont2 );
86
87 hPen2 = ::CreatePen(PS_SOLID, 1, RGB(0,0,0));
88 hOldPen2 = ::SelectObject(hDC2, hPen2);
89 ::SetBkMode(hDC2, TRANSPARENT);
90 ::Ellipse(hDC2, (int)(paperSize.x/2 - dpi), (int)(paperSize.y/2 - dpi), (int)(paperSize.x/2 + dpi), (int)(paperSize.y/2 + dpi));
91 ::SetTextAlign(hDC2, TA_CENTER | TA_BOTTOM);
92 ::TextOutW(hDC2, (int)(paperSize.x/2), (int)(paperSize.y/2 + tmpFont.lfHeight / 2), L"Hello World", 11);
93 ::SelectObject(hDC2, hOldPen2);
94 ::DeleteObject(hPen2);
95 ::EndPage(hDC2);
96
97 ////////////////////////////////////////////////////////////////////////////
98 // Page two
99 // Reuse existing font and DC.
100 ::StartPage(hDC2);
101 ::Ellipse(hDC2, -20, -20, 20, 20);
102 ::SetTextAlign(hDC2, TA_CENTER | TA_BOTTOM);
103 ::TextOutW(hDC2, (int)(paperSize.x/2), (int)(paperSize.y/2 + ArialFont.lfHeight / 2), L"Page 2", 6);
104 ::SelectObject(hDC2, hOldFont2);
105 ::DeleteObject(hArialFont2);
106 ::EndPage(hDC2);
107
108 // Complete the translation
109 pdfDcEx.End();
110
111 // Save the PDF document
112 pdfdoc2.Save(outputPath + "PDFDCEXTest.pdf", SDF::SDFDoc::e_remove_unused, NULL);
113 std::cout << "Saved PDFDCEXTest.pdf\nDone.\n";
114 }
115 catch(Common::Exception& e)
116 {
117 std::cout << e << std::endl;
118 ret = 1;
119 }
120 catch(...)
121 {
122 std::cout << "Unknown Exception" << std::endl;
123 ret = 1;
124 }
125
126 PDFNet::Terminate();
127#endif // defined(_WIN32) && !defined(WINCE)
128 return ret;
129}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales