How to Start Convert PDF Read PDF Edit PDF PDF Report Builder Work with PDF Modules PDF Document PDF Pages Text Image Graph & Path Annotation, Markup & Drawing Redaction Security Digital Signature Forms Watermark Bookmark Link File Attachment File Metadata Printing Work with Other SDKs Barcode read Barcode create OCR Twain

C# PDF Image Editor Library
How to add, insert images to existing PDF file using c# .net


How to Use XDoc.PDF SDK for .NET to Insert & Add Image, Picture or Logo on PDF Page in C#.NET





In this tutorial, you learn how to add images to PDF document, and apply added image with image settings in your C# asp.net web app and Windows application

  • Add an image to PDF page
  • Add an image with aspect ratio settings
  • Insert an image with color space settings
  • Insert an image with quality control

How to add new image to existing PDF file using C#

  1. Download XDoc.PDF Image Editor C# library
  2. Install C# library to add image to PDF document
  3. Step by Step Tutorial












  • An advanced PDF image processing SDK library allows users to insert images to adobe PDF document in C#.NET Class
  • Support .NET WinForms, ASP.NET MVC in IIS, ASP.NET Ajax, Azure cloud service, DNN (DotNetNuke), SharePoint
  • Free evaluation components for inserting images to PDF file in Visual C# .NET WinForms and ASP.NET web application
  • Free online C# source code for PDF image adding in Visual Studio .NET framework program
  • Create high resolution PDF file without image quality losing .NET console application
  • Add multiple images to multipage PDF document in .NET WinForms
  • Support various image formats, like Jpeg or Jpg, Png, Gif, Bmp, Tiff and other bitmap images
  • Import graphic picture, digital photo, signature and logo into PDF document
  • Ability to put image into specified PDF page position and save existing PDF file or output a new PDF file
  • An independent .NET view control supports preview PDF in ASPX webpage
  • Able to zoom and crop image and achieve image resizing
  • Insert images into PDF form field








How to add an image to PDF file using C#


How to insert and add image, picture, digital photo, scanned signature or logo into PDF document page in C#.NET class application? To help you solve this technical problem, we provide this C#.NET PDF image adding control, XDoc.PDF for .NET.

Similar to other toolkits from RasterEdge.com, this C#.NET PDF image adding control is also completely developed in .NET developing platform and compatible with .NET Framework 2.0 and later versions. Using this C# .NET image adding library control for PDF document, you can easily and quickly add an image, picture or logo to any position of specified PDF document file page.





Add an image to a PDF page with the specified position


It is an easy job to add, insert an image to the existing pdf file. To add an image to an existing PDF file:


  1. Create a Bitmap object from an image resource
  2. Get the PDF page you want to add an image to.
  3. Add the Bitmap object to the PDF page with specified position


String inputFilePath = Program.RootPath + "\\" + "1.pdf";
String outputFilePath = Program.RootPath + "\\" + "output.pdf";

//  load a sample image
Bitmap anImage = new Bitmap(Program.RootPath + "\\" + "1.png");

//  open the document
PDFDocument doc = new PDFDocument(inputFilePath);
//  get the first page
PDFPage page = (PDFPage)doc.GetPage(0);
//  set image position in the page: X = 100F, Y = 400F
PointF position = new PointF(100F, 400F);

//  add image to the page
PDFImageHandler.AddImage(page, anImage, position);

//  output the new document
doc.Save(outputFilePath);




Add a scaled Bitmap image to a page with aspect ratio


//  load a sample image
Bitmap bitmap = new Bitmap(Program.RootPath + "\\" + "1.png");

int width = bitmap.Width;
int height = = bitmap.Height;

int scaledWidth = 400;
int scaledHeight = scaledWidth * height / width;

//  set image item option
PDFItemOptions ops = new PDFItemOptions();

//  set target page region to add the image.
ops.Position = new PointF(10F, 10F);
ops.Width = scaledWidth;
ops.Height = scaledHeight;
             
//  open the document
PDFDocument doc = new PDFDocument(inputFilePath);
int pageIndex = 0;

//  add image to the target page
PDFImageHandler.AddImage(doc, pageIndex, bitmap, ops);

//  output the new document
doc.Save(outputFilePath);




Add a background image to PDF using C#


The following C# source code shows how to use an image as PDF background image. You will get more information about How to add, remove PDF background using C# here



String inputFilePath = @"C:\1.pdf";
String outputFilePath = @"C:\1_backgd.b.pdf";
String srcImageFilePath = @"C:\SourceImage.png";

//  open a PDF file
PDFDocument doc = new PDFDocument(inputFilePath);

//  Create a background resource by an image file.
PDFBackgroudRes resBackground = new PDFBackgroundImageRes(new Bitmap(srcImageFilePath));

//  define a page background setting
PDFPageBackground background = new PDFPageBackground(resBackground);
//  Set transparency
background.Opacity = 0.2F;

//  Alignment the background to center.
background.HoriAlignment = BackgroundHoriAlignment.Center;
background.VertAlignment = BackgroundVertAlignemnt.Center;

//  Indicate if use image's acutal size or not.
//  If set to false, it would fill the image to the target page.
//  Default: false
background.EnableAbsoluteScale = true;

//  define page range: to all pages
PageRangeOptions pageRange = new PageRangeOptions();
pageRange.AllPages = true;
pageRange.Subset = PageRangeSubset.All;

//  apply page background settings to all pages
PDFPageFieldHandler.ApplyBackground(doc, background, pageRange);

doc.Save(outputFilePath);






Add image to PDF header and footer using C#


If you want to add, insert image to PDF page header and footer section, please go to How to add image in PDF header and footer using c# for details







Retrieve color space information of an image in the document


If you need know an image color space information inside pdf file, you can try the following c# source code.



String inputFilePath = @"C:\1.pdf";

//  Open file
PDFDocument doc = new PDFDocument(inputFilePath);
//  Get all images in the document
List<PDFImage> imgs = PDFImageHandler.ExtractImages(doc);
foreach (PDFImage img in imgs)
{
    if (!img.IsInlineImage && !img.IsForm)
    {
        //  Get color space name of the image. Eg.: DeviceGray, DeviceRGB, Indexed etc.
        Console.WriteLine("Color Space: {0}", img.GetColorSpaceName());
        //  Indicate if the image is in color (RGB).
        Console.WriteLine("Is RGB:      {0}", img.IsRGB);
        //  Indicate if the image is in grayscale.
        Console.WriteLine("Is Gray:     {0}", img.IsGray);
    }
}




Image quality control


When you add multiple images to a single PDF file, you want to control all images' quality, and make sure all images in the same PDF page are using the same compression mode. You can try the following code.



String inputFilePath = Program.RootPath + "\\" + "2.pdf";
String outputFilePath = Program.RootPath + "\\" + "output2.pdf";

PDFDocument doc = new PDFDocument(inputFilePath);

PDFPage page = (PDFPage)doc.GetPage(0);

List<PDFImage> images = PDFImageHandler.ExtractImages(page);

PDFImageHandler.ReduceImageSize(doc, images[0], 2F);

doc.Save(outputFilePath);