Cross-page seals are a way of stamping seals which often used in our work for some important business documents, such as contracts, personnel files and technical documents. Its main role is to prevent documents from being modified, increased or decreased. This tutorial will demonstrate how to add a cross-page seal to a PDF document in Java applications using a free third-party library called Free Spire.PDF for Java.
First of all, let’s look at the screenshot of the resulting document after adding a cross-page seal.
How to add a cross-page seal
to a PDF document in Java?
Test
environment:
l IntelliJ IDEA
2019
l JDK 1.8
l Free Spire.PDF for Java
Add required dependency to IDEA:
Method 1: Download Free Spire.PDF for Java package from
E-iceblue website, unzip it and manually add Spire.Pdf.jar in the “lib” folder
to IDEA.
Method 2: Create a Maven repository in IDEA, and define Spire.PDF for
Java API dependency in Maven pom.xml as follows.
<repositories>
<repository>
<id>com.e-iceblue</id>
<url>http://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.pdf.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>
Using the code:
import com.spire.pdf.*;
import com.spire.pdf.graphics.*;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.io.*;
public class AddSeamSeals {
public static void main(String[] args) throws IOException {
//load a sample document from disk.
PdfDocument doc = new PdfDocument();
doc.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.pdf");
PdfUnitConvertor convert = new PdfUnitConvertor();
PdfPageBase pageBase = null;
//get the segmented seal image.
BufferedImage[] images = GetImage(doc.getPages().getCount());
float x = 0;
float y = 0;
//draw the picture to the designated location on the PDF page.
for (int i = 0; i < doc.getPages().getCount(); i++)
{
BufferedImage image= images[ i ];
pageBase = doc.getPages().get(i);
x = (float)pageBase.getSize().getWidth() - convert.convertUnits(image.getWidth(), PdfGraphicsUnit.Pixel, PdfGraphicsUnit.Point);
y = (float) pageBase.getSize().getHeight()/ 2;
pageBase.getCanvas().drawImage(PdfImage.fromImage(image), new Point2D.Float(x, y));
}
//save the Pdf file.
doc.saveToFile("output/AddSeamSeals.pdf");
}
//define the GetImage method to segment the seal image according to the number of PDF pages.
static BufferedImage[] GetImage(int num) throws IOException {
String originalImg = "C:\\Users\\Test1\\Desktop\\Image.png";
BufferedImage image = ImageIO.read(new File(originalImg));
int rows = 1;
int cols = num;
int chunks = rows * cols;
int chunkWidth = image.getWidth() / cols;
int chunkHeight = image.getHeight() / rows;
int count = 0;
BufferedImage[] imgs = new BufferedImage[ chunks ];
for (int x = 0; x < rows; x++) {
for (int y = 0; y < cols; y++) {
imgs[ count ] = new BufferedImage(chunkWidth, chunkHeight, image.getType());
Graphics2D gr = imgs[ count++ ].createGraphics();
gr.drawImage(image, 0, 0, chunkWidth, chunkHeight,
chunkWidth * y, chunkHeight * x,
chunkWidth * y + chunkWidth, chunkHeight * x + chunkHeight, Color.WHITE,null);
gr.dispose();
}
}
return imgs;
}
}
No comments:
Post a Comment