Skip to main content
Participant
October 23, 2023
Answered

Script to change line weight and drawing order in PDFs

  • October 23, 2023
  • 2 replies
  • 702 views

Hello everyone. I post here in the hope that someone can help me with a script.

I have a number of PDFs to process and would like to take advantage of illustrator's scripting capabilities to automate certain operations that I currently have to perform manually for each file, but I'm a novice and the script I've tried doesn't work! 

 

The operations I do are not particularly complex and I believe it could be effectively automated, currently I have to:

-open the pdf contained in a certain folder

-select a line with a certain thickness (0.24pt)

-select same stroke weight

-switch all selected lines to 0.14pt.

-select a line with stroke colour rgb 0,0,0

-arrangement>bring to front.

-select a line with stroke rgb 191,0,0

-arrangement>bring to front

-save and go to the next pdf.

 

Here is the script I tried to implement.

When charged, it correctly prompts the user for folder selection. Then, even if the folder contains only one file, Illustrator gets stuck "thinking" for several minutes.

 

// Choose a folder which contains PDF documents
var folder = Folder.selectDialog("Select folder");

if (folder != null) {
    var pdfFiles = folder.getFiles("*.pdf");

    for (var i = 0; i < pdfFiles.length; i++) {
        var pdfFile = pdfFiles[i];
        var doc = app.open(pdfFile);

        // Select all lines and apply modifications if conditions are met
        var lines = doc.pathItems;
        for (var j = 0; j < lines.length; j++) {
            var line = lines[j];
            if (line.stroked && line.strokeWidth == 0.24) {
                line.strokeWidth = 0.14;
            }
            var rgbColor = line.strokeColor;
            if (rgbColor.red == 191 && rgbColor.green == 0 && rgbColor.blue == 0) {
                line.zOrder(ZOrderMethod.BRINGTOFRONT);
            } else if (rgbColor.red == 0 && rgbColor.green == 0 && rgbColor.blue == 0) {
                line.zOrder(ZOrderMethod.BRINGTOFRONT);
            }
        }

        // Save modified PDF
        var saveOptions = new PDFSaveOptions();
        var saveFile = new File(pdfFile.path + "/" + pdfFile.name.replace(".pdf", "_modified.pdf"));
        doc.saveAs(saveFile, saveOptions);
        doc.close(SaveOptions.DONOTSAVECHANGES);
    }

    alert("Operation completed.");
} else {
    alert("No folder selected. The script will be interrupted");
}

 

Does anyone have any idea what the problem might be?

Thanks in advance for your support

This topic has been closed for replies.
Correct answer Sergey Osokin

The operations are really simple, but you've discovered the secret that you have hundreds of thousands of paths in the file. Oh my goodness! 🙂 It's damn hard for scripts to process such documents. The best you can do is to use native commands to select similar objects by attribute. What @Monika Gause  described as an action. The same Select Same, Bring To Front commands can be implemented as a script. But even with native commands, I can see a noticeable processing delay on a test file with 14k objects. It's hard for me to imagine how long a file with 260k paths will take to process.

main();

function main() {
    var color1 = setRGBColor([191, 0, 0]);
    var color2 = setRGBColor([0, 0, 0]);

    // Choose a folder which contains PDF documents
    var folder = Folder.selectDialog("Select folder");
    
    if (folder != null) {
        var pdfFiles = folder.getFiles("*.pdf");
    
        for (var i = 0; i < pdfFiles.length; i++) {
            var pdfFile = pdfFiles[i];
            var doc = app.open(pdfFile);
    
            changeStrokeWidth(doc, 0.24, 0.14);
            app.executeMenuCommand("deselectall");
            moveToFront(doc, color1);
            app.executeMenuCommand("deselectall");
            moveToFront(doc, color2);
    
            // Save modified PDF
            var saveOptions = new PDFSaveOptions();
            var saveFile = new File(pdfFile.path + "/" + pdfFile.name.replace(".pdf", "_modified.pdf"));
            doc.saveAs(saveFile, saveOptions);
            doc.close(SaveOptions.DONOTSAVECHANGES);
        }
    
        alert("Operation completed.");
    } else {
        alert("No folder selected. The script will be interrupted");
    }
}

