PDFsharp & MigraDoc Foundation http://forum.pdfsharp.de/ |
|
Add pdf as image to section http://forum.pdfsharp.de/viewtopic.php?f=4&t=4512 |
Page 1 of 1 |
Author: | truthz03 [ Sun Nov 19, 2023 4:23 pm ] |
Post subject: | Add pdf as image to section |
Hi, until today I used PdfShard from this repository: https://github.com/myvas/PdfSharpCore/t ... .0-beta5-3 Today I found out that the official PdfSharp now also supports .Net6 and so I updated to the new 6.0.0 version. After fixing some behaviour changed I can start and test everything but I miss a feature. The repository from above has support to add pdf as image to a section. I compared the source of the forked repo and the official repo and found the difference. Code: public static XImage FromStream(Stream stream) { if (stream == null) throw new ArgumentNullException("stream"); if (PdfReader.TestPdfFile(stream) > 0) // This row is new return new XPdfForm(stream); // This row is new return new XImage(stream); } Is it possible to also add the check if the stream is a pdf and than return an XPdfForm instead of an XImage? Without this check there will be an error inside the generated pdf wich says that the image has a wrong form I don't want to use the forked repository anymore because the last changes where made some years ago. It would be nice to be able to use the official repo. Thanks |
Author: | truthz03 [ Sun Nov 19, 2023 6:03 pm ] |
Post subject: | Re: Add pdf as image to section |
truthz03 wrote: Hi, until today I used PdfShard from this repository: https://github.com/myvas/PdfSharpCore/t ... .0-beta5-3 Today I found out that the official PdfSharp now also supports .Net6 and so I updated to the new 6.0.0 version. After fixing some behaviour changed I can start and test everything but I miss a feature. The repository from above has support to add pdf as image to a section. I compared the source of the forked repo and the official repo and found the difference. Code: public static XImage FromStream(Stream stream) { if (stream == null) throw new ArgumentNullException("stream"); if (PdfReader.TestPdfFile(stream) > 0) // This row is new return new XPdfForm(stream); // This row is new return new XImage(stream); } Is it possible to also add the check if the stream is a pdf and than return an XPdfForm instead of an XImage? Without this check there will be an error inside the generated pdf wich says that the image has a wrong form I don't want to use the forked repository anymore because the last changes where made some years ago. It would be nice to be able to use the official repo. Thanks Ok, I now downloaded the source code from the Branch "Branch_v6.0.0" and the code looks totally different in comparison to the decompiled version of the Nuget "PDFsharp-MigraDoc (6.0.0)" from VisualStudio. SourceCode of the Branch "Branch_v6.0.0": Code: public static XImage FromStream(Stream stream) { if (stream == null) throw new ArgumentNullException(nameof(stream)); if (PdfReader.TestPdfFile(stream) > 0) return new XPdfForm(stream); return new XImage(stream); } Decompiled source code of the Nuget "PDFsharp-MigraDoc (6.0.0)": Code: public static XImage FromStream(Stream stream) { if (stream == null) { throw new ArgumentNullException("stream"); } return new XImage(ImageImporter.GetImageImporter().ImportImage(stream) ?? throw new InvalidOperationException("Unsupported image format.")) { _stream = stream }; } How is this possible???? |
Author: | truthz03 [ Sun Nov 19, 2023 6:59 pm ] |
Post subject: | Re: Add pdf as image to section |
I have now compiled and tested against the current "Branch_v6.0.0" and it seems that there is something missing. Internally it calls "XImage FromStream(Stream stream)" if I add a base64 pdf with "section.AddImage". The problem is that it uses the code inside the "CORE" section and inside the "CORE" section there is not check if the stream is a pdf. The "GDI/WPF" sections contains such a check I have now added the check also to the "CORE" section and recompiled the code. Now it is working correctly. Code: #if CORE
/// <summary> /// Creates an image from the specified file. /// </summary> /// <param name="path">The path to a BMP, PNG, JPEG, or PDF file.</param> public static XImage FromFile(string path) { if (PdfReader.TestPdfFile(path) > 0) return new XPdfForm(path); var ii = ImageImporter.GetImageImporter(); var i = ii.ImportImage(path); if (i == null) throw new InvalidOperationException("Unsupported image format."); var image = new XImage(i); image._path = path; return image; } /// <summary> /// Creates an image from the specified stream.<br/> /// </summary> /// <param name="stream">The stream containing a BMP, PNG, JPEG, or PDF file.</param> public static XImage FromStream(Stream stream) { if (stream == null) throw new ArgumentNullException(nameof(stream)); if (PdfReader.TestPdfFile(stream) > 0) //This was missing return new XPdfForm(stream); //This was missing var ii = ImageImporter.GetImageImporter(); var i = ii.ImportImage(stream); if (i == null) throw new InvalidOperationException("Unsupported image format."); XImage image = new XImage(i); image._stream = stream; return image; } #endif |
Page 1 of 1 | All times are UTC |
Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |