问题描述
上下文
例如
outputStream = open("destination.pdf","wb")
key = '/Annots'
uri = '/URI'
ank = '/A'
import os
dir_path = os.path.dirname(os.path.realpath(__file__))
cwd = os.getcwd()
for x in range(existing_pdf.getNumPages()):
page = existing_pdf.getPage(x)
page_object = page.getobject()
if key in page_object:
ann = page_object[key]
for a in ann:
u = a.getobject()
if uri in u[ank]:
test = u[ank][uri]
test1 = u[ank].keys()
u[TextStringObject(ank)][TextStringObject(uri)] = TextStringObject(f"file:./foo1.pdf")
output.addPage(page)
# finally,write "output" to a real file
output.write(outputStream)
outputStream.close()
以上不起作用,即 foo1.pdf 未正确链接。 如果我添加“file:///{CWD}/foo1.pdf”,它会起作用。 有没有办法只使用相对路径?
解决方法
在通读 pdf 结构和文档后,我能够编写以下内容并且按预期工作。
for x in range(existing_pdf.getNumPages()):
page = existing_pdf.getPage(x)
page_object = page.getObject()
if key in page_object:
ann = page_object[key]
for a in ann:
u = a.getObject()
if uri in u[ank]:
del u[TextStringObject(ank)][TextStringObject(uri)]
u[TextStringObject(ank)][NameObject('/F')] = TextStringObject(f"./sheets/sheet1.pdf")
u[TextStringObject(ank)][TextStringObject('/S')] = NameObject("/Launch")
u[TextStringObject(ank)][NameObject('/NewWindow')] = BooleanObject(f"true")
output.addPage(page)
# finally,write "output" to a real file
output.write(outputStream)
outputStream.close()