function setRGBColor(rgb) {
    var c = new RGBColor();
    c.red = rgb[0];
    c.green = rgb[1];
    c.blue = rgb[2];
    return c;
}

function changeStrokeWidth(doc, oldVal, newVal) {
    doc.defaultStrokeWidth = oldVal;
    app.executeMenuCommand("Find Stroke Weight menu item");
    doc.defaultStrokeWidth = newVal;
}

function moveToFront(doc, color) {
    doc.defaultStrokeColor = color;
    app.executeMenuCommand("Find Stroke Color menu item");
    app.executeMenuCommand("sendToFront");
}

 

2 replies

Sergey Osokin
Inspiring
October 23, 2023

At first glance, there is no problem. But I used a PDF with a small number of paths. I assume you have a PDF with thousands of paths? Can you show an example on a screenshot or attach the PDF to the message?

giorgi0dmAuthor
Participant
October 23, 2023

unfortunately i cannot upload any of the documents because they are confidential. they'are architectureal drawings and they are quite large, for example I was testing on a 1700 x 850 mm which contains something like 260k paths...

Sergey Osokin
Sergey OsokinCorrect answer
Inspiring
October 23, 2023

The operations are really simple, but you've discovered the secret that you have hundreds of thousands of paths in the file. Oh my goodness! 🙂 It's damn hard for scripts to process such documents. The best you can do is to use native commands to select similar objects by attribute. What @Monika Gause  described as an action. The same Select Same, Bring To Front commands can be implemented as a script. But even with native commands, I can see a noticeable processing delay on a test file with 14k objects. It's hard for me to imagine how long a file with 260k paths will take to process.

main();

function main() {
    var color1 = setRGBColor([191, 0, 0]);
    var color2 = setRGBColor([0, 0, 0]);

    // Choose a folder which contains PDF documents
    var folder = Folder.selectDialog("Select folder");
    
    if (folder != null) {
        var pdfFiles = folder.getFiles("*.pdf");
    
        for (var i = 0; i < pdfFiles.length; i++) {
            var pdfFile = pdfFiles[i];
            var doc = app.open(pdfFile);
    
            changeStrokeWidth(doc, 0.24, 0.14);
            app.executeMenuCommand("deselectall");
            moveToFront(doc, color1);
            app.executeMenuCommand("deselectall");
            moveToFront(doc, color2);
    
            // Save modified PDF
            var saveOptions = new PDFSaveOptions();
            var saveFile = new File(pdfFile.path + "/" + pdfFile.name.replace(".pdf", "_modified.pdf"));
            doc.saveAs(saveFile, saveOptions);
            doc.close(SaveOptions.DONOTSAVECHANGES);
        }
    
        alert("Operation completed.");
    } else {
        alert("No folder selected. The script will be interrupted");
    }
}

function setRGBColor(rgb) {
    var c = new RGBColor();
    c.red = rgb[0];
    c.green = rgb[1];
    c.blue = rgb[2];
    return c;
}

function changeStrokeWidth(doc, oldVal, newVal) {
    doc.defaultStrokeWidth = oldVal;
    app.executeMenuCommand("Find Stroke Weight menu item");
    doc.defaultStrokeWidth = newVal;
}

function moveToFront(doc, color) {
    doc.defaultStrokeColor = color;
    app.executeMenuCommand("Find Stroke Color menu item");
    app.executeMenuCommand("sendToFront");
}

 

Monika Gause
Community Expert
Community Expert
October 23, 2023

That should asl owork in an action.

OPen the file

Create a rectangle

Put a comment into the Attributes panel

Assign the stroke weight

select same

change the stroke weight

Search for the object vie the action panel Enter the comment from the Attributes panel.

Repat steps for stroke color.

 

Make a batch to have it run on a folder.

giorgi0dmAuthor
Participant
October 23, 2023

thank you for the reply, i'll try to do it as an action and see if it works that way.