Background images
These images were written in response to questions such as:
backgroundimage
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.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import java.io.File;
public class BackgroundImage {
public static final String DEST = "./target/sandbox/images/background_image.pdf";
public static final String IMAGE = "./src/main/resources/img/berlin2013.jpg";
public static void main(String[] args) throws Exception {
File file = new File(DEST);
file.getParentFile().mkdirs();
new BackgroundImage().manipulatePdf(DEST);
}
protected void manipulatePdf(String dest) throws Exception {
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
PageSize pageSize = PageSize.A4.rotate();
Document doc = new Document(pdfDoc, pageSize);
PdfCanvas canvas = new PdfCanvas(pdfDoc.addNewPage());
canvas.addImageFittedIntoRectangle(ImageDataFactory.create(IMAGE), pageSize, false);
doc.add(new Paragraph("Berlin!"));
doc.close();
}
}
C#
C#
using System;
using System.IO;
using iText.IO.Image;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Layout;
using iText.Layout.Element;
namespace iText.Samples.Sandbox.Images
{
public class BackgroundImage
{
public static readonly String DEST = "results/sandbox/images/background_image.pdf";
public static readonly String IMAGE = "../../../resources/img/berlin2013.jpg";
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new BackgroundImage().ManipulatePdf(DEST);
}
protected void ManipulatePdf(String dest)
{
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
PageSize pageSize = PageSize.A4.Rotate();
Document doc = new Document(pdfDoc, pageSize);
PdfCanvas canvas = new PdfCanvas(pdfDoc.AddNewPage());
canvas.AddImageFittedIntoRectangle(ImageDataFactory.Create(IMAGE), pageSize, false);
doc.Add(new Paragraph("Berlin!"));
doc.Close();
}
}
}
backgroundtransparent
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.ImageData;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.extgstate.PdfExtGState;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import java.io.File;
public class BackgroundTransparent {
public static final String DEST = "./target/sandbox/images/background_transparent.pdf";
public static final String IMAGE = "./src/main/resources/img/berlin2013.jpg";
public static void main(String[] args) throws Exception {
File file = new File(DEST);
file.getParentFile().mkdirs();
new BackgroundTransparent().manipulatePdf(DEST);
}
protected void manipulatePdf(String dest) throws Exception {
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
PageSize pageSize = PageSize.A4.rotate();
Document doc = new Document(pdfDoc, pageSize);
ImageData image = ImageDataFactory.create(IMAGE);
PdfCanvas canvas = new PdfCanvas(pdfDoc.addNewPage());
canvas.saveState();
PdfExtGState state = new PdfExtGState().setFillOpacity(0.6f);
canvas.setExtGState(state);
Rectangle rect = new Rectangle(0, 0, pageSize.getWidth(), pageSize.getHeight());
canvas.addImageFittedIntoRectangle(image, rect, false);
canvas.restoreState();
doc.add(new Paragraph("Berlin!"));
doc.close();
}
}
C#
C#
using System;
using System.IO;
using iText.IO.Image;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Kernel.Pdf.Extgstate;
using iText.Layout;
using iText.Layout.Element;
namespace iText.Samples.Sandbox.Images
{
public class BackgroundTransparent
{
public static readonly String DEST = "results/sandbox/images/background_transparent.pdf";
public static readonly String IMAGE = "../../../resources/img/berlin2013.jpg";
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new BackgroundTransparent().ManipulatePdf(DEST);
}
protected void ManipulatePdf(String dest)
{
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
PageSize pageSize = PageSize.A4.Rotate();
Document doc = new Document(pdfDoc, pageSize);
ImageData image = ImageDataFactory.Create(IMAGE);
PdfCanvas canvas = new PdfCanvas(pdfDoc.AddNewPage());
canvas.SaveState();
PdfExtGState state = new PdfExtGState().SetFillOpacity(0.6f);
canvas.SetExtGState(state);
Rectangle rect = new Rectangle(0, 0, pageSize.GetWidth(), pageSize.GetHeight());
canvas.AddImageFittedIntoRectangle(image, rect, false);
canvas.RestoreState();
doc.Add(new Paragraph("Berlin!"));
doc.Close();
}
}
}