从 PDF 文件创建图像或 PdfTemplate

发布于 2024-12-02 17:59:45 字数 343 浏览 1 评论 0原文

在使用 itextsharp 库生成 pdf 时,我遇到了这种方法:-

iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(itextsharp.text.pdf.PdfTemplate);

在哪里,我们可以从 PdfTemplate 获取 Image 实例。但是,我不知道如何创建 PdfTemplate,并且没有构造函数采用 pdf 文件名或流。

我想要这个的原因是:我想从 PDF 文件创建一个图像,然后将该图像插入到另一个 pdf 文件中。

有人知道如何创建 PdfTemplate 对象吗?

While using the itextsharp library for pdf generation, I came across this method:-

iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(itextsharp.text.pdf.PdfTemplate);

Where, we can get Image instance from a PdfTemplate. But, I don't know how to create a PdfTemplate and there is no constructor taking a pdf file name or stream.

Why I want this is: I want to create an Image from a PDF file and then isert this image into another pdf file.

Anybody knows how to create PdfTemplate object ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沧笙踏歌 2024-12-09 17:59:45

不幸的是,PdfTemplate 并不完全是您想象的那样。 iText 和 iTextSharp 是 PDF 生成器,但不是 PDF 渲染器,而 PDF 渲染器是将 PDF 转换为图像所需的工具。

也就是说,您仍然可以实现您的目标,具体取决于您正在寻找的质量。

PdfTemplate 更常见的用途之一是子类 PdfImportedPage。如果您从 PdfImportedPage 创建Image,您将不会创建 JPG 或 PNG 或任何栅格,您实际上将拥有包含在中的页面的完整版本一个 Image 对象。这意味着您可以应用诸如 ScaleAbsolute() 或任何您想要的转换,但是当您将其添加到输出 PDF 时,任何文本仍然是真实文本(因此可以选择)。这是质量发挥作用的部分。如果您开始缩放Image,它将(在数学上)完美缩放,但在视觉上它可能会在 Adob​​e Reader 之类的工具中呈现不完美。如果放大就可以了,但是许多屏幕程序不能很好地渲染小字体。我不知道这对你来说是否是一个问题。

不管怎样,下面的代码是一个针对 iTextSharp 5.1.1.0 的完整工作示例。它从现有 PDF 中读取页面,将其缩放 50% 并将其添加到输出 PDF 中。

using System;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;
using System.IO;
using iTextSharp.text.pdf;
using iTextSharp.text;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //PDF file to pull the first page from
            string inputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Input.pdf");
            //PDF file to output
            string outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Output.pdf");


            using (FileStream fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                using (Document doc = new Document())
                {
                    using (PdfWriter w = PdfWriter.GetInstance(doc, fs))
                    {
                        //Open our PDF for writing
                        doc.Open();

                        //We need a reader to pull pages from
                        PdfReader r = new PdfReader(inputFile);

                        //Get the first page of our source PDF
                        PdfImportedPage importedPage = w.GetImportedPage(r, 1);

                        //Insert a new page into our output PDF
                        doc.NewPage();

                        //Create an Image from the imported page
                        iTextSharp.text.Image Img = iTextSharp.text.Image.GetInstance(importedPage);

                        //Just to show why we are using an Image, scale the Image to 50% width and height of the original page
                        Img.ScaleAbsolute(importedPage.Width / 2, importedPage.Height / 2);

                        //Add the Image to the page
                        doc.Add(Img);

                        //Close our output PDF
                        doc.Close();
                    }
                }
            }
            this.Close();
        }
    }
}

The PdfTemplate unfortunately isn't exactly what you think it is. iText and iTextSharp are PDF generators but not PDF renderers which is what you would need to convert a PDF to an image.

That said, you can still accomplish your goal, depending on the quality that you're looking for.

One of the more common uses of PdfTemplate is the subclass PdfImportedPage. If you create an Image from a PdfImportedPage you won't be creating a JPG or PNG or anything raster, you'll actually have a full version of your page wrapped up in an Image object. What this means is that you can apply transforms such as ScaleAbsolute() or whatever you want, but when you add it to the output PDF any text will still be true text (and thus selectable). This is the part where the quality comes in. If you start scaling the Image it will (mathematically) scale perfectly, but visually it might render imperfectly within something like Adobe Reader. If you zoom in it will be fine, but many screen programs don't render small type that well. Whether this is an issue for you or not I don't know.

Anyway, the code below is a full working sample targetting iTextSharp 5.1.1.0. It reads a page from an existing PDF, scales it by 50% and adds it to an output PDF.

using System;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;
using System.IO;
using iTextSharp.text.pdf;
using iTextSharp.text;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //PDF file to pull the first page from
            string inputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Input.pdf");
            //PDF file to output
            string outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Output.pdf");


            using (FileStream fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                using (Document doc = new Document())
                {
                    using (PdfWriter w = PdfWriter.GetInstance(doc, fs))
                    {
                        //Open our PDF for writing
                        doc.Open();

                        //We need a reader to pull pages from
                        PdfReader r = new PdfReader(inputFile);

                        //Get the first page of our source PDF
                        PdfImportedPage importedPage = w.GetImportedPage(r, 1);

                        //Insert a new page into our output PDF
                        doc.NewPage();

                        //Create an Image from the imported page
                        iTextSharp.text.Image Img = iTextSharp.text.Image.GetInstance(importedPage);

                        //Just to show why we are using an Image, scale the Image to 50% width and height of the original page
                        Img.ScaleAbsolute(importedPage.Width / 2, importedPage.Height / 2);

                        //Add the Image to the page
                        doc.Add(Img);

                        //Close our output PDF
                        doc.Close();
                    }
                }
            }
            this.Close();
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文