我想將使用 PyPDF2addLink命令創建的矩形輪廓的顏色從黑色更改為藍色,但我找不到有關它的檔案,也找不到任何解決方案。
from PyPDF2 import PdfFileWriter
from pathlib import Path
output = PdfFileWriter()
output.addBlankPage(width=72, height=72) #page 1 with index 0
output.addBlankPage(width=72, height=72) #page 2 with index 1
output.addLink(0, 1, [10, 10, 50, 30], border=[0, 0, 1])
with Path("link_example.pdf").open(mode="wb") as output_file:
output.write(output_file)
我得到的唯一資訊是評論的自我解釋:“邊框:如果提供,一個描述邊框繪制屬性的陣列。有關詳細資訊,請參閱 PDF 規范。如果省略此引數,將不會繪制邊框。”
uj5u.com熱心網友回復:
經過大量研究,我發現無法使用原始 PyPDF2 代碼更改輪廓的顏色。出于這個原因,我對其進行了一些修改,以便在 RGB 中允許一個額外的顏色引數。
我已經改變了里面addLink的檔案的功能以包含引數pdf.py..PythonXX/Lib/PyPDF2color
def addLink(self, pagenum, pagedest, rect, border=None, color=None, fit='/Fit', *args):
"""
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 color: if provided, an array describing border-color
RGB. See the PDF spec for details. Black 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.
Valid zoom arguments (see Table 8.2 of the PDF 1.7 reference for details):
/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]
"""
pageLink = self.getObject(self._pages)['/Kids'][pagenum]
pageDest = self.getObject(self._pages)['/Kids'][pagedest] # TODO: switch for external link
pageRef = self.getObject(pageLink)
if border is not None:
borderArr = [NameObject(n) for n in border[:3]]
if len(border) == 4:
dashPattern = ArrayObject([NameObject(n) for n in border[3]])
borderArr.append(dashPattern)
else:
borderArr = [NumberObject(0)] * 3
if color is not None:
colorArr = [NameObject(n) for n in color[:3]]
else:
colorArr = [NumberObject(0)] * 3
if isString(rect):
rect = NameObject(rect)
elif isinstance(rect, RectangleObject):
pass
else:
rect = RectangleObject(rect)
zoomArgs = []
for a in args:
if a is not None:
zoomArgs.append(NumberObject(a))
else:
zoomArgs.append(NullObject())
dest = Destination(NameObject("/LinkName"), pageDest, NameObject(fit),
*zoomArgs) # TODO: create a better name for the link
destArray = dest.getDestArray()
lnk = DictionaryObject()
lnk.update({
NameObject('/Type'): NameObject('/Annot'),
NameObject('/Subtype'): NameObject('/Link'),
NameObject('/P'): pageLink,
NameObject('/Rect'): rect,
NameObject('/Border'): ArrayObject(borderArr),
NameObject('/C'): ArrayObject(colorArr),
NameObject('/Dest'): destArray
})
lnkRef = self._addObject(lnk)
if "/Annots" in pageRef:
pageRef['/Annots'].append(lnkRef)
else:
pageRef[NameObject('/Annots')] = ArrayObject([lnkRef])
_valid_layouts = ['/NoLayout', '/SinglePage', '/OneColumn', '/TwoColumnLeft', '/TwoColumnRight', '/TwoPageLeft',
'/TwoPageRight']
現在我可以得到一個藍色的輪廓,只需在函式中包含新引數:
from PyPDF2 import PdfFileWriter
from pathlib import Path
output = PdfFileWriter()
output.addBlankPage(width=72, height=72) #page 1 with index 0
output.addBlankPage(width=72, height=72) #page 2 with index 1
output.addLink(0, 1, [10, 10, 50, 30], border=[0, 0, 1], color=[0, 0, 1])
with Path("link_example.pdf").open(mode="wb") as output_file:
output.write(output_file)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/311215.html
下一篇:阻止訪問偽造PDF頁面的流量
