Convert images to PDF in memory using C# and VB.Net

Convert images to PDF in memory
using C# and VB.Net.


Complete code

using System;
using System.IO;
using System.Collections.Generic;

namespace Sample
{
    class Test
    {

        static void Main(string[] args)
        {
            // Convert images to PDF in memory.			 			
            SautinSoft.PdfVision v = new SautinSoft.PdfVision();

            //v.Serial = "XXXXXXXXXXXXXXX";

            // specify converting options.
            v.PageStyle.PageSize.Auto();
            //v.PageStyle.PageMarginLeft.Inch(1);
            //v.ImageStyle.Heightmm(150);
            //v.ImageStyle.WidthInch(10);			

            // Create array containing paths to different images.
            string[] imgFiles = new string[4]
            { @"..\..\image1.jpg",
              @"..\..\image2.png",
              @"..\..\image3.gif",
              @"..\..\image4.jpg"};

            List<byte[]> imgBytesList = new List<byte[]>();

            foreach (string imgFile in imgFiles)
                imgBytesList.Add(File.ReadAllBytes(imgFile));

            // Convert list with image bytes to PDF bytes.
            byte[] pdfBytes = v.ConvertImageStreamArrayToPDFStream(imgBytesList);
            if (pdfBytes != null)
            {
                // Save PDF stream to a file.
                FileInfo pdfFile = new FileInfo(@"copy.pdf");
                File.WriteAllBytes(pdfFile.FullName, pdfBytes);

                // Open produced PDF in default PDF Reader.
                System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(pdfFile.FullName) { UseShellExecute = true });
            }
        }
    }
}

Download.

        
            Imports System.IO

Namespace Sample
    Friend Class Test

        Shared Sub Main(ByVal args() As String)
            ' Convert images to PDF in memory.			 			
            Dim v As New SautinSoft.PdfVision()

            'v.Serial = "XXXXXXXXXXXXXXX"

            ' specify converting options.
            v.PageStyle.PageSize.Auto()
            'v.PageStyle.PageMarginLeft.Inch(1)
            'v.ImageStyle.Heightmm(150)
            'v.ImageStyle.WidthInch(10)			

            ' Create array containing paths to different images.
            Dim imgFiles() As String = {"..\..\image1.jpg", "..\..\image2.png", "..\..\image3.gif", "..\..\image4.jpg"}

            Dim imgBytesList As New List(Of Byte())()

            For Each imgFile As String In imgFiles
                imgBytesList.Add(File.ReadAllBytes(imgFile))
            Next imgFile

            ' Convert list with image bytes to PDF bytes.
            Dim pdfBytes() As Byte = v.ConvertImageStreamArrayToPDFStream(imgBytesList)
            If pdfBytes IsNot Nothing Then

                ' Save PDF stream to a file.
                Dim pdfFile As New FileInfo("copy.pdf")
                File.WriteAllBytes(pdfFile.FullName, pdfBytes)

                ' Open produced PDF in default PDF Reader.
                System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(pdfFile.FullName) With {.UseShellExecute = True})
            End If
        End Sub
    End Class
End Namespace

Download.


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.