-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
70 lines (66 loc) · 3 KB
/
Program.cs
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
using System.Drawing;
using System.Collections.Generic;
using DevExpress.Pdf;
using DevExpress.Drawing;
namespace CreateGraphics
{
class Program
{
const float DrawingDpi = 72f;
static void Main(string[] args)
{
using (PdfDocumentProcessor processor = new PdfDocumentProcessor())
{
processor.LoadDocument("..\\..\\RotatedDocument.pdf");
using (DXSolidBrush textBrush = new DXSolidBrush(Color.FromArgb(100, Color.Blue)))
AddGraphics(processor, "text", textBrush);
processor.SaveDocument("..\\..\\RotatedDocumentWithGraphics.pdf");
}
}
static void AddGraphics(PdfDocumentProcessor processor, string text, DXSolidBrush textBrush)
{
IList<PdfPage> pages = processor.Document.Pages;
for (int i = 0; i < pages.Count; i++)
{
PdfPage page = pages[i];
using (PdfGraphics graphics = processor.CreateGraphics())
{
SizeF actualPageSize = PrepareGraphics(page, graphics, DrawingDpi, DrawingDpi);
DXFont font = new DXFont("Segoe UI", 20, DXFontStyle.Regular);
SizeF textSize = graphics.MeasureString(text, font, PdfStringFormat.GenericDefault, DrawingDpi, DrawingDpi);
PointF topLeft = new PointF(0, 0);
PointF bottomRight = new PointF(actualPageSize.Width - textSize.Width, actualPageSize.Height - textSize.Height);
graphics.DrawString(text, font, textBrush, topLeft);
graphics.DrawString(text, font, textBrush, bottomRight);
graphics.AddToPageForeground(page, DrawingDpi, DrawingDpi);
}
}
}
static SizeF PrepareGraphics(PdfPage page, PdfGraphics graphics, float dpiX, float dpiY)
{
PdfRectangle cropBox = page.CropBox;
float cropBoxWidth = ConvertFromPdfUnits((float)cropBox.Width, dpiX);
float cropBoxHeight = ConvertFromPdfUnits((float)cropBox.Height, dpiY);
switch (page.Rotate)
{
case 90:
graphics.RotateTransform(-90);
graphics.TranslateTransform(-cropBoxHeight, 0);
return new SizeF(cropBoxHeight, cropBoxWidth);
case 180:
graphics.RotateTransform(-180);
graphics.TranslateTransform(-cropBoxWidth, -cropBoxHeight);
return new SizeF(cropBoxWidth, cropBoxHeight);
case 270:
graphics.RotateTransform(-270);
graphics.TranslateTransform(0, -cropBoxWidth);
return new SizeF(cropBoxHeight, cropBoxWidth);
}
return new SizeF(cropBoxWidth, cropBoxHeight);
}
static float ConvertFromPdfUnits(float pdfValue, float targetDpi)
{
return pdfValue / 72f * targetDpi;
}
}
}