2

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 ?

teenup
  • 7,459
  • 13
  • 63
  • 122

1 Answers1

6

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();
        }
    }
}
Chris Haas
  • 53,986
  • 12
  • 141
  • 274
  • 1
    Its very frustating that we don't have any documentation for itextsharp. Thanks a lot. – teenup Sep 02 '11 at 17:15
  • 1
    iTextSharp is a port of the Java library iText which has tons of documentation. If you can't find a search result using "itextsharp" try just using "itext", you'll see lot a results usually, including a whole book dedicated to it. Otherwise, if I have a question I usually just browse the source. http://itextsharp.svn.sourceforge.net/ – Chris Haas Sep 02 '11 at 18:55