如何将一个pdf中的python链接添加到另一个pdf?

问题描述 投票:0回答:2

我正在尝试找到一个将 pdf 页面中的链接添加到另一个 pdf 页面的功能。

我已经尝试过使用:

addLink(pagenum, pagedest, rect, border=None, fit='/Fit', *args)

来自 pyPDF2,但它只允许内部链接(在同一个 pdf 中)。

有人有想法吗?也许 PyMuPDF?

这是 PyPdf2 中

add_link
的代码:

def add_link(
    self,
    pagenum: int,
    pagedest: int,
    rect: RectangleObject,
    border: Optional[ArrayObject] = None,
    fit: FitType = "/Fit",
    *args: ZoomArgType,
) -> None:
    """
    Add an internal link from a rectangular area to the specified page.

    :param int pagenum: index of the page on which to place the link.
    :param int pagedest: index of the page to which the link should go.
    :param rect: :class:`RectangleObject<PyPDF2.generic.RectangleObject>` or array of four
        integers specifying the clickable rectangular area
        ``[xLL, yLL, xUR, yUR]``, or string in the form ``"[ xLL yLL xUR yUR ]"``.
    :param border: if provided, an array describing border-drawing
        properties. See the PDF spec for details. No border will be
        drawn if this argument is omitted.
    :param str fit: Page fit or 'zoom' option (see below). Additional arguments may need
        to be supplied. Passing ``None`` will be read as a null value for that coordinate.

    .. list-table:: Valid ``zoom`` arguments (see Table 8.2 of the PDF 1.7 reference for details)
       :widths: 50 200

       * - /Fit
         - No additional arguments
       * - /XYZ
         - [left] [top] [zoomFactor]
       * - /FitH
         - [top]
       * - /FitV
         - [left]
       * - /FitR
         - [left] [bottom] [right] [top]
       * - /FitB
         - No additional arguments
       * - /FitBH
         - [top]
       * - /FitBV
         - [left]
    """
    pages_obj = cast(Dict[str, Any], self.get_object(self._pages))
    page_link = pages_obj[PA.KIDS][pagenum]
    page_dest = pages_obj[PA.KIDS][pagedest]  # TODO: switch for external link
    page_ref = cast(Dict[str, Any], self.get_object(page_link))

    border_arr: BorderArrayType
    if border is not None:
        border_arr = [NameObject(n) for n in border[:3]]
        if len(border) == 4:
            dash_pattern = ArrayObject([NameObject(n) for n in border[3]])
            border_arr.append(dash_pattern)
    else:
        border_arr = [NumberObject(0)] * 3

    if isinstance(rect, str):
        rect = NameObject(rect)
    elif isinstance(rect, RectangleObject):
        pass
    else:
        rect = RectangleObject(rect)

    zoom_args: ZoomArgsType = [
        NullObject() if a is None else NumberObject(a) for a in args
    ]
    dest = Destination(
        NameObject("/LinkName"), page_dest, NameObject(fit), *zoom_args
    )  # TODO: create a better name for the link

    lnk = DictionaryObject(
        {
            NameObject("/Type"): NameObject(PG.ANNOTS),
            NameObject("/Subtype"): NameObject("/Link"),
            NameObject("/P"): page_link,
            NameObject("/Rect"): rect,
            NameObject("/Border"): ArrayObject(border_arr),
            NameObject("/Dest"): dest.dest_array,
        }
    )
    lnk_ref = self._add_object(lnk)

    if PG.ANNOTS in page_ref:
        page_ref[PG.ANNOTS].append(lnk_ref)
    else:
        page_ref[NameObject(PG.ANNOTS)] = ArrayObject([lnk_ref])
python pdf hyperlink pypdf pdfrw
2个回答
0
投票

使用 PyMuPDF,您可以通过

page.insert_link(link)
添加 PDF 页面的链接。参数是一个包含必要信息的字典(例如“热点区域”的矩形和目标信息 - 例如页码、其他文件或可执行文件)。

您还可以提取现有链接 - 这将返回相应的字典。然后在进行必要的修改后,使用该返回结果将其插入到其他位置。


0
投票

我想澄清一下 Jorj McKie 的回答。我想在 pdf 文件中创建一个指向另一个 pdf 文件的链接。我尝试过 "{"kind": fitz.LINK_GOTOR, "file": "filename"}" 但出现以下错误:“...../fitz/utils.py”,第 1714 行,在 insert_link 中 annot = getLinkText(page, lnk)" 和 "...../fitz/utils.py",第 1613 行,在 getLinkText 中 r = lnk["来自"] 关键错误:'来自'" 你能告诉我我的代码有什么问题吗? 谢谢。

© www.soinside.com 2019 - 2024. All rights reserved.