3Dモデルを汎用的に閲覧可能なドキュメントに変換することは、エンジニアリングや可視化ツールで頻繁に求められる要件です。Conholdate.Total for Java は、Javaでの STL から PDF への変換をシンプルに行える堅牢な SDK を提供します。このチュートリアルでは、ライブラリのセットアップ方法、完全なコード例の解説、PDF オプションの調整、そして信頼性の高い結果を得るためのベストプラクティスを学びます。
JavaでのSTLからPDFへの変換手順
- STL入力ファイルの検証: ソースのSTLファイルが存在し、読み取り可能であることを確認します。
String inputPath = "sample.stl";
if (!Files.isReadable(Paths.get(inputPath))) {
System.err.println("Input STL file not found or not readable: " + inputPath);
return;
}
- コンバータを初期化する: STL ファイルパスで
Converterインスタンスを作成します。
try (Converter converter = new Converter(inputPath)) {
// conversion logic follows
}
- (オプション)STLロードオプションの適用: カスタムロード動作が必要な場合は、ロードオプションの行のコメントを解除してください。
// LoadOptions loadOptions = new StlLoadOptions();
// converter.setLoadOptions(loadOptions);
- PDF変換オプションの構成: ページサイズ、向き、品質、その他のPDF設定を設定します。
PdfConvertOptions pdfOptions = new PdfConvertOptions();
pdfOptions.setPageSize(PdfPageSize.A4);
pdfOptions.setPageOrientation(PdfPageOrientation.PORTRAIT);
pdfOptions.setQuality(100);
pdfOptions.setEmbedFonts(true);
pdfOptions.setOptimizeForPrint(true);
pdfOptions.setCompressImages(true);
- 変換を実行して検証:
convertを呼び出し、生成された PDF が空でないことを確認します。
converter.convert("sample.pdf", pdfOptions);
File pdfFile = new File("sample.pdf");
if (pdfFile.exists() && pdfFile.length() > 0) {
System.out.println("Generated PDF is valid. Size: " + pdfFile.length() + " bytes.");
}
STL to PDF Java - 完全なコード例
以下の例は、Conholdate.Total for Java を使用した STL から PDF への変換の全体的なワークフローを示しています。
import com.groupdocs.conversion.Converter;
import com.groupdocs.conversion.enums.PdfPageOrientation;
import com.groupdocs.conversion.enums.PdfPageSize;
import com.groupdocs.conversion.options.convert.PdfConvertOptions;
import com.groupdocs.conversion.options.load.LoadOptions;
import com.groupdocs.conversion.options.load.StlLoadOptions;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
public class StlToPdfDemo {
public static void main(String[] args) {
// Input STL file and desired PDF output file
String inputPath = "sample.stl";
String outputPath = "sample.pdf";
// Verify that the input file exists and is readable
if (!Files.isReadable(Paths.get(inputPath))) {
System.err.println("Input STL file not found or not readable: " + inputPath);
return;
}
// Perform conversion inside a try‑with‑resources block to guarantee cleanup
try (Converter converter = new Converter(inputPath)) {
// If STL‑specific loading options are required, uncomment the following lines:
// LoadOptions loadOptions = new StlLoadOptions();
// converter.setLoadOptions(loadOptions);
// Configure PDF conversion options
PdfConvertOptions pdfOptions = new PdfConvertOptions();
pdfOptions.setPageSize(PdfPageSize.A4);
pdfOptions.setPageOrientation(PdfPageOrientation.PORTRAIT);
pdfOptions.setQuality(100); // Highest visual quality
pdfOptions.setEmbedFonts(true); // Ensure fonts are embedded
pdfOptions.setOptimizeForPrint(true); // Optimize PDF for printing
pdfOptions.setCompressImages(true); // Reduce PDF size without quality loss
// Execute the conversion
converter.convert(outputPath, pdfOptions);
System.out.println("STL to PDF conversion completed successfully: " + outputPath);
} catch (Exception ex) {
System.err.println("An error occurred during conversion:");
ex.printStackTrace();
return;
}
// Simple validation of the generated PDF
File pdfFile = new File(outputPath);
if (pdfFile.exists() && pdfFile.length() > 0) {
System.out.println("Generated PDF is valid. Size: " + pdfFile.length() + " bytes.");
} else {
System.err.println("PDF generation failed or resulted an empty file.");
}
}
}
Note: このコード例はコア機能を示しています。プロジェクトで使用する前に、ファイルパスや設定値が実際の環境に合わせて更新されていることを確認し、すべての必須依存関係が正しくインストールされていることを検証し、開発環境で十分にテストしてください。問題が発生した場合は、公式ドキュメント を参照するか、サポートチーム にお問い合わせください。
前提条件とセットアップ
Conholdate Maven リポジトリと SDK の依存関係を pom.xml に追加します:
<repositories>
<repository>
<id>conholdate-repo</id>
<name>Conholdate Maven Repository</name>
<url>https://repository.conholdate.com/repo/</url>
</repository>
</repositories>
<dependency>
<groupId>com.conholdate</groupId>
<artifactId>conholdate-total</artifactId>
<version>24.9</version>
<type>pom</type>
</dependency>
最新の SDK パッケージは ダウンロードページ から入手してください。ライブラリは Java 8 以上が必要です。
PDF 出力オプションの構成
Conholdate.Total for Java は、調整可能な複数の PDF 設定を提供します:
- ページサイズ - 標準サイズ(例:
PdfPageSize.A4またはPdfPageSize.LETTER)から選択します。 - ページ向き -
PdfPageOrientation.PORTRAITまたはPdfPageOrientation.LANDSCAPEに設定します。 - 品質 -
setQuality(int)メソッドは 0 から 100 の値を受け付け、100 が最高の視覚忠実度を提供します。 - フォントの埋め込み -
setEmbedFonts(true)を有効にすると、すべてのフォントが出力 PDF に保持されます。 - 印刷用に最適化 -
setOptimizeForPrint(true)は印刷ワークフロー向けに PDF を調整します。 - 画像の圧縮 -
setCompressImages(true)は目立った品質低下なしにファイルサイズを削減します。
これらのオプションは上記のコードスニペットで示されており、特定のプロジェクト要件に合わせて組み合わせることができます。
結論
Conholdate.Total for Java は、Java における STL から PDF への変換をシンプルかつ強力にします。手順に従い、提供されたコード例を使用し、PDF オプションを調整することで、デスクトップまたはサーバー アプリケーション向けに 3D STL モデルから高品質な PDF を生成できます。製品を本番環境で使用する際は適切なライセンスを取得することを忘れないでください。価格は価格ページで確認でき、一時ライセンスページから一時ライセンスを取得できます。この SDK があれば、Java プロジェクトへの 3D モデル変換の統合は簡単です。
FAQ
Java で Conholdate.Total を使用して STL から PDF への変換はどう実行しますか?
Converterクラスを使用し、PdfConvertOptionsを設定して、完全なコード例に示されているようにconvert()を呼び出します。SDK は 3D モデルを PDF にレンダリングする重い処理を自動で行います。STL から PDF への変換時にサポートされている PDF ページ形式は何ですか?
SDK はPdfPageSizeで定義されているすべての標準 PDF ページサイズと、PdfPageOrientationを使用した縦向きと横向きの両方の向きをサポートします。生成されたPDFにカスタムフォントを埋め込むことは可能ですか?
はい。pdfOptions.setEmbedFonts(true)を設定して、STLレンダリングで使用されたフォントをすべて埋め込み、PDFがどのビューアでも同じように表示されるようにします。ライセンスおよびサポートに関する詳細情報はどこで確認できますか?
ライセンスの詳細は価格ページで確認でき、一時ライセンスページから一時ライセンスをリクエストできます。技術的なサポートが必要な場合は、サポートフォーラムをご利用ください。
