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

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

Matt

, , ,

  • http://www.qadommi.com Mohammad

    Dear sir

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

    Regards

    • 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

            Regards

  • Rajaprabhu

    Hi,
    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.

    • http://www.codenutz.com Matt Whetton

      No problem, thank you for the feedback!

  • 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?

    • http://www.codenutz.com/ Matthew Whetton

      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

        img2.Dispose();

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

        • http://www.codenutz.com/ Matthew Whetton

          No problem. Glad you got it sorted in the end :-)

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