How to Start Tutorials Troubleshooting Main Operations Convert PDF Read PDF Edit PDF PDF Report Generator 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# Image to PDF Library
How to convert multiple images (jpg, png, gif, bmp) to PDF using c# in asp.net, winforms


C# Create PDF from Raster Images, .NET Graphics and REImage File with XDoc.PDF Library for C#.NET Class





In this tutorial, you learn how to convert image files to PDF document programmatically using C#

  • Create PDF file from jpg, bitmap (bmp), png, gif raster image files
  • Convert image to editable PDF document with OCR support
  • Adjust image position on PDF page and zooms
  • Print multiple images in one PDF page or multiple pages
  • Easy to enable image to PDF conversion functions in ASP.NET web app, and Windows application using C# .NET Core or framework

How to convert JPG, PNG, BMP images to PDF programmatically using C#

  1. Download XDoc.PDF image conversion C# library
  2. Install C# library to convert image to PDF programmatically
  3. Step by Step Tutorial








  • Best and professional C# image to PDF converter SDK for Visual Studio .NET
  • Batch convert PDF documents from multiple image formats, including Jpg, Png, Bmp, Gif, Tiff, Bitmap, .NET Graphics, and REImage
  • Support .NET WinForms, ASP.NET MVC in IIS, ASP.NET Ajax, Azure cloud service, DNN (DotNetNuke), SharePoint
  • Create PDF from images in both .NET WinForms and ASP.NET application
  • .NET converter control for exporting high quality PDF from images in C#.NET
  • Component for combining multiple image formats into one or multiple PDF file in C#.NET
  • More PDF conversion: convert pdf to word using c#, convert pdf to html c# online, c# convert word to pdf free, pdf to image c# example, c# convert pdf to svg, convert pdf to tiff c#.
  • Any piece of area is able to be cropped and pasted to PDF page
  • Source codes are available to use in C#.NET class
  • Free library is access to downloading and using in .NET framework


If you want to turn PDF file into image file format in C# application, then RasterEdge XDoc.PDF for .NET can also help with this. It enables you to build a PDF file with one or more images. Various image forms are supported which include Png, Jpeg, Bmp, and Gif raster images, .NET Graphics, as well as REImage (an intermediate class). Some C# programming demos are illustrated below.





C# convert single Raster Image file to PDF


        #region convert single Raster Image file to PDF(file to file)
        internal static void image2Pdf()
        {
            String inputFilePath = @"C:\demo.jpg";
            String outputFilePath = @"C:\output.pdf";
            Bitmap bmp = new Bitmap(inputFilePath);
            List<Bitmap> images = new List<Bitmap>();
            images.Add(bmp);
            PDFDocument doc = new PDFDocument(images.ToArray());
            doc.Save(outputFilePath);
        }
        #endregion

        #region convert single Raster Image file to PDF(Stream to Stream)
        internal static void imageStream2PdfStream()
        {
            String inputFilePath = @"C:\demo.jpg";
            byte[] arr = File.ReadAllBytes(inputFilePath);
            MemoryStream inputStream = new MemoryStream(arr);
            Bitmap bmp = new Bitmap(inputStream);
            List<Bitmap> images = new List<Bitmap>();
            images.Add(bmp);
            PDFDocument doc = new PDFDocument(images.ToArray());
            MemoryStream outputStream = new MemoryStream();
            doc.SaveToStream(outputStream);
        }
        #endregion






Convert image file to editable PDF document using C#



If you need get editable PDF document from an image file, you need OCR SDK to extract text from image files.

using RasterEdge.XDoc.Converter;

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

DocumentSaveOption ops = new DocumentSaveOption(FileType.DOC_PDF);
// The folder that contains '.traineddata' files.
ops.OcrSourcePath = @"C:\Source";
//  Convert to editable PDF file.
ops.IsSearchable = true;

ImageConverter.ToDocument(inputFilePath, outputFilePath, ops);






Image position and placement on the PDF page


