We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy.
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

winrt attempt to insert image into pdf

Thread ID:

Created:

Updated:

Platform:

Replies:

106582 Jan 16,2013 11:03 PM UTC Jul 18,2013 11:11 AM UTC Windows Forms 4
loading
Tags: PDF
BEE
Asked On January 16, 2013 11:03 PM UTC

I would like to insert code into a pdf document but after I convert the image file to stream, I get the ArgumentNullException.

Buffer cannot be null.

Parameter name: buffer

Code:
using (PdfLoadedDocument pdfDocument = new PdfLoadedDocument(await file.OpenStreamForReadAsync()))
{
    var imgFile = await installedFolder.GetFileAsync(@"PathToImage\MyImage.png");

    using (Stream ms = await imgFile.OpenStreamForReadAsync())
    {
        pdfDocument.Pages[0].Graphics.DrawImage(PdfImage.FromStream(ms), 0, 0, 300, 170);
    }
}

George Livingston [Syncfusion]
Replied On January 17, 2013 12:44 PM UTC

Hi Bee,

Thank you for your interest in Syncfusion product.

Currently we do not support PNG images, however we have added a feature request to implement this feature in any our upcoming release. We will let you know once the feature has been implemented.

Please let us know if you have any questions.

Regards,

George


Adam Clark
Replied On February 26, 2013 04:05 PM UTC

I ran into this same issue.  Your documentation is misleading because the first example implies its using a PNG image

http://help.syncfusion.com/UG/winrt/default.htm#!Documents/drawingimages.htm

//Draw bitmage image with image size

PdfImage image = new PdfBitmap(pngImage);

g.DrawImage(image, 0, 0, image.Width, image.Height);

 


Ross Dargan
Replied On July 18, 2013 10:50 AM UTC

It's sad to see that this still is not supported, and the documentation doesn't really make this clear either.

Ross Dargan
Replied On July 18, 2013 11:11 AM UTC

If anyone else encounters this issue here is a workaround.

using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Syncfusion.Pdf.Graphics;
using Windows.Graphics.Imaging;
using Windows.Storage;
using Windows.Storage.Streams;
 
namespace Relay.Forms.UI.Utils
{
    public static class IStorageFileExtensions
    {
        public static async Task<PdfImage> ToPdfImage(this IStorageFile file)
        {
            using (var fileStream = await file.OpenReadAsync())
            {
                IRandomAccessStream stream;
                if (file.ContentType == "image/png")
                {
                    var decoder = await BitmapDecoder.CreateAsync(fileStream);
                    var pixels = await decoder.GetPixelDataAsync();
 
                    stream = new InMemoryRandomAccessStream();
 
                    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);
 
                    encoder.SetPixelData(decoder.BitmapPixelFormat, BitmapAlphaMode.Ignore,
                                         decoder.OrientedPixelWidth, decoder.OrientedPixelHeight,
                                         decoder.DpiX, decoder.DpiY,
                                         pixels.DetachPixelData());
 
                    await encoder.FlushAsync();
 
                }
                else
                {
                    stream = fileStream;
                }
                PdfImage image = PdfImage.FromStream(stream.AsStream());
                return image;
            }
        }
    }
}

Simply call this on your PNG image and it will work fine
IStorageFile fileImage = ...
PdfImage image = await fileImage.ToPdfImage();

CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;