如何在PDFsharp中使用资源图像?

时间:2019-01-21 20:09:16

标签: c# pdfsharp

我正在使用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中的文件,我需要对代码进行哪些更改?

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

您可以使用FromGdiPlusImage中的方法pdfsharp,如下所示:

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的回答

答案 2 :(得分:-1)

将资源转换为字节:

            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