Java  PDFで割印を作る

<p><strong>割印とは、2つ以上の文書にハンコをまたがるように押すことによって、文書の関連性を示すなつ印方法のことをいいます。今日はSpire.PDF for Javaという優れたライブラリを利用して、PDFで割印の作成法を紹介しましょう！</strong></p>
<h4><strong><span style="font-family: 'MS Mincho'; mso-ascii-font-family: Century; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'MS Mincho'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Century; mso-hansi-theme-font: minor-latin;">下準備</span><o:p></o:p></strong></h4>
<p class="MsoNormal"><strong><span lang="EN-US">1.E-iceblue</span><span style="font-family: 'MS Mincho'; mso-ascii-font-family: Century; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'MS Mincho'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Century; mso-hansi-theme-font: minor-latin;">の公式サイトから</span><span lang="EN-US"><a href="https://www.e-iceblue.com/Download/pdf-for-java.html">Free Spire.PDF for Java</a></span><span style="font-family: 'MS Mincho'; mso-ascii-font-family: Century; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'MS Mincho'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Century; mso-hansi-theme-font: minor-latin;">無料版をダウンロードしてください。</span></strong></p>
<p class="MsoNormal"><strong><span lang="EN-US"><o:p> <img src="https://cdn-ak.f.st-hatena.com/images/fotolife/l/lendoris/20210421/20210421105042.png" alt="f:id:lendoris:20210421105042p:plain" title="" class="hatena-fotolife" itemprop="image" /></o:p></span></strong></p>
<p class="MsoNormal"><strong><span lang="EN-US">2. IDE</span><span style="font-family: 'MS Mincho'; mso-ascii-font-family: Century; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'MS Mincho'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Century; mso-hansi-theme-font: minor-latin;">を起動して新規プロジェクトを作成してから、インストールされたファイルにあった相応しい</span><span lang="EN-US">Spire.PDF.jar</span><span style="font-family: 'MS Mincho'; mso-ascii-font-family: Century; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'MS Mincho'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Century; mso-hansi-theme-font: minor-latin;">を参照に追加してください。</span></strong><strong><span lang="EN-US"><br /></span></strong></p>
<p><img src="https://cdn-ak.f.st-hatena.com/images/fotolife/l/lendoris/20210421/20210421105055.png" alt="f:id:lendoris:20210421105055p:plain" title="" class="hatena-fotolife" itemprop="image" /></p>
<p class="MsoNormal"> </p>
<p> </p>
```JAVA
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 {

        //ファイルを読み込みます。
        PdfDocument doc = new PdfDocument();
        doc.loadFromFile("Contract.pdf");

        PdfUnitConvertor convert = new PdfUnitConvertor();
        PdfPageBase pageBase = null;

        //割れた印章のイメージを取得します・
        BufferedImage[] images = GetImage(doc.getPages().getCount());
        float x = 0;
        float y = 0;

        // イメージを指定するPDFページのところへ移動させます。
        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));
        }
        //PDFを保存します。
        doc.saveToFile("Result.pdf");
    }
    //GetImageメソッドを定義し、PDFページ数によって印章のイメージを分割します。
    static BufferedImage[] GetImage(int num) throws IOException {
        String originalImg = "stamp.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;
    }
}
```
<h4><strong><span style="font-family: 'MS Mincho'; mso-ascii-font-family: Century; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'MS Mincho'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Century; mso-hansi-theme-font: minor-latin;">実行結果</span></strong></h4>
<p><img src="https://cdn-ak.f.st-hatena.com/images/fotolife/l/lendoris/20210421/20210421105149.png" alt="f:id:lendoris:20210421105149p:plain" title="" class="hatena-fotolife" itemprop="image" /></p>
<p class="MsoNormal"> </p>