简体   繁体   English

从PDF文件创建图像或PdfTemplate

[英]Create an Image or PdfTemplate from a PDF file

While using the itextsharp library for pdf generation, I came across this method:- 在使用itextsharp库进行pdf生成时,我遇到了这种方法:-

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

Where, we can get Image instance from a PdfTemplate. 在哪里,我们可以从PdfTemplate获取Image实例。 But, I don't know how to create a PdfTemplate and there is no constructor taking a pdf file name or stream. 但是,我不知道如何创建PdfTemplate,并且没有构造器采用pdf文件名或流。

Why I want this is: I want to create an Image from a PDF file and then isert this image into another pdf file. 为什么要这样:我想从PDF文件创建一个图像,然后将该图像插入另一个pdf文件。

Anybody knows how to create PdfTemplate object ? 有人知道如何创建PdfTemplate对象吗?

The PdfTemplate unfortunately isn't exactly what you think it is. 不幸的是, PdfTemplate并非您真正想的那样。 iText and iTextSharp are PDF generators but not PDF renderers which is what you would need to convert a PDF to an image. iText和iTextSharp是PDF生成器,但不是PDF渲染器,这是将PDF转换为图像所需要的。

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 . PdfTemplate的更常见用法之一是子类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. 如果从PdfImportedPage创建Image ,则不会创建JPG或PNG或任何栅格,实际上,您会在Image对象中包含完整版本的页面。 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). 这意味着您可以应用诸如ScaleAbsolute()或任何您想要的转换,但是当您将其添加到输出PDF时,任何文本仍将是纯文本(因此可以选择)。 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. 这就是质量的体现。如果开始缩放Image ,它将(在数学上)完美缩放,但是在视觉上,它可能无法在Adobe Reader之类的Image完美呈现。 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. 无论如何,下面的代码是针对iTextSharp 5.1.1.0的完整工作示例。 It reads a page from an existing PDF, scales it by 50% and adds it to an output PDF. 它从现有的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();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM