gpt4 book ai didi

javascript - Adobe PDF Javascript 提取名称,保存文件

转载 作者:行者123 更新时间:2023-11-28 06:37:32 24 4
gpt4 key购买 nike

在 Adob​​e Acrobat Pro XI 中寻求有关 Javascript 的帮助。

这是我到目前为止所拥有的:

   /* Extract pages to file */
// Regular expression used to acquire the base name of file

var re = /\.pdf$/i;
// filename is the base name of the file Acrobat is working on

var filename = this.documentFileName.replace(re,"");

try {for (var i = 0; i < this.numPages; i+=4)
this.extractPages({
nStart: i,
nEnd: i+3,
});
} catch (e) { console.println("Aborted: " + e) }

现在发生的情况是,它提取页面范围并创建一系列打开的文件,然后我可以重命名这些文件。

我想要做的是添加一个循环以从帐单中提取名称,然后使用该名称作为文件名在文件夹中创建一个新文件。

我发现了这个:

for (var p = 0; p < this.numPages; p++) {
// iterate over all words
for (var n = 0; n < this.getPageNumWords(p); n++) {
if (this.getPageNthWord(p, n) == stringToSearchFor) {
pageArray.push(p);
break;
}
}
}

我认为这会让我达到目标,但我不确定如何将其包含在我的循环中。

我想我需要在文档中搜索静态字符串(在本例中为“学生姓名”),然后返回接下来的两个单词。

最佳答案

使用复制粘贴的代码编写了一个文件来执行此类操作(以及 Adob​​e 论坛上 Thom Parker 的一些帮助,请参阅:https://community.adobe.com/t5/acrobat/step-by-step-instructions-for-saving-a-pdf-in-acrobat-dc-using-a-javascript/m-p/10893916?page=1#M238905)

// https://community.adobe.com/t5/acrobat/how-to-set-up-a-default-value-in-execdialog/td-p/9343585?page=1

var dialogTitle = "Please specify ";
var defaultAnswer = "";

var stringToSearchFor = app.response("Client ID",
dialogTitle, defaultAnswer);

//https://acrobatusers.com/tutorials/print/how-save-pdf-acrobat-javascript/


/* Put script title here */

// Iterates over all pages and find a given string and extracts all
// pages on which that string is found to a new file.

var pageArray = [];

//var stringToSearchFor = "64718";

for (var p = 0; p < this.numPages; p++) {
// iterate over all words
for (var n = 0; n < this.getPageNumWords(p); n++) {
if (this.getPageNthWord(p, n) == stringToSearchFor) {
pageArray.push(p);
break;
}
}
}

if (pageArray.length > 0) {
// extract all pages that contain the string into a new document
var d = app.newDoc(); // this will add a blank page - we need to remove that once we are done
for (var n = 0; n < pageArray.length; n++) {
d.insertPages( {
nPage: d.numPages-1,
cPath: this.path,
nStart: pageArray[n],
nEnd: pageArray[n],
} );
}

// remove the first page
d.deletePages(0);

// https://community.adobe.com/t5/acrobat/how-to-save-as-pdf-form-using-script/td-p/9848947?page=1

// Split Path into an array so it is easy to work with
var aMyPath = this.path.split("/");

// Remove old file name
aMyPath.pop();

// Add new file name
aMyPath.push(stringToSearchFor);

// Put path back together and save
d.saveAs(aMyPath.join("/")+".pdf");

}

关于javascript - Adobe PDF Javascript 提取名称,保存文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34137482/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com