9

Does anyone know of a good .NET library to convert TIFF files, that may be multi-page, to PDF files?

The TIFF files are stored on a file share, and the PDF files need to be stored in the same location as the TIFF file.

The tool is supposed to be used for converting high volumes of TIFF files.

skaffman
  • 398,947
  • 96
  • 818
  • 769
Palle Agermark
  • 346
  • 1
  • 3
  • 5

5 Answers5

10

Here is an example using PDFSharp

using System;
using System.Collections.Generic;
using System.Text;
using PdfSharp.Drawing;
using PdfSharp.Pdf;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument s_document = new PdfDocument();

            PdfPage page = s_document.AddPage();

            XGraphics gfx = XGraphics.FromPdfPage(page);


            XImage image = XImage.FromFile(@"C:\Image.tif");

            page.Width = image.PointWidth;
            page.Height = image.PointHeight;

            gfx.DrawImage(image, 0, 0);

            s_document.Save(@"C:\Doc.pdf");
        }
    }
}
3

You can try our LibTiff.Net library for this. It's free and open-source (BSD License) and comes with tiff2pdf utility that probably does exactly what you need.

Bobrovsky
  • 13,789
  • 19
  • 80
  • 130
2

I used the free iTextSharp library to create a PDF and insert standard System.Drawing.Image objects into the PDF, one per page. The CreateCompressedImageStream simply takes the System.Drawing.Image and saves it as a black and white PNG to reduce the file size.

http://www.itextpdf.com/

    public byte[] CreatePDF(IEnumerable<Image> images)
    {
        if (!images.Any())
        {
            throw new ArgumentException("You haven't specified any images.");
        }

        var stream = new MemoryStream();
        using(var doc = new Document())
        {
            PdfWriter.GetInstance(doc, stream);
            doc.Open();

            int i = 0;
            foreach (var image in images)
            {
                i++;
                var docImage = iTextSharp.text.Image.GetInstance(this.CreateCompressedImageStream(image));
                docImage.ScaleToFit(doc.PageSize.Width, doc.PageSize.Height);
                doc.Add(docImage);

                if (i + 1 < images.Count())
                {
                    doc.NewPage();
                }
            }

            doc.Close();
        }

        return stream.ToArray();
    }

    private Stream CreateCompressedImageStream(Image image)
    {
        MemoryStream imageStream = new MemoryStream();

        var info = ImageCodecInfo.GetImageEncoders().FirstOrDefault(i => i.MimeType.ToLower() == "image/png");
        EncoderParameter colorDepthParameter = new EncoderParameter(Encoder.ColorDepth, 1L);
        var parameters = new EncoderParameters(1);
        parameters.Param[0] = colorDepthParameter;

        image.Save(imageStream, info, parameters);

        imageStream.Position = 0;
        return imageStream;
    }
Kevin McKelvin
  • 3,467
  • 1
  • 27
  • 27
1

Sam Leffler's libtiff ships with various commandline utilities. One of these, tiff2pdf.exe converts TIFFs (including multipage TIFFs) to PDF.

Isn't that an option?

Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
0

The System.Drawing.Imaging.Bitmap class will allow you to open a multipage TIFF and extract each frame. You then need to determine the size of the image in each frame, create PDF pages of that size and then draw the bitmaps on those page.

I have done just the thing for our product Gnostice PDFOne .NET in an article titled "Convert A Multipage TIFF To PDF Using PDFOne .NET published in 2011.

.NET provides the image extraction functionality. Any PDF-creation library will be able to do the rest of the job.

DISCLAIMER: I work for Gnostice.

gn1
  • 526
  • 2
  • 5