Large Image examples
These examples were written in the context of the Help, I only see blank pages in my PDF tutorial.
largeimage1
JAVA
JAVA
package sandbox.images;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PRStream;
import com.itextpdf.text.pdf.PdfDictionary;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.parser.PdfImageObject;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
@WrapToTest
public class LargeImage1 {
public static final String SRC = "resources/pdfs/large_image.pdf";
public static final String DEST = "results/images/large_image1.pdf";
public static void main(String[] args) throws DocumentException, IOException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new LargeImage1().manipulatePdf(SRC, DEST);
}
public void manipulatePdf(String src, String dest) throws DocumentException, IOException {
PdfReader reader = new PdfReader(src);
// The width and the height of a PDF page may not exceed 14400 user units:
Rectangle rect = reader.getPageSize(1);
if (rect.getWidth() < 14400 && rect.getHeight() < 14400) {
System.out.println("The size of the PDF document is within the accepted limits");
System.exit(0);
}
// We assume that there's a single large picture on the first page
PdfDictionary page = reader.getPageN(1);
PdfDictionary resources = page.getAsDict(PdfName.RESOURCES);
PdfDictionary xobjects = resources.getAsDict(PdfName.XOBJECT);
PdfName imgRef = xobjects.getKeys().iterator().next();
PRStream imgStream = (PRStream) xobjects.getAsStream(imgRef);
// We now create a new Image object based on the bytes in the stream
PdfImageObject imgObject = new PdfImageObject(imgStream);
Image img = Image.getInstance(imgObject.getImageAsBytes());
img.scaleToFit(14400, 14400);
img.setAbsolutePosition(0, 0);
reader.close();
// We create a new document with the correct size
Document document = new Document(new Rectangle(img.getScaledWidth(), img.getScaledHeight()));
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
document.add(img);
document.close();
}
}
largeimage2
JAVA
JAVA
package sandbox.images;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PRIndirectReference;
import com.itextpdf.text.pdf.PdfDictionary;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
@WrapToTest
public class LargeImage2 {
public static final String SRC = "resources/pdfs/large_image.pdf";
public static final String DEST = "results/images/large_image2.pdf";
public static void main(String[] args) throws DocumentException, IOException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new LargeImage2().manipulatePdf(SRC, DEST);
}
public void manipulatePdf(String src, String dest) throws DocumentException, IOException {
PdfReader reader = new PdfReader(src);
// The width and the height of a PDF page may not exceed 14400 user units:
Rectangle rect = reader.getPageSize(1);
if (rect.getWidth() < 14400 && rect.getHeight() < 14400) {
System.out.println("The size of the PDF document is withing the accepted limits");
System.exit(0);
}
// We assume that there's a single large picture on the first page
PdfDictionary page = reader.getPageN(1);
PdfDictionary resources = page.getAsDict(PdfName.RESOURCES);
PdfDictionary xobjects = resources.getAsDict(PdfName.XOBJECT);
PdfName imgName = xobjects.getKeys().iterator().next();
Image img = Image.getInstance((PRIndirectReference)xobjects.getAsIndirectObject(imgName));
img.scaleToFit(14400, 14400);
img.setAbsolutePosition(0, 0);
// We create a temporary file that will reuse the image
File tmp = File.createTempFile("large_image", ".pdf", new File("results"));
tmp.deleteOnExit();
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(tmp));
// We create a new first page and add the image
stamper.insertPage(1, new Rectangle(img.getScaledWidth(), img.getScaledHeight()));
stamper.getOverContent(1).addImage(img);
stamper.close();
reader.close();
// We create a new file that only contains the new first page
reader = new PdfReader(tmp.getAbsolutePath());
reader.selectPages("1");
stamper = new PdfStamper(reader, new FileOutputStream(dest));
stamper.close();
reader.close();
}
}