Using XDoc.PDF C# library, you can easily print the image to any position on the PDF page or crop the page to fit the image width and height. You can also adjust the image zoom on the page with or without original aspect ratio.

The following text and C# example source code explains how to convert JPG, PNG, bitmap image on specified PDF page with adjusted image options.

  • The image is added at the top left corner of the PDF page.
  • The image is added at the top right corner of the page
  • The image is converted and printed at the top center of the page
  • The image is placed at the center of the PDF page
  • The image is added at the bottom left corner of the PDF page.
  • The image is added at the bottom right corner of the page
  • The image is converted and printed at the bottom center of the page
  • The image is covered the whole page
  • The image is covered the page with its original aspect ratio kept
  • The PDF page width and height are cropped to fit the image



Top left corner of page

Top center of page

Top right corner of page
 
Center of page
 

Bottom left corner of page

Bottom center of page

Bottom right corner of page

Fit to page

Fit to page with aspect ratio kept

Crop page



Top left corner of the page

  1. Load the image file to be converted
  2. Create a new PDFDocument object with 1 new page
  3. Create a PDFItemOptions object to specify the image covered area on the PDF page
  4. Property Position to set the image corved area top left point. The top left corner of the page position value is new PointF(0, 0)
  5. Property Width to set the image corvered area width in pixel
  6. Property Height to set the image corvered area height in pixel
  7. Utilize the PDFImageHandler.AddImage() function the add the image to the specified area on the pdf page.
  8. Save the PDF file with image inserted
            string inputFilePath = @"W:\\Projects\\Test-Input\\RasterEdge.com\\NetCoreSDK-PDF\\sample-image.JPG";

            string outputFilePath = @"W:\\Projects\\Test-Output\\RasterEdge.com\\NetCoreSDK-PDF\\convert-image-to-pdf-top-left-corner.pdf";

            //  Step 1: Load image.
            Bitmap img = new Bitmap(inputFilePath);


            //  Step 2: Create an empty PDF file with single page by the specified page size (in inch).
            PDFDocument doc = PDFDocument.Create(1, 8.5F, 11.0F);

            // Step 3: set the boundary of the image in the page.
            //  Unit: in pixel (96 dpi)
            PDFItemOptions imageItemOps = new PDFItemOptions();

            // set the image top left corder position on the PDF page
            imageItemOps.Position = new PointF(0, 0);

            //  Calculate the image size in inch.
            float imageWidthInInch = img.Width / img.HorizontalResolution;
            float imageHeightInInch = img.Height / img.VerticalResolution;

            //  Set image size in the page (in pixels).
            //  The library will automatically convert image width and height in pixel into inch using 96 dpi.
            imageItemOps.Width = imageWidthInInch * 96F;
            imageItemOps.Height = imageHeightInInch * 96F;



            //  Step 4: add image to the target page
            PDFImageHandler.AddImage(doc, 0, img, imageItemOps);

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


Top center of the page

            string inputFilePath = @"W:\\Projects\\Test-Input\\RasterEdge.com\\NetCoreSDK-PDF\\sample-image.png";

            string outputFilePath = @"W:\\Projects\\Test-Output\\RasterEdge.com\\NetCoreSDK-PDF\\convert-image-to-pdf-top-center-corner.pdf";

            //  Step 1: Load image.
            Bitmap img = new Bitmap(inputFilePath);

            //  Step 2: Create an empty PDF file with single page by the specified page size (in inch).
            PDFDocument doc = PDFDocument.Create(1, 8.5F, 11.0F);

            // Step 3: set the boundary of the image in the page.
            //  Unit: in pixel (96 dpi)
            PDFItemOptions imageItemOps = new PDFItemOptions();

            //  Calculate the image size in inch.
            float imageWidthInInch = img.Width / img.HorizontalResolution;
            float imageHeightInInch = img.Height / img.VerticalResolution;

            //  Set image size in the page (in pixels).
            //  The library will automatically convert image width and height in pixel into inch using 96 dpi.
            imageItemOps.Width = imageWidthInInch * 96F;
            imageItemOps.Height = imageHeightInInch * 96F;


            // set the image top left corder position on the PDF page
            float pageWidth = 8.5F * 96;

            float imagePosiLeft = pageWidth / 2 - imageItemOps.Width / 2;
            float imagePosiTop = 0;
            imageItemOps.Position = new PointF(imagePosiLeft, imagePosiTop);

            //  Step 4: add image to the target page
            PDFImageHandler.AddImage(doc, 0, img, imageItemOps);

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


