PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Dec 05, 2024 10:24 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: Mon Nov 04, 2024 8:57 am 
Offline

Joined: Sun Nov 03, 2024 4:51 pm
Posts: 5
I'm using .Net Core not old-school ASP.NET and I am trying to insert an image into a document. First I tried the bit in the manual:

string imgFile = "assets/img/logo.jpeg";
section.AddImage(imgFile)

That actually wouldn't compile as it gave the error:
Cannot convert from 'string' to 'MigraDocCore.DocumentObjectModel.MigraDoc.DocumentObjectModel.Shapes.ImageSource.IImageSource

So I tried to convert it like so:
if (ImageSource.ImageSourceImpl == null)
ImageSource.ImageSourceImpl = new ImageSharpImageSource<Rgba32>();

section.AddImage(ImageSource.FromFile(imgFile));

AddImage() now no longer errors at build time, but it still errors at run time always fails to find my file. The reason I think is that it always prepends a slash, i.e in the error exception I can see it's trying to find '/assets/img/logo.jpeg'.

So I gave up that approach, googled a bit more and tried to load it as a bytestream:
byte[] image = LoadImage(imgFile);

The LoadImage() function (below) fails with a null stream as it still can't find the image - although at least it now doesn't prepend the name with a slash.

Thinking there was something wrong with my images I tried it with a url. Several URLs in fact with a variety of image sources. Here's one:
byte[] image = LoadImage("https://forum.pdfsharp.net/styles/subsilver2/imageset/site_logo.gif");

All throw the 'stream == null' error.

What am I missing and/or doing wrong? Is it something to do with .Net Core?

Thanks



-----------------------------------------------------------
FUNCTION:
static byte[] LoadImage(string name)
{
var assembly = Assembly.GetExecutingAssembly();

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


Top
 Profile  
Reply with quote  
PostPosted: Mon Nov 04, 2024 9:47 am 
Offline
PDFsharp Guru
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 1019
Location: CCAA
Please try our library PDFsharp instead.
We do not use IImageSource.

https://www.nuget.org/profiles/PDFsharp-Team

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


Top
 Profile  
Reply with quote  
PostPosted: Tue Nov 05, 2024 5:10 pm 
Offline

Joined: Sun Nov 03, 2024 4:51 pm
Posts: 5
Thank you. I have some progress. I de-installed pdfsharpcore and migradoccore and installed PDFSharp 6.11 and PDFSharp-MigraDoc 6.11 I threw away all the stuff I'd built for having my own font resolver and just used the suggestion in the manual:

if (Capabilities.Build.IsCoreBuild)
GlobalFontSettings.FontResolver = new FailsafeFontResolver();

The good news is section.AddImage() now accepts a string, and my app no longer errors! Woo hoo! All the text prints, indeed it's better than before as it now implements the 'Bold' settings which it wasn't (visibly) doing before.

However it still can't find the image despite me trying a number of permutations of paths, however I now get a nice box saying 'image not found' as opposed to a 'not found' exception being thrown.

So what am I missing when it comes to the paths? I've obviously googled it and being on .Net core I can't use Server.MapPath()


Also as an aside if I look at the implementation of 'Capabilities' above it says 'UNDER CONSTRUCTION - DO NOT USE'. That isn't part of the issue is it?


Top
 Profile  
Reply with quote  
PostPosted: Tue Nov 05, 2024 5:29 pm 
Offline
PDFsharp Guru
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 1019
Location: CCAA
Brovion wrote:
I threw away all the stuff I'd built for having my own font resolver and just used the suggestion in the manual:

if (Capabilities.Build.IsCoreBuild)
GlobalFontSettings.FontResolver = new FailsafeFontResolver();
Which manual?
Basic rule: Do not use FailsafeFontResolver with production code, write your own FontResolver.
Exception: Segoe WP is the only font you need.


Brovion wrote:
However it still can't find the image despite me trying a number of permutations of paths, however I now get a nice box saying 'image not found' as opposed to a 'not found' exception being thrown.

So what am I missing when it comes to the paths?
Alternative: Load the image into a byte[], convert it to a BASE64 string and pass that in the path parameter.
Details here:
https://www.pdfsharp.net/wiki/MigraDoc_ ... mages.ashx

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


Last edited by TH-Soft on Wed Nov 06, 2024 11:59 am, edited 1 time in total.

Top
 Profile  
Reply with quote  
PostPosted: Wed Nov 06, 2024 7:41 am 
Offline

Joined: Sun Nov 03, 2024 4:51 pm
Posts: 5
TH-Soft wrote:
Which manual?
Basic rule: Do not use FailsafeFontResolver with production code, write your own FontResolver.
Exception: Your code only runs on your computers/servers.

This manual:
https://docs.pdfsharp.net/PDFsharp/Topi ... t-PDF.html

Yes, I'll go back to trying the byte array and my own font resolver. I'll report back later.


Top
 Profile  
Reply with quote  
PostPosted: Wed Nov 06, 2024 4:02 pm 
Offline

Joined: Sun Nov 03, 2024 4:51 pm
Posts: 5
Ok, some more progress. Using the byte array method (and adding them as embedded resources in my project) the system now finds the images - but it still won't render them.

JPEG and BMP say "Image could not be read"
SVG says "Image has no valid type" (Fair enough I didn't see that as a valid type, I just wanted to see what would happen).


Top
 Profile  
Reply with quote  
PostPosted: Wed Nov 06, 2024 5:11 pm 
Offline
PDFsharp Guru
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 1019
Location: CCAA
Brovion wrote:
JPEG and BMP say "Image could not be read"
The most common variants of JPEG and BMP work.

If we can replicate the issue, we will investigate it:
https://docs.pdfsharp.net/General/Issue-Reporting.html

If it's a PDFsharp problem then it should be replicable even under Windows.

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


Top
 Profile  
Reply with quote  
PostPosted: Thu Nov 07, 2024 12:58 pm 
Offline

Joined: Sun Nov 03, 2024 4:51 pm
Posts: 5
Ok, thanks. I'll try with other images.


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

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 11 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