28 Replies Latest reply: Sep 15, 2008 11:18 AM by (Leonard_Rosenthol) RSS

    How to save as "optimized" using IAC

      Dear all,

      I am trying to programmatically batch reduce the size of PDFs. Manually, I would go through File>Save as>Adobe PDF files, optimized. From reading the documenation I was expecting that I could achieve the same using (C# and IAC):

      pdDoc.Save((short)Acrobat.PDSaveFlags.PDSaveLinearized, newName);

      However, this doesn't reduce the size of the file at all. Could anyone point me in the right direction?

      Kind regards,

      Martin
        • 1. Re: How to save as "optimized" using IAC
          Community Member
          Linearization is the creating of a file that is "Fast Web View" enabled - it has nothing to do with file size reduction.

          You can use Acrobat's own PDF Optimizer via Acrobat's Batch feature all by itself - no scripting is necessary.

          Leonard
          • 2. Re: How to save as "optimized" using IAC
            Community Member
            Hi Leonard,

            Thanks a lot for the reply. I had a look at the batch processing feature. It might do the job, but not completely. I guess I should have provided more details about what I want to achieve.

            The idea is to create a tool that will create lores version(s) of one PDF or of all PDFs in a specific folder. This tool should integrate with the Windows context menu. So, I right-click on a folder, select the command and all hires PDFs in that folder (and its sub-folders) are converted to lores. Additionally, I would want this tool to come as an installable file. And finally, if the hires PDFs are zipped, the should be extracted prior to the conversion.

            If I want to use the built-in batch processing, there are two problems:
            - How can I create a batch sequence when installing the tool?
            - How can I call this sequence from the tool?

            It would seem much more straightforward, if I could just save a given PDF in its optimized version through the API.

            Cheers,

            Martin
            • 3. Re: How to save as "optimized" using IAC
              (Aandi_Inston) Community Member
              Also, you can only use Linearized if you use PDSaveFull, and there are
              other flags which you could usefully add.

              Aandi Inston
              • 4. Re: How to save as "optimized" using IAC
                Community Member
                All of your users will have Adobe Acrobat Professional installed, correct? Because the Acrobat SDK requires the presence of Acrobat - and PDF Optimizer is only in Professional.

                Assuming this is the case, then your best bet is to write a custom plugin that calls on PDF Optimizer with your settings as you wish them - and exposes some IAC/COM interface for your own tool to call on.

                Leonard
                • 5. Re: How to save as "optimized" using IAC
                  Community Member
                  Hi Leonard,

                  Thanks a lot for the hint. The users would indeed have Acrobat Professional on their machines. I'll look into the plugin and COM interface development, sounds a bit complicated, though.

                  Cheers,

                  Martin
                  • 6. Re: How to save as "optimized" using IAC
                    Community Member
                    If you are all on Acrobat Professional 8, another option would be to use the "Fix Up" functionality of Preflight to build the set of corrections/optimizations that you wish to apply. Make a "droplet" from that, and then call on the droplet from your external application.

                    Leonard
                    • 7. Re: How to save as "optimized" using IAC
                      Community Member
                      Dear all,

                      I am new to Acrobat and I am searching to compress PDF files too, for example by reducing quality level and using Jpeg2000 instead of Jpeg compression. I have read the suggestion below :

                      "You can use Acrobat's own PDF Optimizer via Acrobat's Batch feature all by itself - no scripting is necessary."

                      I wonder where this Acrobat's Batch feature could be found and in which document it is described. I also wonder if it is possible to call it as an external application from a VB6 program (with a Shell call), writing the save options like Jpeg2000 compression as parameters in a command line.

                      Best Regards,

                      Pascal
                      • 8. Re: How to save as "optimized" using IAC
                        (Aandi_Inston) Community Member
                        >I wonder where this Acrobat's Batch feature could be found

                        Advanced menu in Acrobat Professional; Advanced > Document Processing.

                        > and in which document it is described.

                        Acroabt help.

                        > I also wonder if it is possible to call it as an external application

                        No.


                        Aandi Inston
                        • 9. Re: How to save as "optimized" using IAC
                          Community Member
                          Hi Aandi,

                          Thank you for your answer.

                          I created a batch sequence that saves and compresses a file. I finally noticed that Jpeg compression generates a smaller file (but with a worse quality) than with Jpeg2000.

                          Now I wonder what would be the best way to compress a PDF file automatically :

                          - using API like AVDocSaveOptimized function (seen in acrobat_pdfl_api_reference.pdf file) in C/C++ ;

                          - using API like AVDocSaveOptimized in VB6 (it does not seem to be possible because I did not found this function in the Adobe Acrobat 8 Type Library) ;

                          - using API to open a file in Acrobat Pro and launch the batch sequence (if it is possible) ;

                          - using another solution ?

                          Best Regards,

                          Pascal
                          • 10. Re: How to save as "optimized" using IAC
                            Community Member
                            If you are willing to write a plugin in C/C++ to Acrobat, then you have access to all the same functionality as in the PDF Optimizer and can call it as you see fit.

                            Otherwise, you will be limited to manually launching Batch sequences.

                            Leonard
                            • 11. Re: How to save as "optimized" using IAC
                              Community Member
                              Hi Leonard,

                              Thank you for your answer.

                              There are just 2 points that are still unclear for me :

                              - is it possible to call an Acrobat plug-in from a VB6 program ? (I saw that a plug-in is an .api file rather than a .dll)

                              - do PDF Optimizer functions like AVDocSaveOptimized can be called from a C/C++ standalone application ?

                              Best Regards,

                              Pascal
                              • 12. Re: How to save as "optimized" using IAC
                                (Aandi_Inston) Community Member
                                >- is it possible to call an Acrobat plug-in from a VB6 program ? (I saw that a plug-in is an .api file rather than a .dll)

                                Not directly. A plug-in is loaded by Acrobat, and Acrobat allows the
                                plug-in to register menu items, tool buttons and so forth. So it is
                                Acrobat that calls the plug-in, always.

                                It is possible for an external program to ask Acrobat to run a menu
                                item, and so run part of a plug-in. But passing parameters is a
                                separate issue - for example you could put parameters into a file.
                                >
                                >- do PDF Optimizer functions like AVDocSaveOptimized can be called from a C/C++ standalone application ?

                                The Acrobat SDK cannot be used to make standalone applications:
                                Acrobat always does the work. The methods available to you are IAC,
                                which can be a standalone executable using Acrobat, and plug-ins,
                                which are part of Acrobat. AVDocSaveOptimized is only in the plug-in
                                API.


                                Aandi Inston
                                • 13. Re: How to save as "optimized" using IAC
                                  Community Member
                                  Hi folks,

                                  So, the way to go in this would be to create a plugin that saves a file as optimised. This plugin would register a menu item and this menu item could be called from the VB or C# app via Acrobat API. Right?

                                  In this scenario, would it still be possible to have a single installer file for the PDF optimiser tool? That is which also installs the Acrobat plugin?

                                  Kind regards,

                                  Martin
                                  • 14. Re: How to save as "optimized" using IAC
                                    (Aandi_Inston) Community Member
                                    > his plugin would register a menu item and this menu item could be called from the VB or C# app via Acrobat API. Right?

                                    This should work. You still have to consider what back door would be
                                    used to pass parameters, or whether you need parameters (for example
                                    it could just act on the currently open front document).
                                    >
                                    >In this scenario, would it still be possible to have a single installer file for the PDF optimiser tool? That is which also installs the Acrobat plugin?

                                    You mean, could you write an installer that installed an executable,
                                    and also a plug-in, given that someone has already purchased abnd
                                    installed Acrobat? I don't see why not, but bear in mind that the
                                    location for the Acrobat plug_ins folder is not fixed.

                                    Aandi Inston
                                    • 15. Re: How to save as "optimized" using IAC
                                      Hi I am reading the PDF files programmatically using C# , here I am suffering from the reading order in PDF files, as at the some places its column wise and some places its plain text read in line.

                                      I want to know if there is any the solution for the making the reading order correct in PDF files (it should be left to right and Top to bottom).

                                      Touchup text tool makes this task. Does anybody know how to get the touch up tools functionality though program using c# or VB.net?
                                      • 16. Re: How to save as "optimized" using IAC
                                        Community Member
                                        left->right, top->bottom is only true for Roman language documents. For Hebrew or Arabic, for example, it's right->left. For Chinese or Japanese, it may be top->bottom, left->right (for vertical text).

                                        If you are planning to do text extraction on PDF, there is a LOT of complexity involved...

                                        Leonard
                                        • 17. Re: How to save as "optimized" using IAC
                                          I'm lost on this, as well. How, programmatically, do you optimize? I will be running this process as an automated service, so menu items would be out of the question. It will be a .net windows service that scans a directory, appends the PDFs and optimizes them. How do I go about doing this?
                                          • 18. Re: How to save as "optimized" using IAC
                                            (Aandi_Inston) Community Member
                                            > I will be running this process as an automated service, so menu items would be out of the question.

                                            Actually, you can automate menu items. But if they display a dialog,
                                            that can't be suppressed.

                                            > It will be a .net windows service that scans a directory, appends the PDFs and optimizes them. How do I go about doing this?

                                            You don't do this with the Acrobat SDK, I don't think the license
                                            would allow it.

                                            Aandi Inston
                                            • 19. Re: How to save as "optimized" using IAC
                                              Community Member
                                              how do you automate menu items if not through the SDK? the CreateObject call invokes Acrobat.exe...
                                              • 20. Re: How to save as "optimized" using IAC
                                                Community Member
                                                I think you missed something, Stephen...

                                                Acrobat is NOT LICENSED (nor is it technically suitable) for use in an automated/unattached/service environment.
                                                • 21. Re: How to save as "optimized" using IAC
                                                  Community Member
                                                  I tried this:
                                                  Dim outDoc As CAcroPDDoc
                                                  Dim App As CAcroApp
                                                  Dim inDoc As CAcroPDDoc
                                                  Dim outputFileFullPath As String
                                                  Dim inputFileName As String
                                                  Dim i As Integer
                                                  'Dim fileNames(PDFfiles.Count - 1) As String
                                                  Dim xmlFileName As String
                                                  Dim w As String, x As String, y As String, z As String
                                                  Dim numInPages As Integer, numOutPages
                                                  Dim retval As Integer
                                                  Dim tempFileName As String
                                                  Dim xmlFile As String
                                                  Dim processAcrobat() As Process
                                                  Dim trialAttempts As Integer = 6
                                                  Dim triedAttempts As Integer
                                                  Dim finishedOptimization As Boolean
                                                  Dim killedAcrobat As Boolean
                                                  Dim BRetval As Boolean = False
                                                  Dim AcrobatJava As Object
                                                  'Dim pp As VariantType

                                                  'For i = 0 To PDFfiles.Count - 1
                                                  ' fileNames(i) = PDFfiles(i)
                                                  'Next i
                                                  If PDFfiles.Count = 0 Then
                                                  'FileSet.LogMessage("No files were found for this set. Output File was going to be " & outputFileName)
                                                  Exit Function
                                                  End If
                                                  tempFileName = Path.Combine(outputDir, "temp.pdf")

                                                  If PDFfiles.Count > 1 Then
                                                  Try
                                                  'outDoc.New(outputFileFullPath)
                                                  outDoc = CreateObject("AcroExch.PDDoc")
                                                  App = CreateObject("AcroExch.App")
                                                  App.Hide()
                                                  'outDoc.Open(outputFileName)
                                                  File.Copy(PDFfiles(0), PDFfiles(0) + ".tmp", True)
                                                  outDoc.Open(PDFfiles(0) + ".tmp")
                                                  For i = 1 To PDFfiles.Count - 1
                                                  inDoc = CreateObject("AcroExch.PDDoc")
                                                  inputFileName = PDFfiles(i)
                                                  retval = inDoc.Open(inputFileName)
                                                  'outDoc.Attach(inDoc)
                                                  'outDoc.InputCopyAll()
                                                  numOutPages = outDoc.GetNumPages() - 1
                                                  numInPages = inDoc.GetNumPages()

                                                  retval = outDoc.InsertPages(numOutPages, inDoc, 0, numInPages, -1)
                                                  numOutPages = outDoc.GetNumPages()

                                                  inDoc.Close()
                                                  inDoc = Nothing
                                                  'File.Copy(inputFileName, outputDir & "/" & CStr(i) & ".pdf")
                                                  Next i

                                                  'outDoc.Close()
                                                  AcrobatJava = outDoc.GetJSObject()

                                                  App.Hide()
                                                  Dim pp As Object = AcrobatJava.getPrintParams()
                                                  pp.fileName = "file://c/ftp_stream/example/myDoc.ps"
                                                  pp.printerName = ""
                                                  AcrobatJava.print(pp)
                                                  'App.Show()
                                                  'App = Nothing
                                                  AcrobatJava = Nothing
                                                  outDoc.Save(Acrobat.PDSaveFlags.PDSaveFull + Acrobat.PDSaveFlags.PDSaveCollectGarbage + Acrobat.PDSaveFlags.PDSaveLinearized, Path.Combine(outputDir, outputFileName)) 'Path.Combine(outputDir, "final.pdf"))

                                                  outDoc.Close()
                                                  outDoc = Nothing
                                                  BRetval = True
                                                  Catch ex As Exception
                                                  'FileSet.LogMessage("Error Message 1007: Error concatenating Pdf Files for inputfile " & inputFileName & " OutputFile=" & outputFileName & " Error is " & Err.Description)
                                                  BRetval = False
                                                  Finally
                                                  outDoc = Nothing
                                                  Do Until killedAcrobat Or triedAttempts = trialAttempts
                                                  Try
                                                  processAcrobat = System.Diagnostics.Process.GetProcessesByName("Acrobat")
                                                  For i = 0 To processAcrobat.GetLength(0) - 1
                                                  processAcrobat(i).Kill()
                                                  Next
                                                  triedAttempts = triedAttempts + 1
                                                  If File.Exists(inputFileName + ".temp") Then
                                                  File.Delete(inputFileName + ".temp")
                                                  System.Threading.Thread.Sleep(1000)
                                                  End If
                                                  killedAcrobat = True
                                                  Catch exException As Exception
                                                  killedAcrobat = False
                                                  End Try
                                                  Loop
                                                  End Try
                                                  Else
                                                  File.Copy(PDFfiles(0), outputFileName, True)
                                                  End If

                                                  When I get to "getPrintParams" it pops up a dialogue window. How do I get it to not do that?
                                                  • 22. Re: How to save as "optimized" using IAC
                                                    Community Member
                                                    You don't. It WILL do that.

                                                    Please re-read my message concerning Acrobat NOT being suitable (or legal) to be run in the environment you wish...

                                                    Leonard Rosenthol
                                                    Adobe Systems
                                                    • 23. Re: How to save as "optimized" using IAC
                                                      please provide a sample in c++ of how to call AVDocSaveOptimized specifying the header files, references, settings...
                                                      thanks
                                                      • 24. Re: How to save as "optimized" using IAC
                                                        (Aandi_Inston) Community Member
                                                        >please provide a sample in c++ of how to call AVDocSaveOptimized specifying the header files, references, settings...

                                                        Why? When you use the plug-in wizard it sets up the things you need.

                                                        Aandi Inston
                                                        • 25. Re: How to save as "optimized" using IAC
                                                          Community Member
                                                          In adobe there is a function by which you can right click on a group of files, and choose to concatenate them, with a menu option for making the file size smaller. I have tried to scour the documentation for a way to automate this, and have found nothing. Is this at all possible through the SDK?
                                                          • 26. Re: How to save as "optimized" using IAC
                                                            (Aandi_Inston) Community Member
                                                            There is no API directly to this "convert and combine" function. Using
                                                            JavaScript, I _think_ you can convert (may be wrong - might need a
                                                            plug-in). There are then various methods of combining.

                                                            Aandi Inston
                                                            • 27. Re: How to save as "optimized" using IAC
                                                              Community Member
                                                              What's odd is I found a way to combine the PDFs, then save as postscript then distill, but it takes 12 hours, mostly the PDF conversion. the combine and shrink takes about 5. I essentialyl just need a way to shrink the PDF without making postscript of it.