Top right corner of the page

            string inputFilePath = @"W:\\Projects\\Test-Input\\RasterEdge.com\\NetCoreSDK-PDF\\sample-image.png";

            string outputFilePath = @"W:\\Projects\\Test-Output\\RasterEdge.com\\NetCoreSDK-PDF\\convert-image-to-pdf-top-right-corner.pdf";

            //  Step 1: Load image.
            Bitmap img = new Bitmap(inputFilePath);

            //  Step 2: Create an empty PDF file with single page by the specified page size (in inch).
            PDFDocument doc = PDFDocument.Create(1, 8.5F, 11.0F);

            // Step 3: set the boundary of the image in the page.
            //  Unit: in pixel (96 dpi)
            PDFItemOptions imageItemOps = new PDFItemOptions();

            //  Calculate the image size in inch.
            float imageWidthInInch = img.Width / img.HorizontalResolution;
            float imageHeightInInch = img.Height / img.VerticalResolution;

            //  Set image size in the page (in pixels).
            //  The library will automatically convert image width and height in pixel into inch using 96 dpi.
            imageItemOps.Width = imageWidthInInch * 96F;
            imageItemOps.Height = imageHeightInInch * 96F;


            // set the image top left corder position on the PDF page
            float pageWidth = 8.5F * 96;
            float imagePosiLeft = pageWidth - imageItemOps.Width;
            float imagePosiTop = 0;
            imageItemOps.Position = new PointF(imagePosiLeft, imagePosiTop);


            //  Step 4: add image to the target page
            PDFImageHandler.AddImage(doc, 0, img, imageItemOps);

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


Center of the page

            string inputFilePath = @"W:\\Projects\\Test-Input\\RasterEdge.com\\NetCoreSDK-PDF\\sample-image.png";

            string outputFilePath = @"W:\\Projects\\Test-Output\\RasterEdge.com\\NetCoreSDK-PDF\\convert-image-to-pdf-center.pdf";


            //  Step 1: Load image.
            Bitmap img = new Bitmap(inputFilePath);

            //  Step 2: Create an empty PDF file with single page by the specified page size (in inch).
            PDFDocument doc = PDFDocument.Create(1, 8.5F, 11.0F);

            // Step 3: set the boundary of the image in the page.
            //  Unit: in pixel (96 dpi)
            PDFItemOptions imageItemOps = new PDFItemOptions();

            //  Calculate the image size in inch.
            float imageWidthInInch = img.Width / img.HorizontalResolution;
            float imageHeightInInch = img.Height / img.VerticalResolution;

            //  Set image size in the page (in pixels).
            //  The library will automatically convert image width and height in pixel into inch using 96 dpi.
            imageItemOps.Width = imageWidthInInch * 96F;
            imageItemOps.Height = imageHeightInInch * 96F;


            // set the image top left corder position on the PDF page
            float pageWidth = 8.5F * 96;
            float pageHeight = 11.0F * 96;

            float imagePosiLeft = pageWidth / 2 - imageItemOps.Width / 2;
            float imagePosiTop = pageHeight / 2 - imageItemOps.Height / 2;
            imageItemOps.Position = new PointF(imagePosiLeft, imagePosiTop);


            //  Step 4: add image to the target page
            PDFImageHandler.AddImage(doc, 0, img, imageItemOps);

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


