- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 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);
}
}
最佳答案
[编辑] 添加了完整的工作代码......路径硬编码。
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);
}
关于c# - 使用 PdfSharp 将 Tif 文档转换为 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35208731/
我得到了具有其他语言字体的文件。他们无法下载字体,因此希望我将其嵌入pdf。 现在,我只有PDF阅读器,因此无法编辑或创建pdf文件。因此,我决定使用PDFSharp库在C#.NET中快速完成此操作,
使用 PDFsharp .NET 库,我需要将 PDF 文档中的页面大小设置为与图像相同。 示例,图像编号。 1 尺寸为 152 像素 x 1775 像素。 图片编号2 个 100 像素 x 1582
在 Migradoc 和 PDFSharp 示例页面中,有一个将 Migradoc 文档绘制为 PDFSharp 文档:http://www.pdfsharp.net/wiki/MixMigraDoc
我对设置XGraphics的剪切区域有点困惑,pdfSharp只提供了一个方法IntersectClip,这意味着如果我想改变剪切区域,该区域会越来越小(与之前的区域相交),有没有人知道无论如何自由设
在 Migradoc 和 PDFSharp 示例页面中,有一个将 Migradoc 文档绘制为 PDFSharp 文档:http://www.pdfsharp.net/wiki/MixMigraDoc
发现 PDFSharp 在使用比例变换以不同比例绘制时存在问题。 在此示例中,我们以不同比例绘制两个矩形,从 FixedDocumentSequence 生成 XPS,最后使用 PDFsharps X
我正在尝试获取新行,但如果我使用 \n 它不起作用。 任何通过向字符串添加一些东西来换行的方法,例如 \r\n (这也不起作用) gfx.DrawString("Project No \n" + te
是否可以从例如缩放页面? A2 到 A1 与 PDFsharp?我可以通过大小、宽度和高度来设置页面的大小。但是如何缩放页面的内容呢? 最佳答案 根据 Vive 的评论和那里提供的链接,这里有一个使用
如何在渲染之前获取页数?我需要在我的文档中将数字写成单词。 最佳答案 对于页码作为数字: 您不必获取编号,您可以在文档中添加占位符(称为字段),并且 MigraDoc 将在呈现文档时自动插入页码。 见
我正在使用 MigraDoc 创建 PDF。根据我的要求,我必须根据页面\部分的内容添加不同的标题。我可以通过添加新部分并为每个部分定义新标题来实现它。到这里为止,一切正常。 在页脚中,我使用“Add
希望有人可能遇到与此相同的问题并找到解决方案。 我正在尝试将表中的某些行设置为分组,以便如果一行落入下一页,其他行也会进入下一页。 我正在使用 PdfSharp 和 Migradoc 将行 KeepW
无论我是否使用 XTextFormatter,我都会遇到关于 LayoutRectangle 的高度必须为 0 或类似的错误。 new PdfSharp.Drawing.Layout.XTextFor
我正在使用 PDFsharp绘制形状相当复杂的线条。为了简化这个过程,让它更容易理解,我想把原点从页面的左上角改为左下角。目前,更改原点是 not yet implemented .有没有办法可以使用
我得到了以下代码,用于将两个文本框内容简单地添加到 pdf 文件中: using System; using System.Windows.Forms; using PdfSharp.Pdf; usi
我之前使用 PDFsharp 进行了一些文件合并,现在我正在尝试更改几个文件(插入或删除一些页面)并且我遇到了问题,即图书馆看不到页面。它说 PageCount == 0 并且我无法在对象中找到页面(
我正在尝试将我的位图保存在 .pdf 文件中。它不起作用。我没有收到错误,但它不会打开对话框来保存 .pdf 文件。 这是我的代码: PdfDocument doc = new PdfDocument
我在 C# 的 PDFSharp 中显示中文字符时遇到问题。在创建 PDF 字符串的过程中没问题,但在创建 pdf 文件后它不显示它。我找到了一个解决方案,它是 XFont font_small2 =
我目前正在研究 PDF 生成器。我需要先绘制背景图像,然后绘制一个透明度为 85% 的深色图层。我可以很好地绘制它,但是当我想在那之后绘制两个图像时,那些图像也获得了透明度,这不是我想要的。 XBru
是否可以为 PDFsharp 库找到好的文档? 我在 Google 上进行了搜索,但没有找到任何引用文档。 我不知道 XGraphics.RotateAtTransform 和 XGraphics.T
PDFSharp 支持在绘制长文本部分时自动换行: textFormatter.DrawString(text, font, XBrushes.Black, new XRect(x, y, textA
我是一名优秀的程序员,十分优秀!