フッターコンテンツにスキップ
JAVA用IRONPDFの使用

Javaでバイト配列をPDFに変換する方法

This article will discuss how to convert a byte array to a PDF in Java and use the IronPDF Java PDF library.

IronPDF: Java PDF Library

IronPDF Java PDF Library is a popular Java library for generating, reading, and manipulating PDF documents. It is developed by Iron Software, a software development company that specializes in developing productivity libraries and tools.

IronPDF provides a range of features for creating and manipulating PDFs. This includes the ability to:

  1. Convert HTML content to PDF and plain text documents to PDF, to add text and images to PDFs,
  2. Create and fill PDF forms with ease,
  3. Merge multiple PDFs and split PDFs into separate documents,
  4. and more.

The library also includes support for encryption to protect your PDFs and digital signatures for document security.

IronPDF uses a simple and intuitive API that makes it easy for developers of all levels to use. Additionally, IronPDF offers comprehensive documentation, code samples to get you started, and support to help developers get started with the library quickly.

Let's explore how to use IronPDF to convert a byte array to a PDF file.

Install IronPDF Java library in Maven Project

To install the IronPDF Java library in a Maven project, follow these steps:

  1. Open your project's pom.xml file and locate the <dependencies> tag.
  2. Add the following XML code to the <dependencies> tag to add IronPDF for Java as a dependency:

    <!-- No actual dependency provided, placeholder for illustration -->
    <!-- No actual dependency provided, placeholder for illustration -->
    XML
  3. Add the following XML code to the <dependencies> tag to add the SLF4J Simple logger, which IronPDF for Java uses to log status messages to the console.

    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-simple</artifactId>
      <version>2.0.3</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-simple</artifactId>
      <version>2.0.3</version>
    </dependency>
    XML
  4. Save the pom.xml file.

After you save the pom.xml file, Maven will automatically download and install the IronPDF for Java library and the SLF4J Simple logger. You can then use the IronPDF library in your Java code.

Note that the version number used in the XML code above may not be the latest version available when you install the library. You should always use the latest library version for bug fixes and new features.

Sample Code

Here is the sample code to convert the byte array to a PDF file:

package IronPDF.ironpdf_java;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

import com.ironsoftware.ironpdf.*;

public class Test {
    public static void main(String[] args) throws IOException {
        // Define the byte array representing the ASCII characters
        byte[] byteArray = {84, 104, 105, 115, 32, 105, 115, 32, 98, 121, 116, 101, 32, 97, 114, 114, 97, 121};

        // Convert the byte array to a string using US_ASCII encoding
        String data = new String(byteArray, StandardCharsets.US_ASCII);

        // Embed the string data in HTML format
        String HTML = "<h1>" + data + "</h1>";

        // Apply your license key for IronPDF
        License.setLicenseKey("YOUR-LICENSE-KEY");

        // Render the HTML as a PDF document using IronPDF
        PdfDocument pdf = PdfDocument.renderHtmlAsPdf(HTML);

        // Save the PDF file to a specified location
        pdf.saveAs("C:\\byteToPdf.pdf");
    }
}
package IronPDF.ironpdf_java;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

import com.ironsoftware.ironpdf.*;

public class Test {
    public static void main(String[] args) throws IOException {
        // Define the byte array representing the ASCII characters
        byte[] byteArray = {84, 104, 105, 115, 32, 105, 115, 32, 98, 121, 116, 101, 32, 97, 114, 114, 97, 121};

        // Convert the byte array to a string using US_ASCII encoding
        String data = new String(byteArray, StandardCharsets.US_ASCII);

        // Embed the string data in HTML format
        String HTML = "<h1>" + data + "</h1>";

        // Apply your license key for IronPDF
        License.setLicenseKey("YOUR-LICENSE-KEY");

        // Render the HTML as a PDF document using IronPDF
        PdfDocument pdf = PdfDocument.renderHtmlAsPdf(HTML);

        // Save the PDF file to a specified location
        pdf.saveAs("C:\\byteToPdf.pdf");
    }
}
JAVA

Explanation of the Code

Step 1: The provided code demonstrates converting a byte array to a PDF using IronPDF. At the beginning of the code, the necessary Java libraries are imported. This includes the IronPDF library and the StandardCharsets library.

import java.io.IOException;
import java.nio.charset.StandardCharsets;

import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

import com.ironsoftware.ironpdf.*;
JAVA

Step 2: The first step is to create a byte array containing the data to be converted. In this case, the byte array contains the string "This is byte array" in ASCII format.

byte[] byteArray = {84, 104, 105, 115, 32, 105, 115, 32, 98, 121, 116, 101, 32, 97, 114, 114, 97, 121};
byte[] byteArray = {84, 104, 105, 115, 32, 105, 115, 32, 98, 121, 116, 101, 32, 97, 114, 114, 97, 121};
JAVA