Bottom left corner of the page

            string inputFilePath = @"W:\\Projects\\Test-Input\\RasterEdge.com\\NetCoreSDK-PDF\\sample-image.png";

            string outputFilePath = @"W:\\Projects\\Test-Output\\RasterEdge.com\\NetCoreSDK-PDF\\convert-image-to-pdf-bottom-left-corner.pdf";


            //  Step 1: Load image.
            Bitmap img = new Bitmap(inputFilePath);

            //  Step 2: Create an empty PDF file with single page by the specified page size (in inch).
            PDFDocument doc = PDFDocument.Create(1, 8.5F, 11.0F);

            // Step 3: set the boundary of the image in the page.
            //  Unit: in pixel (96 dpi)
            PDFItemOptions imageItemOps = new PDFItemOptions();

            //  Calculate the image size in inch.
            float imageWidthInInch = img.Width / img.HorizontalResolution;
            float imageHeightInInch = img.Height / img.VerticalResolution;

            //  Set image size in the page (in pixels).
            //  The library will automatically convert image width and height in pixel into inch using 96 dpi.
            imageItemOps.Width = imageWidthInInch * 96F;
            imageItemOps.Height = imageHeightInInch * 96F;


            // set the image top left corder position on the PDF page
            float pageWidth = 8.5F * 96;
            float pageHeight = 11.0F * 96;

            float imagePosiLeft = 0;
            float imagePosiTop = pageHeight - imageItemOps.Height;
            imageItemOps.Position = new PointF(imagePosiLeft, imagePosiTop);


            //  Step 4: add image to the target page
            PDFImageHandler.AddImage(doc, 0, img, imageItemOps);

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


Bottom center of the page

            string inputFilePath = @"W:\\Projects\\Test-Input\\RasterEdge.com\\NetCoreSDK-PDF\\sample-image.png";

            string outputFilePath = @"W:\\Projects\\Test-Output\\RasterEdge.com\\NetCoreSDK-PDF\\convert-image-to-pdf-bottom-center-corner.pdf";


            //  Step 1: Load image.
            Bitmap img = new Bitmap(inputFilePath);

            //  Step 2: Create an empty PDF file with single page by the specified page size (in inch).
            PDFDocument doc = PDFDocument.Create(1, 8.5F, 11.0F);

            // Step 3: set the boundary of the image in the page.
            //  Unit: in pixel (96 dpi)
            PDFItemOptions imageItemOps = new PDFItemOptions();

            //  Calculate the image size in inch.
            float imageWidthInInch = img.Width / img.HorizontalResolution;
            float imageHeightInInch = img.Height / img.VerticalResolution;

            //  Set image size in the page (in pixels).
            //  The library will automatically convert image width and height in pixel into inch using 96 dpi.
            imageItemOps.Width = imageWidthInInch * 96F;
            imageItemOps.Height = imageHeightInInch * 96F;


            // set the image top left corder position on the PDF page
            float pageWidth = 8.5F * 96;
            float pageHeight = 11.0F * 96;

            float imagePosiLeft = pageWidth / 2 - imageItemOps.Width / 2;
            float imagePosiTop = pageHeight - imageItemOps.Height;
            imageItemOps.Position = new PointF(imagePosiLeft, imagePosiTop);


            //  Step 4: add image to the target page
            PDFImageHandler.AddImage(doc, 0, img, imageItemOps);

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


Bottom right corner of the page

            string inputFilePath = @"W:\\Projects\\Test-Input\\RasterEdge.com\\NetCoreSDK-PDF\\sample-image.png";

            string outputFilePath = @"W:\\Projects\\Test-Output\\RasterEdge.com\\NetCoreSDK-PDF\\convert-image-to-pdf-bottom-right-corner.pdf";

            //  Step 1: Load image.
            Bitmap img = new Bitmap(inputFilePath);

            //  Step 2: Create an empty PDF file with single page by the specified page size (in inch).
            PDFDocument doc = PDFDocument.Create(1, 8.5F, 11.0F);

            // Step 3: set the boundary of the image in the page.
            //  Unit: in pixel (96 dpi)
            PDFItemOptions imageItemOps = new PDFItemOptions();

            //  Calculate the image size in inch.
            float imageWidthInInch = img.Width / img.HorizontalResolution;
            float imageHeightInInch = img.Height / img.VerticalResolution;

            //  Set image size in the page (in pixels).
            //  The library will automatically convert image width and height in pixel into inch using 96 dpi.
            imageItemOps.Width = imageWidthInInch * 96F;
            imageItemOps.Height = imageHeightInInch * 96F;


            // set the image top left corder position on the PDF page
            float pageWidth = 8.5F * 96;
            float pageHeight = 11.0F * 96;

            float imagePosiLeft = pageWidth - imageItemOps.Width;
            float imagePosiTop = pageHeight - imageItemOps.Height;
            imageItemOps.Position = new PointF(imagePosiLeft, imagePosiTop);


            //  Step 4: add image to the target page
            PDFImageHandler.AddImage(doc, 0, img, imageItemOps);

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


