gpt4 book ai didi

python - Pdftron - 创建与现有元素具有相同样式的新元素

转载 作者:太空宇宙 更新时间:2023-11-04 03:48:06 26 4
gpt4 key购买 nike

我正在尝试使用 PdfTron 创建 PDF 编辑原型(prototype)软件。

我已成功创建界面,用户可以在该界面中单击从 PDF 创建的图像,选择区域,然后将显示一个文本输入框,然后他/她可以在其中输入文本,这将替换 PDF 文件中的内容。

现在文本替换部分有问题。由于没有适用于 Python 的 API 文档(仅示例),我正在关注 Java/Android API 文档。

我现在在哪里。我有以下代码来找出用户选择的矩形中的元素。值x1y1x2y2是基于前端用户选择的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

但是这个返回的 fontObj 类而不是 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

阅读文档我发现你可以创建基于 ObjectFont 对象,例如:

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()

我想不通的是:

  1. 如何从现有元素复制样式。
  2. 如何在文档中定位新元素。
  3. 为什么这个测试代码没有给我可见的结果。据我所知,它创建的新文件在任何地方都没有“我的名字”。

/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/

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