How to reduce PDF file size in VBA?

mintz

Board Regular
Joined
Aug 5, 2015
Messages
129
I currently use this code to add Order# to an existing PDF
Code:
[COLOR=#101094][FONT=Consolas]Function[/FONT][/COLOR][COLOR=#303336][FONT=Consolas] PDF_Numbering[/FONT][/COLOR][COLOR=#303336][FONT=Consolas]([/FONT][/COLOR][COLOR=#303336][FONT=Consolas]order [/FONT][/COLOR][COLOR=#101094][FONT=Consolas]As[/FONT][/COLOR][COLOR=#303336][FONT=Consolas] [/FONT][/COLOR][COLOR=#101094][FONT=Consolas]String[/FONT][/COLOR][COLOR=#303336][FONT=Consolas])[/FONT][/COLOR]
<code style="margin: **** padding: **** border: **** font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; white-space: inherit;">[COLOR=#303336]
[/COLOR][COLOR=#101094]Dim[/COLOR][COLOR=#303336] acroApp [/COLOR][COLOR=#101094]As[/COLOR][COLOR=#303336] Acrobat[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]acroApp
[/COLOR][COLOR=#101094]Dim[/COLOR][COLOR=#303336] myDocument [/COLOR][COLOR=#101094]As[/COLOR][COLOR=#303336] Acrobat[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]AcroPDDoc
[/COLOR][COLOR=#101094]Dim[/COLOR][COLOR=#303336] jso [/COLOR][COLOR=#101094]As[/COLOR][COLOR=#303336] [/COLOR][COLOR=#101094]Object[/COLOR][COLOR=#303336]

[/COLOR][COLOR=#101094]Dim[/COLOR][COLOR=#303336] strPath [/COLOR][COLOR=#101094]As[/COLOR][COLOR=#303336] [/COLOR][COLOR=#101094]String[/COLOR][COLOR=#303336]
[/COLOR][COLOR=#101094]Dim[/COLOR][COLOR=#303336] strFileName [/COLOR][COLOR=#101094]As[/COLOR][COLOR=#303336] [/COLOR][COLOR=#101094]String[/COLOR][COLOR=#303336]
[/COLOR][COLOR=#101094]Dim[/COLOR][COLOR=#303336] intPages [/COLOR][COLOR=#101094]As[/COLOR][COLOR=#303336] [/COLOR][COLOR=#101094]Integer[/COLOR][COLOR=#303336]
[/COLOR][COLOR=#101094]Dim[/COLOR][COLOR=#303336] i [/COLOR][COLOR=#101094]As[/COLOR][COLOR=#303336] [/COLOR][COLOR=#101094]Integer[/COLOR][COLOR=#303336]

[/COLOR][COLOR=#101094]Set[/COLOR][COLOR=#303336] acroApp [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] CreateObject[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"AcroExch.App"[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
[/COLOR][COLOR=#101094]Set[/COLOR][COLOR=#303336] myDocument [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] CreateObject[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"AcroExch.PDDOc"[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]

strPath [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] [/COLOR][COLOR=#7D2727]"C:\Users\Daniel\Downloads\"[/COLOR][COLOR=#303336]
strFileName [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] order [/COLOR][COLOR=#303336]&[/COLOR][COLOR=#303336] [/COLOR][COLOR=#7D2727]".pdf"[/COLOR][COLOR=#303336]

[/COLOR][COLOR=#858C93]'Open file and load JSObject[/COLOR][COLOR=#303336]
[/COLOR][COLOR=#101094]Set[/COLOR][COLOR=#303336] myDocument [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] CreateObject[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"AcroExch.PDDOc"[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
myDocument[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Open [/COLOR][COLOR=#303336]([/COLOR][COLOR=#303336]strPath [/COLOR][COLOR=#303336]&[/COLOR][COLOR=#303336] strFileName[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
[/COLOR][COLOR=#101094]Set[/COLOR][COLOR=#303336] jso [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] myDocument[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]GetJSObject

[/COLOR][COLOR=#858C93]' get number of pages[/COLOR][COLOR=#303336]
intPages [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] myDocument[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]GetNumPages

[/COLOR][COLOR=#858C93]'Write page number[/COLOR][COLOR=#303336]
    jso[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]addWatermarkFromText _
        cText[/COLOR][COLOR=#303336]:=[/COLOR][COLOR=#303336]order [/COLOR][COLOR=#303336]&[/COLOR][COLOR=#303336] [/COLOR][COLOR=#7D2727]"  "[/COLOR][COLOR=#303336],[/COLOR][COLOR=#303336] _
        nFontSize[/COLOR][COLOR=#303336]:=[/COLOR][COLOR=#7D2727]14[/COLOR][COLOR=#303336],[/COLOR][COLOR=#303336] _
        nTextAlign[/COLOR][COLOR=#303336]:=[/COLOR][COLOR=#7D2727]1[/COLOR][COLOR=#303336],[/COLOR][COLOR=#303336] _
        nHorizAlign[/COLOR][COLOR=#303336]:=[/COLOR][COLOR=#7D2727]2[/COLOR][COLOR=#303336],[/COLOR][COLOR=#303336] _
        nVertAlign[/COLOR][COLOR=#303336]:=[/COLOR][COLOR=#7D2727]1[/COLOR][COLOR=#303336],[/COLOR][COLOR=#303336] _
        nVertValue[/COLOR][COLOR=#303336]:=-[/COLOR][COLOR=#7D2727]30[/COLOR][COLOR=#303336],[/COLOR][COLOR=#303336] _

[/COLOR][COLOR=#858C93]'Save document[/COLOR][COLOR=#303336]
[/COLOR][COLOR=#101094]Call[/COLOR][COLOR=#303336] myDocument[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Save[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]1[/COLOR][COLOR=#303336],[/COLOR][COLOR=#303336] strPath [/COLOR][COLOR=#303336]&[/COLOR][COLOR=#303336] strFileName[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]

[/COLOR][COLOR=#858C93]'Clean up[/COLOR][COLOR=#303336]
[/COLOR][COLOR=#101094]Set[/COLOR][COLOR=#303336] jso [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] [/COLOR][COLOR=#7D2727]Nothing[/COLOR][COLOR=#303336]
[/COLOR][COLOR=#101094]Call[/COLOR][COLOR=#303336] acroApp[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]CloseAllDocs
[/COLOR][COLOR=#101094]Set[/COLOR][COLOR=#303336] myDocument [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] [/COLOR][COLOR=#7D2727]Nothing[/COLOR][COLOR=#303336]
[/COLOR][COLOR=#101094]Call[/COLOR][COLOR=#303336] acroApp[/COLOR][COLOR=#303336].[/COLOR][COLOR=#101094]Exit[/COLOR][COLOR=#303336]
[/COLOR][COLOR=#101094]Set[/COLOR][COLOR=#303336] acroApp [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] [/COLOR][COLOR=#7D2727]Nothing[/COLOR][COLOR=#303336]
 [/COLOR]</code>[COLOR=#101094][FONT=Consolas]End[/FONT][/COLOR][COLOR=#303336][FONT=Consolas] [/FONT][/COLOR][COLOR=#101094][FONT=Consolas]Function[/FONT][/COLOR]


However, the modified PDF is 15 times larger in size than the original PDF (original 60kb, modified 800kb)
How do I change the save properties in the <code style="margin: **** padding: 1px 5px; border: **** font-size: 13px; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; white-space: pre-wrap; background-color: rgb(239, 240, 241);">Call myDocument.Save(1, strPath & strFileName)</code> so that it doesn't increase the original file size?
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
when you look at the first files properties before update and then the same files properties after the update and note the differences. I would suspect partly excel overhead but also may print size dpi, and file target
 
Upvote 0
when you look at the first files properties before update and then the same files properties after the update and note the differences. I would suspect partly excel overhead but also may print size dpi, and file target
Yes I also thought the same, then how do I change the save properties?
 
Upvote 0
if we know which is the cause for enlargement then we can identify the tweaks that maybe needed. I don't know off hand
 
Upvote 0
Ok, problem solved completely
The addWaterMarkFromText function actually embeds the font in the output PDF
I found the addField function doesn't embed fonts
That way the original size was kept after adding the text using addField
Thank you for guiding me through :)
 
Upvote 0

Forum statistics

Threads
1,218,183
Messages
6,140,977
Members
450,326
Latest member
KarugaEdwin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top