Merge JPG to PDF using C#

JPG is the most widely used image file format for storing compressed images. PDF, on the other hand, permits documents to be shared in a read-only format without compromising their style or layout. We may occasionally need to combine numerous JPG photos into a PDF document. In this article, we will learn how to merge JPG images into a PDF document using C#.

The following topics shall be covered in this article:

C# API to Merge JPG Images into PDF

For merging two or more JPG images into a PDF document, we will follow a two-step procedure. Firstly, we will be using Aspose.Imaging for .NET to convert JPG to PDF, and then we will merge them into a PDF document using GroupDocs.Merger for .NET API. Please either download the DLLs for the APIs or install them using NuGet.

PM> Install-Package Aspose.Imaging
PM> Install-Package GroupDocs.Merger

Convert JPG to PDF in C#

We can convert any JPG image into a PDF document by following the steps given below:

  1. Load a JPG image using the Image.Load() method.
  2. Finally, call the Image.Save() method to save the image as PDF. It takes output file path as an argument.

The following code sample shows how to convert a JPG to a PDF using C#.

// This code example demonstrates how to convert a JPG image to a PDF document.
// Load JPG image
Image image = Image.Load(@"sample1.jpg");

// Save as PDF
image.Save(@"converted.pdf");
Convert JPG to PDF in C#.

Convert JPG to PDF in C#.

Append JPG Image in PDF using C#

We can append a JPG image into an existing PDF document by following the steps given below:

  1. Load a JPG image using the Image.Load() method.
  2. Convert loaded image to a PDF and save in FileStream using the Image.Save() method.
  3. Load an existing PDF using the Merger class.
  4. Call the Merger.Join() method to join the JPG converted PDF with the loaded PDF.
  5. Finally, call the Merger.Save() method to save the merged PDF. It takes output file path as an argument.

The following code sample shows how to append a JPG image into an existing PDF document using C#.

// This code example demonstrates how to Append JPG in an Exisiting PDF.
// Load JPG image
Image image = Image.Load(@"sample1.jpg");

// Convert to PDF and save in FileStream
FileStream fs = new FileStream("image.pdf", FileMode.Create);
image.Save(fs);

// Load an existing PDF
Merger merger = new Merger(@"sample.pdf");

// Join JPG converted PDF with loaded PDF
merger.Join(fs);

// Save the merged PDF
merger.Save(@"Merged.pdf");
Append JPG Image in PDF using C#.

Append JPG Image in PDF using C#.

Merge Multiple JPG Images into PDF using C#

We can merge multiple JPG images into a PDF document by following the steps given below:

  1. Read all JPG image files from a directory one by one.
  2. Load a JPG image using the Image.Load() method.
  3. Convert the first image to PDF and save the file on a local disk. Otherwise, convert and save in FileStream.
  4. Load previously saved PDF using the Merger class.
  5. Call the Merger.Join() method to join the JPG converted PDF with the loaded PDF.
  6. Finally, call the Merger.Save() method to save the merged PDF. It takes output file path as an argument.

The following code sample shows how to merge multiple JPG images into a PDF document using C#.

// This code example demonstrates how to merge JPG images into a PDF.
int count = 0;
foreach (string fileName in Directory.GetFiles(@"D:\Files\Images\", "*.jpg"))
{
    // Load JPG image
    Image image = Image.Load(fileName);

    if (count == 0)
    {
        // Save PDF file
        image.Save(@"D:\Files\Images\converted.pdf");
        count = 1;   
    }
    else
    {
        // Convert to PDF and save in FileStream
        FileStream fs = new FileStream(fileName + ".pdf", FileMode.Create);
        image.Save(fs);

        // Merge
        using (Merger merger = new Merger(@"D:\Files\images\converted.pdf"))
        {
            merger.Join(fs);
            merger.Save(@"D:\Files\images\converted.pdf");
        }
    }
}
Merge Multiple JPG Images into PDF using C#.

Merge Multiple JPG Images into PDF using C#.

Get a Free License

Please try the API without evaluation limitations by requesting a free temporary license.

Conclusion

In this article, we have learned how to:

  • save JPG image as a PDF document in C#;
  • insert an image in a PDF document programmatically;
  • combine multiple images in a PDF document.

Besides, you can learn more about Aspose.Imaging for .NET API using the documentation. In case of any ambiguity, please feel free to contact us on the forum.

See Also