透かしなしで本番環境でテストしてください。
必要な場所で動作します。
30日間、完全に機能する製品をご利用いただけます。
数分で稼働させることができます。
製品トライアル期間中にサポートエンジニアリングチームへの完全アクセス
この記事では、JavaでPDFファイルを扱う際に使用される最も人気のある2つのライブラリについて取り上げます。
IronPDF
アパッチPDFBox
さて、どのライブラリを使用するべきでしょうか? この記事では、両方のライブラリの主要機能を比較し、どちらが本番環境に最適であるかを判断できるようにします。
IronPDF ライブラリは、Java 8+、Kotlin、および Scala のためのHTML to PDF コンバージョンをサポートしています。 このクリエイターは、Windows、Linux、またはクラウドプラットフォームなど、クロスプラットフォームのサポートを提供します。 これは、正確さ、使いやすさ、そして速度を重視するJava向けに特別に設計されています。
IronPDFはソフトウェア開発者がPDFドキュメントを作成、編集、抽出できるように開発されています。 IronPDF for .NETの成功と人気に基づいています。
IronPDFの主な特徴は次のとおりです:
Apache PDFBoxは、PDFファイルを操作するためのオープンソースのJavaライブラリです。 既存のドキュメントを生成、編集、操作することができます。 ファイルから内容を抽出することもできます。 このライブラリは、ドキュメントに対してさまざまな操作を実行するために使用されるいくつかのユーティリティを提供します。
Apache PDFBoxの注目すべき機能は以下の通りです。
記事の残りは次のとおりです:
IronPDFのインストール
Apache PDFBox のインストール
PDFドキュメントを作成
画像からドキュメントへ
書類の暗号化
ライセンス
結論
それでは、ライブラリをダウンロードしてインストールし、その強力な機能を比較してみましょう。
IronPDF for Java のインストールは簡単です。 それを行うには様々な方法があります。 このセクションでは、最も人気のある二つの方法を示します。
IronPDF JARファイルをダウンロードするには、IronPDFのMavenウェブサイトを訪問して、最新バージョンのIronPDFをダウンロードしてください。
ダウンロードオプションをクリックし、JARをダウンロードしてください。
JARファイルをダウンロードしたら、次にMavenプロジェクトにライブラリをインストールする時です。 どのIDEを使用してもかまいませんが、NetBeansを使用する予定です。 プロジェクトセクションにて:
IronPDFをダウンロードしてインストールする別の方法は、Mavenを使用することです。 pom.xml に依存関係を追加するか、NetBeans の依存関係ツールを使用してプロジェクトに含めるだけです。
以下のコードをコピーして、pom.xmlに貼り付けてください。
xml-mvn-install-ao
#### 依存関係機能を使用してライブラリを追加
- 依存関係を右クリックしてください
- 以下の詳細を最新バージョンで記入し、「依存関係の追加」を選択してください。
<div class="content-img-align-center">
<div class="center-image-wrapper">
<a rel="nofollow" href="/static-assets/ironpdf-java/blog/apache-pdfbox-html-to-pdf-java/apache-pdfbox-html-to-pdf-java-4.webp" target="_blank"><img src="/static-assets/ironpdf-java/blog/apache-pdfbox-html-to-pdf-java/apache-pdfbox-html-to-pdf-java-4.webp" alt="IronPDF for JavaとApache PDF Box for Javaの比較 - 図4: IronPDFの依存関係を追加" class="img-responsive add-shadow"></a>
<p class="content__image-caption">IronPDF依存関係を追加</p>
</div>
</div>
それでは、Apache PDFBoxをインストールしましょう。
## 2. Apache PDFBoxのインストール
PDFBoxはIronPDFと同じ方法でダウンロードしてインストールすることができます。
### 2.1. JAR をダウンロードしてライブラリを手動で追加
PDFBox JARをインストールするには、公式ウェブサイトにアクセスし、最新バージョンの[PDFBoxライブラリをダウンロード](https://pdfbox.apache.org/)してください。
プロジェクトを作成した後、プロジェクトセクションで:
- ライブラリフォルダーを右クリックし、**JAR/フォルダーを追加**オプションを選択します。
<div class="content-img-align-center">
<div class="center-image-wrapper">
<a rel="nofollow" href="/static-assets/ironpdf-java/blog/apache-pdfbox-html-to-pdf-java/apache-pdfbox-html-to-pdf-java-5.webp" target="_blank"><img src="/static-assets/ironpdf-java/blog/apache-pdfbox-html-to-pdf-java/apache-pdfbox-html-to-pdf-java-5.webp" alt="IronPDF For Java と Apache PDF Box for Java の比較 - 図5:ライブラリの追加" class="img-responsive add-shadow"></a>
<p class="content__image-caption">ライブラリの追加</p>
</div>
</div>
- ダウンロードしたJARがあるフォルダーに移動します。
- PDFBox JAR を選択し、[開く] ボタンをクリックします。
<div class="content-img-align-center">
<div class="center-image-wrapper">
<a rel="nofollow" href="/static-assets/ironpdf-java/blog/apache-pdfbox-html-to-pdf-java/apache-pdfbox-html-to-pdf-java-6.webp" target="_blank"><img src="/static-assets/ironpdf-java/blog/apache-pdfbox-html-to-pdf-java/apache-pdfbox-html-to-pdf-java-6.webp" alt="Java用のIronPDFとApache PDF Boxの比較 - 図6: PDFBox JARを開く" class="img-responsive add-shadow"></a>
<p class="content__image-caption">PDFBox JARを開く</p>
</div>
</div>
### Mavenで依存関係としてインストール
#### `pom.xml` に依存関係を追加
以下のコードをコピーして、pom.xmlに貼り付けてください。
```xml
<dependencies>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox-app</artifactId>
<version>3.0.0-alpha3</version>
</dependency>
</dependencies>
こ れに よ り 、 PDFBox の依存関係が自動的にダ ウ ン ロ ー ド さ れ、 リ ポジ ト リ フ ォ ルダに イ ン ス ト ール さ れます。 それで使用できるようになります。
IronPDFは、ファイルを作成するためのさまざまな方法を提供します。 最も重要な2つの方法を見てみましょう。
IronPDFはHTMLからドキュメントを生成することを非常に簡単にします。 次のコードサンプルは、WebページのURLをPDFに変換します。
License.setLicenseKey("YOUR-LICENSE-KEY");
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));
PdfDocument myPdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com");
myPdf.saveAs(Paths.get("url.pdf"));
出力は、以下のURLで、書式を整え、以下のように保存します:
以下のサンプルコードは、HTML文字列を使用してJavaでPDFをレンダリングする方法を示しています。 HTML文字列またはドキュメントを使用して、新しいドキュメントに変換するだけです。
License.setLicenseKey("YOUR-LICENSE-KEY");
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));
PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("<h1> ~Hello World~ </h1> Made with IronPDF!");
myPdf.saveAs(Paths.get("html_saved.pdf"));
出力は以下の通りです:
PDFBoxはさまざまな形式から新しいPDFドキュメントを生成することもできますが、URLやHTML文字列から直接変換することはできません。
以下のコードサンプルは、いくつかのテキストを含むドキュメントを作成します:
//Create document object
PDDocument document = new PDDocument();
PDPage blankPage = new PDPage();
document.addPage(blankPage);
//Retrieving the pages of the document
PDPage paper = document.getPage(0);
PDPageContentStream contentStream = new PDPageContentStream(document, paper);
//Begin the Content stream
contentStream.beginText();
//Setting the font to the Content stream
contentStream.setFont(PDType1Font.TIMES_ROMAN, 12);
//Setting the position for the line
contentStream.newLineAtOffset(25, 700);
String text = "This is the sample document and we are adding content to it.";
//Adding text in the form of a string
contentStream.showText(text);
//Ending the content stream
contentStream.endText();
System.out.println("Content added");
//Closing the content stream
contentStream.close();
//Saving the document
document.save("C:/PdfBox_Examples/my_doc.pdf");
System.out.println("PDF created");
//Closing the document
document.close();
しかし、上記のコード例からcontentStream.newLineAtOffset(25, 700);
を削除してプロジェクトを実行すると、ページの下部に出力されたPDFが生成されます。 これは一部の開発者にとってかなり煩わしいことがあります。というのも、(x,y) 座標を使用してテキストを調整する必要があるからです。 y = 0
は、テキストが下に表示されることを意味します。
IronPDFは、複数の画像を簡単に1つのPDFに変換できます。 複数の画像を単一のドキュメントに追加するためのコードは次のとおりです:
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.List;
// Reference to the directory containing the images that we desire to convert
List<Path> images = new ArrayList<>();
images.add(Paths.get("imageA.png"));
images.add(Paths.get("imageB.png"));
images.add(Paths.get("imageC.png"));
images.add(Paths.get("imageD.png"));
images.add(Paths.get("imageE.png"));
// Render all targeted images as PDF content and save them together in one document.
PdfDocument merged = PdfDocument.fromImage(images);
merged.saveAs("output.pdf");
import java.io.File;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
// Reference to the directory containing the images that we desire to convert
Path imageDirectory = Paths.get("assets/images");
// Create an empty list to contain Paths to images from the directory.
List<Path> imageFiles = new ArrayList<>();
PDDocument doc = new PDDocument();
// Use a DirectoryStream to populate the list with paths for each image in the directory that we want to convert
try (DirectoryStream<Path> stream = Files.newDirectoryStream(imageDirectory, "*.{png,jpg}")) {
for (Path entry : stream) {
imageFiles.add(entry);
}
for (int i = 0; i < imageFiles.size(); i++){
//Add a Page
PDPage blankPage = new PDPage();
doc.addPage(blankPage);
PDPage page = doc.getPage(i);
//Creating PDImageXObject object
PDImageXObject pdImage = PDImageXObject.createFromFile(imageFiles.get(i).toString(),doc);
//creating the PDPageContentStream object
PDPageContentStream contents = new PDPageContentStream(doc, page);
//Drawing the image in the document
contents.drawImage(pdImage, 0, 0);
System.out.println("Image inserted");
//Closing the PDPageContentStream object
contents.close();
}
//Saving the document
doc.save("C:/PdfBox_Examples/sample.pdf");
//Closing the document
doc.close();
} catch (IOException exception) {
throw new RuntimeException(String.format("Error converting images to PDF from directory: %s: %s",
imageDirectory,
exception.getMessage()),
exception);
}
IronPDFでパスワードを使用してPDFを暗号化するためのコードは以下の通りです:
// Open a document(or create a new one from HTML)
PdfDocument pdf = PdfDocument.fromFile(Paths.get("assets/composite.pdf"));
// Edit security settings
SecurityOptions securityOptions = new SecurityOptions();
securityOptions.setOwnerPassword("top-secret");
securityOptions.setUserPassword("sharable");
// Change or set the document encryption password
SecurityManager securityManager = pdf.getSecurity();
securityManager.setSecurityOptions(securityOptions);
pdf.saveAs(Paths.get("assets/secured.pdf"));
Apache PDFBoxは、ファイルをより安全にするためにドキュメントの暗号化も提供します。 メタデータのような追加情報も追加できます。 コードは次のようになります:
//Loading an existing document
File file = new File("C:/PdfBox_Examples/sample.pdf");
PDDocument document = PDDocument.load(file);
//Creating access permission object
AccessPermission ap = new AccessPermission();
//Creating StandardProtectionPolicy object
StandardProtectionPolicy spp = new StandardProtectionPolicy("1234", "1234", ap);
//Setting the length of the encryption key
spp.setEncryptionKeyLength(128);
//Setting the access permissions
spp.setPermissions(ap);
//Protecting the document
document.protect(spp);
System.out.println("Document encrypted");
//Saving the document
document.save("C:/PdfBox_Examples/encrypted.pdf");
//Closing the document
document.close();
IronPDF は、シンプルなPDFアプリケーションの開発に無料で使用でき、商業利用のためにいつでもライセンスを取得できます。IronPDF は、シングルプロジェクトライセンス、シングルデベロッパーライセンス、エージェンシーや多国籍企業向けのライセンス、SaaSおよびOEM再配布ライセンスとサポートを提供しています。 すべてのライセンスは、無料トライアル、30日間の返金保証、および1年間のソフトウェアサポートとアップグレードが利用可能です。
Lite パッケージは $749 で利用可能です。 IronPDF製品には一切の定期料金はかかりません。 ソフトウェアライセンスの詳細情報は、製品のIronPDFライセンスページで確認できます。
Apache PDFBoxは無料で利用できます。 個人用、社内用、商用など、どのように使用されるかにかかわらず、無料です。
現在のバージョンのApache License 2.0は、Apache License 2.0テキストから含めることができます。 ライセンスのコピーを含めるには、作品に含めるだけです。 また、ソースコードの先頭にコメントとして以下の通知を添付することもできます。
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
比較すると、IronPDFは機能性と製品サポートの両面でApache PDFBoxに優れています。 また、最新のソフトウェア開発で必要とされるSaaSやOEMのサポートも提供します。 ただし、このライブラリはApache PDFBoxのように商業利用において無料ではありません。
大規模なソフトウェア・アプリケーションを持つ企業では、ソフトウェア開発中に発生した問題を解決するために、サードパーティ・ベンダーによる継続的なバグ修正やサポートが必要になる場合があります。 これは、Apache PDFBoxのような多くのオープンソースソリューションに欠けているもので、開発者のコミュニティからの自発的なサポートに依存して維持されています。 一言で言えば、IronPDFはビジネスや市場での使用に最適で、Apache PDFBoxは個人や非商用アプリケーションに適しています。
IronPDFの機能をテストするための無料体験版もあります。 試してみてください または IronPDFを購入。
今、Iron Software のすべての製品を Iron Suite で大幅に割引された価格で手に入れることができます。この素晴らしいお得な情報の詳細については、このIron Suite のウェブページをご覧ください。