PDFsharp 找不到图像(图像未找到)

发布于 2024-09-08 14:41:03 字数 316 浏览 3 评论 0原文

我在 ASP.NET MVC 应用程序中使用 PDFsharp。我想添加一个图像,但无论我把它放在哪个目录中,似乎都找不到它。当我尝试复制示例应用程序时,我有这样的代码

 Section section = document.AddSection();
 Image image13 = section.AddImage("../../images/logo.png");

无论我将此图像放在哪个目录中,生成 PDF 时,我都会在 PDF 上看到一条错误消息“找不到图像

还有其他人看到这个问题吗?

I am using PDFsharp in an ASP.NET MVC application. I want to add an image but no matter what directory I put it in, it can't seem to find it. I have code like this as I am trying to copy the sample application

 Section section = document.AddSection();
 Image image13 = section.AddImage("../../images/logo.png");

No matter what directory I put this image in, when the PDF gets generated, I see an error on the PDF saying "Image not found"

Has anyone else seen this issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无需解释 2024-09-15 14:41:03

它可能正在寻找完整路径?

尝试

Image image13 = section.AddImage(Server.MapPath("~/images/logo.png"));

It might be looking for a full path?

Try

Image image13 = section.AddImage(Server.MapPath("~/images/logo.png"));
巨坚强 2024-09-15 14:41:03

我在 asp.net mvc 应用程序中使用 pdfsharp。

顺便说一句:您使用的不是 PDFsharp,而是 MigraDoc。

MigraDoc 搜索相对于当前目录的图像。 ASPX 页面被编译到临时目录(而不是项目目录)并从临时目录执行。因此相对路径将无法按预期工作。

Assembly.CodeBase 可能有助于定位图像; Assembly.Location 表示临时目录。

Assembly.CodeBase 可以在 ASP.NET 和 .NET 之间共享的代码中使用。
也可以使用 Server.MapPath(按照 Marko 的建议),但它仅适用于 ASP.NET。

i am using pdfsharp in a asp.net mvc application.

BTW: You are not using PDFsharp, you are using MigraDoc.

MigraDoc searches the images relative to the current directory. ASPX pages are compiled to and are executed from a temporary directory, not from the project directory. Therefore relative paths will not work as expected.

Assembly.CodeBase might help to locate the images; Assembly.Location indicates the temporary directory.

Assembly.CodeBase can be used in code that is shared between ASP.NET and .NET.
Server.MapPath can also be used (as suggested by Marko), but it works in ASP.NET only.

琉璃梦幻 2024-09-15 14:41:03

MigraDoc Document 对象具有 ImagePath 属性,允许您指定将搜索图像的目录(用 semikola 分隔多个目录)。

如果可以找到相对于程序集位置的图像,则可以使用 Assembly.CodeBase 来定位程序集(如我之前的回答中所述)。

The MigraDoc Document object has an ImagePath property that allows you to specify the directories that will be searched for images (separate multiple directories with semikola).

If images can be found relative to the location of the assembly, then Assembly.CodeBase can be used to locate the assembly (as mentioned in my earlier answer).

半城柳色半声笛 2024-09-15 14:41:03

旧线程,但可能对某人有用

这对我有用:

Document doc = new Document();
doc.ImagePath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).Replace("file:\\", "");

然后我将图像设置为始终复制,并通过 bin 目录将其相对路径添加到我的 Web 项目中。

Old thread but might come in handy for someone

This worked for me:

Document doc = new Document();
doc.ImagePath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).Replace("file:\\", "");

I then set my image to copy always and added it with its relative path by bin directory in my Web project.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文