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-2024 by Apryse Software Inc. All Rights Reserved.
3//
4
5using System;
6using System.Drawing;
7using System.Drawing.Drawing2D;
8
9using pdftron;
10using pdftron.Common;
11using pdftron.Filters;
12using pdftron.SDF;
13using pdftron.PDF;
14
15namespace PDFDCTestCS
16{
17	/// <summary>
18	/// This sample shows how to create and use PDFDC (i.e. a PDF Device Context).
19	/// Windows developers can use standard GDI or GDIPlus API-s to write on PDFDC 
20	/// and to generate PDF documents based on their existing drawing functions.
21	/// 
22	/// The second portion of this sample shows how to create and use PDFDCEX.
23	/// Windows developers can use standard GDI or GDIPlus API-s to write multi-page
24	/// PDF documents using existing drawing code.
25	///
26	/// PDFDCEX can also be used to implement file conversion from any printable 
27	/// file format to PDF (i.e. a virtual PDF printer driver).
28	/// </summary>
29	class Class1
30	{
31		private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
32		static Class1() {}
33		
34		// Relative path to the folder containing test files.
35		const string input_path = "../../../../TestFiles/";
36		const string output_path = "../../../../TestFiles/Output/";
37
38		/// <summary>
39		/// The main entry point for the application.
40		/// </summary>
41		[STAThread]
42		static void Main(string[] args)
43		{
44			PDFNet.Initialize(PDFTronLicense.Key);
45
46			try
47			{
48				//////////////////////////////////////////////////////////////////////////
49				// First the PDFDC
50				// Start with a PDFDoc to put the picture into, and a PDFDC to translate GDI to PDF
51				using (PDFDoc pdfdoc = new PDFDoc())
52				{
53					PDFDC pdf_dc = new PDFDC();
54
55					// Set the scale between GDI logical units and the PDF page at 50/inch.
56					pdf_dc.SetDPI(50);
57
58					// Create a page to put the GDI content onto
59					Page page = pdfdoc.PageCreate();
60
61					// Begin the translation from GDI to PDF.
62					// Provide the page to place the picture onto, and the bounding box for the content.
63					// We're going to scale the GDI content to fill the page while preserving the aspect
64					// ratio.
65					// Get back a GDIPlus Graphics Object
66					using (Graphics gr = pdf_dc.Begin(page, page.GetCropBox()))
67					{
68						// Create a path that consists of a single polygon.
69						System.Drawing.Point[] polyPoints = 
70						{
71							new System.Drawing.Point(10, 10),
72							new System.Drawing.Point(150, 10), 
73							new System.Drawing.Point(100, 75),
74							new System.Drawing.Point(100, 150)
75						};
76
77						GraphicsPath path = new GraphicsPath();
78						path.AddPolygon(polyPoints);
79
80						// Construct a region based on the path.
81						Region region = new Region(path);
82
83						// Draw the outline of the region.
84						Pen pen = Pens.Black;
85						gr.DrawPath(pen, path);
86
87						// Set the clipping region of the Graphics object.
88						gr.SetClip(region, CombineMode.Replace);
89
90						// Draw some clipped strings.
91						FontFamily fontFamily = new FontFamily("Arial");
92						System.Drawing.Font font = new System.Drawing.Font(
93							fontFamily,
94							36, FontStyle.Bold,
95							GraphicsUnit.Pixel);
96						SolidBrush solidBrush = new SolidBrush(Color.FromArgb(255, 255, 0, 0));
97
98						gr.DrawString(
99							"A Clipping Region",
100							font, solidBrush,
101							new PointF(15, 25));
102
103						gr.DrawString(
104							"A Clipping Region",
105							font,
106							solidBrush,
107							new PointF(15, 68));
108
109
110						pdf_dc.End(); // Close PDF Device Context
111					}
112
113					// Add the page to the document
114					pdfdoc.PagePushBack(page);
115
116					//////////////////////////////////////////////////////////////////////////
117					// Page two
118					page = pdfdoc.PageCreate();
119					using (Graphics gr = pdf_dc.Begin(page, page.GetCropBox(), true))
120					{
121						Rectangle myRectangle = new Rectangle(0, 0, 100, 50);
122						LinearGradientBrush myLinearGradientBrush = new LinearGradientBrush(
123							myRectangle,
124							Color.Blue,
125							Color.Green,
126							LinearGradientMode.Horizontal);
127						gr.FillEllipse(myLinearGradientBrush, myRectangle);
128
129						// Complete the translation
130						pdf_dc.End();
131					}
132
133					// Add the page to the document
134					pdfdoc.PagePushBack(page);
135
136					pdfdoc.Save(output_path + "PDFDCTest.pdf", SDFDoc.SaveOptions.e_remove_unused);
137				}
138				Console.WriteLine("Saved PDFDCTest.pdf");
139
140
141				//////////////////////////////////////////////////////////////////////////
142				// 
143				// Now for PDFDCEX
144				// Start with a PDFDoc to put the picture into, and a PDFDCEX to translate GDI to PDF
145				using (PDFDoc pdfdoc2 = new PDFDoc())
146				{
147					PDFDCEX pdf_dcex = new PDFDCEX();
148
149					// Begin the translation from GDI to PDF.
150					// The page dimensions and the converter DPI determine the coordinate system.
151					// Unlike PDFDC, the drawings will not be scaled to fit the bounding box.
152					pdf_dcex.Begin(pdfdoc2);
153
154					// Start the first page -- Get back a Graphics Object
155					System.Drawing.Graphics gr2 = pdf_dcex.StartPage();
156
157					// Create a path that consists of a single polygon.
158					System.Drawing.Point[] polyPoints2 = 
159					{
160						new System.Drawing.Point(10, 10),
161						new System.Drawing.Point(150, 10), 
162						new System.Drawing.Point(100, 75),
163						new System.Drawing.Point(100, 150)
164					};
165
166					GraphicsPath path2 = new GraphicsPath();
167					path2.AddPolygon(polyPoints2);
168
169					// Construct a region based on the path.
170					Region region2 = new Region(path2);
171
172					// Draw the outline of the region.
173					Pen pen2 = Pens.Black;
174					gr2.DrawPath(pen2, path2);
175
176					// Set the clipping region of the Graphics object.
177					gr2.SetClip(region2, CombineMode.Replace);
178
179					// Draw some clipped strings.
180					FontFamily fontFamily2 = new FontFamily("Arial");
181					System.Drawing.Font font2 = new System.Drawing.Font(
182						fontFamily2,
183						36, FontStyle.Bold,
184						GraphicsUnit.Pixel);
185					SolidBrush solidBrush2 = new SolidBrush(Color.FromArgb(255, 255, 0, 0));
186
187					gr2.DrawString(
188						"A Clipping Region",
189						font2, solidBrush2,
190						new PointF(15, 25));
191
192					gr2.DrawString(
193						"A Clipping Region",
194						font2,
195						solidBrush2,
196						new PointF(15, 68));
197
198					pdf_dcex.EndPage();
199					// at this point the Graphics Context gr2 has been disposed
200
201					//////////////////////////////////////////////////////////////////////////
202					// Page two -- get a new Graphics Object
203
204					gr2 = pdf_dcex.StartPage();
205
206					Rectangle myRectangle2 = new Rectangle(0, 0, 100, 50);
207					LinearGradientBrush myLinearGradientBrush2 = new LinearGradientBrush(
208						myRectangle2,
209						Color.Blue,
210						Color.Green,
211						LinearGradientMode.Horizontal);
212					gr2.FillEllipse(myLinearGradientBrush2, myRectangle2);
213					gr2.DrawString(
214						"An Ellipse Filled with a Linear Gradient",
215						font2, solidBrush2,
216						new PointF(15, 200));
217					pdf_dcex.EndPage();
218
219					// Complete the translation
220					pdf_dcex.End();
221
222					pdfdoc2.Save(output_path + "PDFDCEXTest.pdf", SDFDoc.SaveOptions.e_remove_unused);
223				}
224				Console.WriteLine("Saved PDFDCEXTest.pdf");
225				Console.WriteLine("Done.");
226			}
227			catch (PDFNetException e)
228			{
229				Console.WriteLine(e.Message);
230			}
231			PDFNet.Terminate();
232		}
233	}
234}
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}
1'
2' Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3'
4
5Imports System
6Imports System.Drawing
7Imports System.Drawing.Drawing2D
8
9Imports pdftron
10Imports pdftron.Common
11Imports pdftron.Filters
12Imports pdftron.SDF
13Imports pdftron.PDF
14
15' This sample shows how to create and use PDFDC (i.e. a PDF Device Context).
16' Windows developers can use standard GDI or GDIPlus API-s to write on PDFDC 
17' and to generate PDF documents based on their existing drawing functions.
18' 
19' The second portion of this sample shows how to create and use PDFDCEX.
20' Windows developers can use standard GDI or GDIPlus API-s to write multi-page
21' PDF documents using existing drawing code.
22'
23' PDFDCEX can also be used to implement file conversion from any printable 
24' file format to PDF (i.e. a virtual PDF printer driver).
25
26Module PDFDCTestVB
27	Dim pdfNetLoader As PDFNetLoader
28	Sub New()
29		pdfNetLoader = pdftron.PDFNetLoader.Instance()
30	End Sub
31
32	' Relative path to the folder containing test files.
33	Dim input_path As String = "../../../../TestFiles/"
34	Dim output_path As String = "../../../../TestFiles/Output/"
35
36	Sub Main()
37
38		PDFNet.Initialize(PDFTronLicense.Key)
39
40		Try
41
42			Console.WriteLine("-------------------------------------------------")
43
44			' Start with a PDFDoc to put the picture into, and a PDFDC to translate GDI to PDF
45			Using pdf_doc As PDFDoc = New PDFDoc
46				Dim pdf_dc As PDFDC = New PDFDC
47				' Set the scale between GDI logical units and the PDF page at 50/inch.
48				pdf_dc.SetDPI(50)
49
50				' Create a page to put the GDI content onto
51				Dim pg As Page = pdf_doc.PageCreate()
52
53				' Begin the translation from GDI to PDF.
54				' Provide the page to place the picture onto, and the bounding box for the content.
55				' We're going to scale the GDI content to fill the page while preserving the aspect
56				''/ ratio.
57				' Get back a GDI Device Context
58				Using gr As Graphics = pdf_dc.Begin(pg, pg.GetCropBox())
59					Dim myImage As System.Drawing.Image = System.Drawing.Image.FromFile(input_path + "butterfly.png")
60					Dim myTextureBrush As TextureBrush = New TextureBrush(myImage)
61					gr.FillEllipse(myTextureBrush, 0, 0, 100, 50)
62
63					pdf_dc.End() ' Close PDF Device Context
64				End Using
65
66				' Add the page to the document
67				pdf_doc.PagePushBack(pg)
68
69				pdf_doc.Save(output_path + "PDFDCTest.pdf", SDF.SDFDoc.SaveOptions.e_linearized)
70			End Using
71			Console.WriteLine("Wrote " + output_path + "PDFDCTest12.pdf")
72
73			''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
74			' 
75			' Now the PDFDCEX example
76			' Start with a PDFDoc to put the picture into, and a PDFDCEX to translate GDI to PDF
77			Using pdf_doc2 As PDFDoc = New PDFDoc
78				Dim pdf_dcex As PDFDCEX = New PDFDCEX
79
80				' Begin the translation from GDI to PDF.
81				' Get back a GDI Device Context -- this will be managed by the PDFDCEX class
82				pdf_dcex.Begin(pdf_doc2)
83				Dim gr2 As Graphics = pdf_dcex.StartPage()
84
85				Dim myImage2 As System.Drawing.Image = System.Drawing.Image.FromFile(input_path + "butterfly.png")
86				Dim myTextureBrush2 As TextureBrush = New TextureBrush(myImage2)
87				gr2.FillEllipse(myTextureBrush2, 0, 0, 100, 50)
88
89				pdf_dcex.EndPage() ' This also disposes of the Graphics Object gr2
90				pdf_dcex.End() ' Close PDF Device Context
91
92				pdf_doc2.Save(output_path + "PDFDCEXTest.pdf", SDF.SDFDoc.SaveOptions.e_linearized)
93			End Using
94			Console.WriteLine("Wrote " + output_path + "PDFDCEXTest.pdf")
95			Console.WriteLine("Done.")
96
97		Catch ex As PDFNetException
98
99			Console.WriteLine(ex.Message)
100
101		Catch ex As Exception
102
103			MsgBox(ex.Message)
104
105		End Try
106		PDFNet.Terminate()
107	End Sub
108
109End Module
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales