PDF for UWP | ComponentOne
Using PDF for UWP / Adding Images
In This Topic
    Adding Images
    In This Topic

    Adding images to PDF for UWP documents is also easy; all the work is done by the C1PdfDocument.DrawImage method.

    C1PdfDocument.DrawImage draws a given image at a specified location and has parameters that provide control over the image alignment and scaling. In the following example, the image is center-aligned within the rectangle and scaled to keep the aspect ratio. The sample declares a C1PdfDocument class called 'pdf' and calls method for drawing image.

    This code is used to draw the same image as follows:

    VB
    Copy Code
    Dim rect As Rect = pdf.PageRectangle
    rect = PdfUtils.Inflate(rect, -150, -150)
    Dim ras As New InMemoryRandomAccessStream()
    ' load image into writeable bitmap
    Dim wb As New WriteableBitmap(880, 660)
    Dim ProjectFolder = Windows.ApplicationModel.Package.Current.InstalledLocation
    Dim file As StorageFile = Await ProjectFolder.GetFileAsync("image.jpg")
    wb.SetSource(Await file.OpenReadAsync())
    Dim rcPic As New Rect(New Point(0, 0), New Point(pdf.PageSize.Width, pdf.PageSize.Height))
    ' draw on page preserving aspect ratio
    pdf.DrawImage(wb, rect, ContentAlignment.MiddleCenter, Stretch.Uniform)
    
    C#
    Copy Code
    Rect rect = pdf.PageRectangle;
    rect = PdfUtils.Inflate(rect, -150, -150);
    
    InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream();
    
    // load image into writeable bitmap
    WriteableBitmap wb = new WriteableBitmap(880, 660);
               
    var ProjectFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
    StorageFile file = await ProjectFolder.GetFileAsync("image.jpg");
    
    wb.SetSource(await file.OpenReadAsync());
    Rect rcPic = new Rect(new Point(0, 0), new Point(pdf.PageSize.Width, pdf.PageSize.Height));
    
    // draw on page preserving aspect ratio
    pdf.DrawImage(wb, rect, ContentAlignment.MiddleCenter, Stretch.Uniform);      
    

    The PDF document will look similar to this:

     

     

     

    Notice that you can render any regular .NET Image object, including Metafiles. Metafiles are not converted into bitmaps; they are played into the document and thus retain the best possible resolution. If you want to add charts or technical drawings to your PDF document, Metafiles are better than bitmap images.

    Bitmap images are managed automatically by PDF for UWP. If you render the same image several times (in a page header for example), only one copy of the image is saved into the PDF file.