将嵌入资源图像添加到 MigraDoc 文档
我想将图像添加到 MigraDoc 文档的标题中,但是在文档生成中硬编码文件系统路径对于许多问题来说都有点问题 - 最重要的是它让我有点恶心,而且,保持部署简单(r)。
在我看来,如果我可以将图像作为资源嵌入到程序集中,然后在 PDF 需要时将其提取出来,那似乎是理想的选择,但似乎没有任何直接或内置的方法可以做到这一点。有什么技巧或想法吗?
I'd like to add an image to the header of a MigraDoc document, but hardcoding a filesystem path in the document generation is slightly problematic for a number of issues - not the least of which is that it makes me a little queasy, but also, to keep deployment simple(r).
It would seem to me ideal if I could embed the image as a resource in the assembly, and just extract it when it was needed for the PDF, but there doesn't seem ot be any straightforward or built-in way to do this. Any tricks or ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
PDFSharp/MigraDoc 1.50 包含另一种方法来执行此操作。来自 MigraDoc wiki:
PDFSharp/MigraDoc 1.50 includes another way to do this. From the MigraDoc wiki:
不可以,MigraDoc 不允许这样做。有一个 hack,但它仅在您使用 ASP .NET 并且不使用文档预览时才有效。请参阅官方论坛中的这些帖子,其中详细解释了该问题:
您可以通过解决方法使用嵌入资源,即暂时保存它们并通过以下方式删除它们完成后的处理方法和析构函数。示例:
然后您可以通过
MigraDocObject.AddImage("temp.png");
使用它,但请务必随后删除图像或添加检查图像是否已存在以及是否具有正确的文件大小(如果用户更换它)。No, MigraDoc does not allow this. There is a hack, but it works only if you use ASP .NET and you are not using the document preview. See these threads in the official forum which explain the problem in detail:
You can use the embedded resources via a workaround, i.e. save them temporarily and delete them via the dispose method and the destructor after you are finished. Example:
Then you can use it via
MigraDocObject.AddImage("temp.png");
But be sure to delete the image afterwards or add a check if the image already exists and if it has the correct file size (in case the user replaced it).