C# 使用PdfSharp将Tif文档转换为PDF

C# 使用PdfSharp将Tif文档转换为PDF,c#,.net,image,winforms,pdf,C#,.net,Image,Winforms,Pdf,我正在使用WinForms。在我的表格中,我有一个显示tif图像文档的picturebox。我使用PdfSharp作为我的参考之一,将tif文档转换为pdf文档。好消息是我可以转换当前显示在picturebox中的一个tif页面 问题是当我有一个tif文档有超过1页时,我无法将它们全部转换为单个Pdf文件。例如,如果我有一个包含5页的tif文档图像,我想按下一个按钮,将所有这5个tif页面转换为5个pdf页面 对于测试,这里有一份5页的tif文档 链接: using PdfSharp; usin

我正在使用WinForms。在我的表格中,我有一个显示tif图像文档的picturebox。我使用PdfSharp作为我的参考之一,将tif文档转换为pdf文档。好消息是我可以转换当前显示在picturebox中的一个tif页面

问题是当我有一个tif文档有超过1页时,我无法将它们全部转换为单个Pdf文件。例如,如果我有一个包含5页的tif文档图像,我想按下一个按钮,将所有这5个tif页面转换为5个pdf页面

对于测试,这里有一份5页的tif文档

链接:

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);
        }
    }
我的代码:

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

我已经有一段时间没有使用PdfSharp了,但是您应该能够在图像上调用
GetFrameCount
方法,该方法将告诉您它有多少页


然后,您可以使用
SelectActiveFrame
方法选择哪个页面处于活动状态。

[Edit]添加了完整的工作代码…带有硬编码的路径

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);
}
试试看
{
字符串destinaton=@“C:\Temp\Junk\new\u PDF\u TIF\u Document.PDF”;
Image MyImage=Image.FromFile(@“C:\Temp\Junk\Sample tif document 5 pages.tiff”);
PdfDocument doc=新PdfDocument();
对于(int PageIndex=0;PageIndeximg.高度)
{
page.Orientation=PageOrientation.Landscape;
}
其他的
{
page.Orientation=页面方向.纵向;
}
单据页.新增(页);
XGraphics xgr=XGraphics.FromPdfPage(文档页[PageIndex]);
xgr.DrawImage(img,0,0);
}
单据保存(目的地);
doc.Close();
MyImage.Dispose();
Show(“文件保存成功!”,“成功”,MessageBoxButtons.OK,MessageBoxIcon.Information);
}
捕获(例外情况除外)
{
MessageBox.Show(例如Message,“Error”,MessageBoxButtons.OK,MessageBoxIcon.Error);
}

对不起,我还是个编程新手。谢谢你把它写出来让我学习:)@SteveWellens-这很好,对我很有用。我只看到一个小问题——我有一个.tif文件,一旦它转换成.pdf,它的一部分就会被切断(在右边)。在转换成.pdf或类似的格式之前,它是否可能稍微缩小一些?有些.tif文件大小不同,我不知道如何处理这个问题,以确保转换后的pdf文档显示.tif文件中的所有内容。对于那些与@BobSki有相同问题的人,请参阅。我必须找到解决这个问题的方法,我通过将tif页面缩放到适合pdf页面尺寸的大小来做到这一点。我还必须研究如何缩放以保持原始图像的纵横比。