PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sun Oct 13, 2024 11:13 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Sun Nov 19, 2023 4:23 pm 
Offline

Joined: Sun Nov 19, 2023 11:48 am
Posts: 5
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 :cry:

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


Top
 Profile  
Reply with quote  
PostPosted: Sun Nov 19, 2023 6:03 pm 
Offline

Joined: Sun Nov 19, 2023 11:48 am
Posts: 5
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 :cry:

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????


Top
 Profile  
Reply with quote  
PostPosted: Sun Nov 19, 2023 6:59 pm 
Offline

Joined: Sun Nov 19, 2023 11:48 am
Posts: 5
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


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 5 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:  
cron
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group