-
Notifications
You must be signed in to change notification settings - Fork 39
/
CompressAllTechniques.vb
101 lines (82 loc) · 4.18 KB
/
CompressAllTechniques.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
Imports System
Imports System.Diagnostics
Imports System.IO
Imports System.Windows.Forms
Imports Microsoft.VisualBasic
Imports BitMiracle.Docotic.Pdf
Namespace BitMiracle.Docotic.Pdf.Samples
Public NotInheritable Class CompressAllTechniques
Public Shared Sub Main()
' NOTE:
' When used in trial mode, the library imposes some restrictions.
' Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
' for more information.
Const originalFile As String = "Sample Data\jpeg.pdf"
Const compressedFile As String = "CompressAllTechniques.pdf"
Using pdf As New PdfDocument(originalFile)
' 1. Recompress images
For Each page As PdfPage In pdf.Pages
For Each painted As PdfPaintedImage In page.GetPaintedImages()
Dim image As PdfImage = painted.Image
' image that is used as mask or image with attached mask are
' not good candidates for recompression
If image.IsMask OrElse image.Mask IsNot Nothing OrElse image.Width < 8 OrElse image.Height < 8 Then
Continue For
End If
' get size of the painted image
Dim width As Integer = Math.Max(1, CInt(painted.Bounds.Width))
Dim height As Integer = Math.Max(1, CInt(painted.Bounds.Height))
If image.ComponentCount = 1 AndAlso image.BitsPerComponent = 1 AndAlso image.Compression <> PdfImageCompression.Group4Fax Then
image.RecompressWithGroup4Fax()
ElseIf width < image.Width OrElse height < image.Height Then
' NOTE: PdfImage.ResizeTo() method is not supported in version for .NET Standard
If image.ComponentCount = 3 Then
image.ResizeTo(width, height, PdfImageCompression.Jpeg, 90)
Else
image.ResizeTo(width, height, PdfImageCompression.Flate, 9)
End If
End If
Next
Next
' 2 Setup save options
pdf.SaveOptions.Compression = PdfCompression.Flate
pdf.SaveOptions.UseObjectStreams = True
pdf.SaveOptions.RemoveUnusedObjects = True
pdf.SaveOptions.OptimizeIndirectObjects = True
pdf.SaveOptions.WriteWithoutFormatting = True
' 3. Remove structure information
pdf.RemoveStructureInformation()
' 4. Flatten form fields
' Controls become uneditable after that
pdf.FlattenControls()
' 5. Clear metadata
pdf.Metadata.Basic.Clear()
pdf.Metadata.DublinCore.Clear()
pdf.Metadata.MediaManagement.Clear()
pdf.Metadata.Pdf.Clear()
pdf.Metadata.RightsManagement.Clear()
pdf.Metadata.[Custom].Properties.Clear()
For Each schema As XmpSchema In pdf.Metadata.Schemas
schema.Properties.Clear()
Next
pdf.Info.Clear(False)
' 6. Unembed fonts
For Each font As PdfFont In pdf.GetFonts()
' Only unembed popular fonts installed in the typical OS. You can extend
' the list of such fonts in the "if" statement below.
If font.Name = "Arial" OrElse font.Name = "Verdana" Then
font.Unembed()
End If
Next
pdf.Save(compressedFile)
End Using
Dim message As String = String.Format(
"Original file size: {0} bytes;" & vbCr & vbLf & "Compressed file size: {1} bytes",
New FileInfo(originalFile).Length,
New FileInfo(compressedFile).Length
)
MessageBox.Show(message)
Process.Start(compressedFile)
End Sub
End Class
End Namespace