PDFsharp & MigraDoc Foundation http://forum.pdfsharp.de/ |
|
Inserting images in pdfsharpcore / migradoccore http://forum.pdfsharp.de/viewtopic.php?f=2&t=4716 |
Page 1 of 1 |
Author: | Brovion [ Mon Nov 04, 2024 8:57 am ] |
Post subject: | Inserting images in pdfsharpcore / migradoccore |
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; } } |
Author: | TH-Soft [ Mon Nov 04, 2024 9:47 am ] |
Post subject: | Re: Inserting images in pdfsharpcore / migradoccore |
Please try our library PDFsharp instead. We do not use IImageSource. https://www.nuget.org/profiles/PDFsharp-Team |
Author: | Brovion [ Tue Nov 05, 2024 5:10 pm ] |
Post subject: | Re: Inserting images in pdfsharpcore / migradoccore |
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? |
Author: | TH-Soft [ Tue Nov 05, 2024 5:29 pm ] |
Post subject: | Re: Inserting images in pdfsharpcore / migradoccore |
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: Which manual?if (Capabilities.Build.IsCoreBuild) GlobalFontSettings.FontResolver = new FailsafeFontResolver(); 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. Alternative: Load the image into a byte[], convert it to a BASE64 string and pass that in the path parameter.So what am I missing when it comes to the paths? Details here: https://www.pdfsharp.net/wiki/MigraDoc_ ... mages.ashx |
Author: | Brovion [ Wed Nov 06, 2024 7:41 am ] |
Post subject: | Re: Inserting images in pdfsharpcore / migradoccore |
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. |
Author: | Brovion [ Wed Nov 06, 2024 4:02 pm ] |
Post subject: | Re: Inserting images in pdfsharpcore / migradoccore |
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). |
Author: | TH-Soft [ Wed Nov 06, 2024 5:11 pm ] |
Post subject: | Re: Inserting images in pdfsharpcore / migradoccore |
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. |
Author: | Brovion [ Thu Nov 07, 2024 12:58 pm ] |
Post subject: | Re: Inserting images in pdfsharpcore / migradoccore |
Ok, thanks. I'll try with other images. |
Page 1 of 1 | All times are UTC |
Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |