PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 12:42 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Tue Jun 30, 2020 11:46 pm 
Offline

Joined: Tue Jun 30, 2020 11:39 pm
Posts: 3
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.


Top
 Profile  
Reply with quote  
PostPosted: Wed Jul 01, 2020 12:15 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
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.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Wed Jul 01, 2020 1:54 pm 
Offline

Joined: Tue Jun 30, 2020 11:39 pm
Posts: 3
Dear Thomas,

Do you have a code example for this?

Regards, Ganesh


Top
 Profile  
Reply with quote  
PostPosted: Wed Jul 01, 2020 2:32 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
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)".

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Fri Jul 03, 2020 4:03 pm 
Offline

Joined: Tue Jun 30, 2020 11:39 pm
Posts: 3
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


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 127 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group