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. Image for the cookie policy date

Need help in creating pdf with images

Hi,

I have a requirement wherein I have to create PDF out of the framework elements. Say, I have labels, textboxes. I have to capture an image for the same and insert it into a PDF. As I came to know, only jpegs are supported, so I tried creating a jpeg for the control and then creating a pdf. Here is the code I am using:

public sealed partial class CustomControl: UserControl
    {
 public async Task Print(StorageFile file,)
        {
            //Creates a new PDF document.
            PdfDocument doc = new PdfDocument();

            //Adds a page to the document.
            PdfPage page = doc.Pages.Add();

            //Creates Pdf graphics for the page.
            PdfGraphics g = page.Graphics;

            //Creates a solid brush.
            PdfBrush brush = new PdfSolidBrush(new PdfColor(255,0,0));

            //Sets the font.
            PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12);

            
            PdfImage image = new PdfBitmap(await CreateBitmap(this));

            g.DrawImage(image, new System.Drawing.PointF(10, 10));

            await doc.SaveAsync(file);
            //g.DrawImage()
        }

        async public Task<System.IO.Stream> CreateBitmap(FrameworkElement element)
        {
            // render element to image (WinRT)
            var renderTargetBitmap = new RenderTargetBitmap();
            await renderTargetBitmap.RenderAsync(element);
            var wb = new WriteableBitmap(renderTargetBitmap.PixelWidth, renderTargetBitmap.PixelHeight);
            (await renderTargetBitmap.GetPixelsAsync()).CopyTo(wb.PixelBuffer);
            var rect = new Rect(0, 0, renderTargetBitmap.PixelWidth, renderTargetBitmap.PixelHeight);

            StorageFolder folder = Windows.Storage.ApplicationData.Current.LocalFolder;

            await wb.SaveToFile(folder, "test1.jpeg");
            StorageFile file = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync("test1.jpeg");
            Stream stream = await file.OpenStreamForReadAsync();
            return stream;
        }
}

But this still gives me the same error that jpeg is only supported. Can you please let me know where I am going wrong.

1 Reply

KC Karthikeyan Chandrasekar Syncfusion Team September 22, 2014 04:41 AM UTC

Hi Winny,

Thank you for using Syncfusion Products.

 

Could you please provide us the generated image("test1.jpeg"), which you are trying to draw in PDF document so that it will be easy for our investigation.

 

Thanks,

Karthikeyan.C


Loader.
Live Chat Icon For mobile
Up arrow icon