// 获取当前文档的总页数
var totalPages = this.numPages;
// 获取当前文档的名称
var docName = decodeURI(this.documentFileName);
// 获取当前文档的全路径
var docPath = decodeURI(this.path);
// 获取当前文档的父路径
var docParentPath = decodeURI(this.path.replace(/[^/\\]*$/, ""));
var pt = 72/25.4;
var doc = this;
// 遍历每一页
for (var i = 0; i < totalPages; i++) {
//添加一个文字 /C/Users/Administrator/Desktop/cut测试 - 副本.pdf《80.1x69.5》[1-3]
var pageWidth = this.getPageBox("Media", i)[2]/pt;
var pageHeight = this.getPageBox("Media", i)[1]/pt;
var content =docPath+"《"+ pageWidth.toFixed(1)+'x'+pageHeight.toFixed(1)+"》"+"["+ (i + 1) + "-" + totalPages + "]";
var text = new Text(doc,i);
text.content = content;
text.x = 20;
text.color = getCMYKColor(100,0,100,0);
text.draw();
//添加一个填充的色的矩形
var fillRect = new Rect(doc,i);
fillRect.x = 50;
fillRect.y = 50;
fillRect.w = 10;
fillRect.h = 10;
fillRect.fillColor = getCMYKColor(100,100,0,0);
fillRect.draw()
//添加一个描边色的矩形
var strokRect = new Rect(doc,i);
strokRect.borderWidth = 0.25;
strokRect.strokeColor = getCMYKColor(100,0,100,0);
strokRect.fillColor = getNoColor();
strokRect.x = 20;
strokRect.y = 20;
strokRect.w = 20;
strokRect.h = 20;
strokRect.draw();
//添加一个描边色的矩形
var fillAndStrokRect = new Rect(doc,i);
fillAndStrokRect.borderWidth = 1;
fillAndStrokRect.strokeColor = getCMYKColor(0,0,100,0);
fillAndStrokRect.fillColor =getCMYKColor(0,100,0,0);
fillAndStrokRect.x = 0;
fillAndStrokRect.y = 20;
fillAndStrokRect.w = 20;
fillAndStrokRect.h = 5;
fillAndStrokRect.draw();
//添加一个描边色的矩形
var circle = new Circle(doc,i);
circle.borderWidth = 1;
circle.strokeColor = getCMYKColor(0,0,100,0);
circle.fillColor =getCMYKColor(0,100,0,0);
circle.x = 20;
circle.y = 0;
circle.w = 20;
circle.h = 5;
circle.draw();
//添加一个线条
var line = new Line(doc,i);
line.borderWidth = 0.25;
line.strokeColor = getCMYKColor(100,0,0,0);
line.x1 = 20;
line.y1 = 10;
line.x2 = 5;
line.y2 = 5;
line.draw();
try{
//插入pdf文档尺寸不对,具体原因不明
//插入一个pdf文档
var pdf = new Pdf(doc,i);
//pdf的路径填写为正确的路径
pdf.path = "C:\\Users\\Administrator\\Desktop\\新建文件夹 (7)\\插入测试.pdf";
pdf.sourceIndex = i;
pdf.draw();
}catch (e) {
//有可能失败 因为路径不存在
}
try{
//插入一个图片文档
var image = new Image(doc,i);
image.path = "C:\\Users\\Administrator\\Desktop\\新建文件夹 (7)\\图片测试.jpg";
image.bOnTop = false;
image.x = 5;
image.y = 5;
image.draw();
}catch (e) {
//有可能失败 因为路径不存在
}
}
//下面是方法可以不需要修改
function getCMYKColor(cc, mm, yy, kk) {
return ["CMYK", cc/100.0, mm/100.0, yy/100.0, kk/100.0];
}
function getNoColor() {
return color.transparent;
}
/**
* 文字类
* @param doc 类似ai里面的var doc = activeDocument
* @param content 内容
* @param index 页码
* @param fontName 字体
* @param fontSize 字号
* @param x x坐标
* @param y y坐标
* @param cc 颜色
* @param mm
* @param yy
* @param kk
* @constructor
*/
function Text(doc,index,content,fontName,fontSize,x,y,color) {
var pt = 72/25.4;
this.doc = doc;
this.content = content || "默认内容"; // 默认内容
this.index = (index !== undefined) ? index : 0; // 默认页码为0
this.fontName = fontName || "MicrosoftYaHei"; // 默认字体为Helvetica
this.fontSize = fontSize || 5; // 默认字体大小为12
this.x = (x !== undefined) ? x : 0; // 默认水平位置为0
this.y = (y !== undefined) ? y : 0; // 默认垂直位置为0
this.color = (color !== undefined) ? color : getCMYKColor(0,0,0,100); // 默认垂直位置为0
this.draw = function(){
this.doc.addWatermarkFromText({
cText: this.content,
nTextAlign: app.constants.align.left,
nHorizAlign: app.constants.align.left,
nVertAlign: app.constants.align.bottom,
nHorizValue: this.x * pt,
nVertValue: this.y * pt,
aColor: this.color,
cFont: this.fontName,//"MicrosoftYaHei",
nFontSize: this.fontSize,
nStart: this.index,
nEnd: this.index,
bOnTop: true
});
};
}
/**
* 添加矩形
* @param doc 必须传入
* @param x
* @param y
* @param w
* @param h
* @param index
* @param strokeColor
* @param borderWidth
* @param fillColor
* @constructor
*/
function Rect(doc,index,x,y,w,h,strokeColor,borderWidth,fillColor) {
var pt = 72/25.4;
this.doc = doc;
this.x = (x !== undefined) ? x : 0;
this.y = (y !== undefined) ? y : 0;
this.w = (w !== undefined) ? w : 0;
this.h = (h !== undefined) ? h : 0;
this.index = (index !== undefined) ? index : 0;
this.borderWidth = (borderWidth !== undefined) ? borderWidth : 0;
this.strokeColor = (strokeColor !== strokeColor) ? strokeColor : getNoColor();
this.fillColor = (fillColor !== fillColor) ? fillColor : getCMYKColor(0,0,0,100);
this.draw = function () {
this.doc.addAnnot({
type: "Square",
page: this.index, // 页面索引,从0开始
rect: [this.x*pt, this.y*pt, (this.x+ this.w)*pt, (this.y+ this.h)*pt],
strokeColor: this.strokeColor, // 边框颜色
fillColor: this.fillColor, // 填充颜色
borderWidth: this.borderWidth * pt // 边框宽度
});
this.doc.flattenPages();
}
}
/**
* 添加椭圆
* @param doc
* @param x
* @param y
* @param w
* @param h
* @param index
* @param strokeColor
* @param borderWidth
* @param fillColor
* @constructor
*/
function Circle(doc,index,x,y,w,h,strokeColor,borderWidth,fillColor) {
var pt = 72/25.4;
this.doc = doc;
this.x = (x !== undefined) ? x : 0;
this.y = (y !== undefined) ? y : 0;
this.w = (w !== undefined) ? w : 0;
this.h = (h !== undefined) ? h : 0;
this.index = (index !== undefined) ? index : 0;
this.borderWidth = (borderWidth !== undefined) ? borderWidth : 0;
this.strokeColor = (strokeColor !== strokeColor) ? strokeColor : getNoColor();
this.fillColor = (fillColor !== fillColor) ? fillColor : getCMYKColor(0,0,0,100);
this.draw = function () {
this.doc.addAnnot({
type: "Circle",
page: this.index, // 页面索引,从0开始
rect: [this.x*pt, this.y*pt, (this.x+ this.w)*pt, (this.y+ this.h)*pt],
strokeColor: this.strokeColor, // 边框颜色
fillColor: this.fillColor, // 填充颜色
borderWidth: this.borderWidth * pt // 边框宽度
});
this.doc.flattenPages();
}
}
/**
* 添加线条
* @param doc
* @param x
* @param y
* @param w
* @param h
* @param index
* @param strokeColor
* @param borderWidth
* @param fillColor
* @constructor
*/
function Line(doc,index,x1,y1,x2,y2,strokeColor,borderWidth) {
var pt = 72/25.4;
this.doc = doc;
this.x1 = (x1 !== undefined) ? x1 : 0;
this.y1 = (y1 !== undefined) ? y1 : 0;
this.x2 = (x2 !== undefined) ? x2 : 0;
this.y2 = (y2 !== undefined) ? y2 : 0;
this.index = (index !== undefined) ? index : 0;
this.borderWidth = (borderWidth !== undefined) ? borderWidth : 0;
this.strokeColor = (strokeColor !== strokeColor) ? strokeColor : getNoColor();
this.draw = function () {
this.doc.addAnnot({
type: "Line",
page: this.index, // 页面索引,从0开始
// rect: [this.x1*pt, this.y1*pt, this.x2*pt, this.y2*pt],
points: [[this.x1*pt, this.y1*pt], [this.x2*pt, this.y2*pt]],
strokeColor: this.strokeColor, // 边框颜色
borderWidth: this.borderWidth * pt // 边框宽度
});
this.doc.flattenPages();
}
}
function Pdf(doc,index,path,x,y,bOnTop) {
var pt = 72/25.4;
this.doc = doc;
this.index = (index !== undefined) ? index : 0;
this.x = (x !== undefined) ? x : 0;
this.y = (y !== undefined) ? y : 0;
this.path = path;
this.bOnTop = bOnTop ? bOnTop : false;
this.draw = function () {
this.doc.addWatermarkFromFile({
cDIPath: this.path,
nStart: this.index,
nEnd: this.index,
nHorizAlign: app.constants.align.left, // 水平对齐方式
nVertAlign: app.constants.align.bottom, // 垂直对齐方式
// bPercentage: false,
nHorizValue: this.x*pt, // 水平偏移量
nVertValue: this.y*pt, // 垂直偏移量(注意:Y坐标是从页面底部开始计算的,所以需要取负值)
nScale: 1.0, // 缩放比例
bOnTop:this.bOnTop
});
}
}
function Image(doc,index,path,x,y,bOnTop) {
var pt = 72/25.4;
this.doc = doc;
this.index = (index !== undefined) ? index : 0;
this.x = (x !== undefined) ? x : 0;
this.y = (y !== undefined) ? y : 0;
this.path = path;
this.bOnTop = bOnTop ? bOnTop : false;
this.draw = function () {
this.doc.addWatermarkFromFile({
cDIPath: this.path,
nStart: this.index,
nEnd: this.index,
nHorizAlign: app.constants.align.left, // 水平对齐方式
nVertAlign: app.constants.align.bottom, // 垂直对齐方式
// bPercentage: false,
nHorizValue: this.x*pt, // 水平偏移量
nVertValue: this.y*pt, // 垂直偏移量(注意:Y坐标是从页面底部开始计算的,所以需要取负值)
nScale: 1.0, // 缩放比例
bOnTop:this.bOnTop
});
}
}
04-23
04-17