2013-02-22 63 views
2

我正在嘗試編寫一個javascript應用程序來輸出indesign(CS5)中所選圖像/組的長度和寬度,並將所選內容保存到.png文件中。問題是使用選擇的visibleBounds生成的長度和寬度與導出圖像的長度和寬度略有不同。更具體地說,如果圖像高度大於寬度,生成的高度將與生成的.png高度相同,但生成的寬度將略小。相反,如果寬度較大,則生成的高度將略小。這裏是我一直在使用的代碼:檢索導出的indesign的正確長度和寬度.png

dest = Folder.selectDialog('Save report'); 
selected = app.activeDocument.selection[0]; 
filer = new File (dest+'/'+'testImage.png'); 
h = selected.visibleBounds[2] - selected.visibleBounds[0]; 
w = selected.visibleBounds[3] - selected.visibleBounds[1]; 
alert('height: '+h+'\nwidth: '+w); 
selected.exportFile(ExportFormat.PNG_FORMAT, filer, false); 

我還應該指出,這個問題只發生在相對較小的圖像上。看起來圖像越小,影響越大。任何幫助將不勝感激。

+0

曾嘗試使用geometricBounds而不是visibleBounds? – fabianmoronzirfas 2013-02-23 11:59:05

+0

是的,geometricBounds給了我完全相同的結果:/ – 2013-02-25 15:32:03

+0

hm。也許嘗試設置腳本使用的單位。 http://jongware.mit.edu/idcs5/pc_ViewPreference.html app.activeDocument.viewPreferences.properties = { horizo​​ntalMeasurementUnits:MeasurementUnits.MILLIMETERS, verticalMeasurementUnits = MeasurementUnits.MILLIMETERS } – fabianmoronzirfas 2013-02-26 08:17:58

回答

1

我也發現這個問題,即使是完全相同的圖像導出到不同的大小取決於其在頁面上的位置。我想問題在於,inDesign在最底層使用釐米或英寸,而不是像素。

但是,我最終做的解決這個問題的方法是,將圖像放置在inDesign文檔中,一旦它被導出,並檢查寬度和高度以確定兩個值。 此解決方案只有在知道圖像大小之後纔有效,一旦它被導出,我沒有找到一種方法在導出圖像之前知道大小,因爲有時大小會發生變化而沒有任何明顯的原因:

selected.exportFile(ExportFormat.PNG_FORMAT, filer, false); 
//These lines load the image into the document, check the size of the image file previously exported, and writes the correct measure into the XML file 
var imageFile = File(filer); 
var imageGraphic = app.activeDocument.pages.item(0).place(imageFile, null); 
imageGraphicItem = imageGraphic[0]; 
var imageFrame = imageGraphicItem.parent; 
var correctImageWidth = Math.round(imageFrame.visibleBounds[3]-imageFrame.visibleBounds[1]); 
var correctImageHeight = Math.round(imageFrame.visibleBounds[2]-imageFrame.visibleBounds[0]); 
//Do something 
imageGraphicItem.parent.remove(); 

希望它有幫助!