• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How to reduce/optimze PDF file size with VBA?

Guest
Sep 09, 2016 Sep 09, 2016

Copy link to clipboard

Copied

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?

TOPICS
Acrobat SDK and JavaScript

Views

9.9K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 09, 2016 Sep 09, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Sep 09, 2016 Sep 09, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 10, 2016 Sep 10, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Sep 10, 2016 Sep 10, 2016

Copy link to clipboard

Copied

Oh, it's just part of a code I copied from another post for saving PDF from VBA, not quite sure what the 1 means in the Call

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 10, 2016 Sep 10, 2016

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Sep 10, 2016 Sep 10, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 11, 2016 Sep 11, 2016

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 11, 2016 Sep 11, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Sep 11, 2016 Sep 11, 2016

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Employee ,
Sep 11, 2016 Sep 11, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Sep 11, 2016 Sep 11, 2016

Copy link to clipboard

Copied

If that is the case, what other function can I use to add text but not embed the font?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Employee ,
Sep 11, 2016 Sep 11, 2016

Copy link to clipboard

Copied

You cant’ – because embedded fonts is important.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Sep 11, 2016 Sep 11, 2016

Copy link to clipboard

Copied

Hmm, how do I get around that? when I add text and save it manually, it doesn't embed the font? (because it maintains the original size)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 11, 2016 Sep 11, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Sep 11, 2016 Sep 11, 2016

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 12, 2016 Sep 12, 2016

Copy link to clipboard

Copied

"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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Sep 12, 2016 Sep 12, 2016

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 12, 2016 Sep 12, 2016

Copy link to clipboard

Copied

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).

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Sep 12, 2016 Sep 12, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 12, 2016 Sep 12, 2016

Copy link to clipboard

Copied

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).

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Sep 12, 2016 Sep 12, 2016

Copy link to clipboard

Copied

LATEST

Yes I imagined that, though writing a plugin sounds quite complicated

If there's another, neater way to add text (other than addField or addWatermarkFromText) without embedding the font and without having to flatten the pages, I'd definitely prefer that over addField. Yes it's absolutely fine if the function uses one of the base 14 fonts

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines