JavaでGroupDocs Conversion Javaを使用してtxtをPDFに変換する

クイック回答

  • txt を pdf java 変換を処理するライブラリは何ですか? GroupDocs.Conversion Java.
  • インデントは保持できますか? はい – TxtLeadingSpacesOptions.ConvertToIndent を設定します。
  • 番号検出はサポートされていますか? setDetectNumberingWithWhitespaces(true) を有効にします。
  • ライセンスは必要ですか? テストには無料トライアルで動作しますが、本番環境ではフルライセンスが必要です。
  • 必要な Java バージョンは? JDK 11 以上。

txt to pdf java とは?

txt to pdf java は、プレーンテキスト(.txt)ドキュメントを Java コードで PDF ファイルに変換するプロセスです。GroupDocs.Conversion Java は低レベルの PDF 作成詳細を抽象化し、レイアウト、インデント、番号付けを保持するための高レベルオプションを提供します。

txt to pdf java に GroupDocs Conversion Java を使用する理由

GroupDocs Conversion Java は、典型的な 8 コアサーバーで 1 秒あたり最大 500 ページ を処理し、50 以上の入力・出力フォーマット をサポートし、手動の後処理なしで複雑なテキスト書式を保持できます。これにより、正確なビジュアル忠実度が求められる法的契約書、技術マニュアル、コードスニペットのバッチ処理に最適です。

前提条件

  • GroupDocs.Conversion for Java バージョン 25.2 以降。
  • JDK 11 以上が開発マシンにインストールされていること。
  • Maven 対応 IDE(例:IntelliJ IDEA または Eclipse)。
  • Java プロジェクト構造と Maven 依存関係の基本的な知識。

GroupDocs.Conversion for Java の設定

Maven 設定

Add the GroupDocs repository and dependency to your pom.xml:

<repositories>
   <repository>
      <id>repository.groupdocs.com</id>
      <name>GroupDocs Repository</name>
      <url>https://releases.groupdocs.com/conversion/java/</url>
   </repository>
</repositories>

<dependencies>
   <dependency>
      <groupId>com.groupdocs</groupId>
      <artifactId>groupdocs-conversion</artifactId>
      <version>25.2</version>
   </dependency>
</dependencies>

ライセンス取得

GroupDocs.Conversion offers a free trial, temporary licenses for extended testing, and full‑purchase options. Visit the purchase page to explore your options.

基本的な初期化

After the Maven step, you can initialize the conversion engine:

import com.groupdocs.conversion.Converter;

public class InitializeConversion {
    public static void main(String[] args) {
        // Path to the input TXT document.
        String inputFile = "SampleText.txt";

        // Create a converter instance using the text file.
        Converter converter = new Converter(inputFile);

        System.out.println("Initialization complete!");
    }
}

実装ガイド

We’ll walk through each feature step‑by‑step, explaining the purpose of every option before the code appears.

機能 1: 高度なオプションで txt を PDF に変換する

This feature shows how to convert a text file to PDF while controlling formatting behaviors such as leading spaces and numbering detection.

TXT 変換のロードオプション設定

TxtLoadOptions defines how the source text is interpreted before conversion.

The TxtLoadOptions class lets you specify whitespace handling, line‑break behavior, and numbering detection.

import com.groupdocs.conversion.options.load.TxtLoadOptions;
import com.groupdocs.conversion.options.load.TxtLeadingSpacesOptions;

// Create TxtLoadOptions instance.
TxtLoadOptions loadOptions = new TxtLoadOptions();
loadOptions.setLeadingSpacesOptions(TxtLeadingSpacesOptions.ConvertToIndent); // Convert leading spaces to indents.
loadOptions.setDetectNumberingWithWhitespaces(true); // Detect numbering with whitespaces.

コンバータの初期化

The Converter class is the core engine that accepts a source document and a set of load options, then produces the desired output format.

import com.groupdocs.conversion.Converter;

