1

I'm using the PDFsharp library to do some simple manipulation of PDF files.
I have the following code to copy an image from a folder into an existing PDF document - it works as expected:

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);
}

To make the solution more portable I've moved the image file AnImage.png into the projects resources - here:

Properties.Resources.AnImage

But what changes do I need to make to the code in order to use the resources file rather than the file saved in the C-Drive?

whytheq
  • 34,466
  • 65
  • 172
  • 267
  • 2
    Possible duplicate of [How to get path of Properties.Resources.Image in .NET](https://stackoverflow.com/questions/14265492/how-to-get-path-of-properties-resources-image-in-net) You can get the image using the last answer in this post by doing `XImage imageMu = XImage.FromGdiPlusImage(Properties.Resources.AnImage);` – Ryan Wilson Jan 21 '19 at 20:47
  • To get projects resources you would do `Assembly.GetExecutingAssembly().GetManifestResourceStream("Properties.Resources.AnImage.png")` then `XImage.FromStream`? Not sure if that's part of PDFSharp api. – penleychan Jan 21 '19 at 20:49
  • Also found this on their wiki, maybe this will help you: http://www.pdfsharp.net/wiki/MigraDoc_FilelessImages.ashx?HL=stream – penleychan Jan 21 '19 at 20:51

3 Answers3

2

You can use the method FromGdiPlusImage of pdfsharp, like so:

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

Answer comes from another Stack Overflow post and answer at:

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

If as you said in your comment, you can't use FromGdiPlusImage there is the option of loading it as a stream, this was pulled from another Stack Overflow post:

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# - answer by David Icardi

Ryan Wilson
  • 10,223
  • 2
  • 21
  • 40
  • hmmm ... I'm getting a message "'XImage' does not contain a definition for 'FromGdiPlusImage'" .... in XImage.cs I can see a commented out static method FromGdiPlusImage - what do I need to do to be able to get access to it? – whytheq Jan 22 '19 at 11:16
  • @whytheq If it is commented out, can't you just uncomment it? – Ryan Wilson Jan 22 '19 at 13:15
  • @whytheq If you can't uncomment and use `FromGdiPlusImage` I added to my answer, which is just a reference to another post on the site. – Ryan Wilson Jan 22 '19 at 13:25
1

You can use XImage.FromStream after obtaining a stream for the image resource.

BTW: It is more efficient and will probably create smaller PDFs if you create the XImage only once and use it for all pages.

0

Convert the Resource to bytes:

            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
  • This post does not answer the question here! The question is about PDFsharp (XImage class), the answer is for MigraDoc (Image class). A better solution for MigraDoc can be found here: http://pdfsharp.net/wiki/MigraDoc_FilelessImages.ashx – I liked the old Stack Overflow Nov 20 '20 at 07:56