-
Notifications
You must be signed in to change notification settings - Fork 3
/
dialogWatermark.vb
719 lines (719 loc) · 38.7 KB
/
dialogWatermark.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
Imports System.Windows.Forms
Imports System.Drawing
Imports System.IO
Imports iTextSharp.text.pdf
Imports iTextSharp.text
Imports System.Threading.Tasks
Class dialogWatermark
''' <summary>
''' PdForms.net - An open source pdf form editor
''' Copyright 2018 NK-INC.COM All Rights reserved.
''' PdForms.net utilizes iTextSharp technologies.
''' Website: www.pdforms.net (source code), www.pdforms.com (about)
''' </summary>
Public watermarkBitmap As Bitmap = Nothing
Public watermarkData() As Byte
Public watermarkText As String
Public watermarkWidth As Single = 0, watermarkHeight As Single = 0
Public watermarkTextOnly As Boolean = False
Public pdfBytesOriginal As Byte()
Public pdfBytesWatermark As Byte()
Public frmParent As Form
Private Function AddWatermarkText(bytes As Byte(), bf As BaseFont, text As String, angle As Single, fontSize As Single, fontColor As BaseColor) As Byte()
Using ms = New MemoryStream(10 * 1024)
Using reader = New PdfReader(bytes)
Using stamper = New PdfStamper(reader, ms)
Dim times As Integer = reader.NumberOfPages
For i As Integer = 1 To times
Dim dc = stamper.GetOverContent(i)
AddWaterMark(dc, text, bf, fontSize, angle, fontColor, reader.GetPageSizeWithRotation(i))
Next
stamper.Close()
End Using
End Using
Return ms.ToArray()
End Using
End Function
Private Function AddWatermarkTextToPage(bytes As Byte(), pageIndex As Integer, bf As BaseFont, text As String, angle As Single, fontSize As Single, fontColor As BaseColor) As Byte()
Using ms = New MemoryStream()
Using reader = New PdfReader(bytes)
Using stamper = New PdfStamper(reader, ms)
Dim dc = stamper.GetOverContent(CInt(pageIndex + 1))
AddWaterMark(dc, text, bf, fontSize, angle, fontColor, reader.GetPageSizeWithRotation(CInt(pageIndex + 1)))
stamper.Close()
End Using
End Using
Return ms.ToArray()
End Using
End Function
Private Function AddWatermarkImage(bytes As Byte(), bf As BaseFont, text As String, angle As Single, fontSize As Single, fontColor As BaseColor) As Byte()
Using ms = New MemoryStream(10 * 1024)
Using reader = New PdfReader(bytes)
Using stamper = New PdfStamper(reader, ms)
Dim times As Integer = reader.NumberOfPages
For i As Integer = 1 To times
Dim dc = stamper.GetOverContent(i)
AddWaterMark(dc, text, bf, fontSize, angle, fontColor, reader.GetPageSizeWithRotation(i))
Next
stamper.Close()
End Using
End Using
Return ms.ToArray()
End Using
End Function
Private Function AddWatermarkImageToPage(bytes As Byte(), pageIndex As Integer, bf As BaseFont, text As String, angle As Single, fontSize As Single, fontColor As BaseColor) As Byte()
Using ms = New MemoryStream()
Using reader = New PdfReader(bytes)
Using stamper = New PdfStamper(reader, ms)
Dim dc = stamper.GetOverContent(CInt(pageIndex + 1))
AddWaterMark(dc, text, bf, fontSize, angle, fontColor, reader.GetPageSizeWithRotation(CInt(pageIndex + 1)))
stamper.Close()
End Using
End Using
Return ms.ToArray()
End Using
End Function
Public Sub AddWaterMark(dc As PdfContentByte, text As String, font As BaseFont, fontSize As Single, angle As Single, color As BaseColor, realPageSize As iTextSharp.text.Rectangle, Optional rect As iTextSharp.text.Rectangle = Nothing)
Dim gstate = New PdfGState()
dc.SaveState()
dc.SetGState(gstate)
dc.SetColorFill(color)
dc.BeginText()
dc.SetFontAndSize(font, fontSize)
Dim ps = If(rect, realPageSize)
Dim x = (ps.Right + ps.Left) / 2
Dim y = (ps.Bottom + ps.Top) / 2
dc.ShowTextAligned(Element.ALIGN_CENTER, text, x, y, angle)
dc.EndText()
dc.RestoreState()
End Sub
Public Function AddWaterMark(dc As PdfContentByte, imgWatermark As System.Drawing.Image, font As BaseFont, fontSize As Single, angle As Single, color As BaseColor, realPageSize As iTextSharp.text.Rectangle, Optional rect As iTextSharp.text.Rectangle = Nothing) As Byte()
If frmParent.GetType() Is GetType(frmMain) Then
End If
Return Nothing
End Function
Public Function AddImageToPage(pdfBytes() As Byte, pageIndex As Integer, ByVal img As System.Drawing.Image, ByVal GetOverContent As Boolean, rectPDFWatermark As System.Drawing.RectangleF, rectPDF As System.Drawing.RectangleF) As Byte()
If img Is Nothing Then
Return pdfBytes
ElseIf img.Width <= 0 And img.Height <= 0 Then
Return pdfBytes
End If
Try
Dim jpg As System.Drawing.Image = DirectCast(img.Clone(), System.Drawing.Image)
Dim jpgStream As New MemoryStream
Dim frmImgRot As New frmImageRotation
Try
jpg = frmImgRot.ImageRotation_PictureBox.Image.Clone
Catch ex As Exception
jpg = frmImgRot.ImageRotation_PictureBox.Image.Clone
Finally
End Try
jpg.Save(jpgStream, System.Drawing.Imaging.ImageFormat.Png)
If jpgStream.CanSeek Then
jpgStream.Seek(0, SeekOrigin.Begin)
End If
jpg.Dispose()
jpg = Nothing
Dim bmp As New Bitmap(jpgStream)
Dim page_width As Single = 0, page_height As Single = 0
Dim rectImage As iTextSharp.text.Rectangle = Nothing
Using mPDF As New MemoryStream()
Try
page_width = rectPDF.Width
page_height = rectPDF.Height
rectImage = New iTextSharp.text.Rectangle(page_width, page_height)
Catch ex As Exception
page_width = bmp.Width
page_height = bmp.Height
End Try
Dim r As New iTextSharp.text.Rectangle(CInt(page_width), CInt(page_height))
Dim bytes() As Byte = pdfBytes
Dim pdfReaderDoc As New PdfReader(bytes)
Dim stamp As New PdfStamper(pdfReaderDoc, mPDF)
Dim pages As Integer = bmp.GetFrameCount(Imaging.FrameDimension.Page)
Dim image As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(bmp, System.Drawing.Imaging.ImageFormat.Png)
If Not rectPDFWatermark = Nothing Then
If rectPDFWatermark.Width <= 5 Or rectPDFWatermark.Height <= 5 Then
image.SetAbsolutePosition(0, 0)
image.ScaleAbsoluteHeight(stamp.Reader.GetPageSizeWithRotation(CInt(pageIndex + 1)).Height)
image.ScaleAbsoluteWidth(stamp.Reader.GetPageSizeWithRotation(CInt(pageIndex + 1)).Width)
Else
image.SetAbsolutePosition(rectPDFWatermark.Left, rectPDFWatermark.Top - rectPDFWatermark.Height)
image.ScaleAbsoluteHeight(rectPDFWatermark.Height)
image.ScaleAbsoluteWidth(rectPDFWatermark.Width)
End If
Else
image.SetAbsolutePosition(0, 0)
image.ScaleAbsoluteHeight(stamp.Reader.GetPageSizeWithRotation(CInt(pageIndex + 1)).Height)
image.ScaleAbsoluteWidth(stamp.Reader.GetPageSizeWithRotation(CInt(pageIndex + 1)).Width)
End If
If GetOverContent Then
stamp.GetOverContent(CInt(pageIndex + 1)).AddImage(image)
Else
stamp.GetUnderContent(CInt(pageIndex + 1)).AddImage(image)
End If
stamp.Writer.CloseStream = False
stamp.Close()
stamp.Dispose()
stamp = Nothing
Return mPDF.ToArray()
End Using
Catch ex As Exception
Throw ex
Finally
End Try
Return Nothing
End Function
Public Sub New()
InitializeComponent()
End Sub
Public Sub New(ByRef frmMain1 As frmMain)
InitializeComponent()
frmParent = frmMain1
End Sub
Private Function ResizeImage(newWidth As Integer, newHeight As Integer, imgPhoto As System.Drawing.Image) As System.Drawing.Image
Dim sourceWidth As Integer = imgPhoto.Width
Dim sourceHeight As Integer = imgPhoto.Height
If sourceWidth < sourceHeight Then
Dim buff As Integer = newWidth
newWidth = newHeight
newHeight = buff
End If
Dim sourceX As Integer = 0, sourceY As Integer = 0, destX As Integer = 0, destY As Integer = 0
Dim nPercent As Single = 0, nPercentW As Single = 0, nPercentH As Single = 0
nPercentW = (CSng(newWidth) / CSng(sourceWidth))
nPercentH = (CSng(newHeight) / CSng(sourceHeight))
If nPercentH < nPercentW Then
nPercent = nPercentH
destX = System.Convert.ToInt16((newWidth - (sourceWidth * nPercent)) / 2)
Else
nPercent = nPercentW
destY = System.Convert.ToInt16((newHeight - (sourceHeight * nPercent)) / 2)
End If
Dim destWidth As Integer = CInt(sourceWidth * nPercent)
Dim destHeight As Integer = CInt(sourceHeight * nPercent)
Dim bmPhoto As New Bitmap(newWidth, newHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution)
Dim grPhoto As Graphics = Graphics.FromImage(bmPhoto)
grPhoto.FillRectangle(Brushes.White, 0, 0, bmPhoto.Width, bmPhoto.Height)
grPhoto.SmoothingMode = Drawing2D.SmoothingMode.None
grPhoto.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
grPhoto.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
If destX > 0 Or destY > 0 Then
grPhoto.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBilinear
Else
grPhoto.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
End If
grPhoto.DrawImage(imgPhoto, New System.Drawing.Rectangle(destX, destY, destWidth, destHeight), New System.Drawing.Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), GraphicsUnit.Point)
grPhoto.Dispose()
Return bmPhoto
End Function
Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
Dim frm As frmMain = DirectCast(Me.frmParent, frmMain)
frm.DeleteTempFilesImageCache()
frm.Session = frm.EncryptPDFDocument(addWatermark(""))
frm.A0_LoadPDF()
frm.refreshPDFImage()
Me.DialogResult = System.Windows.Forms.DialogResult.OK
Me.Close()
End Sub
Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
Me.Close()
End Sub
Private Sub picBarcode_Click(sender As Object, e As EventArgs) Handles previewPicWatermark.Click
End Sub
Private Sub appearance_OpacityScroll_Scroll(sender As Object, e As EventArgs) Handles appearance_OpacityScroll.Scroll
If Not appearance_OpacityScroll.Value = appearance_OpacityUpDwn.Value Then
appearance_OpacityUpDwn.Value = appearance_OpacityScroll.Value
displayPreviewDelay()
End If
End Sub
Private Sub appearance_chkScaleRelativeToTargetPage_CheckedChanged(sender As Object, e As EventArgs) Handles appearance_chkScaleRelativeToTargetPage.CheckedChanged
appearance_ScalePercentUpDwn.Enabled = appearance_chkScaleRelativeToTargetPage.Checked
End Sub
Private Sub appearance_OpacityUpDwn_ValueChanged(sender As Object, e As EventArgs) Handles appearance_OpacityUpDwn.ValueChanged
If Not appearance_OpacityScroll.Value = appearance_OpacityUpDwn.Value Then
appearance_OpacityScroll.Value = appearance_OpacityUpDwn.Value
displayPreviewDelay()
End If
End Sub
Private Sub source_radText_CheckedChanged(sender As Object, e As EventArgs) Handles source_radText.CheckedChanged
source_pnlText.Enabled = source_radText.Checked
source_pnlFile.Enabled = Not source_radText.Checked
End Sub
Private Sub source_radFile_CheckedChanged(sender As Object, e As EventArgs) Handles source_radFile.CheckedChanged
source_pnlText.Enabled = source_radText.Checked
source_pnlFile.Enabled = Not source_radText.Checked
End Sub
Private Sub source_textPicFontColor_Click(sender As Object, e As EventArgs) Handles source_textPicFontColor.Click
Try
ColorDialog1.Color = source_textPicFontColor.BackColor
Select Case ColorDialog1.ShowDialog(Me)
Case DialogResult.OK, DialogResult.Yes
source_textPicFontColor.BackColor = ColorDialog1.Color
displayPreviewDelay()
Case Else
End Select
Catch ex As Exception
Err.Clear()
End Try
End Sub
Private Sub LinkLabel1_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs)
End Sub
Private Sub dialogWatermark_Load(sender As Object, e As EventArgs) Handles Me.Load
Try
pdfDelayPause = True
cmbSavedSettings.SelectedIndex = 0
source_textCmbFontSize.SelectedIndex = 0
position_VDistUnits.SelectedIndex = 0
position_VDistAlign.SelectedIndex = 1
position_HDistUnits.SelectedIndex = 0
position_HDistAlign.SelectedIndex = 1
Label20.Text = " of " & DirectCast(frmParent, frmMain).btnPage.Items.Count.ToString()
previewPageNumberUpDwn.Minimum = 1
previewPageNumberUpDwn.Maximum = DirectCast(frmParent, frmMain).btnPage.Items.Count
DirectCast(frmParent, frmMain).LoadFontsList(source_textCmbFont)
If source_textCmbFont.Items.Count > 0 Then
source_textCmbFont.SelectedIndex = 0
End If
source_textCmbFontSize.Text = "200"
source_textCmbFont.SelectedItem = "Arial Black"
position_HDistAlign.SelectedIndex = 1
pdfDelayPause = False
displayPreviewDelay()
Catch ex As Exception
Err.Clear()
End Try
End Sub
Public Enum convertUnits
Pixels = 0
Inches = 1
Percent = 2
Centimeters = 3
Millimeters = 4
Picas = 5
Points = 6
End Enum
Public Function convertToPixels(qty As Single, fromUnits As convertUnits) As Single
Select Case fromUnits
Case convertUnits.Pixels
Return qty * 1
Case convertUnits.Inches
Return qty * 96
Case convertUnits.Centimeters
Return qty * 37.79527559055
Case convertUnits.Millimeters
Return qty * 3.779527559055
Case convertUnits.Picas
Return qty * 16
Case convertUnits.Points
Return qty * 1.333333333333
End Select
End Function
Public Function convertFromPixels(qty As Single, toUnits As convertUnits) As Single
Select Case toUnits
Case convertUnits.Pixels
Return qty * 1
Case convertUnits.Inches
Return qty * 0.01041666666666
Case convertUnits.Centimeters
Return qty * 0.02645833333333
Case convertUnits.Millimeters
Return qty * 0.2645833333333
Case convertUnits.Picas
Return qty * 0.0625
Case convertUnits.Points
Return qty / 0.75
End Select
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
displayPreview()
End Sub
Dim pdfPageBytes() As Byte = Nothing
Dim pdfPage As Long = -1
Public pdfDelay As DateTime = DateTime.Now
Public pdfDelayPause As Boolean = True
Private Sub tmrDelayPreview_Tick(sender As Object, e As EventArgs) Handles tmrDelayPreview.Tick
Try
If pdfDelayPause Then Return
If pdfDelay <= DateTime.Now Then
tmrDelayPreview.Enabled = False
displayPreview()
Return
End If
Catch ex As Exception
Err.Clear()
End Try
End Sub
Public Sub displayPreviewDelay(Optional lngMilSec As Long = 1000)
tmrDelayPreview.Enabled = False
pdfDelay = DateTime.Now.AddMilliseconds(lngMilSec)
tmrDelayPreview.Interval = lngMilSec + 10
tmrDelayPreview.Enabled = True
End Sub
Public Sub displayPreview()
Try
Dim frm As frmMain = DirectCast(frmParent, frmMain)
Dim r As PdfReader = Nothing
If Not pdfPage = previewPageNumberUpDwn.Value Or pdfPageBytes Is Nothing Then
pdfPageBytes = frm.Session
r = New PdfReader(pdfPageBytes, frm.getBytes(frm.pdfOwnerPassword))
r.SelectPages(previewPageNumberUpDwn.Value.ToString())
pdfPage = previewPageNumberUpDwn.Value
pdfPageBytes = frm.getPDFBytes(r)
Else
r = New PdfReader(pdfPageBytes)
End If
Dim pdfPageBytes2() As Byte = AddWatermark(pdfPageBytes, "")
Dim img As Bitmap = Bitmap.FromStream(New MemoryStream(frm.A0_LoadImageGhostScript(pdfPageBytes2, frm.pdfOwnerPassword, 1, r.GetPageSizeWithRotation(1).Width, r.GetPageSizeWithRotation(1).Height, False)))
previewPicWatermark.Image = img.Clone
Catch ex As Exception
Err.Clear()
End Try
End Sub
Public pdfBytes() As Byte = Nothing
Public Function addWatermark(Optional selectPages As String = "") As Byte()
Try
Dim frm As frmMain = DirectCast(frmParent, frmMain)
pdfBytes = frm.Session
Dim r As New PdfReader(pdfBytes, frm.getBytes(frm.pdfOwnerPassword))
If Not selectPages = "" Then
r.SelectPages(selectPages)
pdfBytes = frm.getPDFBytes(r)
pdfBytes = AddWatermark(pdfBytes, "")
Else
pdfBytes = AddWatermark(pdfBytes, frm.pdfOwnerPassword)
End If
Return pdfBytes
Catch ex As Exception
Err.Clear()
End Try
Return Nothing
End Function
Private Sub appearance_rotation45Neg_CheckedChanged(sender As Object, e As EventArgs) Handles appearance_rotation45Neg.CheckedChanged
If appearance_rotation45Neg.Checked Then appearance_rotationCustomUpDwn.Value = -45
End Sub
Private Sub appearance_rotation0_CheckedChanged(sender As Object, e As EventArgs) Handles appearance_rotation0.CheckedChanged
If appearance_rotation0.Checked Then appearance_rotationCustomUpDwn.Value = 0
End Sub
Private Sub appearance_rotation45_CheckedChanged(sender As Object, e As EventArgs) Handles appearance_rotation45.CheckedChanged
If appearance_rotation45.Checked Then appearance_rotationCustomUpDwn.Value = 45
End Sub
Private Sub appearance_rotationCustom_CheckedChanged(sender As Object, e As EventArgs) Handles appearance_rotationCustom.CheckedChanged
End Sub
Dim fnWatermark As String = ""
Private Sub source_fileBtnBrowse_Click(sender As Object, e As EventArgs) Handles source_fileBtnBrowse.Click
Try
Select Case OpenFileDialog1.ShowDialog(Me)
Case DialogResult.Yes, DialogResult.OK
fnWatermark = OpenFileDialog1.FileName
picWatermark.Image = System.Drawing.Image.FromFile(fnWatermark)
displayPreviewDelay()
End Select
Catch ex As Exception
Err.Clear()
End Try
End Sub
Public Function RotateImg(ByVal bmpimage As Bitmap, ByVal angle As Single) As Bitmap
Dim w As Integer = bmpimage.Width
Dim h As Integer = bmpimage.Height
Dim pf As System.Drawing.Imaging.PixelFormat = Nothing
pf = bmpimage.PixelFormat
Dim tempImg As New Bitmap(w, h, pf)
Dim g As Graphics = Graphics.FromImage(tempImg)
g.DrawImageUnscaled(bmpimage, 1, 1)
g.Dispose()
Dim path As New System.Drawing.Drawing2D.GraphicsPath()
path.AddRectangle(New RectangleF(0.0F, 0.0F, w, h))
Dim mtrx As New System.Drawing.Drawing2D.Matrix()
mtrx.Rotate(angle)
Dim rct As RectangleF = path.GetBounds(mtrx)
Dim newImg As New Bitmap(Convert.ToInt32(rct.Width), Convert.ToInt32(rct.Height), pf)
g = Graphics.FromImage(newImg)
g.TranslateTransform(-rct.X, -rct.Y)
g.RotateTransform(angle)
g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
g.DrawImageUnscaled(tempImg, 0, 0)
g.Dispose()
tempImg.Dispose()
Return newImg
End Function
Public Shared Function ResizeImage(ByVal image As System.Drawing.Image, ByVal size As Size, Optional ByVal preserveAspectRatio As Boolean = True) As System.Drawing.Image
Dim newWidth As Integer
Dim newHeight As Integer
If preserveAspectRatio Then
Dim originalWidth As Integer = image.Width
Dim originalHeight As Integer = image.Height
Dim percentWidth As Single = CSng(size.Width) / CSng(originalWidth)
Dim percentHeight As Single = CSng(size.Height) / CSng(originalHeight)
Dim percent As Single = If(percentHeight < percentWidth,
percentHeight, percentWidth)
newWidth = CInt(originalWidth * percent)
newHeight = CInt(originalHeight * percent)
Else
newWidth = size.Width
newHeight = size.Height
End If
Dim newImage As System.Drawing.Image = New Bitmap(newWidth, newHeight)
Using graphicsHandle As Graphics = Graphics.FromImage(newImage)
graphicsHandle.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
graphicsHandle.DrawImage(image, 0, 0, newWidth, newHeight)
End Using
Return newImage
End Function
Private Sub source_textTxtText_TextChanged(sender As Object, e As EventArgs) Handles source_textTxtText.TextChanged
displayPreviewDelay()
End Sub
Private Sub source_textCmbFont_SelectedIndexChanged(sender As Object, e As EventArgs) Handles source_textCmbFont.SelectedIndexChanged
displayPreviewDelay()
End Sub
Private Sub previewPageNumberUpDwn_ValueChanged(sender As Object, e As EventArgs) Handles previewPageNumberUpDwn.ValueChanged
displayPreviewDelay()
End Sub
Private Sub source_textRadFontAlignLeft_CheckedChanged(sender As Object, e As EventArgs) Handles source_textRadFontAlignLeft.CheckedChanged
displayPreviewDelay()
End Sub
Private Sub source_textRadFontAlignCenter_CheckedChanged(sender As Object, e As EventArgs) Handles source_textRadFontAlignCenter.CheckedChanged
displayPreviewDelay()
End Sub
Private Sub source_textRadFontAlignRight_CheckedChanged(sender As Object, e As EventArgs) Handles source_textRadFontAlignRight.CheckedChanged
displayPreviewDelay()
End Sub
Private Sub appearance_rotationCustomUpDwn_ValueChanged(sender As Object, e As EventArgs) Handles appearance_rotationCustomUpDwn.ValueChanged
displayPreviewDelay()
End Sub
Private Sub appearance_radLocationBehindPage_CheckedChanged(sender As Object, e As EventArgs) Handles appearance_radLocationBehindPage.CheckedChanged
displayPreviewDelay()
End Sub
Private Sub appearance_radLocationOnTop_CheckedChanged(sender As Object, e As EventArgs) Handles appearance_radLocationOnTop.CheckedChanged
displayPreviewDelay()
End Sub
Private Sub position_VDistNumberUpDwn_ValueChanged(sender As Object, e As EventArgs) Handles position_VDistNumberUpDwn.ValueChanged
displayPreviewDelay()
End Sub
Private Sub position_VDistUnits_SelectedIndexChanged(sender As Object, e As EventArgs) Handles position_VDistUnits.SelectedIndexChanged
displayPreviewDelay()
End Sub
Private Sub position_VDistAlign_SelectedIndexChanged(sender As Object, e As EventArgs) Handles position_VDistAlign.SelectedIndexChanged
displayPreviewDelay()
End Sub
Private Sub position_HDistNumberUpDwn_ValueChanged(sender As Object, e As EventArgs) Handles position_HDistNumberUpDwn.ValueChanged
displayPreviewDelay()
End Sub
Private Sub position_HDistUnits_SelectedIndexChanged(sender As Object, e As EventArgs) Handles position_HDistUnits.SelectedIndexChanged
displayPreviewDelay()
End Sub
Private Sub position_HDistAlign_SelectedIndexChanged(sender As Object, e As EventArgs) Handles position_HDistAlign.SelectedIndexChanged
displayPreviewDelay()
End Sub
Private Sub source_textCmbFontSize_SelectedIndexChanged(sender As Object, e As EventArgs) Handles source_textCmbFontSize.SelectedIndexChanged
displayPreviewDelay()
End Sub
Public Function AddWatermark(pdfBytes() As Byte, pdfOwnerPassword As String) As Byte()
Dim reader As New PdfReader(pdfBytes, DirectCast(frmParent, frmMain).getBytes(pdfOwnerPassword))
Dim n As Integer = reader.NumberOfPages
Dim m As New MemoryStream
Dim stamper As New PdfStamper(reader, m)
stamper.RotateContents = False
Dim gs1 As New PdfGState()
gs1.FillOpacity = CSng(appearance_OpacityScroll.Value / 100)
Dim over As PdfContentByte = Nothing
Dim pagesize As iTextSharp.text.Rectangle
Dim x As Single, y As Single
For i As Integer = 1 To n
pagesize = reader.GetPageSize(i)
Dim alignMent As Integer = Element.ALIGN_CENTER
If source_textRadFontAlignLeft.Checked Then
alignMent = Element.ALIGN_LEFT
ElseIf source_textRadFontAlignCenter.Checked Then
alignMent = Element.ALIGN_CENTER
ElseIf source_textRadFontAlignRight.Checked Then
alignMent = Element.ALIGN_RIGHT
End If
If source_radText.Checked Then
Dim defaultFont As iTextSharp.text.Font = Nothing
Try
Select Case source_textCmbFont.SelectedIndex
Case 0
defaultFont = FontFactory.GetFont(BaseFont.COURIER, BaseFont.WINANSI, False)
Case 1
defaultFont = FontFactory.GetFont(BaseFont.COURIER_BOLD, BaseFont.CP1252, True)
Case 2
defaultFont = FontFactory.GetFont(BaseFont.COURIER_BOLDOBLIQUE, BaseFont.WINANSI, False)
Case 3
defaultFont = FontFactory.GetFont(BaseFont.COURIER_OBLIQUE, BaseFont.WINANSI, False)
Case 4
defaultFont = FontFactory.GetFont(BaseFont.HELVETICA, BaseFont.WINANSI, False)
Case 5
defaultFont = FontFactory.GetFont(BaseFont.HELVETICA_BOLD, BaseFont.WINANSI, False)
Case 6
defaultFont = FontFactory.GetFont(BaseFont.HELVETICA_BOLDOBLIQUE, BaseFont.WINANSI, False)
Case 7
defaultFont = FontFactory.GetFont(BaseFont.HELVETICA_OBLIQUE, BaseFont.WINANSI, False)
Case 8
defaultFont = FontFactory.GetFont(BaseFont.TIMES_ROMAN, BaseFont.WINANSI, False)
Case 9
defaultFont = FontFactory.GetFont(BaseFont.TIMES_BOLD, BaseFont.WINANSI, False)
Case 10
defaultFont = FontFactory.GetFont(BaseFont.TIMES_ITALIC, BaseFont.WINANSI, False)
Case 11
defaultFont = FontFactory.GetFont(BaseFont.TIMES_BOLDITALIC, BaseFont.WINANSI, False)
Case 12
defaultFont = FontFactory.GetFont(BaseFont.SYMBOL, BaseFont.WINANSI, False)
Case 13
defaultFont = FontFactory.GetFont(BaseFont.ZAPFDINGBATS, BaseFont.WINANSI, False)
Case Else
Try
defaultFont = New iTextSharp.text.Font(FontFactory.GetFont(source_textCmbFont.Items(source_textCmbFont.SelectedIndex).ToString(), CSng(source_textCmbFontSize.Text), iTextSharp.text.Font.NORMAL, New BaseColor(source_textPicFontColor.BackColor)))
Catch exCreateFontEmbeded As Exception
Err.Clear()
End Try
End Select
defaultFont.Size = CSng(source_textCmbFontSize.Text)
defaultFont.Color = New BaseColor(source_textPicFontColor.BackColor)
Catch ex As Exception
Throw ex
End Try
If appearance_chkScaleRelativeToTargetPage.Checked Then
Dim scale As Single = appearance_ScalePercentUpDwn.Value / 100
Dim width As Single = pagesize.Width, height As Single = pagesize.Height
Dim fontSize As Integer = 1
Do While ((width * scale) >= (defaultFont.BaseFont.GetWidthPoint(source_textTxtText.Text, fontSize))) And ((height * scale) >= (defaultFont.BaseFont.GetAscentPoint(source_textTxtText.Text, fontSize) - defaultFont.BaseFont.GetDescentPoint(source_textTxtText.Text, fontSize)))
Try
fontSize += 1
defaultFont.Size = CSng(fontSize)
Catch ex As Exception
Throw ex
End Try
Loop
fontSize -= 1
defaultFont.Size = CSng(fontSize)
source_textCmbFontSize.Text = fontSize
End If
Select Case position_VDistAlign.SelectedIndex
Case 0
y = (pagesize.Top()) - ((defaultFont.BaseFont.GetAscentPoint(source_textTxtText.Text, CSng(source_textCmbFontSize.Text))) - (defaultFont.BaseFont.GetDescentPoint(source_textTxtText.Text, CSng(source_textCmbFontSize.Text))))
If position_VDistNumberUpDwn.Value <> 0 Then
y = y - convertToPixels(position_VDistNumberUpDwn.Value, position_VDistUnits.SelectedIndex)
End If
Case 1
y = ((pagesize.Top() + pagesize.Bottom()) / 2) - ((defaultFont.BaseFont.GetAscentPoint(source_textTxtText.Text, CSng(source_textCmbFontSize.Text)) - defaultFont.BaseFont.GetDescentPoint(source_textTxtText.Text, CSng(source_textCmbFontSize.Text))) / 2)
If position_VDistNumberUpDwn.Value <> 0 Then
y = y - convertToPixels(position_VDistNumberUpDwn.Value, position_VDistUnits.SelectedIndex)
End If
Case 2
y = (pagesize.Bottom())
If position_VDistNumberUpDwn.Value <> 0 Then
y = y - convertToPixels(position_VDistNumberUpDwn.Value, position_VDistUnits.SelectedIndex)
End If
End Select
Select Case position_HDistAlign.SelectedIndex
Case 0
x = ((pagesize.Left()) + convertToPixels((defaultFont.BaseFont.GetWidthPoint(source_textTxtText.Text, CSng(source_textCmbFontSize.Text))), convertUnits.Points) / 2.5)
If position_HDistNumberUpDwn.Value <> 0 Then
x = x - convertToPixels(position_HDistNumberUpDwn.Value, position_HDistUnits.SelectedIndex)
End If
Case 1
x = (pagesize.Left() + pagesize.Right()) / 2
If position_HDistNumberUpDwn.Value <> 0 Then
x = x - convertToPixels(position_HDistNumberUpDwn.Value, position_HDistUnits.SelectedIndex)
End If
Case 2
x = ((pagesize.Right()) - convertToPixels(defaultFont.BaseFont.GetWidthPoint(source_textTxtText.Text, CSng(source_textCmbFontSize.Text)), convertUnits.Points) / 2.5)
If position_HDistNumberUpDwn.Value <> 0 Then
x = x - convertToPixels(position_HDistNumberUpDwn.Value, position_HDistUnits.SelectedIndex)
End If
End Select
If appearance_radLocationOnTop.Checked Then
over = stamper.GetOverContent(i)
Else
over = stamper.GetUnderContent(i)
End If
over.SaveState()
over.SetGState(gs1)
Dim p As New iTextSharp.text.Phrase(source_textTxtText.Text, defaultFont)
ColumnText.ShowTextAligned(over, alignMent, p, x, y, appearance_rotationCustomUpDwn.Value)
Else
Dim bitmapWatermark As Bitmap = New Bitmap(picWatermark.Image.Clone(), CInt(picWatermark.Image.Width * CSng(source_fileAbsScale.Value / 100)), CInt(picWatermark.Image.Height * CSng(source_fileAbsScale.Value / 100)))
Dim imgWatermark As System.Drawing.Image = Nothing
If appearance_chkScaleRelativeToTargetPage.Checked Then
Dim scale As Single = appearance_ScalePercentUpDwn.Value / 100
Dim width As Single = pagesize.Width, height As Single = pagesize.Height
bitmapWatermark = ResizeImage(bitmapWatermark, New Drawing.Size(CInt(scale * width), CInt(scale * height)), True)
End If
If appearance_rotationCustomUpDwn.Value <> 0 Then
imgWatermark = RotateImg(bitmapWatermark, appearance_rotationCustomUpDwn.Value)
Else
imgWatermark = bitmapWatermark
End If
Dim img__1 As iTextSharp.text.Image = Nothing
Select Case Path.GetExtension(fnWatermark).ToString.TrimStart("."c)
Case "jpeg", "jpg"
img__1 = iTextSharp.text.Image.GetInstance(imgWatermark, System.Drawing.Imaging.ImageFormat.Jpeg)
Case "png"
img__1 = iTextSharp.text.Image.GetInstance(imgWatermark, System.Drawing.Imaging.ImageFormat.Png)
Case "gif"
img__1 = iTextSharp.text.Image.GetInstance(imgWatermark, System.Drawing.Imaging.ImageFormat.Gif)
Case "bmp"
img__1 = iTextSharp.text.Image.GetInstance(imgWatermark, System.Drawing.Imaging.ImageFormat.Bmp)
Case "tiff", "tif"
img__1 = iTextSharp.text.Image.GetInstance(imgWatermark, System.Drawing.Imaging.ImageFormat.Tiff)
End Select
If img__1 Is Nothing Then Return Nothing
Dim w As Single = img__1.ScaledWidth()
Dim h As Single = img__1.ScaledHeight()
Select Case position_VDistAlign.SelectedIndex
Case 0
y = (pagesize.Top()) - h / 2
If position_VDistNumberUpDwn.Value <> 0 Then
y = y - convertToPixels(position_VDistNumberUpDwn.Value, position_VDistUnits.SelectedIndex)
End If
Case 1
y = ((pagesize.Top() + pagesize.Bottom()) / 2)
If position_VDistNumberUpDwn.Value <> 0 Then
y = y - convertToPixels(position_VDistNumberUpDwn.Value, position_VDistUnits.SelectedIndex)
End If
Case 2
y = (pagesize.Bottom()) + h / 2
If position_VDistNumberUpDwn.Value <> 0 Then
y = y - convertToPixels(position_VDistNumberUpDwn.Value, position_VDistUnits.SelectedIndex)
End If
End Select
Select Case position_HDistAlign.SelectedIndex
Case 0
x = (pagesize.Left()) + w / 2
If position_HDistNumberUpDwn.Value <> 0 Then
x = x - convertToPixels(position_HDistNumberUpDwn.Value, position_HDistUnits.SelectedIndex)
End If
Case 1
x = (pagesize.Left() + pagesize.Right()) / 2
If position_HDistNumberUpDwn.Value <> 0 Then
x = x - convertToPixels(position_HDistNumberUpDwn.Value, position_HDistUnits.SelectedIndex)
End If
Case 2
x = (pagesize.Right()) - w / 2
If position_HDistNumberUpDwn.Value <> 0 Then
x = x - convertToPixels(position_HDistNumberUpDwn.Value, position_HDistUnits.SelectedIndex)
End If
End Select
If appearance_radLocationOnTop.Checked Then
over = stamper.GetOverContent(i)
Else
over = stamper.GetUnderContent(i)
End If
over.SaveState()
over.SetGState(gs1)
over.AddImage(img__1, w, 0, 0, h, x - (w / 2), y - (h / 2))
End If
over.RestoreState()
Next
stamper.Writer.CloseStream = False
stamper.Close()
reader.Close()
Return m.ToArray
End Function
Private Sub source_textCmbFontSize_TextChanged(sender As Object, e As EventArgs) Handles source_textCmbFontSize.TextChanged
displayPreviewDelay()
End Sub
End Class