通过 Python 将图像添加到 PDF 文档

Python 使用自己的 API 将图像插入到 PDF 的库。

使用 Python 库将图像添加到 PDF 文档

为了添加 PDF 格式的图像,我们将使用 Aspose.PDF for .NET API,这是一款功能丰富、功能强大且易于使用的适用于 python-net 平台的文档处理 API。打开 NuGet 软件包管理器,搜索 aspose.pdf 然后安装。您也可以从软件包管理器控制台使用以下命令。

Python Package Manager Console

pip install aspose-pdf

使用 Python 将图像添加到 PDF


你需要 Aspose.PDF for .NET 在你的环境中试用代码。

1.创建文档对象并打开输入 PDF 文档。 1.获取要添加图像的页面。 1.将图像添加到页面的资源集合中。 1.使用 GSave 运算符保存当前的图形状态。 1.使用 concateNateMatrix 运算符指定图像的放置位置。 1.使用 Do 运算符在页面上绘制图像。 1.使用 GreStore 运算符保存更新的图形状态。 1.保存 PDF 文件。

将图像添加到 PDF 文档-Python。

<% images-add.code-block.subtitle %>

    import aspose.pdf as ap 
   
    input_file = DIR_INPUT + "sample.pdf"
    output_pdf = DIR_OUTPUT + "add_image.pdf"
    image_file = DIR_INPUT + "logo.jpg"
    # Open document
    document = ap.Document(input_file)

    document.pages[1].add_image(image_file, ap.Rectangle(20, 730, 120, 830, True))

    document.save(output_pdf)