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

4 Flares Twitter 2 Facebook 0 StumbleUpon 1 Google+ 0 LinkedIn 1 4 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.


, , ,

  • Mohammad

    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.

    • Matt Whetton

      No problem, thank you for the feedback!

4 Flares Twitter 2 Facebook 0 StumbleUpon 1 Google+ 0 LinkedIn 1 4 Flares ×