pdfrw checkbox AS values gets unwanted parentheses

回眸只為那壹抹淺笑 提交于 2021-02-20 02:41:04

问题


So I'm trying to mark checkboxes in a PDF file with pdfrw. That means changing the checkboxes "AS" value from the default "/Off" to "/Yes". pdfrw is currently forcing parentheses around my "/Yes" when it writes to file, so it doesn't work. How do I keep it from adding these parentheses?

My code:

template_pdf = pdfrw.PdfReader(input_pdf_path)
annotations = template_pdf.pages[0][/Annots]
for annotation in annotations:
    annotation.update(pdfrw.PdfDict(AS="/Yes"))
    pdfrw.PdfWriter().write(output_pdf_path, template_pdf)

My checkboxes in the PDF-file ends up like this, first word in third line is the important one:

1661 0 obj
<</AP <</D <</Off 1845 0 R /Yes 1846 0 R>> /N <</Yes 1847 0 R>>>> /AS
    (/Yes) /F 4 /FT /Btn /MK <</CA (l)>> /P 1608 0 R /Rect
    [101.275 576.22 107.395 584.91] /Subtype /Widget /T (box1) /Type
    /Annot>>
endobj

But to make the check mark actually show up in most PDF-viewers it needs to be like this:

1661 0 obj
<</AP <</D <</Off 1845 0 R /Yes 1846 0 R>> /N <</Yes 1847 0 R>>>> /AS
    /Yes /F 4 /FT /Btn /MK <</CA (l)>> /P 1608 0 R /Rect
    [101.275 576.22 107.395 584.91] /Subtype /Widget /T (box1) /Type
    /Annot>>
endobj

I could always just open the file in python and just replace all the instances of "(/Yes)" with "/Yes", but there has to be some way to get the right values with pdfrw.


回答1:


I think that you might need to use PdfName('Yes') instead of "/Yes":

annotation.update(pdfrw.PdfDict(AS=pdfrw.PdfName('Yes')))

I didn't test this. But it works.




回答2:


annotation.update(pdfrw.PdfDict(V=pdfrw.PdfName('On')))

Above worked for me.




回答3:


template.Root.Pages.Kids[0].Annots[0].update(PdfDict(AS=PdfName('On'), V=PdfName('On')))


来源:https://stackoverflow.com/questions/54526703/pdfrw-checkbox-as-values-gets-unwanted-parentheses

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!