Merge JPG to PDF using C#

JPG is the most widely used image file format for storing compressed photos. PDF provides a read‑only document format that preserves layout and styling across platforms. Frequently you need to combine several JPG pictures into a single PDF file. This article explains how to merge JPG images into a PDF using C#, covering conversion, appending, and batch merging techniques.

The following topics are covered:

C# API to Merge JPG Images into PDF

Merging JPG files into a PDF involves two steps. First, use Aspose.Imaging for .NET to convert each JPG to a PDF page. Second, combine the generated PDF pages with GroupDocs.Merger for .NET. Both libraries are available via NuGet or direct download.

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

Convert JPG to PDF in C#

To convert a single JPG image to a PDF document:

  1. Load the JPG using the Image.Load() method.
  2. Call the Image.Save() method, specifying a .pdf file name.

The sample below demonstrates the conversion process.

// 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#

Appending a JPG to an existing PDF requires converting the JPG to a temporary PDF stream and then merging it with the target document:

  1. Load the JPG with Image.Load().
  2. Save the image as PDF into a FileStream using Image.Save().
  3. Initialise a Merger instance with the existing PDF file.
  4. Use Merger.Join() to attach the JPG‑PDF stream.
  5. Save the combined document with Merger.Save().
// Append a JPG image to an existing PDF.
// Load JPG image
Image image = Image.Load(@"sample1.jpg");

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

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

// Join the JPG‑PDF stream with the existing PDF
merger.Join(fs);

// Save the merged result
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#

When you need to combine many JPG files into a single PDF, follow these steps:

  1. Enumerate all JPG files in a folder.
  2. Load each image with Image.Load().
  3. Convert the first image directly to a PDF file; subsequent images are saved to FileStream objects.
  4. Use Merger to join each temporary PDF with the main document.
  5. Save the final merged PDF.
// Merge all JPG files in a folder into one 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)
    {
        // First image creates the base PDF
        image.Save(@"D:\Files\Images\converted.pdf");
        count = 1;
    }
    else
    {
        // Convert subsequent images to PDF streams
        FileStream fs = new FileStream(fileName + ".pdf", FileMode.Create);
        image.Save(fs);

        // Merge with the base PDF
        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

Request a free temporary license to evaluate the APIs without restrictions: a free temporary license.

Conclusion

In this guide you learned how to:

  • Convert a JPG image to a PDF file using C#.
  • Append a JPG image to an existing PDF document.
  • Merge multiple JPG files into a single PDF document.

For deeper details on Aspose.Imaging for .NET, consult the official documentation. If you have questions, feel free to join the discussion on the forum.

See Also