0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

JavaでPDF文書にスタンプを追加する方法

Last updated at Posted at 2023-04-24

PDFスタンプは、紙上のゴム印のような働きをするPDFの注釈です。ゴム印のように、PDFスタンプには、テキストまたは画像から作成されたカスタムグラフィックがあります。通常、それはあなたの意図を効果的に伝えるものになります。この記事では、無料のFree Spire.PDF for Javaを使用して、JavaでPDF文書に画像や動的なテキストでスタンプを追加す方法を紹介します。

【依存関係の追加】

この方法は、無料のFree Spire.PDF for Javaが必要ですので、先にjarファイルをインポートしてください。

1. Maven

Maven を使用している場合、プロジェクトの pom.xml ファイルに以下のコードを追加することで、簡単にアプリケーションに JAR ファイルをインポートすることができます。

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf.free</artifactId>
        <version>5.1.0</version>
    </dependency>
</dependencies>

2. 公式サイトよりJarファイルをダウンロード

まず、Free Spire.PDF for Javaの公式サイトよりzipファイルをダウンロードします。zipファイルを解凍し、libフォルダの下にあるSpire.Pdf.jarファイルを依存関係としてプロジェクトにインポートしてください。

予備知識

項目 説明
PdfDocument クラス PDFドキュメントのモデルを表す。
PdfDocument.loadFromFile() メソッド ファイルパスから既存のPDFファイルを読み込む。
PdfDocument.getPages().get() メソッド PDFドキュメントの特定のページをインデックスで取得する。
PdfTemplate クラス テキストや画像などを描画できる矩形の図形領域を表す。
PdfTemplate.getGraphics().drawString() メソッド テンプレートにパスを描く。
PdfTemplate.getGraphics().drawPath() メソッド テンプレートに文字を描く。
PdfRubberStampAnnotation クラス PDFドキュメント内のゴム印を表す。
PdfRubberStampAnnotation.getAppearance() メソッド ゴム印注釈の外観を取得または設定する。
PdfPageBase.getAnnotationsWidget.add() メソッド 特定のページに注釈を追加する。
PdfDocument.savetToFile() メソッド ドキュメントをPDFファイルに保存する。

PdfTemplateクラスは、テキストや画像、日付や時間など、好きな情報を描けるキャンバスを表します。PdfRubberStampAnnotationクラスは、PDFページ上にテキストや図形をゴム印のように表示するゴム印注釈を表します。ここでのPdfTemplateは、ゴム印の外観を作成するために使用されます。

PDFに画像のスタンプを追加

画像スタンプは、既存の画像に基づいて作成されたスタンプです。以下は、Free Spire.PDF for Javaを使用して、PDFドキュメントに画像のスタンプを追加する手順です。

  • PdfDocument のオブジェクトを作成します。
  • PdfDocument.loadFromFile() メソッドを使用して PDF ファイルを読み込みます。
  • PdfDocument.getPages().get() メソッドを使用して、特定のページを取得します。
  • スタンプの外観を作成するために使用する PdfTemplate オブジェクトを作成します。
  • PdfImage.fromImage() メソッドを使用して画像を読み込みます。
  • PdfTemplate.getGraphics().drawImage() メソッドを使用してテンプレート上に画像を描画します。
  • PdfRubberStampAnnotation のオブジェクトを作成し、外観としてスタンプにこのテンプレートを適用します。
  • PdfPageBase.getAnnotationsWidget().add() メソッドを使用して、選択したページにスタンプを追加します。
  • PdfDocument.saveToFile() メソッドを使用して、ドキュメントをPDFファイルに保存します。

Java

import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.annotations.PdfRubberStampAnnotation;
import com.spire.pdf.annotations.appearance.PdfAppearance;
import com.spire.pdf.graphics.PdfImage;
import com.spire.pdf.graphics.PdfTemplate;

import java.awt.geom.Rectangle2D;

public class addImageStamp {
    public static void main(String[] args) {

        //PdfDocumentのオブジェクトを作成する
        PdfDocument doc = new PdfDocument();

        //PDFドキュメントを読み込む
        doc.loadFromFile("C:/ビジネス請求書.pdf");

        //最後のページを取得する
        PdfPageBase page = doc.getPages().get(doc.getPages().getCount() - 1);

        //画像ファイルを読み込む
        PdfImage image = PdfImage.fromFile("C:/Paid.png");

        //画像の幅と高さを取得する
        int width = image.getWidth();
        int height = image.getHeight();

        //画像のサイズに応じたPdfTemplateオブジェクトを作成する
        PdfTemplate template = new PdfTemplate(width, height);

        //テンプレートに画像を描く
        template.getGraphics().drawImage(image, 0, 0, width, height);

        //ゴム印の注釈を作成し、その位置と場所を指定する
        Rectangle2D rect = new Rectangle2D.Float((float) (page.getActualSize().getWidth() - width - 80), (float) (page.getActualSize().getHeight() - height - 187), width, height);
        PdfRubberStampAnnotation stamp = new PdfRubberStampAnnotation(rect);

        //PdfAppearanceのオブジェクトを作成する
        PdfAppearance pdfAppearance = new PdfAppearance(stamp);

        //テンプレートを外観の通常状態として設定する
        pdfAppearance.setNormal(template);

        //スタンプに外観を適用する
        stamp.setAppearance(pdfAppearance);

        //スタンプ注釈をPDFに追加する
        page.getAnnotationsWidget().add(stamp);

        //ファイルを保存する
        doc.saveToFile("画像スタンプの追加.pdf");
        doc.close();
    }
}

PDFに画像のスタンプを追加

PDFにカスタムの動的なスタンプを追加

