如何替换pdf文件中的文本? [英] How to replace text within a pdf file?
问题描述
是否可以使用Google Apps脚本替换PDF文件中的文本?
我试着在替换时没有成功,下面的代码看起来像字符串以我无法理解的方式编码。
var pdfFile = DocsList.getFileById(pdf-doc-id);
var asBlob = pdfFile.getBlob();
var asString = asBlob.getDataAsString();
var s2s =旧字符串;
var s2r =new string;
var repString = asString.replace(s2s,s2r);
var repBlob = Utilities.newBlob(repString).setContentType(application / pdf)。setName(Testing);
DocsList.createFile(repBlob);
EDIT1:我得到一个空的pdf回来
任何想法?
谢谢
c $ c> getDataAsString()不返回PDF文件的文本内容,而是返回文件二进制内容的文本表示。该功能适用于任何文件,甚至那些没有文本的文件(如图片)。
不幸的是,我认为你完全可以用Apps Script来完成你的目标。如果您可以使用云端硬盘用户界面将Google PDF导入为PDF文档,那么您可以使用Apps脚本的DocumentApp修改文档并将其作为PDF导出。
Is it possible to replace text within a pdf file using Google Apps Script?
I am trying the following code without success on the replace, it seems like the string is encoded in a way I cannot understand.
var pdfFile = DocsList.getFileById("pdf-doc-id");
var asBlob = pdfFile.getBlob();
var asString = asBlob.getDataAsString();
var s2s = "old string";
var s2r = "new string";
var repString = asString.replace(s2s, s2r);
var repBlob = Utilities.newBlob(repString).setContentType("application/pdf").setName("Testing");
DocsList.createFile(repBlob);
EDIT1: I got an empty pdf back
Any ideas?
Thanks
The function getDataAsString()
doesn't return the textual content of a PDF file, but instead a textual representation of the binary content of the file. That function works on any file, even those that don't have text (like images).
Unfortunately I don't think you can fully accomplish your goal with Apps Script. If you are able to import your PDF as a Google Document using the Drive UI, then you can use Apps Script's DocumentApp to modify the document and export it as a PDF.
这篇关于如何替换pdf文件中的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!