无法使用 PDFsharp 将图像添加到 PDF 文件

Cannot add image to PDF file using PDFsharp

我正在使用 ASP.NET 应用程序,我正在尝试向 PDF 文件添加徽标,但当我没有显示完整路径时,它说 logo.png 不存在。

我试过很多方法来添加这张图片,但除了完整路径外没有任何效果。

这是我的代码:

const string PngSamplePath = "/web_images/logo.png";
XImage image = XImage.FromFile(PngSamplePath);
gfx.DrawImage(image, 220, 120, 50, 50);

FromFile 等待徽标的完整路径。

所以你必须使用 MapPath 将你的相对路径 (~/web_images/logo.png) 解析为完整路径。

ASP.NET MVC1 -> MVC3

string path = HttpContext.Current.Server.MapPath("~/web_images/logo.png");

ASP.NET MVC4

string path = Server.MapPath("~/web_images/logo.png");

现在 path 变成即::C\MyApplication\web_images\logo.png

然后:

XImage image = XImage.FromFile(path)

很难说没有看到你的目录,但是你总是可以使用波浪号 (~) 指向你的应用程序目录的根目录并从那里构建。

"~/your/file/path.png"