動的なスタンプは、コンピュータやIDパネルから情報を取得し、スタンプにユーザー名、日付、時刻の情報を含めることができます。以下は、Free Spire.PDF for Javaを使用して、PDFドキュメントに動的なスタンプを追加する手順です。

  • PdfDocument のオブジェクトを作成します。
  • PdfDocument.loadFromFile() メソッドを使用して PDF ファイルを読み込みます。
  • PdfDocument.getPages().get() メソッドを使用して、特定のページを取得します。
  • スタンプの外観を作成するための PdfTemplate オブジェクトを作成します。
  • PdfTemplate.getGraphics().drawPath() メソッドを使用して、テンプレート上に角丸の矩形を描画します。
  • PdfTemplate.getGraphics().drawString() メソッドを使用して、テンプレート上に文字を描画します。
  • PdfRubberStampAnnotation のオブジェクトを作成し、外観としてスタンプにテンプレートを適用します。
  • PdfPageBase.getAnnotationsWidget().add() メソッドを使用して、選択したページにスタンプを追加します。
  • PdfDocument.saveToFile() メソッドを使用して、ドキュメントをPDFファイルに保存します。

Java

import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.annotations.PdfRubberStampAnnotation;
import com.spire.pdf.annotations.appearance.PdfAppearance;
import com.spire.pdf.graphics.*;

import java.awt.*;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.text.SimpleDateFormat;
import java.util.Map;

public class addDynamicStamp {
    public static void main(String[] args) {

        //PdfDocumentのオブジェクトを作成する
        PdfDocument document = new PdfDocument();

        //PDFファイルを読み込む
        document.loadFromFile("C:/ビジネス請求書.pdf");

        //最後のページを取得する
        PdfPageBase page = document.getPages().get(document.getPages().getCount() - 1);

        //PDFテンプレートを作成する
        PdfTemplate template = new PdfTemplate(220, 50);

        //2つのフォントを作成する
        PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Yu Mincho", Font.ITALIC, 16), true);
        PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Times New Roman", Font.ITALIC, 12), true);

        //ソリッド ブラシとグラデーション ブラシを作成する
        PdfSolidBrush solidBrush = new PdfSolidBrush(new PdfRGBColor(Color.blue));
        Rectangle2D rect1 = new Rectangle2D.Float();
        rect1.setFrame(new Point2D.Float(0, 0), template.getSize());
        PdfLinearGradientBrush linearGradientBrush = new PdfLinearGradientBrush(rect1, new PdfRGBColor(Color.white), new PdfRGBColor(113, 191, 234), PdfLinearGradientMode.Horizontal);

        //角丸の矩形パスを作成する
        int CornerRadius = 10;
        PdfPath path = new PdfPath();
        path.addArc(template.getBounds().getX(), template.getBounds().getY(), CornerRadius, CornerRadius, 180, 90);
        path.addArc(template.getBounds().getX() + template.getWidth() - CornerRadius, template.getBounds().getY(), CornerRadius, CornerRadius, 270, 90);
        path.addArc(template.getBounds().getX() + template.getWidth() - CornerRadius, template.getBounds().getY() + template.getHeight() - CornerRadius, CornerRadius, CornerRadius, 0, 90);
        path.addArc(template.getBounds().getX(), template.getBounds().getY() + template.getHeight() - CornerRadius, CornerRadius, CornerRadius, 90, 90);
        path.addLine(template.getBounds().getX(), template.getBounds().getY() + template.getHeight() - CornerRadius, template.getBounds().getX(), template.getBounds().getY() + CornerRadius / 2);

        //テンプレートにパスを描く
        template.getGraphics().drawPath(linearGradientBrush, path);
        template.getGraphics().drawPath(PdfPens.getBlue(), path);

        //動的なテキストをテンプレートに描画する
        String s1 = "認証済み\n";
        Map<String, String> map = System.getenv();
        String userName = map.get("USERNAME");
        String s2 = userName + ", ";
        String s3 = dateToString(new java.util.Date(), "yyyy-MM-dd HH:mm:ss");
        template.getGraphics().drawString(s1, font1, solidBrush, new Point2D.Float(5, 5));
        template.getGraphics().drawString(s2 + s3, font2, solidBrush, new Point2D.Float(5, 28));

        //ゴム印を作成し、サイズと位置を指定する
        Rectangle2D rect2 = new Rectangle2D.Float();
        rect2.setFrame(new Point2D.Float((float) (page.getActualSize().getWidth() - template.getWidth() - 80), (float) (page.getActualSize().getHeight() - template.getHeight() - 105)), template.getSize());
        PdfRubberStampAnnotation stamp = new PdfRubberStampAnnotation(rect2);

        //PdfAppearanceのオブジェクトを作成し、テンプレートを通常の状態として適用する
        PdfAppearance appearance = new PdfAppearance(stamp);
        appearance.setNormal(template);

        //スタンプに外観を適用する
        stamp.setAppearance(appearance);

        //注釈コレクションにスタンプ注釈を追加する
        page.getAnnotationsWidget().add(stamp);

        //ファイルを保存する
        document.saveToFile("動的なスタンプの追加.pdf");
        document.close();
    }

    //日付を文字列に変換する
    public static String dateToString(java.util.Date poDate, String pcFormat) {
        SimpleDateFormat loFormat = new SimpleDateFormat(pcFormat);
        return loFormat.format(poDate);
    }
}

PDFにカスタムの動的なスタンプを追加

この記事では、PDFファイルにスタンプを追加する方法を紹介します。Free Spire.PDF for Javaでは、PDFファイルに透かしやその他の多くの要素を追加することも可能です。より多くの情報を得るためにSpire.PDF for Javaチュートリアルに移動してください。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?