【问题标题】:Convert Tif document to PDF with PdfSharp使用 PdfSharp 将 Tif 文档转换为 PDF
【发布时间】:2016-05-14 12:28:56
【问题描述】:

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

问题是当我有一个超过 1 页的 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);
        }
    }

【问题讨论】:

    标签: c# .net image winforms pdf


    【解决方案1】:

    我已经有一段时间没有使用 PdfSharp,但是您应该可以在您的图像上调用 GetFrameCount 方法,它会告诉您它有多少页。

    然后你可以使用SelectActiveFrame方法来选择哪个页面是活动的。

    【讨论】:

      【解决方案2】:

      [编辑] 添加了完整的工作代码...带有硬编码的路径。

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

      【讨论】:

      • 抱歉,我还是编程新手。谢谢你写出来让我学习:)
      • @SteveWellens - 这很棒,对我有用。我只看到一个小问题 - 我有一个 .tif 文件,一旦它转换为 .pdf - 它的一部分会被切断(在右侧)。在将其转换为 .pdf 或类似的东西之前,是否可能以某种方式稍微缩小?一些 .tif 文件的大小不同,我只是不确定如何解决这个问题以确保转换后的 pdf 文档显示 .tif 文件中的所有内容
      • 对于那些与@BobSki 有相同问题的人,请参阅stackoverflow.com/a/24199315/411115。我必须找到一种方法来解决这个问题,我通过将 tif 页面缩放到适合 pdf 页面尺寸的大小来做到这一点。我还必须查找如何缩放保持原始图像的纵横比。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-16
      • 1970-01-01
      • 2015-12-19
      • 1970-01-01
      • 2017-12-08
      • 1970-01-01
      相关资源
      最近更新 更多