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でWord文書を暗号化PDFに変換する方法

Last updated at Posted at 2023-08-11

ドキュメントをPDFに変換することで、デバイス間の共有と読み取りが簡単になり、ドキュメントの見栄えを維持できます。
変換時に暗号化を適用することで、特定の人物のみがコンテンツを閲覧・編集できるように制限し、情報漏洩や改ざんを防ぐことができます。
この記事では、Free Spire.Doc for Javaを使用して、Wordドキュメントをパスワード保護のPDFに変換する方法を紹介します。

【依存関係の追加】

この記事の方法は、無料のWord文書処理Java API、Free Spire.Doc for Javaが必要です。このAPIは、公式サイトからダウンロードするか、Mavenでプロジェクトに導入することができます。Mavenのコードは以下の通りです。

<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.doc.free</artifactId>
        <version>5.2.0</version>
    </dependency>
</dependencies>

Wordドキュメントをパスワード保護PDFに変換する

Spire.Doc for Javaでは、Document.saveToFile(String, ToPdfParameterList) メソッドを使用して、Wordドキュメントをパスワード保護のPDFに変換できます。
ToPdfParameterList パラメータを使用して、暗号化の適用など、WordからPDFへの変換設定を制御できます。
以下が詳細な手順です:

  • Documentオブジェクトを作成します。
  • Document.loadFromFile() でWordドキュメントを読み込むみます。
  • ToPdfParameterList のインスタンスを作成します。
  • ToPdfParameterList.getPdfSecurity().encrypt() でPDFの開くパスワードと権限パスワードを設定します。
  • Document.saveToFile(String, ToPdfParameterList) でWordをパスワード保護PDFに変換します。

コード例

java

import com.spire.doc.Document;
import com.spire.doc.ToPdfParameterList;
import com.spire.pdf.security.PdfEncryptionKeySize;
import com.spire.pdf.security.PdfPermissionsFlags;

public class convertWordToPdfWithPassword {
    public static void main(String[] args) {
        // Documentオブジェクトの作成
        Document doc = new Document();

        // Wordドキュメントの読み込み
        doc.loadFromFile("サンプル.docx");

        // ToPdfParameterListの作成
        ToPdfParameterList parameter = new ToPdfParameterList();

        // PDFの開くパスワードと許可パスワードの設定
        parameter.getPdfSecurity().encrypt("openpassword", "permissionpassword",
                PdfPermissionsFlags.None, PdfEncryptionKeySize.Key_128_Bit);

        // Wordをパスワード保護PDFに変換
        doc.saveToFile("暗号化PDF.pdf", parameter);
    }
}

Wordドキュメントをパスワード保護PDFに変換する

上記は、Free Spire.Doc for Javaを使用してWord文書をパスワードで保護されたPDF文書に変換する方法を示しています。 Free Spire.Doc for Javaのその他の機能については、Spire.Doc for Javaのチュートリアルをご覧ください。また、Spire.Doc関連の問題について議論したい場合は、Spire.Docフォーラムにアクセスしてください。

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?