Multiple images
This example was written in response to the question Click How to add multiple images into a single PDF?
multipleimages
JAVA
JAVA
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2023 Apryse Group NV
Authors: Apryse Software.
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
package com.itextpdf.samples.sandbox.images;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import java.io.File;
public class MultipleImages {
public static final String DEST = "./target/sandbox/images/multiple_images.pdf";
public static final String[] IMAGES = {
"./src/main/resources/img/berlin2013.jpg",
"./src/main/resources/img/javaone2013.jpg",
"./src/main/resources/img/map_cic.png"
};
public static void main(String[] args) throws Exception {
File file = new File(DEST);
file.getParentFile().mkdirs();
new MultipleImages().manipulatePdf(DEST);
}
protected void manipulatePdf(String dest) throws Exception {
Image image = new Image(ImageDataFactory.create(IMAGES[0]));
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
Document doc = new Document(pdfDoc, new PageSize(image.getImageWidth(), image.getImageHeight()));
for (int i = 0; i < IMAGES.length; i++) {
image = new Image(ImageDataFactory.create(IMAGES[i]));
pdfDoc.addNewPage(new PageSize(image.getImageWidth(), image.getImageHeight()));
image.setFixedPosition(i + 1, 0, 0);
doc.add(image);
}
doc.close();
}
}
C#
C#
using System;
using System.IO;
using iText.IO.Image;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
namespace iText.Samples.Sandbox.Images
{
public class MultipleImages
{
public static readonly String DEST = "results/sandbox/images/multiple_images.pdf";
public static readonly String[] IMAGES =
{
"../../../resources/img/berlin2013.jpg",
"../../../resources/img/javaone2013.jpg",
"../../../resources/img/map_cic.png"
};
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new MultipleImages().ManipulatePdf(DEST);
}
protected void ManipulatePdf(String dest)
{
Image image = new Image(ImageDataFactory.Create(IMAGES[0]));
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
Document doc = new Document(pdfDoc, new PageSize(image.GetImageWidth(), image.GetImageHeight()));
for (int i = 0; i < IMAGES.Length; i++)
{
image = new Image(ImageDataFactory.Create(IMAGES[i]));
pdfDoc.AddNewPage(new PageSize(image.GetImageWidth(), image.GetImageHeight()));
image.SetFixedPosition(i + 1, 0, 0);
doc.Add(image);
}
doc.Close();
}
}
}