PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

PDF images scrambled in Acrobat, but fine in Firefox
https://forum.pdfsharp.net/viewtopic.php?f=2&t=4077
Page 1 of 1

Author:  AlchemyBlinker [ Tue Dec 24, 2019 5:38 pm ]
Post subject:  PDF images scrambled in Acrobat, but fine in Firefox

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.

Author:  TH-Soft [ Fri Dec 27, 2019 12:59 pm ]
Post subject:  Re: PDF images scrambled in Acrobat, but fine in Firefox

Hi!

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

Author:  AlchemyBlinker [ Fri Dec 27, 2019 4:35 pm ]
Post subject:  Re: PDF images scrambled in Acrobat, but fine in Firefox

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.

Author:  IRlyDunno [ Mon Dec 30, 2019 9:35 am ]
Post subject:  Re: PDF images scrambled in Acrobat, but fine in Firefox

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.

Author:  TH-Soft [ Mon Dec 30, 2019 11:21 am ]
Post subject:  Re: PDF images scrambled in Acrobat, but fine in Firefox

AlchemyBlinker wrote:
XImage.FromFile produces the same results.
Also when using the WPF build?

Are you using the latest PDFsharp version?

Author:  IRlyDunno [ Mon Dec 30, 2019 3:26 pm ]
Post subject:  Re: PDF images scrambled in Acrobat, but fine in Firefox

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;
    }

Author:  AlchemyBlinker [ Mon Dec 30, 2019 9:11 pm ]
Post subject:  Re: PDF images scrambled in Acrobat, but fine in Firefox

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

Author:  TH-Soft [ Thu Jan 02, 2020 2:14 pm ]
Post subject:  Re: PDF images scrambled in Acrobat, but fine in Firefox

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.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/