taji01
taji01

Reputation: 2615

Convert Tif document to PDF with PdfSharp

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

Upvotes: 4

Views: 12873

Answers (2)

Steve Wellens
Steve Wellens

Reputation: 20620

[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);
}

Upvotes: 10

Daniel Mann
Daniel Mann

Reputation: 59020

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.

Upvotes: 1

Related Questions