将 3D 模型转换为通用可查看的文档是工程和可视化工具的常见需求。 Conholdate.Total for Java 提供了强大的 SDK,使得在 Java 中将 STL 转换为 PDF 变得简单直观。在本教程中,您将设置库,完整演示代码示例,调整 PDF 选项,并学习可靠结果的最佳实践。

在 Java 中将 STL 转换为 PDF 的步骤

  1. 验证 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;
}
  1. 初始化转换器:使用 STL 文件路径创建 Converter 实例。
try (Converter converter = new Converter(inputPath)) {
    // conversion logic follows
}
  1. (可选)应用 STL 加载选项:如果需要自定义加载行为,请取消注释加载选项行。
// LoadOptions loadOptions = new StlLoadOptions();
// converter.setLoadOptions(loadOptions);
  1. 配置 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);
  1. 执行转换并验证: 调用 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 转 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.");
        }
    }
}

注意: 此代码示例演示了核心功能。在将其用于项目之前,请确保更新所有文件路径和配置值以匹配实际环境,验证已正确安装所有必需的依赖项,并在开发环境中进行彻底测试。如果遇到任何问题,请参阅官方文档或联系支持团队获取帮助。

前置条件和设置

将 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,将 3D 模型转换集成到您的 Java 项目中轻而易举。

FAQs

  • 如何在 Java 中使用 Conholdate.Total 执行 STL 到 PDF 的转换?
    使用 Converter 类,配置 PdfConvertOptions,并调用 convert(),如完整代码示例所示。SDK 负责将 3D 模型渲染为 PDF 的繁重工作。

  • 在 STL 转 PDF 转换过程中支持哪些 PDF 页面格式?
    SDK 支持 PdfPageSize 中定义的所有标准 PDF 页面尺寸,并通过 PdfPageOrientation 支持纵向和横向两种方向。

  • 是否可以在生成的 PDF 中嵌入自定义字体?
    是的。将 pdfOptions.setEmbedFonts(true) 设置为嵌入 STL 渲染中使用的任何字体,确保 PDF 在任何查看器上显示一致。

  • 在哪里可以找到有关许可和支持的更多信息?
    许可详情可在定价页面查看,临时许可证可在临时许可证页面申请。技术帮助请访问支持论坛。

阅读更多