- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试使用 PdfTron 创建 PDF 编辑原型(prototype)软件。
我已成功创建界面,用户可以在该界面中单击从 PDF 创建的图像,选择区域,然后将显示一个文本输入框,然后他/她可以在其中输入文本,这将替换 PDF 文件中的内容。
现在文本替换部分有问题。由于没有适用于 Python 的 API 文档(仅示例),我正在关注 Java/Android API 文档。
我现在在哪里。我有以下代码来找出用户选择的矩形中的元素。值x1
、y1
、x2
、y2
是基于前端用户选择的PDF坐标。
rect = Rect(x1, y1, x2, y2)
text = ''
extractor = TextExtractor()
extractor.Begin(page)
line = extractor.GetFirstLine()
words = []
while line.IsValid():
word = line.GetFirstWord()
while word.IsValid():
elRect = word.GetBBox()
elRect.Normalize()
if elRect.IntersectRect(elRect, rect):
text += ' ' + word.GetString()
words.append(word)
word = word.GetNextWord()
line = line.GetNextLine()
words
基本上是数组,我在其中存储稍后需要替换为新元素的内容。
现在是问题。我希望新元素具有与旧文本相同的样式和字体。Api ( link) 告诉我使用
style = words[0].GetStyle()
给我单词的样式,我可以使用从样式中获取字体
font = style.GetFont()
文档:https://www.pdftron.com/pdfnet/mobile/docs/Android/pdftron/PDF/TextExtractor.Style.html
但是这个返回的 font
是 Obj
类而不是 Font
类。
显然,创建带有字体的新文本元素需要 Font
类的对象。
因为
element = eb.CreateTextBegin(font, 10.0);
产生一个错误:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/alan/.virtualenvs/pdfprint/local/lib/python2.7/site-packages/PDFNetPython2.py", line 5056, in CreateTextBegin
def CreateTextBegin(self, *args): return _PDFNetPython2.ElementBuilder_CreateTextBegin(self, *args)
NotImplementedError: Wrong number or type of arguments for overloaded function 'ElementBuilder_CreateTextBegin'.
Possible C/C++ prototypes are:
pdftron::PDF::ElementBuilder::CreateTextBegin(pdftron::PDF::Font,double)
pdftron::PDF::ElementBuilder::CreateTextBegin()
也许有更好的方法来达到相同的结果?
编辑1
阅读文档我发现你可以创建基于 Object
的 Font
对象,例如:
font = Font(style.GetFont())
尽管如此,仍然坚持使用这些样式创建元素。
/edit1
编辑2
我使用下面的代码来测试写入文件:
style = elements[0].GetStyle()
font = Font(style.GetFont())
fontsize = style.GetFontSize()
eb = ElementBuilder()
element = eb.CreateTextBegin(font, 10.0)
writer.WriteElement(element)
element = eb.CreateTextRun('My Name')
element.SetTextMatrix(10, 0, 0, 10, 100, 100)
gstate = element.GetGState()
gstate.SetTextRenderMode(GState.e_fill_text)
gstate.SetStrokeColorSpace(ColorSpace.CreateDeviceRGB())
gstate.SetStrokeColor(ColorPt(1, 1, 1))
element.UpdateTextMetrics()
writer.WriteElement(element)
writer.WriteElement(eb.CreateTextEnd())
writer.End()
from core.helpers import ensure_dir
ensure_dir(output_filename)
doc.Save(output_filename, SDFDoc.e_linearized)
doc.Close()
我想不通的是:
/Edit2
最佳答案
根据上面的代码,您似乎想要根据页面上第一个单词使用的字体样式(字体名称 + 颜色)将一些文本附加到现有页面。
上面的代码有几个问题。您正在设置描边颜色而不是填充:
gstate.SetTextRenderMode(GState.e_fill_text)
gstate.SetStrokeColorSpace(ColorSpace.CreateDeviceRGB());
gstate.SetStrokeColor(ColorPt(1, 1, 1))
尝试
gstate.SetTextRenderMode(GState.e_fill_text)
gstate.SetFillColorSpace(ColorSpace.CreateDeviceRGB());
gstate.SetFillColor(ColorPt(1, 0, 0)) // hardcode to red … for testing purposes only
主要问题很可能与字体处理有关。您正在劫持现有字体并假设该字体使用“标准编码”。但是,此字体可能未使用标准编码。此外,现有 PDF 中的字体通常是子集(这意味着字体不包含完整的字形列表,而仅包含文档中存在的字符引用)。因此,您可能会看到 notdef 或空格而不是预期的文本。这个问题和其他一些问题在此处进行了介绍:
https://groups.google.com/d/msg/pdfnet-sdk/RBTuJG2uILk/pGkrKnqZ_YIJ https://groups.google.com/d/msg/pdfnet-sdk/2y8s5aehq-c/xyknr9W5r-cJ
作为解决方案,您可以找到匹配的系统字体(例如,基于字体名称和其他属性)并创建新字体,而不是直接使用嵌入字体。 PDFNet 提供了一种实用方法 Font.Create(doc, font) , or Font.Create(doc, "Font name") .
此方法将创建 Unicode 字体,因此您应该使用 eb.CreateUnicodeTextRun() 而不是 eb.CreateTextRun()。
或者,您可以使用 AcroForm 作为模板(参见 InteractiveForms 示例)并使用 pdfdoc.FattenAnnotations() 来结束文档的只读版本。
关于python - Pdftron - 创建与现有元素具有相同样式的新元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22732289/
PDFTron 可以选择提供自定义 Javascript 文件来修改查看器,但我不确定该文件中应该包含什么,而且我在网上找不到任何示例。 该网站有如下描述: Viewer Options - By d
我正在使用 PdfTron SDK 并且我正在尝试在用户放大后绘制注释,注释需要在书的侧面(第三张图片)但是当我们放大时它正在绘制书的中心(图 1 和 2)。 缩放示例(错误状态): 没有缩放的例子(
我正在使用 pdftron 库来渲染和编辑 pdf。我有一个场景,我必须获取注释的坐标并将其保存到数据库,如果形状被删除,则从数据库中删除这些坐标。我设法记录了绘制坐标和删除坐标,但绘制坐标和删除坐标
我正在尝试使用 PdfTron 创建 PDF 编辑原型(prototype)软件。 我已成功创建界面,用户可以在该界面中单击从 PDF 创建的图像,选择区域,然后将显示一个文本输入框,然后他/她可以在
在 PDFTron WebViewer 中,当单击标题中的注释工具,然后单击正在查看的 PDF 时,左侧面板中会弹出回复框: 如果单击时间戳旁边的 3 个点,然后单击“编辑”: 您将看到评论框: 有没
在 PDFTron Viewer 2.2.2 中,我们添加了拖放缩略图以移动页面。 但在 3.0.1 和 3.1.0 中,这是不可能的。您是否阻止了拖放事件? (所有浏览器都受影响) 这里是我们 co
我将 PDFTron 用于内部网站。当我单击 PDFTron 中的自由文本工具时,它工作正常。我需要的是在文本工具的 focusout() 上我需要选择平移工具。 有人可以指导我如何去做吗? 最佳答案
将批量 html 文件转换为 pdf 时,出现转换失败错误。对于同一个 html 文件,有时它可以工作。主要是每当有更多文件时,一两个就会失败。 PdfTron 版本:2016 年 9 月,环境:Li
我有一个 pdf tron sdk 集成的问题,我单独尝试过但没有用,所以我尝试运行他们的示例之一,但我总是有 5 个未能解决文件错误。他们的文档不容易阅读而且我没有理解如何通过 gradle 或手动
我正在尝试绘制注释,在 80% 的情况下,注释可以正确绘制,但有时注释会在中间被切断,在我放大和缩小后,注释会完全绘制。 这是我绘制注释的代码: public void createNoteIconO
我正处于最终确定 PDFTron 的阶段,并且正在尝试一些重要的功能。有必要在 PDF 上显示图像并对用户交互使用react。通过给定的示例,我发现我必须使用 PTFileAttachment,如下所
我有一个 PDFTron webviewer,我用它来呈现 pdf 表单。当用户单击外部保存按钮时,我需要从 webviewer 中提取表单 xml。 我试过了 var am = readerCont
有没有办法抑制页面出现时覆盖在页面上然后在淡入淡出动画中消失的页码(例如“1/253”)?它是 PTPDFViewCtrl View 中的一个 UILabel,但我在文档中找不到任何指示如何配置它以防
我尝试使用 PDFTron 添加输入字段,但 PDF 中没有显示任何内容, 我关注了这个文档: https://www.pdftron.com/documentation/samples/js/Int
我在尝试与 PDFTron 集成时遇到一个奇怪的错误,即 PDFNet Android SDK: UnsatisfiedLinkError: Couldn't load PDFNetC: findLi
我试图获取一个 blob 作为 URL,但我在这一行收到错误: xhr.send() 错误消息是 angular.js:13920 Error: Failed to execute 'send on
我正在尝试获取所有注释,例如:INK 并从 Android 保存到数据库。 我查看了 PDFTron 示例,尤其是 ElementReaderAdvTest。我可以跟踪它处理 Element.e_pa
我有 PDFTron 的演示项目,在我本地的相同项目位置也有 Booking.pdf。 我编写了以下代码将 PDF 加载到 PDFTron 中 #viewer
我们正在使用 PdfTron 将 PDF 转换为 XOD,以便可以在 XPS 查看器中查看。 PDFTron 部署在 PCF 上并作为服务使用。 遇到这样的情况:一个简单的扫描 pdf 文件(5mb)
我想在我的应用程序上使用 PDFTron webviewer。我该怎么做? “注意:我安装了@pdftron 节点模块” 最佳答案 绝对可以在 Electron 应用程序中使用 WebViewer。我
我是一名优秀的程序员,十分优秀!