Copy link to clipboard
Copied
I am working with a client to prepare template print artwork for a custom print tool which will be run through their website. Users pick a template, edit the text in defined fields and change the images in defined frames, and the software generates a print-ready PDF.
The tool can accept a PDF upload of the template but requires that all custom elements (in our case mostly text and image swaps) have defined coordinates in pixels. Doing this manually will be quite painstaking so I was wondering if there is a way to export the coordinates of all page elements that we could then work through semi-automatically.
So far I've used IDML in a text editor to find the coordinates, but they don't seem to make any logical sense - in the business card I tried as a simple example, the origin point is listed as 317.57972440853706 -380.9635826724157.
Is there a way:
I suppose a corollary to this is that the method would ideally take less time than simply noting and converting coordinate values manually - though if it's only slightly more, it may have future use. Is this a goal anyone here has accomplished before and if so, how did you go about it?
Here it is all put together:
var main = function() {
var currHPrefs = app.viewPreferences.horizontalMeasurementUnits;
var currVPrefs = app.viewPreferences.verticalMeasurementUnits;
app.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.PIXELS;
app.viewPreferences.verticalMeasurementUnits = MeasurementUnits.PIXELS;
var allPIs = app.documents[0].allPageItems;
for (var i = 0; i < allPIs.length; i++) {
try {
logger(allPIs[i].constructor.name);
...
Copy link to clipboard
Copied
Here's a quick .jsx script that will all log all page item coordinates to a txt file on your desktop called: image_coords_log.txt. Run it on an open doc, with your coords set to pixels.
var main = function() {
var allPIs = app.documents[0].allPageItems;
for (var i = 0; i < allPIs.length; i++) {
try {
logger(allPIs[i].geometricBounds);
} catch(e) {
}
}
};
var logger = function(msg) {
try {
var f = File(Folder.desktop + "/image_coords_log.txt");
f.encoding = "UTF-8";
f.open("a");
f.writeln(msg);
f.close();
}
catch(e) {
}
};
main();
Copy link to clipboard
Copied
Thanks, this seems like a good start. Is there a simple way to add the type of element associated with the coordinates? E.g.:
var main = function() {
var allPIs = app.documents[0].allPageItems;
for (var i = 0; i < allPIs.length; i++) {
try {
logger(allPIs[i]./*PageItemType*/);
logger(allPIs[i].geometricBounds);
} catch(e) {
}
}
};
Just because these still need to be human-readable to allow us to do the upload and know that the object we're putting at 0,0 is a text box, rather than a line, for example.
Copy link to clipboard
Copied
Change your comment to:
constructor.name
Copy link to clipboard
Copied
Should note that the numbers you see are the: y1, x1, y2, x2 coordinates, respectively.
Copy link to clipboard
Copied
Thanks! This is working and is nearly there. The coordinates just seem a little bit off. The horizontal and vertical units are set to pixels, but this Oval, for example, is identified as having its origin at X: 483 (which is correct) but Y: 5.9 (when I can see from the rulers that it begins at around Y: 16):
Is there anything obvious I've missed?
Copy link to clipboard
Copied
By changing the measurement units I've discovered that it the script is outputting the X coordinates in pixels but the Y coordinates in mm. My settings suggest both dimensions are in pixels - any advice?
Copy link to clipboard
Copied
Right click on your Y ruler and change the settings to Pixels.
You could also do it in the script at the start of main():
app.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.PIXELS;
app.viewPreferences.verticalMeasurementUnits = MeasurementUnits.PIXELS;
Copy link to clipboard
Copied
Here it is all put together:
var main = function() {
var currHPrefs = app.viewPreferences.horizontalMeasurementUnits;
var currVPrefs = app.viewPreferences.verticalMeasurementUnits;
app.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.PIXELS;
app.viewPreferences.verticalMeasurementUnits = MeasurementUnits.PIXELS;
var allPIs = app.documents[0].allPageItems;
for (var i = 0; i < allPIs.length; i++) {
try {
logger(allPIs[i].constructor.name);
logger(allPIs[i].geometricBounds);
} catch(e) {
}
}
app.viewPreferences.horizontalMeasurementUnits = currHPrefs;
app.viewPreferences.verticalMeasurementUnits = currVPrefs;
};
var logger = function(msg) {
try {
var f = File(Folder.desktop + "/image_coords_log.txt");
f.encoding = "UTF-8";
f.open("a");
f.writeln(msg);
f.close();
}
catch(e) {
}
};
main();
Copy link to clipboard
Copied
This works perfectly. Thank you!
Copy link to clipboard
Copied
Hm. Wouldn't it make sense to base this on visibleBounds instead of geometricBounds ?
Regards,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
Unclear what kind of software they're using, but seems like replacing one image at XY coords with another would be the same regardless of whether a stroke was involved or not. OP, if stroke is important and you want to include it in your calculation, use visibleBounds instead of geometricBounds.
Copy link to clipboard
Copied
I think you’ll need to set the document’s ruler units to pixels rather than the app’s. Also there is the zero point and ruler origin to consider:
var main = function() {
var doc = app.documents[0];
var currHPrefs = doc.viewPreferences.horizontalMeasurementUnits;
var currVPrefs = doc.viewPreferences.verticalMeasurementUnits;
var currRO = doc.viewPreferences.rulerOrigin;
var currZP = doc.zeroPoint
doc.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.PIXELS;
doc.viewPreferences.verticalMeasurementUnits = MeasurementUnits.PIXELS;
doc.viewPreferences.rulerOrigin = RulerOrigin.PAGE_ORIGIN
doc.zeroPoint = [0, 0];
var allPIs = doc.allPageItems;
for (var i = 0; i < allPIs.length; i++) {
try {
logger(allPIs[i].constructor.name);
logger(allPIs[i].geometricBounds);
} catch(e) {
}
}
doc.viewPreferences.horizontalMeasurementUnits = currHPrefs;
doc.viewPreferences.verticalMeasurementUnits = currVPrefs;
doc.viewPreferences.rulerOrigin = currRO
doc.zeroPoint = currZP;
};
var logger = function(msg) {
try {
var f = File(Folder.desktop + "/image_coords_log.txt");
f.encoding = "UTF-8";
f.open("a");
f.writeln(msg);
f.close();
}
catch(e) {
}
};
main();