Wednesday, April 20, 2011

TIFF to PDF Conversion using .Net - Free



            string infilename = "c:\\temp\\faxsample.tif";
            PdfDocument document = new PdfDocument();
            Bitmap bm = new Bitmap(infilename);
            int total = bm.GetFrameCount(FrameDimension.Page);
            for (Int32 k = 0; k < total; k++)
            {
                Bitmap bm2 = new Bitmap(infilename);
                bm2.SelectActiveFrame(FrameDimension.Page, k);
                XImage image = XImage.FromGdiPlusImage(bm2);
                PdfPage page1 = document.AddPage();
                
                // Get Image width, Height and Resolution and Set output document Width and Height in Inches
                page1.Width = XUnit.FromInch(image.Width / image.HorizontalResolution);
                page1.Height = XUnit.FromInch(image.Height / image.VerticalResolution);
                XGraphics gfx = XGraphics.FromPdfPage(page1, XGraphicsPdfPageOptions.Append);
                gfx.DrawImage(image, 0, 0);
                gfx.Dispose();
                image.Dispose();
                page1.Close();
                bm2.Dispose();
            } 
            document.Save("c:\\temp\\faxsample.pdf");
            Console.WriteLine("Total Pages: " + total);


So Simple as that. Add pdfsharp in the reference nothing else to install.
Pdfsharp can be obtained from PDFSharp.

No comments: