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# Images to PDF Library
How to convert multiple JPG, bitmap images in PDF page using C# in ASP.NET, MVC, Windows application

Complete C# sample source code to show how to quickly create PDF page with multiple JPG images converted in ASP.NET, Windows application

The following C# tutorial will show how to convert multiple images (jpg, bitmap, png) into single PDF page. For example, you will read, put 12 images in one pdf page. All 12 images will be displayed in three rows, 4 images per row.

How to convert multiple JPG, BMP images to PDF page using C#

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

C# function to convert multiple images in PDF page

The text and C# sample source code below explain a function to convert a bunch of images on one PDF page. All images will be inserted in specified area.

  • All images will be displayed in specified area in one PDF page.
  • The whole target area will be like a table with rows and columns.
  • The list of images will be displayed in the cells, from left to right, from top to down. Each cell will show one image.
  • If the list has null object, the cell will not show any image

Here are the details about the C# function PrintMultipleImagesOnPageTable() source code below

  1. Create a List object to hold all cells' boundaries area information. The cells count value is rowsCount * columnsCount.
  2. From left column to right, and from top row to bottom, calculate and get all cells' boundaries data
  3. For each image in the imagesList object, calculate the image's displaying as fit to height boundary, based on its cell's bounary.
  4. Now all images are displayed as fit to height in the target area in a PDF page.
  5. Utilize PDFImageHandler.AddImage() funtion to add each image to specific area in the PDF page.

        public static void PrintMultipleImagesOnPageTable(PDFPage page, List<Bitmap> imagesList,
                                                            PointF position, float width, float height,
                                                            int rowsCount, int columnsCount, float cellPadding)
            if (rowsCount < 1)
                throw new ArgumentException("rowsCount must be positive.");
            if (columnsCount < 1)
                throw new ArgumentException("columnsCount must be positive.");

            //  Total cells
            int cellCount = rowsCount * columnsCount;
            //  Width of each cell (including padding)
            float cellWidth = width / columnsCount;
            //  Height of each cell (including padding)
            float cellHeight = height / rowsCount;
            //  Width of each cell (without padding)
            float cellWidthWithoutPadding = cellWidth - cellPadding * 2;
            //  Height of each cell (without padding). Also the expected image height.
            float cellHeightWithoutPadding = cellHeight - cellPadding * 2;
            //  Start position of the table.
            float startPtX = position.X;
            float startPtY = position.Y;
            //  From left to right, from top to bottom.
            //  To get all cell bounaries (without padding).
            List<RectangleF> cellBoundaries = new List<RectangleF>();
            for (int row = 0; row < rowsCount; row++)
                float y = startPtY + row * cellHeight + cellPadding;
                for (int col = 0; col < columnsCount; col++)
                    float x = startPtX + col * cellWidth + cellPadding;
                    cellBoundaries.Add(new RectangleF(x, y, cellWidthWithoutPadding, cellHeightWithoutPadding));

            int n = Math.Min(cellCount, imagesList.Count);
            for (int i = 0; i < n; i++)
                Bitmap img = imagesList[i];
                //  Skip cell if the image is null.
                if (img == null) continue;

                RectangleF boundary = cellBoundaries[i];
                //  Fit image to boundary
                float ratio = boundary.Height / img.Height;
                float finalWidth = img.Width * ratio;
                //  Keep center alignment in horizontal.
                float shiftX = (boundary.Width - finalWidth) / 2F;
                RectangleF imgBoundary = new RectangleF(boundary.X + shiftX, boundary.Y,
                                                        finalWidth, boundary.Height);

                //  Add image to the actual region
                PDFImageHandler.AddImage(page, img, imgBoundary);

Run the above function

The C# below will dynamically create 12 images with red background, and convert images into one PDF page using the above PrintMultipleImagesOnPageTable() function

  1. Programatically generate 20 bitmap images, each image has red background and one numeric id on it.
  2. The 7th image is null object, and the matched cell will not display any images
  3. Call PrintMultipleImagesOnPageTable() function to add the above images on a specified area in PDF page (Top left corder point is 50 pixel, 100 pixel, 400 pixel in width, and 500 pixel in height), in 3 rows, 4 columns. Each cell has 10 pixel padding space on top, bottom, left, and right.
  4. Use function PDFDocument.Save() to save the PDF document with multiple images inserted.

            string outputFilePath = @"W:\\Projects\\Test-Output\\\\NetCoreSDK-Tutorial-PDF\\convert-multiple-images-to-pdf-page.pdf";

            List<Bitmap> imagesList = new List<Bitmap>();
            for (int i = 0; i < 20; i++)
                int w = 200 + 20 * i;
                int h = 400 + 20 * i;

                Bitmap bitmap = new Bitmap(w, h);
                using (Graphics g = Graphics.FromImage(bitmap))
                    g.DrawString((i + 1).ToString(), new Font("Arial", 36F, FontStyle.Regular), new SolidBrush(Color.Black), 50, 50);

                if (i==6)

            PDFDocument doc = PDFDocument.Create(1, 8.5F, 11.0F);
            PDFPage page = (PDFPage)doc.GetPage(0);
            PrintMultipleImagesOnPageTable(page, imagesList, new PointF(50, 100), 400, 500, 3, 4, 10);