【问题标题】:How do I use a Resources Image with PDFsharp?如何在 PDFsharp 中使用资源图像?
【发布时间】:2022-04-04 21:44:18
【问题描述】:

我正在使用 PDFsharp 库对 PDF 文件进行一些简单的操作。
我有以下代码将图像从文件夹复制到现有的 PDF 文档中 - 它按预期工作:

public void AddImagePDF()
{
  this.DrawPage(this.PDFdoc.Pages[0]);
  this.DrawPage(this.PDFdoc.Pages[1]);
  this.DrawPage(this.PDFdoc.Pages[2]);
}
private void DrawPage(PdfPage page)
{
  XGraphics gfx = XGraphics.FromPdfPage(page);
  DrawPng(gfx);
}
private void DrawPng(XGraphics gfx)
{

  XImage imageMu = XImage.FromFile(@"C:\Images\AnImage.png");
  double width = imageMu.PixelWidth * 7.0 / imageMu.HorizontalResolution;
  double height = imageMu.PixelHeight * 7.0 / imageMu.HorizontalResolution;
  gfx.DrawImage(imageMu,500,30,width,height); 

  this.PDFdoc.Save(this.DestinationFullPath);
}

为了使解决方案更便携,我已将图像文件 AnImage.png 移动到项目资源中 - 此处:

Properties.Resources.AnImage

但是我需要对代码进行哪些更改才能使用资源文件而不是保存在 C-Drive 中的文件?

【问题讨论】:

标签: c# pdfsharp


【解决方案1】:

你可以使用pdfsharp的方法FromGdiPlusImage,像这样:

XImage imageMu = XImage.FromGdiPlusImage(Properties.Resources.AnImage);

答案来自另一个 Stack Overflow 帖子,答案在:

How to get path of Properties.Resources.Image in .NET

如果正如您在评论中所说,您不能使用FromGdiPlusImage,则可以选择将其作为流加载,这是从另一个 Stack Overflow 帖子中提取的:

System.Reflection.Assembly thisExe = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream file = 
    thisExe.GetManifestResourceStream("Properties.Resources.AnImage.png");
Ximage yourImage = XImage.FromStream(file);

Load image from resources area of project in C# - David Icardi 的回答

【讨论】:

  • hmmm ...我收到一条消息“'XImage'不包含'FromGdiPlusImage'的定义” ....在XImage.cs中我可以看到一个注释掉的静态方法FromGdiPlusImage -我需要做什么才能访问它?
  • @whytheq 如果被注释掉了就不能取消注释吗?
  • @whytheq 如果您不能取消注释并使用 FromGdiPlusImage 我添加到我的答案中,这只是对网站上另一篇帖子的引用。
【解决方案2】:

获取图片资源流后可以使用XImage.FromStream

顺便说一句:如果您只创建一次 XImage 并将其用于所有页面,它可能会更有效并且可能会创建更小的 PDF。

【讨论】:

    【解决方案3】:

    将资源转换为字节:

                using System;
                using System.Drawing;
                using System.IO;
                using ITNETAWF.Properties; //WinForms Project
    
                using PDFDocument = MigraDoc.DocumentObjectModel.Document;
                using PDFImage = MigraDoc.DocumentObjectModel.Shapes.Image;
                using PDFRelativeHorizontal = MigraDoc.DocumentObjectModel.Shapes.RelativeHorizontal;
                using PDFRelativeVertical = MigraDoc.DocumentObjectModel.Shapes.RelativeVertical;
                using PDFSection = MigraDoc.DocumentObjectModel.Section;
                using PDFShapePosition = MigraDoc.DocumentObjectModel.Shapes.ShapePosition;
                using PDFWrapStyle = MigraDoc.DocumentObjectModel.Shapes.WrapStyle;
    
    
                namespace PdfSharpImage
                {
                    public class Class1
                    {
                        private void CreatePage()
                        {
                            byte[] zbytData;
                            Bitmap zbmpData;
                            string zstrDataB64;
                            MemoryStream zmstFlujoMemoria;
                            PDFDocument document;
                            PDFImage image;
                            PDFSection section;
    
    
                            // Each MigraDoc document needs at least one section.
                            document = new PDFDocument();
                            section = document.AddSection();
    
                            // Put a logo in the header
                            //---------------------------------------------------------------------------------------------------------------------------------------------------------------//
                            //  Logo MFG 225x32.png is an image in the .resx file 
                            //  Logo MFG 225x32.png  width/height=107/32 pixel  width/height=2.83/0.85 cm  width/height=1.11/0.33 inch
                            //  
                            //  Resources.resx file Code:
                            //  <data name="Logo MFG 225x32" type="System.Resources.ResXFileRef, System.Windows.Forms">
                            //    <value>..\Resources\Logo MFG 225x32.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
                            //  </data>
                            //---------------------------------------------------------------------------------------------------------------------------------------------------------------//
                            zbmpData = Resources.ResourceManager.GetObject("Logo MFG 225x32") as Bitmap;
                            zbytData = new byte[13750]; // 13750 - The size in bytes of the Image
                            zmstFlujoMemoria = new MemoryStream(zbytData);
                            zbmpData.Save(zmstFlujoMemoria, System.Drawing.Imaging.ImageFormat.Bmp);
                            zstrDataB64 = String.Format("base64:{0}", Convert.ToBase64String(zbytData));
                            //image = section.Headers.Primary.AddImage("../../PowerBooks.png");
                            image = section.Headers.Primary.AddImage(zstrDataB64);
                            //image.Height = "2.5cm";
                            image.Height = "1.11cm";
                            image.LockAspectRatio = true;
                            image.RelativeVertical = PDFRelativeVertical.Line;
                            image.RelativeHorizontal = PDFRelativeHorizontal.Margin;
                            image.Top = PDFShapePosition.Top;
                            image.Left = PDFShapePosition.Right;
                            image.WrapFormat.Style = PDFWrapStyle.Through;
                        }
                    }
                }
                // http://www.pdfsharp.net/wiki/Invoice-sample.ashx
                // http://www.pdfsharp.net/wiki/MigraDoc_FilelessImages.ashx
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-03
    • 1970-01-01
    • 1970-01-01
    • 2021-10-26
    • 2019-03-07
    • 2019-11-11
    • 1970-01-01
    相关资源
    最近更新 更多