我该如何以编程方式在大量的PDF文件中搜索和替换文本?我想删除一组文件中添加的URL。我已经能够使用Adobe Pro中的批处理下的JavaScript删除链接,但链接文本仍然存在。我看到有人建议使用文本触摸,手动操作有效,但我不想手动修改1300个文件。
我该如何以编程方式在大量的PDF文件中搜索和替换文本?我想删除一组文件中添加的URL。我已经能够使用Adobe Pro中的批处理下的JavaScript删除链接,但链接文本仍然存在。我看到有人建议使用文本触摸,手动操作有效,但我不想手动修改1300个文件。
由于文档格式的图形特性,查找PDF中的文本可能会本质上变得困难--您正在搜索的字母在文件中可能不是连续的。尽管如此,CAM::PDF 具有一些搜索-替换功能和启发式技巧。请尝试使用 changepagestring.pl,看看它是否适用于您的PDF文档。
安装方法:
$ cpan install CAM::PDF
# start a new terminal if this is your first cpan module
$ changepagestring.pl input.pdf oldtext newtext output.pdf
cpan install CAM::PDF
却可以正常工作。 - Matthew Lock我也变得绝望了。我安装了10个需要付费的PDF编辑器,但都没有成功:
只需使用pdftk + 编辑器:
替换PDF文件中的文本
Use pdftk to uncompress PDF page streams
pdftk original.pdf output original.uncompressed.pdf uncompress
Replace the text (sometimes this
works, sometimes it doesn't) within original.uncompressed.pdf
Repair the modified (and now broken) PDF
pdftk original.uncompressed.pdf output original.uncompressed.fixed.pdf
(from Joel Dare)
http://help.adobe.com/en_US/Acrobat/9.0/Professional/WS5E28D332-9FF7-4569-AFAD-79AD60092D4D.w.html
似乎即使是未压缩的PDF,有时文本格式仍然会出现问题。这使得“常规”文本替换(如sed
)无法工作或不是很简单。
我找不到任何看起来适用于字形间距偏移量的东西,即像这样的文本(在PDF中似乎非常常见),在此示例中,“其他信息”一词被存储为:
[(O)-16(ther i)-20(nformati)-11(on )]TJ
我尝试编写一个工具来满足这个需求。对于常见的用例,它可以正常工作。你可以在这里查看。
首先解压缩你的pdf文件,然后进入已检出的git代码目录并执行以下命令:
语法
$ crystal replaceinpdf.cr input_filename.pdf "something you want replaced" "what you want it replaced with" output_filename.pdf
祝您愉快!欢迎提出请求。
replaceinpdf SC13R0J_HTML/MANUAL.HTM/rm13r0j/ewd/contents/relay/pdf/JC_01.pdf 黒色 black out.pdf
。 - Douglas Held我建议您使用VeryPDF PDF Text Replacer Command Line软件批量替换PDF页面中的文本,您可以运行pdftr.exe轻松地替换PDF页面中的文本,例如:
pdftr.exe -contentreplace "我的名字=>你的名字" D:\in.pdf D:\out.pdf
pdftr.exe -searchandoverlaytext "我的名字=>你的名字" D:\in.pdf D:\out.pdf
pdftr.exe -searchandoverlaytext "我的名字=>D:\temp\myname.png*20*20" D:\in.pdf D:\out.pdf
pdftr.exe -pagerange 1-3 -contentreplace "旧文本=>新文本||VeryPDF=>VeryDOC||我的名字=>你的名字" D:\in.pdf D:\out.pdf
pdftr.exe -searchtext "字符串" C:\in.pdf
pdftr.exe -pagerange 1 -searchtext "字符串" C:\in.pdf
pdftr.exe -pagerange 1 -searchandoverlaytext "旧文本=>新文本||VeryPDF=>VeryDOC||我的名字=>你的名字" D:\in.pdf D:\out.pdf
pdftr.exe -overlaytextfontname "Arial" -overlaytextcolor FF0000 -overlaybgcolor 00FF00 -searchandoverlaytext "旧文字=>新文字||VeryPDF=>VeryDOC||我的名字=>你的名字" D:\in.pdf D:\out.pdf
pdftr.exe -opw 123 -upw 456 -contentreplace "旧文字=>新文字||VeryPDF=>VeryDOC||我的名字=>你的名字" D:\in.pdf D:\out.pdf
pdftr.exe -searchandoverlaytext "PDFcamp打印机=>VeryPDF打印机" -overlaytextfontsize 8 D:\in.pdf D:\out.pdf
pdftr.exe -searchandoverlaytext "PDFcamp打印机=>VeryPDF打印机" -overlaytextfontsize 80% D:\in.pdf D:\out.pdf
我不确定是否愿意花费大量精力编写代码来修改您的1300个文件,而现有程序可以帮助您完成此任务。前几天,我使用了Infix的专业版,使用其“查找和替换文件”功能批量修改了近100个文件,效果非常好。为了找到类似于Microsoft Word的查找和替换功能,我已经评估了其他程序。Infix是我发现唯一能够胜任这项工作的程序。请查看:http://www.iceni.com/infix-pro.htm
虽然这个问题需要编程解决,但我还是想分享一个免费在线工具,它帮助我批量替换了一些PDF文件中的文本:
http://www.pdfdu.com/pdf-replace-text.aspx
在替换文本后,我没有注意到任何广告或其他修改的PDF文件。
我无法使用我尝试过的软件在本地进行更改。我认为主要问题是我缺少PDF中使用的字体,即使使用Acrobat Pro也无法正常工作。在线工具没有抱怨并产生了很好的结果。
虽然这是一个很旧的帖子,但我想分享一下一个用于在PDF中搜索和替换文本的Node.js软件包选项:Aspose.PDF Cloud SDK for Node.js。它是付费产品,但提供每月150次免费API调用。
const { PdfApi } = require("asposepdfcloud");
const { TextReplaceListRequest }= require("asposepdfcloud/src/models/textReplaceListRequest");
const { TextReplace }= require("asposepdfcloud/src/models/textReplace");
// Get Client ID and Client Secret from https://dashboard.aspose.cloud/
pdfApi = new PdfApi("xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx", "xxxxxxxxxxxxxxxxxxxxxx");
var fs = require('fs');
const name = "02_pages.pdf";
const remoteTempFolder = "Temp";
//const localTestDataFolder = "C:\\Temp";
//const path = remoteTempFolder + "\\" + name;
//const outputFile= "Replace_output.pdf";
// Upload File
//pdfApi.uploadFile(path, fs.readFileSync(localTestDataFolder + "\\" + name)).then((result) => {
// console.log("Uploaded File");
// }).catch(function(err) {
// Deal with an error
// console.log(err);
//});
const textReplace= new TextReplace();
textReplace.oldValue= "origami";
textReplace.newValue= "aspose";
textReplace.regex= false;
const textReplace1= new TextReplace();
textReplace1.oldValue= "candy";
textReplace1.newValue= "biscuit";
textReplace1.regex= false;
const trr = new TextReplaceListRequest();
trr.textReplaces = [textReplace,textReplace1];
// Replace text
pdfApi.postDocumentTextReplace(name, trr, null, remoteTempFolder).then((result) => {
console.log(result.body.code);
}).catch(function(err) {
// Deal with an error
console.log(err);
});
//Download file
//const outputPath = "C:/Temp/" + outputFile;
//pdfApi.downloadFile(path).then((result) => {
// fs.writeFileSync(outputPath, result.body);
// console.log("File Downloaded");
//}).catch(function(err) {
// Deal with an error
// console.log(err);
//});