4

I’m using WinForms. In my form I have a picturebox that displays tif image documents. I’m using PdfSharp as one of my references to convert the tif documents to pdf documents. The good news is I can convert one of the tif pages that is currently displayed in the picturebox.

The problem is when I have a tif document that has more than 1 page, I cannot convert them all into on single Pdf file. For example if I have a tif document image that contains 5 pages, I would want to press a button and convert all those 5 tif pages into 5 pdf pages.

For testing here is a tif document with 5 pages.

Link: http://www.filedropper.com/sampletifdocument5pages

My Code:

using PdfSharp;
using PdfSharp.Pdf;
using PdfSharp.Drawing;

    private string srcFile, destFile;
    bool success = false;

    private void Open_btn_Click(object sender, EventArgs e)
    {
        OpenFileDialog dlg = new OpenFileDialog();
        dlg.Title = "Open Image";

        if (dlg.ShowDialog() == DialogResult.OK)
        {
            pictureBox1.Image = Image.FromFile(dlg.FileName);

            lbl_SrcFile.Text = dlg.FileName; 
        }
        dlg.Dispose();
    }

    private void Save_btn_Click(object sender, EventArgs e)
    {
        SaveImageToPDF();
    }

    private void SaveImageToPDF()
    {
        try
        {
            string source = lbl_SrcFile.Text;
            string savedfile = @"C:\image\Temporary.tif";
            pictureBox1.Image.Save(savedfile);
            source = savedfile;
            string destinaton = @"C:\image\new_PDF_TIF_Document.pdf";

            PdfDocument doc = new PdfDocument();
            var page = new PdfPage();


            XImage img = XImage.FromFile(source);

            if (img.Width > img.Height)
            {
                page.Orientation = PageOrientation.Landscape;
            }
            else
            {
                page.Orientation = PageOrientation.Portrait;
            }

            doc.Pages.Add(page);

            XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[0]); xgr.DrawImage(img, 0, 0);

            doc.Save(destinaton);
            doc.Close();
            img.Dispose(); //dispose img in order to free the tmp file for deletion (Make sure the PDF file is closed thats being used)
            success = true;
            MessageBox.Show("                     File saved successfully! \n\nLocation: C:\\image\\New PDF Document.pdf", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            System.Diagnostics.Process.Start(destinaton);
            File.Delete(savedfile);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

enter image description here

taji01
  • 2,527
  • 8
  • 36
  • 80

2 Answers2

10

[Edit] Added full working code...with paths hard-coded.

try
{
    string destinaton = @"C:\Temp\Junk\new_PDF_TIF_Document.pdf";

    Image MyImage = Image.FromFile(@"C:\Temp\Junk\Sample tif document 5 pages.tiff");

    PdfDocument doc = new PdfDocument();

    for (int PageIndex = 0; PageIndex < MyImage.GetFrameCount(FrameDimension.Page); PageIndex++)
    {
        MyImage.SelectActiveFrame(FrameDimension.Page, PageIndex);

        XImage img = XImage.FromGdiPlusImage(MyImage);

        var page = new PdfPage();

        if (img.Width > img.Height)
        {
            page.Orientation = PageOrientation.Landscape;
        }
        else
        {
            page.Orientation = PageOrientation.Portrait;
        }
        doc.Pages.Add(page);

        XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[PageIndex]);

        xgr.DrawImage(img, 0, 0);
    }

    doc.Save(destinaton);
    doc.Close();
    MyImage.Dispose();

    MessageBox.Show("File saved successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Steve Wellens
  • 20,506
  • 2
  • 28
  • 69
  • Sorry I'm still new to programming. Thanks for writing it out so i could study :) – taji01 Feb 04 '16 at 21:18
  • @SteveWellens - this is great and it works for me. I only see one little issue - i have a .tif file and once it's converted to .pdf - part of it gets cut off (on right side). Is it possibly to somehow zoom out slightly before converting it to .pdf or something along the lines? Some of the .tif files vary in size and I'm just not sure how to go about this issue to make sure that the converted pdf document display all that was in .tif file – BobSki Mar 10 '17 at 16:01
  • For those who have the same issue as @BobSki, see https://stackoverflow.com/a/24199315/411115. I had to find a way to solve this, I did so by scaling the tif pages to a size that fits the pdf page dimensions. I had to also look up how to scale maintaining aspect ratio of the original image. – Daniel Lee Mar 26 '18 at 19:36
  • For those with the same problem as @BobSki: You can call `DrawImage` with two more parameter to specify the size of the image in the PDF. This allows you to draw the image as is in the available area. It may still make sense to shrink large images to reduce the file size of the final PDF. – I liked the old Stack Overflow Jan 11 '23 at 10:24
1

It's been a while since I've used PdfSharp, but you should be able to call the GetFrameCount method on your image, which will tell you how many pages it has.

Then you can use the SelectActiveFrame method to choose which page is active.

Daniel Mann
  • 57,011
  • 13
  • 100
  • 120