XDoc.PDF
Features
Tech Specs
How-to C#
How-to VB.NET
Pricing
C# PDF: How to PDF Create PDF Export File and Page Process PDF Read PDF Write Form Process Document Protect Annotation & Drawing PDF Print WPF Viewer & Editor Work with Other SDKs Barcode Read Barcode Create OCR Twain
Create PDF from Images
  |  
Home ›› XDoc.PDF ›› C# PDF: Create PDF from Images

C#.NET PDF - Create PDF from Images in C# with XDoc.NET PDF Control


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




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


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


Note: When you get the error "Could not load file or assembly 'RasterEdge.Imaging.Basic' or any other assembly or one of its dependencies. An attempt to load a program with an incorrect format", please check your configure as follows:

       

       If you are using x64 libraries/dlls, Right click the project -> Properties -> Build -> Platform target: x64.

       

       If using x86, the platform target should be x86.




        #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




C# convert two or multiple Raster Image files to PDF (batch convert)





        #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




.NET Class Namespace Required



In order to run the sample code, the following steps would be necessary.


Add necessary references:


  RasterEdge.Imaging.Basic.dll


  RasterEdge.Imaging.Basic.Codec.dll


  RasterEdge.Imaging.Drawing.dll


  RasterEdge.Imaging.Font.dll


  RasterEdge.Imaging.Processing.dll


  RasterEdge.XImage.Raster.dll


  RasterEdge.XImage.Raster.Core.dll


  RasterEdge.XDoc.PDF.dll


Use corresponding namespaces;


  using RasterEdge.Imaging.Basic;


  using System.Drawing;


  using RasterEdge.XDoc.PDF;