最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

PDF form checkboxes checking using python and pdfrw - Stack Overflow

programmeradmin1浏览0评论

Has anyone experience with pdf form checkbox checking? The case is that within a django application, based on annotations of pdf forms I map and identify checkboxes I want to either check, or leave empty based on django orm model data. The function is being called and executed correctly, but it does not make actuall cross, or tick in checkbox. It only rather slightly mislocate an overlaying "shadow", which form still caries (it seems like the particular field can be still edited)

  • function works locally, but not in django app
  • although it works locally, it do not create crosses, or ticks, but circle points (a huge dot within checkbox)
        def check_the_checkbox(pdf_annotation, nm_substring):

            # regex identifiaction of checkbox and construction of checkbox_field_name_composer here


            if checkbox_field_name_composer and checkbox_field_name_composer in checkboxes_to_map.keys():
                parent = annotation.get('/Parent')
                if "/AP" in annotation and checkbox_field_name_composer in nm_substring:
                    ap_element = annotation.get('/AP')

                    # Explicitly set checkbox to checked by modifying the AP element
                    if isinstance(ap_element, PdfDict):
                        ap_element.update(PdfDict(D="/Yes"))  # Set the appearance to checked
                        ap_element.update(PdfDict(N="/Yes"))  # Normal state (checked)
                        annotation.update(PdfDict(AS=pdfrw.PdfName('Yes')))  # Set appearance state to checked
                        print(f"Updated AP Element to checked: {ap_element}")

                    if not isinstance(ap_element, PdfDict):
                        ap_element = PdfDict(D="/Yes", N="/Yes")  # Ensure the AP is set correctly
                        annotation.update(ap_element)
                        print(f"Set AP Element to default (checked): {ap_element}")

                    if parent and ("/T" not in annotation.keys() and parent.get('/FT') == '/Btn'):
                        # Mark the checkbox as checked by setting the value to "On"
                        annotation.update(PdfDict(V=pdfrw.PdfName('On')))
                        print("Checkbox marked as checked.")

                        # Optionally adjust the Rect to ensure proper display
                        rect = annotation.get('/Rect')
                        if rect:
                            # Extract numeric values from the PdfObjects
                            x0 = extract_numeric(rect[0])
                            y0 = extract_numeric(rect[1])
                            x1 = extract_numeric(rect[2])
                            y1 = extract_numeric(rect[3])

                            # Calculate new width and height (if needed)
                            new_width = x1 - x0
                            new_height = y1 - y0
                            scale_factor = 0.8  # Adjust the scale factor to make the circle smaller
                            new_width *= scale_factor
                            new_height *= scale_factor

                            # Update the Rect with the new size
                            annotation.update(PdfDict(Rect=[x0, y0, x0 + new_width, y0 + new_height]))
                            print(f"Updated Rect with new size: {annotation.get('/Rect')}")
                else:
                    print(f"Annotation does not match checkbox: {annotation.keys()}")
            else:
                print(f"Checkbox field name {checkbox_field_name_composer} not found in checkboxes_to_map

The code contains debugging prints and I reckon that I perhaps do not update correct annotation or do not update it in a correct way.

发布评论

评论列表(0)

  1. 暂无评论