使用PdfSharp将Tif文档转换为PDF

4
我正在使用WinForms。在我的表单中,我有一个picturebox显示tif图像文档。我使用PdfSharp作为我的引用之一来将tif文档转换为pdf文档。好消息是,我可以转换当前在picturebox中显示的tif页面之一。
问题在于,当我有一个包含多个页面的tif文档时,我无法将它们全部转换为单个Pdf文件。例如,如果我有一个包含5个页面的tif文档图像,则希望按下按钮并将所有这些5个tif页面转换为5个pdf页面。
以下是一个具有5个页面的tif文档进行测试。
链接:http://www.filedropper.com/sampletifdocument5pages 我的代码:
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

3个回答

10

[编辑] 添加了完整的可工作代码...路径已经硬编码。

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

抱歉,我还是编程新手。感谢您写下来让我学习 :) - taji01
@SteveWellens - 这很棒,对我有用。我只看到一个小问题 - 我有一个 .tif 文件,一旦转换为 .pdf - 其中的一部分会被切掉(在右侧)。在将其转换为 .pdf 或类似操作之前,是否可能以某种方式略微缩小视图?一些 .tif 文件的大小不同,我不确定如何解决此问题,以确保转换后的 pdf 文档显示 .tif 文件中的所有内容。 - BobSki
对于那些遇到与@BobSki相同问题的人,请参见https://dev59.com/SXI-5IYBdhLWcg3wTWdu#24199315。我不得不找到一种解决方法,通过将tif页面缩放到适合pdf页面尺寸的大小来解决此问题。我还必须查找如何保持原始图像的纵横比例缩放的方法。 - Daniel Lee
对于像@BobSki一样遇到同样问题的人:您可以调用DrawImage并增加两个参数来指定PDF中图像的大小。这使您能够在可用区域内按原样绘制图像。缩小大型图像以减小最终PDF文件的大小仍然是有意义的。 - I liked the old Stack Overflow

1

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

然后,你可以使用SelectActiveFrame方法选择哪一页是活动的。


-1

PDFsharp-gdi

(程序相关)
public static void ToPDF() {
try
{
    PdfDocument doc = new PdfDocument();
    string source = @"D:\TIF\bb.tif";
    string destinaton = @"D:\TIF\bb.pdf";
    Image img = Image.FromFile(source);

    for (int PageIndex = 0; PageIndex < img.GetFrameCount(FrameDimension.Page); PageIndex++)
    {
        img.SelectActiveFrame(FrameDimension.Page, PageIndex);
        XImage xImg = XImage.FromGdiPlusImage(img);
        double width = Math.Ceiling((double)(xImg.Width * 72) / 96);
        double height = Math.Ceiling((double)(xImg.Height * 72) / 96);
        var page = new PdfPage();
        if (xImg.Width > xImg.Height)
        {
            page.Orientation = PdfSharp.PageOrientation.Landscape;
        }
        else
        {
            page.Orientation = PdfSharp.PageOrientation.Portrait;
        }
        page.Width = width;
        page.Height = height;
        doc.Pages.Add(page);
        XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[PageIndex]);
        xgr.DrawImage(xImg, 0, 0, width, height);
    }

    doc.Save(destinaton);
    doc.Close();
    img.Dispose();
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message.ToString());
}
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接