Step 3: Next, the byte array is converted to a string using the US_ASCII character encoding. This is done by calling the String constructor and passing the byte array and the appropriate character encoding as parameters.

String data = new String(byteArray, StandardCharsets.US_ASCII);
String data = new String(byteArray, StandardCharsets.US_ASCII);
JAVA

Step 4: An HTML string is then created using the data from the byte array. The string is wrapped in an H1 tag to make it stand out in the PDF.

String HTML = "<h1>" + data + "</h1>";
String HTML = "<h1>" + data + "</h1>";
JAVA

Step 5: After creating the HTML string, the IronPDF library renders the HTML as a PDF. The PDF is then saved to a file on the local disk with the given filename.

// Apply your license key
License.setLicenseKey("YOUR-LICENSE-KEY");
PdfDocument pdf = PdfDocument.renderHtmlAsPdf(HTML);
pdf.saveAs("C:\\byteToPdf.pdf");
// Apply your license key
License.setLicenseKey("YOUR-LICENSE-KEY");
PdfDocument pdf = PdfDocument.renderHtmlAsPdf(HTML);
pdf.saveAs("C:\\byteToPdf.pdf");
JAVA

Output File

Here is the output PDF document generated by IronPDF Java PDF Library:

How to Convert Byte Array to PDF in Java, Figure 1: Output PDF generated by IronPDF Java Output PDF generated by IronPDF Java

結論

By following the steps outlined in this article, you can convert a byte array to a PDF file and save it on your local disk.

IronPDF is a commercial library and requires a license purchase for production environments. The cost for an IronPDF license starts at $799 per developer per year; there are other license options available depending on the number of developers and deployment servers.

However, IronPDF offers a free trial of IronPDF that allows developers to test the library and evaluate its features and capabilities. This free trial includes full access to all features of the library. Developers can use it to convert up to 20 pages of a PDF document.

よくある質問

Javaでバイト配列をPDFに変換するにはどうすればよいですか?

IronPDF Javaライブラリを使用してバイト配列をPDFに変換するには、まずバイト配列をASCII文字に変換し、それらをHTMLマークアップに埋め込みます。その後、renderHtmlAsPdfメソッドを使用してHTMLをPDFドキュメントに変換します。

MavenプロジェクトでPDFライブラリをインストールする手順は何ですか?

MavenプロジェクトでIronPDFをインストールするには、pom.xmlファイルの<dependencies>セクションにIronPDF依存性を追加します。加えて、SLF4J Simpleロガーの依存性を含めます。ファイルを保存すると、Mavenが自動的にライブラリをダウンロードし、インストールします。

Java PDFライブラリには無料試用版がありますか?

はい、IronPDFは無料試用版を提供しており、最大20ページのPDFドキュメントの変換などの機能を購入前に試すことができます。

HTMLコンテンツはJavaライブラリを使用してPDFに変換できますか?

はい、IronPDFはHTMLコンテンツをPDFドキュメントに変換する機能を提供しています。また、テキストや画像の追加、フォームの作成と入力などの機能も提供しています。

IronPDF for Javaでライセンスキーを適用する方法は?

ライセンスキーはJavaコードでLicense.setLicenseKeyメソッドを使用して、IronPDFを本番使用するために承認します。

IronPDF for Javaの主要な機能は何ですか?

IronPDFは、HTMLからPDFへの変換、フォームの作成と入力、PDFのマージと分割、セキュリティのための暗号化、デジタル署名など、わかりやすいAPIを通じて提供しています。

Java PDFライブラリにおけるロガーの役割は何ですか?

IronPDF for Javaでは、SLF4J Simpleロガーがコンソールにステータスメッセージを記録し、開発者がアプリケーションをデバッグおよび監視するのを助けます。

IronPDFのドキュメントとコードサンプルはどこで見つけることができますか?

IronPDFのドキュメントとコードサンプルは、IronPDFウェブサイトで利用可能で、開発者が効率的に始めるための包括的なガイダンスと例を提供しています。

IronPDF for Javaのライセンスオプションは何ですか?

IronPDFは、開発者とデプロイメントサーバーの数に応じたさまざまなライセンスオプションを提供する商用ライブラリです。ライセンスは、開発者1人あたり年間特定の料金から始まります。

Darrius Serrant
フルスタックソフトウェアエンジニア(WebOps)

Darrius Serrantは、マイアミ大学でコンピュータサイエンスの学士号を取得し、Iron SoftwareでフルスタックWebOpsマーケティングエンジニアとして働いています。若い頃からコーディングに惹かれ、コンピューティングを神秘的かつアクセス可能なものとし、創造性と問題解決のための完璧な媒体と考えていました。

Iron Softwareでは、新しいものを創造することと、複雑なコンセプトをより理解しやすくすることを楽しんでいます。Resident Developerの一人として、次世代に専門知識を共有するために、学生を教えることにも志願しました。

Darriusにとって、その仕事は価値があり、実際の影響があるため、満足感があります。