Skip to main content
September 9, 2016
Question

How to reduce/optimze PDF file size with VBA?

  • September 9, 2016
  • 7 replies
  • 12326 views

I use this code to add Order# to an existing PDF

Function PDF_Numbering(order As String)

Dim acroApp As Acrobat.acroApp

Dim myDocument As Acrobat.AcroPDDoc

Dim jso As Object

Dim strPath As String

Dim strFileName As String

Dim intPages As Integer

Dim i As Integer

Set acroApp = CreateObject("AcroExch.App")

Set myDocument = CreateObject("AcroExch.PDDOc")

strPath = "C:\Users\Daniel\Downloads\"

strFileName = order & ".pdf"

'Open file and load JSObject

Set myDocument = CreateObject("AcroExch.PDDOc")

myDocument.Open (strPath & strFileName)

Set jso = myDocument.GetJSObject

' get number of pages

intPages = myDocument.GetNumPages

'Write page number

    jso.addWatermarkFromText _

        cText:=order & "  ", _

        nFontSize:=14, _

        nTextAlign:=1, _

        nHorizAlign:=2, _

        nVertAlign:=1, _

        nVertValue:=-30, _

'Save document

Call myDocument.Save(1, strPath & strFileName)

'Clean up

Set jso = Nothing

Call acroApp.CloseAllDocs

Set myDocument = Nothing

Call acroApp.Exit

Set acroApp = Nothing

However, the modified PDF is 15 times larger in size than the original PDF (original 60kb, modified 800kb)

Why is this happening and how do I correct this?

This topic has been closed for replies.

7 replies

Legend
September 12, 2016

1) There are a number of ways to add text including form fields and comments of various types. Someone may have a favourite. In any case you don't seem to be trying to match the original font in your call - just using the default?

2) I thought we might be heading here. Although Optimized PDF sits under File > Save As, it isn't a Save function, but a separate one. I don't know of an interface to it unless you write a plug-in (but equally I wouldn't be surprised to find there was a way).

September 12, 2016

I found there is a function called AVDocSaveOptimized though not quite sure how to use it

After some time researching other people's questions I found this solution:

https://www.experts-exchange.com/questions/21416303/Add-text-to-pdf-file-from-vb.html#answer13971818

It uses the addField function you suggested, which doesn't embed font but creates a backround & border, but that is fixed using flattenPages

That way, I got the same output size as I got with manually adding text

That is a nastier solution than adding just plain text but it got me going

I guess I'll stay with that solution unless we find a better way

Karl Heinz  Kremer
Community Expert
Community Expert
September 12, 2016

AVDocSaveOptimized is a function form the plug-in API, which can only be used if you are actually creating a plug-in (written in C or C++).

This is one of the ways to add text to a PDF document. Keep in mind that that the flattenPages() method will flatten all interactive elements on the pages you apply it to. This will also embed the font that is used for the form field, unless you use on of the "base 14" fonts (Helvetica, Times Roman, Courier, Symbol, Zapf Dingbats and their variations).

Legend
September 12, 2016

"Do you mean, run the code on the original file (60kb), making it bigger (800kb), then open it manually, add text manually, save manually, and tell you the output size?"

Almost. Take out the "add text manually". And for "save manually" I mean "save in the various ways you know about in the user interface". The aim here is to do the add watermark and nothing else, then see if you can reduce the space manually. We could spend a lot of your time on saving/optimizing the file in code, only to find it doesn't help.

September 12, 2016

I find that saving the PDF as follows:

File > Save As Other... > Optimized PDF... > OK

Reduces the modified file size back to the original (from 800kb back to 60kb)

image.png

The PDF Optimizer window does show the font was embedded after running the code

So the question now:

1) Is there any function other than addWatermarkFromText that I can use to add text, without it embedding the font? Or else,

2) How do I Save As Other > Optimzed PDF?

Legend
September 11, 2016

That wasn't the test I asked you to try.

September 11, 2016

Do you mean, run the code on the original file (60kb), making it bigger (800kb), then open it manually, add text manually, save manually, and tell you the output size?

Legend
September 11, 2016

Here are the constants:

PDSaveFlags
enum, 7 members.  'Constants for PDDoc save flags.
Public Enum PDSaveFlags

  'Write changes only.
  PDSaveIncremental = 0  '&H0

  'Write the entire file.
  PDSaveFull = 1  '&H1

  'Write a copy of the file into the file.
  PDSaveCopy = 2  '&H2

  'Save the file in a linearized fashion.
  PDSaveLinearized = 4  '&H4

  'Writes a PostScript header as part of the saved file.
  PDSaveWithPSHeader = 8  '&H8

  'Specifies that it's OK to store in binary file.
  PDSaveBinaryOK = 16  '&H10

  'Remove unreferenced objects, often reducing file size.
  PDSaveCollectGarbage = 32  '&H20
End Enum

September 11, 2016

I just ran the code 7 times (each time with a different flag value 0/1/2/4/8/16/32) and all resulted in about the same file size 800kb

Maybe it is the addWatermarkFromText function itself that increases the size?

lrosenth
Adobe Employee
Adobe Employee
September 11, 2016

I expect that it IS that function, because it embeds the font

Legend
September 11, 2016

Not sure I follow your last sentence. I'm asking specifically whether you can take a file prepared with the JavaScript addWatermarkFromText operation as in your code, making a big file; open it THEN save it manually, and have the file go back to the original size?

Legend
September 10, 2016

Ok, you should refer to the SDK to find the meaning of the flags. Some values will make bigger files than others.

I assume you've already checked that manually saving the file makes it smaller? Not much point automating it if you can't do it manually.

September 10, 2016

I found this:

Acrobat DC SDK Documentation

Acrobat DC SDK Documentation

But it doesn't indicate what the "1" stands for

Yes, I just edited and saved the PDF manually and the original size was kept

Legend
September 9, 2016

What are your PDSaveFlags? I don't know the numeric equivalents.

September 10, 2016

How do I find out what my PDSaveFlags are? I'm new to the Acrobat SDK

Legend
September 10, 2016

It's the first parameter passed to the Save. You chose "1".  How did you choose it?