winforms 使用PdfSharp将Tif文档转换为PDF

e4eetjau  于 2022-11-16  发布在  其他
关注(0)|答案(3)|浏览(155)

我使用WinForms。在我的表单中,我有一个显示tif图像文档的图片框。我使用PdfSharp作为我的一个引用,将tif文档转换为pdf文档。好消息是我可以转换当前显示在图片框中的tif页面之一。
问题是当我有一个tif文档,有超过1页,我不能把他们都转换成一个单一的Pdf文件。例如,如果我有一个tif文档图像,包含5页,我想按下一个按钮,并把所有这些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);
        }
    }

5f0d552i

5f0d552i1#

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

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

tct7dpnv2#

自从我使用PdfSharp以来已经有一段时间了,但是你应该能够在你的图像上调用GetFrameCount方法,它会告诉你它有多少页。
然后,您可以使用SelectActiveFrame方法来选择哪个页面是活动的。

um6iljoc

um6iljoc3#

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

相关问题