Fit to the page

            string inputFilePath = @"W:\\Projects\\Test-Input\\RasterEdge.com\\NetCoreSDK-PDF\\sample-image.png";

            string outputFilePath = @"W:\\Projects\\Test-Output\\RasterEdge.com\\NetCoreSDK-PDF\\convert-image-to-pdf-fit-to-page.pdf";

            //  Step 1: Load image.
            Bitmap img = new Bitmap(inputFilePath);

            //  Step 2: Create an empty PDF file with single page by the specified page size (in inch).
            PDFDocument doc = PDFDocument.Create(1, 8.5F, 11.0F);

            // Step 3: set the boundary of the image in the page.
            //  Unit: in pixel (96 dpi)
            PDFItemOptions imageItemOps = new PDFItemOptions();

            // set the image top left corder position on the PDF page
            float pageWidth = 8.5F * 96;
            float pageHeight = 11.0F * 96;

            imageItemOps.Position = new PointF(0, 0);
            imageItemOps.Width = pageWidth;
            imageItemOps.Height = pageHeight;


            //  Step 4: add image to the target page
            PDFImageHandler.AddImage(doc, 0, img, imageItemOps);

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


Fit to the page with aspect ratio kept

            string inputFilePath = @"W:\\Projects\\Test-Input\\RasterEdge.com\\NetCoreSDK-PDF\\sample-image.png";

            string outputFilePath = @"W:\\Projects\\Test-Output\\RasterEdge.com\\NetCoreSDK-PDF\\convert-image-to-pdf-fit-to-page-with-aspect-ratio.pdf";

            //  Step 1: Load image.
            Bitmap img = new Bitmap(inputFilePath);

            //  Step 2: Create an empty PDF file with single page by the specified page size (in inch).
            PDFDocument doc = PDFDocument.Create(1, 8.5F, 11.0F);

            // Step 3: set the boundary of the image in the page.
            //  Unit: in pixel (96 dpi)
            PDFItemOptions imageItemOps = new PDFItemOptions();

            // set the image top left corder position on the PDF page
            float pageWidth = 8.5F * 96;
            float pageHeight = 11.0F * 96;


            float imageWidth = img.Width;
            float imageHeight = img.Height;

            float zoomWidth = pageWidth / imageWidth;
            float zoomHeight = pageHeight / imageHeight;

            float imagePosiTop = 0;
            float imagePosiLeft = 0;

            float zoomImage = zoomWidth;
            if (zoomWidth > zoomHeight)
            {
                zoomImage = zoomHeight;

                imagePosiLeft = pageWidth / 2 - imageWidth * zoomImage / 2;
            }
            else
            {
                imagePosiTop = pageHeight / 2 - imageHeight * zoomImage / 2;
            }


            //  set target page region to add the image.
            imageItemOps.Position = new PointF(imagePosiLeft, imagePosiTop);
            imageItemOps.Width = imageWidth * zoomImage;
            imageItemOps.Height = imageHeight * zoomImage;


            //  Step 4: add image to the target page
            PDFImageHandler.AddImage(doc, 0, img, imageItemOps);

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


