ComponentOne PDF for .NET
In This Topic
    Images
    In This Topic

    Pdf lets you display images in the document with ease. In addition, it also lets you customize the quality of the image added in the Pdf document.

    The following image shows an image added in a Pdf document.

    Images can be added to PDF documents using DrawImage method of the C1PdfDocument class. The DrawImage method draws a given image at a specified location and has parameters that provide control over the image alignment and scaling. It has three overloads as described in the following table:

    Overloads Descriptions
    DrawImage(System.Drawing.Image img, System.Drawing.RectangleF rc) Draws the specified System.Drawing.Image object at the specified location, stretching it to fit the destination rectangle.
    DrawImage(System.Drawing.Image img, System.Drawing.RectangleF rc, System.Drawing.ContentAlignment align, C1.Pdf.ImageSizeMode mode) Draws the specified System.Drawing.Image object at the specified location, adjusting the image size as specified by the align and mode parameters.
    DrawImage(System.Drawing.Image img, System.Drawing.RectangleF rcImage, System.Drawing.RectangleF rcClip) Draws the specified System.Drawing.Image object at the specified location, clipping the output to the given clipping rectangle.

    To add images in Pdf document, use the following code. This code uses an image located at the bin directory of the project folder.

    C#
    Copy Code
    RectangleF rect = pdf.PageRectangle;
    //Stretch image to fill the rectangle
    rect.Inflate(-100, -150);
    //Create an image from file
    Image img = Image.FromFile("images.jpg");
    //Draw the image on Pdf document
    pdf.DrawImage(img, rect);
    

    Note 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. PDF for .NET also supports EMF+ metafiles. For detailed information on using metafiles in PDF library, see Metafiles.

    Bitmap images are managed automatically by PDF for .NET. 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. You can control the quality of the images using the C1PdfDocumentBase.ImageQuality property, which allows you to trade-off image quality versus file size.

    Set Image Quality

    You can achieve a good balance between the file size and the visual quality of an image by optimizing the image quality. PDF for .NET allows you to optimize the quality of the images using ImageQuality property of the C1PdfDocument class. The ImageQuality property accepts one of the following inputs from the ImageQuality enumeration as mentioned in table below:

    Enumeration Values Descriptions
    Default High quality, medium/large file size.
    High Highest quality, largest file size.
    Low Low quality, small file size.
    Medium Medium quality, medium file size.

    To set the highest quality of an image in a PDF document, use the following code.

    C#
    Copy Code
    //setting image quality
     pdf.ImageQuality= ImageQuality.High;