PdfSharpCore image rendering issue
Asked Answered
M

1

9

Completely unable to render JPEG image to PDF using PdfSharpCore.

Code is as simple as

public byte[] GetPdfContent()
{
    ImageSource.ImageSourceImpl = new ImageSharpImageSource();

    var document = new PdfDocument();
    var logo = XImage.FromFile("logo.jpg");

    // this is test line, I'm saving XImage result and it is identical to the source file!
    File.WriteAllBytes("logo1.jpg", logo.AsJpeg().ToArray());

    var page = document.AddPage();
    var gfx = XGraphics.FromPdfPage(page);
    gfx.DrawImage(logo, 0, 0);

    using (var stream = new MemoryStream())
    {
         document.Save(stream, false);
         return stream.ToArray();
    }
}

I know that ImageSource.ImageSourceImpl has to be set, and I've set it to the simplest ImageSharp-based implementation: ImageSource.ImageSourceImpl = new ImageSharpImageSource() which is indeed valid because XImage is being saved as logo1.jpg properly.

But my PDF is visually blank. Binary content is there, and it seem that all format properties are OK, but binary data is somewhat different from the source.

Here is how my ImageSharp implementation saves/loads the image:

public class ImageSharpImageSource : ImageSource
{
    protected override IImageSource FromFileImpl(string path, int? quality = 75)
    {
        return new ImageSharpImageSourceImpl(path, () =>
        {
            return Image.Load<Rgb24>(path, new JpegDecoder());
        }, (int)quality);
    }

    private class ImageSharpImageSourceImpl : IImageSource
    { 
        public void SaveAsJpeg(MemoryStream ms)
        {
            Image.SaveAsJpeg(ms, new JpegEncoder());
        }
    }
}

Finally, PDF piece:

7 0 obj   % PdfSharpCore.Pdf.Advanced.PdfImage
<<
  /BitsPerComponent 8
  /ColorSpace /DeviceRGB
  /Filter /DCTDecode
  /Height 340
  /Interpolate true
  /Length 16443
  /Subtype /Image
  /Type /XObject
  /Width 340
>>
stream <<BINARY DATA>>

Please help, I'm stuck for a few days! New to this stuff so probably missing something?..

NOTE: gfx.DrawString() methods work properly, so I'm able to render text with the same setup.

Momentous answered 21/6, 2018 at 5:39 Comment(4)
NOTE: This question is not about the original PDFsharp version, it is about a port of PDFsharp.Agone
@Ballyoleidjit It's mentioned in title, but there is no tag for PdfSharpCoreMomentous
@IvanZverev did you ever get a resolution to this in PdfSharpCore?Llywellyn
@PeterReay unfortunately, noMomentous
B
0

Please use this method:-

    internal void SaveImageAsPdf(string imageFileName, string pdfFileName, int width = 600)
    {
        using (var document = new PdfDocument())
        {
            PdfPage page = document.AddPage();
            using (XImage image = XImage.FromFile(imageFileName))
            {
                var height = (int)(((double)width / (double)image.PixelWidth) * image.PixelHeight);

                page.Width = width;
                page.Height = height;

                XGraphics graphics = XGraphics.FromPdfPage(page);
                graphics.DrawImage(image, 0, 0, width, height);                
            }
            document.Save(pdfFileName);
        }
    }

And if you need More Reference like once we convert an image to pdf and then delete that image also so kindly refer this:-Link For More

Broadfaced answered 28/6, 2018 at 8:49 Comment(3)
It seems you missed that this question is about PdfSharpCore and not about PDFsharp. Referencing a solution that works with PDFsharp will not solve the issue at hand.Agone
Ohk.let me check for pdfsharpcoreBroadfaced
This is exactly what I'm doing. Have no idea why it is still empty. Probably a bug in PdfSharpCore.Momentous

© 2022 - 2024 — McMap. All rights reserved.