Merge images
Aspose.PDF 21.4 allows you to combine Images. Merge Images method checks the contents of a specific folder and works with the specified type of files in it. When working with merging pictures, we specify ‘inputImagesStreams’, Image Format and Image Merge Mode (as example - vertical) of our file. Then we save our result in FileOutputStream.
Follow the next code snippet for resolve your task:
Merge Images
private static void MergeImages01()
{
var dataDir = RunExamples.GetDataDir_AsposePdf_Images(); // Updated to use dynamic path
// Get all image files matching the pattern "MergeImages*.jpg"
var fileStreams = Directory.GetFiles(dataDir, "MergeImages*.jpg")
.OrderBy(f => f)
.Select(f => File.OpenRead(f))
.Cast<Stream>()
.ToList();
// Use 'using' block to ensure proper resource management for both input and output streams
using (Stream inputStream = Aspose.Pdf.Facades.PdfConverter.MergeImages(fileStreams, Aspose.Pdf.Drawing.ImageFormat.Jpeg, ImageMergeMode.Vertical, 1, 1))
using (FileStream outputStream = new FileStream(dataDir + "MergeImages_out.jpg", FileMode.Create)) // Output file with '_out' suffix
{
// Copy merged images to the output file
inputStream.CopyTo(outputStream);
}
}
The second example works the same as the previous one, but the merged images will be saved horizontally.
private static void MergeImages02()
{
var dataDir = RunExamples.GetDataDir_AsposePdf_Images(); // Using dynamic path
// Get all image files matching the pattern "MergeImages*.jpg"
var fileStreams = Directory.GetFiles(dataDir, "MergeImages*.jpg")
.OrderBy(f => f)
.Select(f => File.OpenRead(f))
.Cast<Stream>()
.ToList();
// Use 'using' block to ensure proper resource management for both input and output streams
using (Stream inputStream =
Aspose.Pdf.Facades.PdfConverter.MergeImages(fileStreams, Aspose.Pdf.Drawing.ImageFormat.Jpeg, ImageMergeMode.Horizontal, 1, 1))
using (FileStream outputStream = new FileStream(dataDir + "MergeImages02_out.jpg", FileMode.Create)) // Output file with '_out' suffix
{
// Copy merged images to the output file
inputStream.CopyTo(outputStream);
}
}
In the third example, we will merge the pictures by centering them. Two horizontally, two vertically.
private static void MergeImages03()
{
var dataDir = RunExamples.GetDataDir_AsposePdf_Images(); // Using dynamic path
// Get all image files matching the pattern "MergeImages*.jpg"
var fileStreams = Directory.GetFiles(dataDir, "MergeImages*.jpg")
.OrderBy(f => f)
.Select(f => File.OpenRead(f))
.Cast<Stream>()
.ToList();
// Use 'using' block to ensure proper resource management for both input and output streams
using (Stream inputStream =
Aspose.Pdf.Facades.PdfConverter.MergeImages(fileStreams, Aspose.Pdf.Drawing.ImageFormat.Jpeg, ImageMergeMode.Center, 2, 2))
using (FileStream outputStream = new FileStream(dataDir + "MergeImages03_out.jpg", FileMode.Create)) // Output file with '_out' suffix
{
// Copy merged images to the output file
inputStream.CopyTo(outputStream);
}
}
Also, Aspose.PDF for Java present you the opportunity to combine pictures and save them in the Tiff format, using MergeImagesAsTiff Method.
private static void MergeImages04()
{
var dataDir = RunExamples.GetDataDir_AsposePdf_Images(); // Using dynamic path
// Get all image files matching the pattern "MergeImages*.jpg"
var fileStreams = Directory.GetFiles(dataDir, "MergeImages*.jpg")
.OrderBy(f => f)
.Select(f => File.OpenRead(f))
.Cast<Stream>()
.ToList();
// Use 'using' block to ensure proper resource management for both input and output streams
using (Stream inputStream =
Aspose.Pdf.Facades.PdfConverter.MergeImagesAsTiff(fileStreams))
using (FileStream outputStream = new FileStream(dataDir + "MergeImages_out.tiff", FileMode.Create)) // Output file with '_out' suffix
{
// Copy merged images to the output file
inputStream.CopyTo(outputStream);
}
}
To save the merged images as one image on PDF page, we place them in the imageStream, place the result on the page with addImage method, where we specify the coordinates where we want to place them.
private static void MergeImages05()
{
var dataDir = RunExamples.GetDataDir_AsposePdf_Images(); // Using dynamic path
// Get all image files matching the pattern "MergeImages*.jpg"
var fileStreams = Directory.GetFiles(dataDir, "MergeImages*.jpg")
.OrderBy(f => f)
.Select(f => File.OpenRead(f))
.Cast<Stream>()
.ToList();
// Use 'using' block to ensure proper resource management for both input and output streams
using (Stream inputStream =
Aspose.Pdf.Facades.PdfConverter.MergeImages(fileStreams, Aspose.Pdf.Drawing.ImageFormat.Jpeg, ImageMergeMode.Vertical, 1, 1))
using (MemoryStream outputStream = new MemoryStream()) // Output to MemoryStream
{
// Copy merged images to the MemoryStream
inputStream.CopyTo(outputStream);
// Create a new Document instance and add a page
var document = new Aspose.Pdf.Document();
var page = document.Pages.Add();
// Add the image from the MemoryStream to the page
page.AddImage(outputStream, new Aspose.Pdf.Rectangle(10, 120, 400, 720));
// Save the document with '_out' suffix
document.Save(dataDir + "MergeImages_out.pdf");
}
}