/
Program.vb
64 lines (57 loc) · 3.2 KB
/
Program.vb
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
Imports System.Drawing
Imports System.Collections.Generic
Imports DevExpress.Pdf
Imports DevExpress.Drawing
Namespace CreateGraphics
Friend Class Program
Const DrawingDpi As Single = 72.0F
Shared Sub Main(ByVal args As String())
Using processor As PdfDocumentProcessor = New PdfDocumentProcessor()
processor.LoadDocument("..\..\RotatedDocument.pdf")
Using textBrush As DXSolidBrush = New DXSolidBrush(Color.FromArgb(100, Color.Blue))
AddGraphics(processor, "text", textBrush)
End Using
processor.SaveDocument("..\..\RotatedDocumentWithGraphics.pdf")
End Using
End Sub
Private Shared Sub AddGraphics(ByVal processor As PdfDocumentProcessor, ByVal text As String, ByVal textBrush As DXSolidBrush)
Dim pages As IList(Of PdfPage) = processor.Document.Pages
For i As Integer = 0 To pages.Count - 1
Dim page As PdfPage = pages(i)
Using graphics As PdfGraphics = processor.CreateGraphics()
Dim actualPageSize As SizeF = PrepareGraphics(page, graphics, DrawingDpi, DrawingDpi)
Dim font As DXFont = New DXFont("Segoe UI", 20, DXFontStyle.Regular)
Dim textSize As SizeF = graphics.MeasureString(text, font, PdfStringFormat.GenericDefault, DrawingDpi, DrawingDpi)
Dim topLeft As PointF = New PointF(0, 0)
Dim bottomRight As PointF = 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)
End Using
Next
End Sub
Private Shared Function PrepareGraphics(ByVal page As PdfPage, ByVal graphics As PdfGraphics, ByVal dpiX As Single, ByVal dpiY As Single) As SizeF
Dim cropBox As PdfRectangle = page.CropBox
Dim cropBoxWidth As Single = ConvertFromPdfUnits(CSng(cropBox.Width), dpiX)
Dim cropBoxHeight As Single = ConvertFromPdfUnits(CSng(cropBox.Height), dpiY)
Select Case 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)
End Select
Return New SizeF(cropBoxWidth, cropBoxHeight)
End Function
Private Shared Function ConvertFromPdfUnits(ByVal pdfValue As Single, ByVal targetDpi As Single) As Single
Return pdfValue / 72.0F * targetDpi
End Function
End Class
End Namespace