PDFDC

Sample C# 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 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}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales