Creating a Multi Page PDF from a TIFF | TIFF to PDF Converter

0 Flares Twitter 0 Facebook 0 StumbleUpon 0 Google+ 0 LinkedIn 0 0 Flares ×

I’ve been working on a project recently that had a requirement to do a tiff to pdf conversion on the fly, and serve these pdf’s over the web. The added wrinkle was that these tiff files were stored in a database – so I wasn’t going to reading or writing from the filesystem. This isn’t a huge problem, but it did throw 90% of the examples out of the window!

I opted to use PdfSharp to do the conversion this – it’s a really great open source library and did exactly what I needed.

So here we go:

snippet 1:

To start with I retrieved my data from the database into a byte array, wrote it into a memory stream object and finally created an Image object from the memory stream. Next onto creating the pdf document:

snippet 2:

As you can see from the code, this is where PdfSharp comes into play (I opted for the GDI+ version) – creating a PdfDocument, XGraphics object, PdfPage and loading the image into page. I guess the real magic here when using the XImage.FromGdiPlusImage method to load the in memory image file into a pdf writeable object.

Finally was writing this back to the response stream (in ASP.NET obviously!):

snippet 3:

I wont go into too much detail about this, its pretty straight forward stuff. For me the 2 things really worth mentioning is the doc.Save() method which saves the pdf to a new memory stream, and the responseStream.CopyTo method which copies one stream to another (new to .net 4 I think!).

This all worked fine but there was one further complication – the TIFFs might be multi-page. In those examples the pdf would only ever contain the first page. To overcome this I had to loop over the page frames and add a new pdf page for each one. This replaces snippet 2 with the following:

snippet 2(v2):

I was pleasantly surprised with how straight forward this was to achieve, and in particular how quickly it all worked.


, , ,

  • Dear sir

    Good Day , please how i can get it as web Page , and thank you very much for this is solutions


    • Matt

      In order to do this you’d probably have to convert the TIFF into several PNGs (or JPGs), store them on your server and then return an HTML page that displays them.

      It wouldn’t be quite as simple as just returning a web page I’m afraid.

      • Mohammad

        Regards, please if you can help me to send it as web page ,

        • Matt

          Hi Mohammad – I’m afraid it cant be simply returned as a web page. A webpage can only contain text and references to images. To do the kind of thing you’re asking would require a lot more work.

          • mohammad

            i hope if you can do it as soon as possible , and whats your want from my side


  • Rajaprabhu

    Let me know, is there any possibility to convert PDF to Tiff without using any 3rd party tool, kindly suggest any free library or sample code.

  • Jeson

    Simply beautiful. Simply awesome. God bless you for sharing this.

  • smitty_novakovich

    I’m signing up just so i can post how terrible this guide is; “lets just create a guide where the codes all jumbled around, mention various variables which have no context on what they do.. Oh and to make it extra infuriating lets not mention the sodding classes used on the project.
    How about you make this code useful and post the complete code?

    • Hi Smitty, sorry that this article hasn’t helped you. I wrote this several years ago and it wasn’t really meant as a complete guide, but more some helpful pointers in how to get this sort of thing done. It also looks like a few of the code snippets have lost their formatting a bit – which I’l correct asap.

      More specifically, what exactly are you trying to do / having problems with? I’d be more than happy to give you a hand or update my article to be more complete etc.

      • smitty_novakovich

        Sorry, was annoying when this was the only article i could find on this subject, anyway, I managed to piece the following together in the end, hopefully this will help others!:

        using PdfSharp.Pdf;
        using PdfSharp.Drawing;
        using System.Drawing;
        using System.Drawing.Imaging;

        PdfDocument doc = new PdfDocument(); // initialize pdf document

        Image img2 = Image.FromFile(THE LOCATION OF YOUR FILE); // gather the src image

        // check how many pages are in the tiff.
        Guid objGuid = img2.FrameDimensionsList[0];
        FrameDimension objDimension = new FrameDimension(objGuid);
        // subtract one from the amount of pages, due to index start at 0, not 1.
        int pageCount = img2.GetFrameCount(objDimension) – 1;

        // loop through the number of images the file contains
        for (int i = 0; i <= pageCount; i++)
        PdfPage page = doc.AddPage(); // start by adding a page
        XGraphics xgr = XGraphics.FromPdfPage(page);

        img2.SelectActiveFrame(objDimension, i); // activate the n-th page in the file

        XImage img = XImage.FromGdiPlusImage(img2); // fill the XImage with the GDI+ Image
        // Set sizes of canvas and image
        page.Width = img.PointWidth;
        page.Height = img.PointHeight;
        xgr.DrawImage(img, 0, 0);

        } // end of loop


        // save the file and close it.
        doc.Save(@"YOUR FILE NAME" + ".pdf");

  • Very helpful post Matt Whetton, thanks 🙂

  • DanniLi

    I have used another two open source library ghostscript and itextpdf, they are both great to convert tiff to pdf, however my company don’t allow to use open source control, so I choose the third party library to convert multipage tiff to pdf in c#:

The Essential App Marketing Kit
Subscribe To My Newsletter To Get an Entire Chapter From The Book for FREE
Never display this again
0 Flares Twitter 0 Facebook 0 StumbleUpon 0 Google+ 0 LinkedIn 0 0 Flares ×