使用 ExtendScript for Adob​​e Illustrator 以特定文件大小导出 PNG

问题描述

我正在尝试在 Extendscript 中创建一个脚本来绘制一些东西,然后将整个画板导出为 4500x5400 像素(或任何其他特定尺寸)的 PNG。 这是我现在拥有的代码

//create new document with my desired size     
var myDoc = app.documents.add();
myDoc.width = 4500;
myDoc.height = 5400;
    
//create a rectangle that is smaller than the artboard
var rect = myDoc.layers[0].pathItems.rectangle(700,300,72,100);

//export Image with name newImage.png (I have a specific path here) 
exportFiletoPNG24 ("~/newImage.png");
    

function exportFiletoPNG24(dest) {
  if (app.documents.length > 0) {
    var exportOptions = new ExportOptionsPNG24();
    exportOptions.artBoardClipping = true; //this option makes sure I export 
                                          //the whole artboard and not just 
                                          //the rectangle    
    var type = ExportType.PNG24;
    var fileSpec = new File(dest);
    
    app.activeDocument.exportFile(fileSpec,type,exportOptions);       
  }
}

该脚本确实将整个画板导出为 png。但是 PNG 的大小始终为 612x792 像素。我怎样才能让它以 4500x5400 像素导出?

解决方法

取而代之的是:

var myDoc = app.documents.add();
myDoc.width = 4500;
myDoc.height = 5400;

以这种方式创建新文档是有意义的:

var preset    = app.getPresetSettings("Web");
preset.units  = RulerUnits.Pixels;
preset.width  = 4500;
preset.height = 5400;
var myDoc     = app.documents.addDocument("Web",preset);

或者您可以创建一个默认大小的新文档,然后以这种方式进行更改:

var myDoc = app.documents.add();
myDoc.artboards[0].artboardRect = [0,5400,4500,0];

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...