Documents for PDF .NET Edition
Features / Images
In This Topic
    Images
    In This Topic

    Images are generally used to illustrate important information in your document and highlight points raised in the text. GcPdf allows you to draw an image on a page using DrawImage method of GcGraphics class. In case the same Image object (identified by reference, not by content) is rendered multiple times in a GcPdfDocument, GcPdf considers it to be the same image, and adds only one image data to the PDF, referencing it from all places where it is used in the document. The library supports various image formats, such as BMP, GIF (single frame only), JPEG, and PNG. Additionally, on Windows, TIFF, JpegXR, and ICO formats are also supported.

    In addition, GcPdf library provides ImageAlign class to let you align images in different ways using properties such as AlignHorzAlignVertBestFitTileHorz, etc. The library also allows you to control the image quality such as compressing color values, setting JPEG image quality, etc. through ImageOptions property available in GcPdfDocument class.

    PDF Images

    Add Image From File

    To add an image in a PDF document, load the image in your application using Image.FromFile method. This method will store the image in an object of Image class. Once, the image is added, you can use the DrawImage method provided by the GcGraphics class to render the image.

    C#
    Copy Code
    public void CreatePDF(Stream stream)
    {
        GcPdfDocument doc = new GcPdfDocument();
        var page = doc.NewPage();
        var g = page.Graphics;
        // Add image to the application
    
        var image = GrapeCity.Documents.Drawing.Image.FromFile
         (Path.Combine("Resources", "Images", "clouds.jpg"));
    
        // Use DrawImage to render the image
        g.DrawImage(image, new RectangleF(30.6F, 30.7F, 40.8F, 100.9F), 
            null, ImageAlign.CenterImage);
        // Save the PDF file
        doc.Save(stream);
    }
    
    Back to Top

    Add Image From Stream

    To add an image in a PDF document using stream, you need to store image in a stream using Image.FromStream method. Once the image is stored, it can be added to the application. Then, you can use the DrawImage method provided by the GcGraphics class to render the image.

    C#
    Copy Code
    public void CreatePDF(Stream stream)
    {
    GcPdfDocument doc = new GcPdfDocument();
    var page = doc.NewPage();
    var g = page.Graphics;
    string fileName = @"C:\Users\Admin\Desktop\clouds.png";
    FileStream fs = new FileStream(fileName, System.IO.FileMode.Open);
    
    // Add image to the application
    var image = GrapeCity.Documents.Drawing.Image.FromStream(fs);
    // Use DrawImage to render the image
    g.DrawImage(image, new RectangleF(30.6F, 30.7F, 40.8F, 100.9F), null, ImageAlign.CenterImage);
    // Save the PDF file
    doc.Save(stream);
    }
    

    Back to Top

    Set Image Opacity

    To render an image with a specified transparency, you can add an image to a PDF document using the DrawImage method that takes opacity as one of the parameters.

    C#
    Copy Code
    //Create a basic pdf
    GcPdfDocument doc = new GcPdfDocument();
    GcPdfGraphics g = doc.NewPage().Graphics;
    g.DrawString("A sample document showing an image with controlled opacity.",
        new TextFormat() { Font = StandardFonts.Times, FontSize = 12 }, new PointF(72, 72));
                
    //Add an image by controlling its opacity
    var image = RawImage.FromFile(Path.Combine("Resources", "sea.jpg"), 
                                  RawImageFormat.Jpeg, 800, 532);
    ImageAlign ia = new ImageAlign(ImageAlignHorz.Center, ImageAlignVert.Center, 
                                   true, true, true, false, false);
    g.DrawImage(image, new RectangleF(100, 100, 180, 100), null, ImageAlign.ScaleImage,0.3F);
    
            
    //Save the final pdf
    doc.Save("AddImage_Opacity.pdf"); 
    
    Console.WriteLine("Press any key to exit");
    Console.ReadKey();
    
    Back to Top

    Extract Image

    To extract an image from a PDF document, use GetImages method:

    1. Load a PDF document containing image using Load method of the GcPdfDocument class.
    2. Extract the image(s) from the PDF document using GetImages method of the GcPdfDocument class.
    3. Draw the extracted image(s) on another PDF document using the Graphics.DrawImage method.
    4. Save the document using Save method of the GcPdfDocument class.
      C#
      Copy Code
      using (FileStream fs = new FileStream(Path.Combine("Resources", "Wetlands.pdf"),
             FileMode.Open, FileAccess.Read))
      {
          GcPdfDocument docSrc = new GcPdfDocument();
      
          // Load an existing PDF with some images
          docSrc.Load(fs);
          
          //Extract information about images from the loaded PDF
           var imageInfos = docSrc.GetImages();
      
          GcPdfDocument doc = new GcPdfDocument();
          var textPt = new PointF(72, 72);
      
          foreach (var imageInfo in imageInfos)
          {
              // The same image may appear on multiple locations, 
              // imageInfo includes page indices and locations on pages;
              
              var g = doc.NewPage().Graphics;
              g.DrawImage(imageInfo.Image, new RectangleF(10,0,400,400), null, ImageAlign.ScaleImage);
          }
          doc.Save("ExtractImage.pdf");
      }
      Console.WriteLine("Press any key to exit");
      Console.ReadKey();
      

    Back to Top

    For more information about implementation of images using GcPdf, see GcPdf sample browser.