问题描述
我正在使用WinForms.在我的表单中,我有一个显示tif图像文档的图片框.我将PdfSharp用作将tif文档转换为pdf文档的参考之一.好消息是,我可以转换当前在图片框中显示的tif页面之一.
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.
问题是,当我拥有一页以上的tif文档时,无法将它们全部转换成单个Pdf文件.例如,如果我有一个包含5页的tif文档图像,我想按一个按钮并将所有5个tif页转换为5个pdf页.
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.
为了测试,这里有一个5页的tif文档.
For testing here is a tif document with 5 pages.
链接: 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);
}
}
推荐答案
添加了完整的工作代码...带有硬编码的路径.
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);
}
这篇关于使用PdfSharp将Tif文档转换为PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!