// Create converter instance with specific options.
Converter converter = new Converter(inputFile, () -> loadOptions);

PDF 変換オプションの設定

PdfConvertOptions controls PDF‑specific settings like page size, compression level, and font embedding.

The PdfConvertOptions class ensures the generated PDF matches your quality and size requirements.

import com.groupdocs.conversion.options.convert.PdfConvertOptions;

// Prepare PDF conversion options.
PdfConvertOptions convertOptions = new PdfConvertOptions();

変換の実行

Calling convert on the Converter instance writes the PDF to the target path you specify.

// Convert TXT to PDF with advanced settings applied.
converter.convert(outputFile, convertOptions);
System.out.println("Conversion complete. Check YOUR_OUTPUT_DIRECTORY for the result.");

機能 2: 高度なテキスト処理のためのロードオプション設定

This feature demonstrates how to fine‑tune the load options for text files that need precise formatting control.

TxtLoadOptions の設定

TxtLeadingSpacesOptions determines how leading spaces are treated during conversion.

Setting TxtLeadingSpacesOptions.ConvertToIndent tells the engine to transform leading spaces into visual indents in the PDF.

TxtLoadOptions txtLoadOptions = new TxtLoadOptions();
txtLoadOptions.setLeadingSpacesOptions(TxtLeadingSpacesOptions.ConvertToIndent);
txtLoadOptions.setDetectNumberingWithWhitespaces(true);

// These options ensure that the PDF conversion respects the original formatting nuances.

実用的な応用例

  1. 法的文書 – 条項の番号付けとインデントを作成時と同じ正確さで保持します。
  2. 技術マニュアル – 手動で再フォーマットすることなく、階層化リストやコードブロックを保持します。
  3. ソースコードのドキュメント – 元のインデントを保持し、レビュー担当者が読みやすい PDF にします。

These scenarios illustrate how groupdocs conversion java can be embedded into content‑management pipelines, automated report generators, or document‑archival services.

パフォーマンス上の考慮点

  • メモリ使用量: 200 MB を超えるファイルの場合、JVM ヒープ (-Xmx2g 以上) を増やして OutOfMemoryError を防止します。
  • バッチ処理: 複数ファイルで単一の Converter インスタンスを再利用し、初期化オーバーヘッドを削減します。
  • バージョン更新: 最新の GroupDocs.Conversion にアップグレードすると、通常 10‑15 % の速度向上 と追加のフォーマットサポートが得られます。

よくある問題と解決策

問題発生原因簡単な対処法
インデントが失われるTxtLeadingSpacesOptions が設定されていないsetLeadingSpacesOptions(TxtLeadingSpacesOptions.ConvertToIndent) を呼び出す
番号付きリストがプレーンテキストとして表示されるsetDetectNumberingWithWhitespaces が false になっているtrue に設定して有効化する
出力 PDF が空白になるoutputFile パスが間違っている、または書き込み権限がないパスを確認し、書き込み権限があることを確認する
大きなファイルで OutOfMemoryError が発生するデフォルトの JVM ヒープが小さすぎるヒープサイズを増やす(-Xmx2g 以上)

よくある質問

Q: Can I convert multiple TXT files at once?
A: Yes, GroupDocs.Conversion supports batch processing, allowing you to convert a collection of text files in a single loop.

Q: What if my converted PDF doesn’t look as expected?
A: Double‑check the load options for leading spaces and numbering detection; adjusting these settings resolves most formatting discrepancies.

Q: Is there a limit to the size of TXT files I can convert?
A: GroupDocs.Conversion can handle very large files, but performance depends on available memory. For files exceeding 500 MB, consider splitting them or increasing the JVM heap.

リソース

Explore these resources to deepen your understanding and make the most of groupdocs conversion java in your projects. Happy coding!


最終更新日: 2026-08-19
テスト対象: GroupDocs.Conversion 25.2 for Java
作者: GroupDocs

関連チュートリアル