Author Topic: Comments Flattener  (Read 41 times)

0 Members and 1 Guest are viewing this topic.

nightslayer23

  • Newbie
  • *
  • Posts: 52
Comments Flattener
« on: September 13, 2019, 09:18:40 AM »
Could you translate a custom script for flattening all annotations, highlights and comments?

We've found these often drop off at the print level.

We have a current java script that does it perfect, but you have to do it file - by - file.

Be great if there was a way to do this as a batch.

the program that the script belongs in is: https://www.uvsar.com/projects/acrobat/flattener/

RTT

  • Administrator
  • *****
  • Posts: 829
Re: Comments Flattener
« Reply #1 on: September 14, 2019, 01:06:44 AM »
No internal code to do this, but here is a script to automate Acrobat Professional to do it.
Code: [Select]
var objShell = new ActiveXObject("shell.application");
var objFolder;
objFolder = objShell.BrowseForFolder(0, "Select the destination folder", 0x10, 0x11);

if (objFolder != null) {
    var DestFolder = objFolder.self.path;
    var fso = new ActiveXObject("Scripting.FileSystemObject");
    var app = pdfe.CreateObject("AcroExch.App");
    var baseDoc = pdfe.CreateObject("AcroExch.PDDoc");
    var ProgressBar = pdfe.ProgressBar;
    ProgressBar.max = pdfe.SelectedFiles.Count;

    for (var i = 0; i < pdfe.SelectedFiles.Count; i++) {
        try {
            FullPathFilename = pdfe.SelectedFiles(i).Filename;
            var Filename = FullPathFilename.substr(FullPathFilename.lastIndexOf('\\') + 1);
            var DestFullPathFilename = fso.BuildPath(DestFolder, Filename);

            if (FullPathFilename == DestFullPathFilename) {
                DestFullPathFilename = FullPathFilename.substr(0, FullPathFilename.lastIndexOf('.') - 1) + '_flattened.pdf';
            }

            pdfe.echo('Flattening: ' + FullPathFilename);
            ProgressBar.position = i + 1;

            baseDoc.Open(FullPathFilename);

            try {
                var JSObject = baseDoc.getJSObject();
                JSObject.addScript("flatten", "function flatten(){this.flattenPages();}flatten();");
                JSObject.removeScript("flatten");
                baseDoc.Save(1, DestFullPathFilename);
                pdfe.echo(" [OK]", 0, 1);
            } catch (err) {
                pdfe.echo(err, 0xFF0000);
                pdfe.echo("");
            }
            baseDoc.close();
        } catch (err) {
            pdfe.echo(err, 0xFF0000);
            pdfe.echo("");
        }
    }
    app.Exit();
    pdfe.echo('\nDone');
}

nightslayer23

  • Newbie
  • *
  • Posts: 52
Re: Comments Flattener
« Reply #2 on: September 16, 2019, 12:06:11 AM »
so this script is to be installed into acrobat correct?

this isn't a script we could add to the PDF Shell Tools context menu?


never-mind! I see it works from the context menu! fantastic..

what do I need to take out of this script so it is a save over original file ? I'd like to add the script to a super script that I have that checks size & orientation but flattens comments at the same time (which I've already put together). same with a version that checks size, orientation & coverage, would need it to flatten prior to checking for coverage.

RTT

  • Administrator
  • *****
  • Posts: 829
Re: Comments Flattener
« Reply #3 on: September 16, 2019, 12:17:15 AM »
so this script is to be installed into acrobat correct?
No. This is a PDF-ShellTools script, to run from the PDF-ShellTools scripts interface, so could be added to the PDF-ShellTools context menu. It just needs Acrobat Professional to be installed, as it uses it to do the work.

nightslayer23

  • Newbie
  • *
  • Posts: 52
Re: Comments Flattener
« Reply #4 on: September 16, 2019, 12:35:05 AM »
yes I worked out how to use it just after I posted.

please see my last post for other enquiry

RTT

  • Administrator
  • *****
  • Posts: 829
Re: Comments Flattener
« Reply #5 on: September 17, 2019, 01:06:22 AM »
what do I need to take out of this script so it is a save over original file ?
This should do it.
Code: [Select]
var app = pdfe.CreateObject("AcroExch.App");
var baseDoc = pdfe.CreateObject("AcroExch.PDDoc");

var ProgressBar = pdfe.ProgressBar;
ProgressBar.max = pdfe.SelectedFiles.Count;

for (var i = 0; i < pdfe.SelectedFiles.Count; i++) {
    try {
        ProgressBar.position = i + 1;
        var FileObj = pdfe.SelectedFiles(i);
        var FullPathFilename = FileObj.Filename;
        FileObj.Close();

        pdfe.echo('Flattening: ' + FullPathFilename);

        baseDoc.Open(FullPathFilename);

        try {
            var JSObject = baseDoc.getJSObject();
            JSObject.addScript("flatten", "function flatten(){this.flattenPages();}flatten();");
            JSObject.removeScript("flatten");
            baseDoc.Save(1, FullPathFilename);
            pdfe.echo(" [OK]", 0, 1);
        } catch (err) {
            pdfe.echo(err, 0xFF0000);
            pdfe.echo("");
        }
        baseDoc.Close();
    } catch (err) {
        pdfe.echo(err, 0xFF0000);
        pdfe.echo("");
    }
}
app.Exit();
pdfe.echo('\nDone');