Reusing images
repeatimage
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
*/
/**
* <p>
* In this example, we take an image that is present in the background,
* and we add the same image (by its reference) to the foreground so that
* it covers the OCR'd text.
*/
package com.itextpdf.samples.sandbox.images;
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDictionary;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfName;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfStream;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.xobject.PdfImageXObject;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.borders.SolidBorder;
import com.itextpdf.layout.element.Image;
import java.io.File;
public class RepeatImage {
public static final String DEST = "./target/sandbox/images/repeat_image.pdf";
public static final String SRC = "./src/main/resources/pdfs/chinese.pdf";
public static void main(String[] args) throws Exception {
File file = new File(DEST);
file.getParentFile().mkdirs();
new RepeatImage().manipulatePdf(DEST);
}
protected void manipulatePdf(String dest) throws Exception {
PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
Document doc = new Document(pdfDoc);
PdfPage firstPage = pdfDoc.getFirstPage();
Rectangle pageSize = firstPage.getPageSize();
// Assume that there is a single XObject in the source document
// and this single object is an image.
PdfDictionary pageDict = firstPage.getPdfObject();
PdfDictionary pageResources = pageDict.getAsDictionary(PdfName.Resources);
PdfDictionary pageXObjects = pageResources.getAsDictionary(PdfName.XObject);
PdfName imgRef = pageXObjects.keySet().iterator().next();
PdfStream imgStream = pageXObjects.getAsStream(imgRef);
PdfImageXObject imgObject = new PdfImageXObject(imgStream);
Image image = new Image(imgObject);
image.setFixedPosition(0, 0);
image.setBorder(new SolidBorder(ColorConstants.BLACK, 5));
image.scaleAbsolute(pageSize.getWidth(), pageSize.getHeight());
doc.add(image);
doc.close();
}
}
C#
C#
using System;
using System.IO;
using System.Linq;
using iText.Kernel.Colors;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Xobject;
using iText.Layout;
using iText.Layout.Borders;
using iText.Layout.Element;
namespace iText.Samples.Sandbox.Images
{
public class RepeatImage
{
public static readonly String DEST = "results/sandbox/images/repeat_image.pdf";
public static readonly String SRC = "../../../resources/pdfs/chinese.pdf";
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new RepeatImage().ManipulatePdf(DEST);
}
protected void ManipulatePdf(String dest)
{
PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
Document doc = new Document(pdfDoc);
PdfPage firstPage = pdfDoc.GetFirstPage();
Rectangle pageSize = firstPage.GetPageSize();
// Assume that there is a single XObject in the source document
// and this single object is an image.
PdfDictionary pageDict = firstPage.GetPdfObject();
PdfDictionary pageResources = pageDict.GetAsDictionary(PdfName.Resources);
PdfDictionary pageXObjects = pageResources.GetAsDictionary(PdfName.XObject);
PdfName imgRef = pageXObjects.KeySet().First();
PdfStream imgStream = pageXObjects.GetAsStream(imgRef);
PdfImageXObject imgObject = new PdfImageXObject(imgStream);
Image image = new Image(imgObject);
image.SetFixedPosition(0, 0);
image.SetBorder(new SolidBorder(ColorConstants.BLACK, 5));
image.ScaleAbsolute(pageSize.GetWidth(), pageSize.GetHeight());
doc.Add(image);
doc.Close();
}
}
}
reuseimage
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.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDictionary;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfName;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfStream;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.xobject.PdfImageXObject;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import java.io.File;
public class ReuseImage {
public static final String DEST = "./target/sandbox/images/reuse_image.pdf";
public static final String SRC = "./src/main/resources/pdfs/single_image.pdf";
public static void main(String[] args) throws Exception {
File file = new File(DEST);
file.getParentFile().mkdirs();
new ReuseImage().manipulatePdf(DEST);
}
protected void manipulatePdf(String dest) throws Exception {
PdfDocument srcDoc = new PdfDocument(new PdfReader(SRC));
PdfDocument resultDoc = new PdfDocument(new PdfWriter(dest));
Document doc = new Document(resultDoc, PageSize.A4.rotate());
// Assume that there is a single XObject in the source document
// and this single object is an image.
PdfDictionary pageDict = srcDoc.getFirstPage().getPdfObject();
PdfDictionary pageResources = pageDict.getAsDictionary(PdfName.Resources);
PdfDictionary pageXObjects = pageResources.getAsDictionary(PdfName.XObject);
PdfName imgRef = pageXObjects.keySet().iterator().next();
PdfStream imgStream = pageXObjects.getAsStream(imgRef);
PdfImageXObject imgObject = new PdfImageXObject((PdfStream) imgStream.copyTo(resultDoc));
srcDoc.close();
Image image = new Image(imgObject);
image.scaleToFit(PageSize.A4.getHeight(), PageSize.A4.getWidth());
image.setFixedPosition((PageSize.A4.getHeight() - image.getImageScaledWidth()) / 2,
(595 - image.getImageScaledHeight()) / 2);
doc.add(image);
doc.close();
}
}
C#
C#
using System;
using System.IO;
using System.Linq;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Xobject;
using iText.Layout;
using iText.Layout.Element;
namespace iText.Samples.Sandbox.Images
{
public class ReuseImage
{
public static readonly String DEST = "results/sandbox/images/reuse_image.pdf";
public static readonly String SRC = "../../../resources/pdfs/single_image.pdf";
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new ReuseImage().ManipulatePdf(DEST);
}
protected void ManipulatePdf(String dest)
{
PdfDocument srcDoc = new PdfDocument(new PdfReader(SRC));
PdfDocument resultDoc = new PdfDocument(new PdfWriter(dest));
Document doc = new Document(resultDoc, PageSize.A4.Rotate());
// Assume that there is a single XObject in the source document
// and this single object is an image.
PdfDictionary pageDict = srcDoc.GetFirstPage().GetPdfObject();
PdfDictionary pageResources = pageDict.GetAsDictionary(PdfName.Resources);
PdfDictionary pageXObjects = pageResources.GetAsDictionary(PdfName.XObject);
PdfName imgRef = pageXObjects.KeySet().First();
PdfStream imgStream = pageXObjects.GetAsStream(imgRef);
PdfImageXObject imgObject = new PdfImageXObject((PdfStream) imgStream.CopyTo(resultDoc));
srcDoc.Close();
Image image = new Image(imgObject);
image.ScaleToFit(PageSize.A4.GetHeight(), PageSize.A4.GetWidth());
image.SetFixedPosition((PageSize.A4.GetHeight() - image.GetImageScaledWidth()) / 2,
(PageSize.A4.GetWidth() - image.GetImageScaledHeight()) / 2);
doc.Add(image);
doc.Close();
}
}
}