PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

PDFSharp.Xamarin.Forms: add image to PDF from Resource
https://forum.pdfsharp.net/viewtopic.php?f=2&t=4151
Page 1 of 1

Author:  ganesh96 [ Tue Jun 30, 2020 11:46 pm ]
Post subject:  PDFSharp.Xamarin.Forms: add image to PDF from Resource

Hi all,

I have a Xamarin.Forms for Android and iOS, where I use PDFSharp.Xamarin.Forms to create a PDF-file and put some text on this with color. This file never gets saved on the phone, since it will only E-mail the file (with System.Net.Mail). This all works, but I have difficulties with the following:

I have an image (for Android in /Resources/drawable/image.jpg and for iOS in /Resources/image.jpg) which I want to add to the PDF-file. I Googled some examples, but nothing worked for me so far, since non of these examples are referring to the resources folder in Android and iOS.

Is it even possible to do this? If so, how?

Hope someone can help.

Author:  TH-Soft [ Wed Jul 01, 2020 12:15 pm ]
Post subject:  Re: PDFSharp.Xamarin.Forms: add image to PDF from Resource

AIUI the Xamarin version is a partial port of a partial port of the original PDFsharp.

With the original PDFsharp you can access embedded resources as `Stream` objects which you can use with `XImage` methods.

Author:  ganesh96 [ Wed Jul 01, 2020 1:54 pm ]
Post subject:  Re: PDFSharp.Xamarin.Forms: add image to PDF from Resource

Dear Thomas,

Do you have a code example for this?

Regards, Ganesh

Author:  TH-Soft [ Wed Jul 01, 2020 2:32 pm ]
Post subject:  Re: PDFSharp.Xamarin.Forms: add image to PDF from Resource

Look here:
https://stackoverflow.com/a/62646299/162529

You don't have to do the BASE64 encoding, just pass the Stream to the XImage class.

Just look how to use "GetManifestResourceStream(name)".

Author:  ganesh96 [ Fri Jul 03, 2020 4:03 pm ]
Post subject:  Re: PDFSharp.Xamarin.Forms: add image to PDF from Resource

Hi Thomas,

So I followed the solution given by you, but I do have an issue.

My code:
var assembly = Assembly.GetExecutingAssembly();
var name = "company.devision.current.Resources.image.jpg";

using (Stream stream = assembly.GetManifestResourceStream(name))
{
if (stream == null)
throw new ArgumentException("No resource with name " + name);

int count = (int)stream.Length;
byte[] data = new byte[count];
stream.Read(data, 0, count);

XImage im = XImage.FromStream(() => stream);
gfx.DrawImage(im, 0, 0, 250, 140);
}

I get the following error in the last line where I want to draw the image: System.NullReferenceException: 'Object reference not set to an instance of an object.'

gfx comes from 'XGraphics gfx = XGraphics.FromPdfPage(page);' and I use gfx through the entire code, so I don't think the problems is there.


Do you have any idea what might be wrong?

Regards, Ganesh

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/