Java を使って PDF にテキストスタンプを追加

Java ライブラリの Aspose.PDF を使用してプログラムでテキストスタンプを作成する

Java ライブラリを使用して PDF にテキストスタンプを追加する方法

PDF にテキストスタンプを追加するには、Aspose.PDF for Java API を使用します。これは Java プラットフォーム用の機能豊富で強力で使いやすい変換 API です。最新バージョンを Maven から直接ダウンロードし、pom.xml に次の構成を追加することで、Maven ベースのプロジェクトにインストールできます。

Repository

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java AP</name>
    <url>https://releases.aspose.com/java/repo/</url>
</repository>

Dependency

<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>version of aspose-pdf API</version>
</dependency>

PDF ドキュメント Java にテキストスタンプを追加


ご使用の環境でコードを試すには、Aspose.PDF for Java が必要です。

  1. Document のインスタンスを持つ PDF を読み込みます。
  2. Document オブジェクトを使用して PDF ドキュメントを開きます。
  3. テキストスタンプを作成し、そのプロパティを定義します。
  4. addStampメソッドを使用してテキストスタンプをページに追加する

Python を使用して PDF にテキストスタンプを追加します

String inputFileName = "AddTextStamp.pdf";
String outputFileName = "AddTextStamp_out.pdf";

// Open document
Document pdfDocument = new Document(inputFileName);
// Create text stamp
TextStamp textStamp = new TextStamp("Sample Stamp");
// Set whether stamp is background
textStamp.setBackground(true);
// Set origin
textStamp.setXIndent(100);
textStamp.setYIndent(100);
// Rotate stamp
textStamp.setRotate(Rotation.on90);
// Set text properties
textStamp.getTextState().setFont(FontRepository.findFont("Arial"));
textStamp.getTextState().setFontSize(14.0F);
textStamp.getTextState().setFontStyle(FontStyles.Bold | FontStyles.Italic);
textStamp.getTextState().setForegroundColor(Color.getGreen());
// Add stamp to particular page
pdfDocument.getPages().get_Item(1).addStamp(textStamp);
// Save output document
pdfDocument.save(outputFileName);
pdfDocument.close();