-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
75 lines (67 loc) · 3.79 KB
/
Program.cs
File metadata and controls
75 lines (67 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using DevExpress.Drawing;
using DevExpress.Pdf;
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
namespace DocumentCreationAPI {
class Program {
static void Main(string[] args) {
string docPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),"Result.pdf");
using (PdfDocumentProcessor processor = new PdfDocumentProcessor()) {
// Create an empty document.
processor.CreateEmptyDocument(docPath);
// Create and draw PDF graphics.
using (PdfGraphics graph = processor.CreateGraphicsWorldSystem()) {
DrawGraphics(graph);
// Render a page with graphics.
processor.RenderNewPage(PdfPaperSize.Letter, graph);
}
}
// Generate a watermark.
AddWatermark("Not for sale",docPath,docPath);
Process.Start(new ProcessStartInfo(docPath) { UseShellExecute = true });
}
// Draw graphics inside a PDF document.
static void DrawGraphics(PdfGraphics graph) {
// Draw text lines on the page.
DXSolidBrush black = (DXSolidBrush)DXBrushes.Black;
DXFont font1 = new DXFont("Times New Roman", 32, DXFontStyle.Bold);
graph.DrawString("PDF Document Processor", font1, black, 140, 50);
DXFont font2 = new DXFont("Arial", 20);
graph.DrawString("Display, Print and Export PDF Documents", font2, black, 128, 120);
DXFont font3 = new DXFont("Arial", 10);
graph.DrawString("The PDF Document Processor is a non-visual component " +
"that provides the application programming interface of the PDF Viewer.", font3, black, 16, 180);
}
// Add a watermark with custom text.
static void AddWatermark(string text,string fileName,string resultFileName) {
using (PdfDocumentProcessor documentProcessor = new PdfDocumentProcessor()) {
string fontName = "Arial Black";
int fontSize = 12;
PdfStringFormat stringFormat = PdfStringFormat.GenericTypographic;
stringFormat.Alignment = PdfStringAlignment.Center;
stringFormat.LineAlignment = PdfStringAlignment.Center;
documentProcessor.LoadDocument(fileName);
using (DXSolidBrush brush = new DXSolidBrush(Color.FromArgb(63,Color.Black))) {
DXFont font = new DXFont(fontName,fontSize);
foreach (var page in documentProcessor.Document.Pages) {
var watermarkSize = page.CropBox.Width * 0.75;
using (PdfGraphics graphics = documentProcessor.CreateGraphicsPageSystem()) {
SizeF stringSize = graphics.MeasureString(text,font);
float scale = (float)(watermarkSize / (double)stringSize.Width);
graphics.TranslateTransform((float)(page.CropBox.Width * 0.5),(float)(page.CropBox.Height * 0.5));
graphics.RotateTransform((float)45.0);
graphics.TranslateTransform((float)(-stringSize.Width * scale * 0.5),(float)(-stringSize.Height * scale * 0.5));
DXFont actualFont = new DXFont(fontName,fontSize * scale);
RectangleF rect = new RectangleF(0,0,stringSize.Width * scale,stringSize.Height * scale);
graphics.DrawString(text,actualFont,brush,rect,stringFormat);
graphics.AddToPageForeground(page);
}
}
}
documentProcessor.SaveDocument(resultFileName);
}
}
}
}