PDFsharp & MigraDoc Foundation

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

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: Tue Dec 24, 2019 5:38 pm 
Offline

Joined: Tue Dec 24, 2019 5:14 pm
Posts: 3
We are using PDFSharp to gather a series of images from a folder and put them into a PDF file, one image per page. For a certain set of images (jpgs, converted from pngs), the resulting PDF document appears corrupted when opening in Adobe, but looks good in Firefox. Is this a known issue, or something having to do with how we are building the file?

Here is the relevant code being used:

Code:
private void AddImageToPDF(string imagePath, ref PdfDocument doc)
{
    Image MyImage = Image.FromFile(imagePath);
    AddImageToPDF(MyImage, ref doc);
}

private void AddImageToPDF(Image image, ref PdfDocument doc)
{
    try
    {
        int numPages = doc.Pages.Count;
        using (Image MyImage = image)
        {
            for (int _pageIndex = 0; _pageIndex < MyImage.GetFrameCount(FrameDimension.Page); _pageIndex++)
            {
                MyImage.SelectActiveFrame(FrameDimension.Page, _pageIndex);

                XImage img = XImage.FromGdiPlusImage(MyImage);
                img.Interpolate = true;

                var page = new PdfPage() { Orientation = img.PixelWidth > img.PixelHeight ? PageOrientation.Landscape : PageOrientation.Portrait };
                doc.Pages.Add(page);

                using (var xg = XGraphics.FromPdfPage(doc.Pages[_pageIndex + numPages]))
                {
                    xg.DrawImage(img, 0, 0);
                }
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}


If anyone has any thoughts or advice, it would be much appreciated... I'm just about stumped on this one. I would attach the file itself but it has client-sensitive information; please let me know if there's any more information I can provide.


Top
 Profile  
Reply with quote  
PostPosted: Fri Dec 27, 2019 12:59 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
Hi!

Try Windows XP or Windows 2000 (MS messed something up with later Windows versions) or try XImage.FromFile instead of XImage.FromGdiPlusImage.

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


Top
 Profile  
Reply with quote  
PostPosted: Fri Dec 27, 2019 4:35 pm 
Offline

Joined: Tue Dec 24, 2019 5:14 pm
Posts: 3
Thank you for the reply! Unfortunately this is for a production interface, so I can't tell the client to use an OS that is no longer supported as a solution. XImage.FromFile produces the same results.


Top
 Profile  
Reply with quote  
PostPosted: Mon Dec 30, 2019 9:35 am 
Offline

Joined: Tue Aug 06, 2019 10:45 am
Posts: 45
I am also, currently having this problem,

I have noticed that there are a few bugs that complain about the same issue:
https://forum.pdfsharp.net/viewtopic.php?f=2&t=3219
https://forum.pdfsharp.net/viewtopic.php?f=3&t=1286
https://forum.pdfsharp.net/viewtopic.php?f=3&t=3304

these are just some..
You just need to search "migradoc jpg corrupted" on google and there are several results.

The problem seams to be related to only some .jpg images, I have 2 for example, one made from adobe ilustrator,m and another that I just grabed on the internet to test, and the adobe one is giving me the same problem, but the one I grabed on the net isn't.

the solution e most of them seams to be converting the image from .jpg to .png
this is obviously quite ineficient and I would appretiate if there was a better way to resolve this, since I am also currently having this problem.



Hope this answer helps somehow.


Last edited by IRlyDunno on Mon Dec 30, 2019 11:30 am, edited 1 time in total.

Top
 Profile  
Reply with quote  
PostPosted: Mon Dec 30, 2019 11:21 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
AlchemyBlinker wrote:
XImage.FromFile produces the same results.
Also when using the WPF build?

Are you using the latest PDFsharp version?

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


Top
 Profile  
Reply with quote  
PostPosted: Mon Dec 30, 2019 3:26 pm 
Offline

Joined: Tue Aug 06, 2019 10:45 am
Posts: 45
Here's the solution I found that works, you might need to modify it for your use case.

I convert the image to png in case it isn't png, then I read the converted image, I do this since png never gave me any error like jpg.
Note that this code isn't really efficient, but it gets the job done as a work around/quick fix

Code:
    /// <summary>
    /// check if the image exists, or if the image is png
    /// if the image is png then I just read the images bytes
    /// else I convert the image from whatever type she is to png and then read the bytes
    /// </summary>
    /// <param name="imageName">the name of the image that is about to be read</param>
    /// <returns>byte array of the image data </returns>
    public static byte[] GetBytesFromImage(string imageName)
    {
        byte[] bytes = null;

        string imagePath = "..\\..\\Imagens";
        imageName = imageName == "" ? "some_default_image.jpg" : imageName;
        string fpath = imagePath + imageName;


        if (File.Exists(fpath) && imageName.EndsWith("png"))
        {
            using (MemoryStream ms = new MemoryStream())
            using (FileStream imageStream = new FileStream(fpath, FileMode.Open, FileAccess.Read))
            {
                imageStream.CopyTo(ms);
                bytes = ms.ToArray();
            }
        }
        else
        {
            using (MemoryStream image = new MemoryStream())
            using (System.Drawing.Image bmpImageToConvert = System.Drawing.Image.FromFile(fpath))
            using (System.Drawing.Image bmpNewImage = new Bitmap(bmpImageToConvert.Width, bmpImageToConvert.Height))
            using (Graphics gfxNewImage = Graphics.FromImage(bmpNewImage))
            {
                gfxNewImage.DrawImage(bmpImageToConvert, new Rectangle(0, 0, bmpNewImage.Width, bmpNewImage.Height), 0, 0, bmpImageToConvert.Width, bmpImageToConvert.Height, GraphicsUnit.Pixel);

                bmpNewImage.Save(image, ImageFormat.Png);
                bytes = image.ToArray();
            }
        }

        return bytes;
    }

    /// <summary>
    /// Get the instance of a MigraDoc.DocumentObjectModel.Shapes.Image from byte[]
    /// using XImage from PdfSharp to set the width and height automatically.
    /// </summary>
    /// <param name="img">byte array of the image data</param>
    /// <returns>instance of MigraDoc.DocumentObjectModel.Shapes.Image</returns>
    public static MigraDoc.DocumentObjectModel.Shapes.Image GetImageInstance(byte[] img)
    {
        MigraDoc.DocumentObjectModel.Shapes.Image image = null;

        using (MemoryStream stream = new MemoryStream(img))
        using (PdfSharp.Drawing.XImage imageAux = PdfSharp.Drawing.XImage.FromStream(stream))
        {
            image = new MigraDoc.DocumentObjectModel.Shapes.Image("base64:" + Convert.ToBase64String(img));
            image.Width  = imageAux.PointWidth;
            image.Height = imageAux.PointHeight;
        }

        return image;
    }


Top
 Profile  
Reply with quote  
PostPosted: Mon Dec 30, 2019 9:11 pm 
Offline

Joined: Tue Dec 24, 2019 5:14 pm
Posts: 3
TH-Soft wrote:
Are you using the latest PDFsharp version?


Nope! Turns out I wasn't... and updating resolved the issue. I can't believe I didn't think to check that myself. Thank you so much!!


Top
 Profile  
Reply with quote  
PostPosted: Thu Jan 02, 2020 2:14 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
AlchemyBlinker wrote:
I can't believe I didn't think to check that myself.
There is a good reason why the forum rules ask people to indicate the version number when asking questions here.
Good to hear the issue is now resolved.

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


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: No registered users and 137 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