6 Replies Latest reply on Feb 4, 2017 7:05 AM by Karl Heinz Kremer

    Optimizing PDF through VB .net application

    parthasarathi.adhikary

      I am working with a VB .net application which actually saves selected PDF to PS and then using distiller it saves the PS file back to PDF which is "optimized" and smaller in size. Following is the code I am using [running in a loop for all files in a directory]:

       

       

                   ''Kill Acrobat Process
                   ''Kill Distiller Process

                  Dim pdfFilePath As String = "E:/TestPDF/abcd.pdf"
                  Dim convSplitedPage As Acrobat.AcroPDDoc = New Acrobat.AcroPDDoc

                  convSplitedPage.Open(pdfFilePath)

                  Dim Converter As New Object

                  Converter = convSplitedPage.GetJSObject()

                 Converter.saveAs(System.IO.Path.GetDirectoryName(pdfFilePath) & "\" & System.IO.Path.GetFileNameWithoutExtension(pdfFilePath) & ".ps", "com.adobe.acrobat.ps")

                  convSplitedPage.Close()

                  System.IO.File.Delete(System.IO.Path.GetDirectoryName(pdfFilePath) & "\" & System.IO.Path.GetFileNameWithoutExtension(pdfFilePath) & ".pdf")

                  Dim pdf As PdfDistiller = New PdfDistiller

                  pdf.FileToPDF(System.IO.Path.GetDirectoryName(pdfFilePath) & "\" & System.IO.Path.GetFileNameWithoutExtension(pdfFilePath) & ".ps", System.IO.Path.GetDirectoryName(pdfFilePath) & "\" & System.IO.Path.GetFileNameWithoutExtension(pdfFilePath) & ".pdf", "Smallest File Size")

                  System.IO.File.Delete(System.IO.Path.GetDirectoryName(pdfFilePath) & "\" & System.IO.Path.GetFileNameWithoutExtension(pdfFilePath) & ".ps")

       

      When runnning this from the Visual studio I can successfully generate the optimized PDF, But when the application is deployed to "C:\Program Files", I am getting various issues. More over the issues are intermittent and can be replicated from Visual Studio also. The issues are as below:

       

      1. Remote Procedure Call Failed: when executing Converter.SaveAs().

       

      Am I doing anything wrong. Please note the issue is not occuring everytime.
      Please suggest.

        • 1. Re: Optimizing PDF through VB .net application
          Karl Heinz Kremer Adobe Community Professional

          What you are doing (PDF->PS->PDF - something usually referred to as "refrying" the PDF) is not optimizing a PDF document. Yes, you may end up with a smaller file, that that is not because it's optimized, it's because you've eliminated information that should be part of a well made PDF file. You end up with a lower quality PDF file. Take a look here for some background information about what you are doing by refrying the PDF: http://www.gwg.org/wp-content/uploads/RefryingPDF.pdf

           

          Regarding your problems, the first thing I would check is to see if the files that are causing problems are protected. If they are, you will not be able to refry them.

          1 person found this helpful
          • 2. Re: Optimizing PDF through VB .net application
            parthasarathi.adhikary Level 1

            Firstly thank you very much for your valuable input and the article on "Refrying".

             

            Actually we are building a dot net application that splits pdf pages and store them into a specific disk path. Now for some PDF files the extracted pages [saved as PDF] are of very large size which actually reduces our disk space and also affecting user experience when showing them through a viewer. When these PDF files are optimized as stated in my previous post, the extracted pages size get reduced. Also the quality is not heavily compromised.

             

            However I can see that "Refrying" is not the best practice and somehow I need to get rid of it. Thanks for your input. Can you please let me know How I can optimize PDF using Javascript API? Also I need to determine the PDFs which are not optimized one, can I detect that from Javascript API? The process need to run on background.

            • 3. Re: Optimizing PDF through VB .net application
              Test Screen Name Most Valuable Participant

              "Optimzed" isn't an on/off state you can test for.

              • 4. Re: Optimizing PDF through VB .net application
                Karl Heinz Kremer Adobe Community Professional

                You need to find out what is causing these large files. You can do that with Adobe Acrobat Pro when you select to save as an optimized PDF file, and then select the "Audit Space Usage" button in the upper right corner:

                 

                2017-01-31_09-33-39.png

                 

                The results will tell you what you need to do to shrink the file size. This could for example be to subset embed all fonts, or to downsample images or whatever else is necessary to get to a more appropriate file size. Once you know what it is, you can then look for mechanisms to do that in your programming environment.

                • 5. Re: Optimizing PDF through VB .net application
                  parthasarathi.adhikary Level 1

                  Thanks for your input Karl.

                  I done some further investigation on the original PDF and the splitted PDF [each page].
                  I checked for that and it reveals that 99% of space is used for the images in pdf. The initial file was of 30 MB and when extracted to the pages all the pages [there are 2800 pages] are of 22 MB of size. That is why I am running out of space when splitting the PDF into pages. I think there are some common images in every page [company log] that is very large in size. In the original PDF somehow acrobat refers the same image in all pages that is why it was of 30 MB, But when splitted into pages all the page have the common image and hence they are altogether bigger in size than of the original file. I need to somehow compress the logo to get size reduced in individual pages by using acrobat JS API.

                   

                  Please let me know your thoughts on this, if I am going in correct direction.

                  • 6. Re: Optimizing PDF through VB .net application
                    Karl Heinz Kremer Adobe Community Professional

                    That is very likely the case. You can now try to find out what the size of that image is by using the Object Inspector in the Output Preview tool. You can then create a custom Preflight profile to downsample these images (or if you find an existing profile that works for you, use that). Preflight can be automated via JavaScript, so you can do that from within your VB application as well - either by trying to use the preflight tool via the JSObject directly, or by adding a folder level script to Acrobat that you can then call via the JSObject (chances are that the latter solution is easier to implement).