我想在pdf中绘制一个形状,例如矩形。我尝试过下面的代码,但它在 pdf 中添加文本。我怎样才能画它?
# Add text to Existing PDF using Python
from pyPdf import PdfFileWriter, PdfFileReader
import StringIO
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
packet = StringIO.StringIO()
# create a new PDF with Reportlab
can = canvas.Canvas(packet, pagesize=letter)
TXT="Sample Text"
can.drawString(300, 70, TXT) #coordinates (x,y)
can.save()
#move to the beginning of the StringIO buffer
packet.seek(0)
new_pdf = PdfFileReader(packet)
# read your existing PDF
existing_pdf = PdfFileReader(file("original.pdf", "rb"))
output = PdfFileWriter()
# add the "watermark" (which is the new …Run Code Online (Sandbox Code Playgroud)