如何在pdfsharp.dll的帮助下在pdf文件中添加图像 [英] How to add a image in pdf file with the help of pdfsharp.dll
本文介绍了如何在pdfsharp.dll的帮助下在pdf文件中添加图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发Windows应用程序以在pdfsharp.dll文件的帮助下生成pdf。我想用pdf生成图像。在添加图像时,我收到了一个错误,例如
不支持给定路径的格式。
我的代码是
XImage img = XImage.FromFile(C:/Users/D3/Desktop/img1.jpg);
I'm developing windows application to generate pdf with the help of pdfsharp.dll file.I want to generate a image in pdf.While adding the image,i got an error like
The given path's format is not supported.
My code is
XImage img = XImage.FromFile("C:/Users/D3/Desktop/img1.jpg");
推荐答案
string pdfpath = Server.MapPath(PDFs);
string imagepath = Server.MapPath(Images);
文档doc = new Document();
try
{
PdfWriter.GetInstance(doc,new FileStream(pdfpath +/ Image.pdf,FileMode.Create));
doc.Open();
doc.Add(新段落(GIF));
图像gif = Image.GetInstance(imagepath +/ mikesdotnetting。 gif);
doc.Add(gif);
}
catch(Exception ex)
{
//记录错误;
}
最后
{
doc。 C lost();
}
string pdfpath = Server.MapPath("PDFs");
string imagepath = Server.MapPath("Images");
Document doc = new Document();
try
{
PdfWriter.GetInstance(doc, new FileStream(pdfpath + "/Images.pdf", FileMode.Create));
doc.Open();
doc.Add(new Paragraph("GIF"));
Image gif = Image.GetInstance(imagepath + "/mikesdotnetting.gif");
doc.Add(gif);
}
catch (Exception ex)
{
//Log error;
}
finally
{
doc.Close();
}
此代码适用于TIFF文件!
您的解决方案可能是这样的:
This code works well with TIFF files!
Your solution can be something like this:
string name = @"path\of\your\image";
string dest = @"path\of\destination";
// each source file separate
PdfSharp.Pdf.PdfDocument doc = new PdfSharp.Pdf.PdfDocument();
XImage img = XImage.FromFile(name);
img.Interpolate = false;
int width = img.PixelWidth;
int height = img.PixelHeight;
PdfSharp.Pdf.PdfPage page = new PdfSharp.Pdf.PdfPage
{
Width = width,
Height = height
};
doc.Pages.Add(page);
XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[0]);
xgr.DrawImage(img, 0, 0, width, height);
img.Dispose();
xgr.Dispose();
// save to destination file
FileInfo fi = new FileInfo(name);
doc.Save(dest + "\\" + name + ".PDF");
doc.Dispose();
这篇关于如何在pdfsharp.dll的帮助下在pdf文件中添加图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文