Skip to main content
New Participant
July 14, 2025
Answered

Help with Illustrator script to embed ALL linked items

  • July 14, 2025
  • 3 replies
  • 1977 views

Hi everyone! I'm a graphic designer trying to create a JavaScript script for Adobe Illustrator to automatically embed all linked images.

The Problem

I often work with files that contain a large number of linked images. When I need to send a PDF with everything embedded, I have to manually embed each image—sometimes clicking hundreds of times. It's extremely tedious.

I know I can Shift-click all the linked items and choose Links panel menu > Embed Image(s), but Illustrator prompts a confirmation for each individual image, which defeats the purpose.

There must be a more efficient and less painful way to do this.

The Approach

With the help of ChatGPT, I've tried several versions of a script that loop through all placedItems and call .embed() on them. However, they all produce the same issue:

  • Only some images (like every other one) get embedded.

  • Some images change size or shift position after being embedded.

I’ve attached screenshots showing the inconsistency. Any ideas why this is happening or how to fix it?

Thanks in advance!

if (app.documents.length > 0) {
    var doc = app.activeDocument;
    var linkedItems = doc.placedItems;
    var count = 0;

    for (var i = 0; i < linkedItems.length; i++) {
        var item = linkedItems[i];
        if (!item.embedded) {
            try {
                item.embed();
                count++;
            } catch (e) {
                alert("No se pudo incrustar el elemento: " + item.file.name + "\nError: " + e);
            }
        }
    }

    alert("Se incrustaron " + count + " elementos enlazados.");
} else {
    alert("No hay documentos abiertos.");
}



Correct answer Kurt Gold

An additional note: If you save your Illustrator document as .ai you can turn on the "Include Linked Files" checkbox in the Illustrator Options dialog to embed all linked files in one go.

 

You then just have to close and reopen the document to get the version with the embedded files.

3 replies

Kurt Gold
Kurt GoldCorrect answer
Community Expert
July 15, 2025

An additional note: If you save your Illustrator document as .ai you can turn on the "Include Linked Files" checkbox in the Illustrator Options dialog to embed all linked files in one go.

 

You then just have to close and reopen the document to get the version with the embedded files.

CarlosCanto
Community Expert
July 16, 2025

awesome tip, I learned something today.

Inspiring
July 15, 2025

Perhaps I'm missing something, but why would you write a script to do this?

 

Illustrator already embeds all the images when you save as a PDF.

 

If the problem is that you want the original resolution/format, you can change the PDF options when you export so that the images are not resampled.

CarlosCanto
Community Expert
July 15, 2025

That's what I thought, but I've heard from a couple of clients they're having the same issue of missing linked images in the final pdf.

 

I have not seen this problem personally.

Inspiring
July 18, 2025

Andy’s correct: an Ai-generated PDF file has ALL bitmap images embedded upon saving, so that PDF will open and display correctly in any PDF viewer app.

 

The “missing linked images” problem occurs when you reopen that Ai-generated PDF file in Illustrator.

 

If that PDF was saved with the  “Preserve Illustrator Editing Capabilities” option checked, Illustrator automatically attempts to relink those placed items to the original bitmap files. If it cannot find files with those names in the local filesystem, it throws up those annoying “error” dialogs that it cannot find a linked file, asking you to manually relink or ignore. You can click “Ignore”, of course, in which case the embedded bitmap is shown—but that page item still appears in Ai as a PlacedItem with missing file link, not as a RasterItem as you’d expect, making it problematic to work with. 

 

This behavior is both logical and stupid. For instance, what is the right behavior if the PDF file’s author linked a file named “foo.png”, and there is a completely different file also named “foo.png” on the user’s system? Ai is pretty dumb: it has no way to know if the “foo.png” file it finds locally is identical to the file that was originally used [2], so your PDF’s contents could end up looking drastically different (and wrong!) when reopened in Ai.

 

On opening an Ai-generated PDF that contains linked bitmaps, Ai should always ask its user:

 

1. Do you want it to use the PDF’s embedded bitmaps OR relink to the original bitmap files?

 

And, if you choose to relink but a placed item cannot be relinked because no bitmap file was found:

 

2. Do you want the item automatically converted to a raster item (using the embedded bitmap), OR do you want the embedded bitmap extracted to a file and the item re-linked to that, OR leave the item with a missing link for the user to fix manually?

 

See also (ugh): https://illustrator.uservoice.com/search?filter=merged&query=pdf%20image%20relink

 

--

 

[1] An option to export all embedded bitmaps from a PDF as individual .png files would also be nice to have. But there are already 3rd-party tools which do that, so best KISS.

 

[2] Similarly, it’d be nice if Ai’s automatic relinking detected if a found “foo.png” file appears different to the original linked “foo.png” file—a simple checksum comparison—and asks how you want to proceeed.


quote

[1] An option to export all embedded bitmaps from a PDF as individual .png files would also be nice to have. But there are already 3rd-party tools which do that, so best KISS.

 

I’ve just had a quick rummage on Exchange to see if there’s an existing plugin to export all the embedded bitmaps to file and auto-relink to them, and didn’t find anything (although Exchange is a neglected dumpster with a lot of spammy junk and its search feature is lousy so YMMV). A quick web search didn’t find anything either (but ditto).

 

Illustrator’s scripting API is famously useless with PlacedItems that have missing links; however, the C++ SDK has a deeper reach so may be more useful. Unfortunately the Ai SDK only includes functions for interrogating a document’s Ai data, so (AFAIK) extracting the embedded raster data to individual PNG/TIFF/whatever files will still have to be done using a separate PDF library. But I’m sure there are suitable open-source C/C++ PDF libraries (that aren‘t GPL/LGPL) that can do this.

 

One challenge: how to match up automatically the PDF’s embedded bitmap data with the embedded Ai data, so that each PlacedItem (AIArtHandle*) is relinked to the correct bitmap file. I expect it can be done as both PDF library and Ai SDK can read each objects’ position and rotation (although their coordinate systems will be different).

 

An “Open PDF File relinking to exported bitmaps” would be trivial for Ai to provide as a built-in feature (since it can access ALL of the PDF file’s data), but we all know Adobe’s not super-responsive (as the mountain of open tickets on UserVoice can attest). If someone were to create a third-party C++ plugin (and enjoys the challenge) it sounds like there might be some market interest for such a product amongst prepress/graphics professionals.

CarlosCanto
Community Expert
July 14, 2025

you need to start embedding from the bottom up. After each iteration the next item gets the previous items index, ie second item (index 1) will become index 0 after the first item (index 0) gets embedded. 

 

this line reverses the process order

for (var i = linkedItems.length-1; i>=0; i--) {

 

full script

if (app.documents.length > 0) {
    var doc = app.activeDocument;
    var linkedItems = doc.placedItems;
    var count = 0;

    // for (var i = 0; i < linkedItems.length; i++) {
    for (var i = linkedItems.length-1; i>=0; i--) {
        var item = linkedItems[i];
        if (!item.embedded) {
            try {
                item.embed();
                count++;
            } catch (e) {
                alert("No se pudo incrustar el elemento: " + item.file.name + "\nError: " + e);
            }
        }
    }

    alert("Se incrustaron " + count + " elementos enlazados.");
} else {
    alert("No hay documentos abiertos.");
}

 

Inspiring
July 15, 2025

In addition to my other answer, I would like to recommend ChatGPT. The documentation is so spread out and confusing that it can be hard to advance. ChatGPT has helped me enormously in the last few months in spotting bugs, explaining what is possible or not, and how all the different parts function together.