Crop page

            string inputFilePath = @"W:\\Projects\\Test-Input\\RasterEdge.com\\NetCoreSDK-PDF\\sample-image.png";

            string outputFilePath = @"W:\\Projects\\Test-Output\\RasterEdge.com\\NetCoreSDK-PDF\\convert-image-to-pdf-crop-page.pdf";

            //  Step 1: Load image.
            Bitmap img = new Bitmap(inputFilePath);

            //  Calculate the image size in inch.
            float imageWidthInInch = img.Width / img.HorizontalResolution;
            float imageHeightInInch = img.Height / img.VerticalResolution;

            //  Step 2: Create an empty PDF file with single page by the specified page size (in inch).
            PDFDocument doc = PDFDocument.Create(1, imageWidthInInch, imageHeightInInch);

            // Step 3: set the boundary of the image in the page.
            //  Unit: in pixel (96 dpi)
            PDFItemOptions imageItemOps = new PDFItemOptions();

            float imageWidth = imageWidthInInch * 96.0f;
            float imageHeight = imageHeightInInch * 96.0f;

            imageItemOps.Position = new PointF(0, 0);
            imageItemOps.Width = imageWidth;
            imageItemOps.Height = imageHeight;


            //  Step 4: add image to the target page
            PDFImageHandler.AddImage(doc, 0, img, imageItemOps);

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






How to convert two or multiple images to PDF using C#?


The following C# code shows how to convert list of images into a PDF file. One image will be displayed in one PDF page.

You could also convert multiple images in a pdf page. View full C# source code at How to convert multiple JPG, bmp images in single PDF page using C#



        #region convert two or multiple Raster Image files to PDF (batch convert and sinle thread)
        internal static void imageFilesToPdfFiles()
        {
            String inputDirectory = @"C:\input\";
            String outputDirectory = @"C:\output\";
            String[] files = Directory.GetFiles(inputDirectory, "*.*");
            foreach (String filePath in files)
            {
                int startIdx = filePath.LastIndexOf("\\");
                int endIdx = filePath.LastIndexOf(".");
                String docName = filePath.Substring(startIdx + 1, endIdx - startIdx - 1);
                Bitmap bmp = new Bitmap(filePath);
                List<Bitmap> images = new List<Bitmap>();
                images.Add(bmp);
                PDFDocument doc = new PDFDocument(images.ToArray());
                doc.Save(outputDirectory + docName);
            }
        }
        #endregion




C# combine multiple Raster Image files into PDF


        #region combine multiple Raster Image files into PDF
        internal static void combineMultipleImagesIntoPdf()
        {
            String filePath1 = @"C:\demo1.jpg";
            String filePath2 = @"C:\demo1.bmp";
            String filePath3 = @"C:\demo1.png";

            Bitmap image1 = new Bitmap(filePath1);
            Bitmap image2 = new Bitmap(filePath2);
            Bitmap image3 = new Bitmap(filePath3);

            List<Bitmap> images = new List<Bitmap>();
            images.Add(image1);
            images.Add(image2);
            images.Add(image3);

            PDFDocument doc = new PDFDocument(images.ToArray());
            doc.Save(@"C:\output.pdf");
        }
        #endregion




C# insert Raster Image file into pdf document, and create a new PDF file


        #region insert Raster Image file into pdf document, and create a new PDF file
        internal static void insertImageToPdf()
        {
            String filePath = @"C:\demo.png";
            Bitmap bmp = new Bitmap(filePath);
            List<Bitmap> images = new List<Bitmap>();
            images.Add(bmp);
            PDFDocument pdf = new PDFDocument(images.ToArray());
            int pageCount = pdf.GetPageCount();
            List<BasePage> pages = new List<BasePage>();
            for (int i = 0; i < pageCount; i++)
            {
                pages.Add(pdf.GetPage(i));
            }
            String outputPdf = @"C:\output.pdf";
            PDFDocument desDoc = new PDFDocument(outputPdf);
            int insertLocation = 2;
            desDoc.InsertPages(pages.ToArray(), insertLocation);
            desDoc.Save(@"C:\desDocumcnet.pdf");
        }
        #endregion