將複雜的 3D 模型轉換為單一、可共享的 PDF 是工程團隊常見的需求,因為他們需要將視覺資料嵌入報告或文件中。Aspose.3D Cloud SDK for .NET 提供了強大的函式庫,使在 C# 中將 3D 轉換為 PDF 變得簡單且可靠。在本指南中,您將學習如何設置 SDK、編寫轉換程式碼、使用等效的 cURL 命令,並微調轉換設定以獲得最佳效能。完成後,您將擁有一個可整合至任何 .NET 應用程式的完整功能解決方案。

開始之前:先決條件與安裝

  • Operating System: Windows、Linux 或 macOS,搭配 .NET 6.0+ 執行環境。
  • IDE: Visual Studio 2022 或任何支援 .NET 開發的編輯器。
  • Aspose Cloud Account: 您需要從 Aspose Cloud 儀表板取得 ClientIdClientSecret
  • NuGet Package: 透過指令列安裝 SDK。
dotnet add package Aspose.3D-Cloud

從官方發行頁面下載最新的二進位檔和文件: Aspose.3D Cloud SDK for .NET 下載。安裝套件後,將以下配置片段添加到您的專案中,以驗證 Aspose Cloud。

var config = new Configuration
{
    ClientId = "YOUR_CLIENT_ID",
    ClientSecret = "YOUR_CLIENT_SECRET"
};

在配置就緒後,您已準備好開始轉換過程。

C# 中的 3D 轉 PDF 轉換:逐步說明

步驟 1:載入來源文件

首先,在磁碟上找到 3D 模型檔案(OBJSTLFBX,等等),然後建立一個 FileInfo 物件。

string inputFilePath = Path.Combine(Environment.CurrentDirectory, "model.obj");
var inputFile = new FileInfo(inputFilePath);

步驟 2:定義輸出參數

將目標格式設定為 pdf,並可選擇指定儲存選項。SDK 將返回包含 PDF 資料的 Stream

string targetFormat = "pdf";   // Output format
string outPath = null;         // Let the API return the file stream
string storage = null;         // Default storage
string folder = null;          // Root folder

步驟 3:呼叫轉換 API

ThreeDApi 客戶端上呼叫 PostConvert3D。此呼叫執行實際的轉換。

Stream pdfStream = threeDApi.PostConvert3D(
    file: inputFile,
    format: targetFormat,
    outPath: outPath,
    storage: storage,
    folder: folder
);

如需了解此方法的更多詳細資訊,請參閱 API 參考

步驟 4:將 PDF 流儲存至磁碟

將返回的串流寫入本機上的檔案。

string outputFilePath = Path.Combine(Environment.CurrentDirectory, "model.pdf");
using (var fileStream = new FileStream(outputFilePath, FileMode.Create, FileAccess.Write))
{
    pdfStream.CopyTo(fileStream);
}

第5步:優雅地處理錯誤

將轉換邏輯包裝在 try‑catch 區塊中,以捕獲 API 和執行時例外。

try
{
    // Conversion code here
}
catch (ApiException apiEx)
{
    Console.WriteLine($"API error: {apiEx.ErrorCode} - {apiEx.Message}");
}
catch (Exception ex)
{
    Console.WriteLine($"Unexpected error: {ex.Message}");
}

透過這些步驟,您已完成完整的 3D 轉 PDF 轉換(C#) 工作流程。

完整可運作的 3D 轉 PDF 轉換 C# 範例

以下程式碼展示了上述完整的實作。

using System;
using System.IO;
using Aspose.ThreeD.Cloud.Sdk.Api;
using Aspose.ThreeD.Cloud.Sdk.Client;
using Aspose.ThreeD.Cloud.Sdk.Model;

namespace ThreeDToPdfDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            // Configuration – replace with your actual Aspose Cloud credentials
            var config = new Configuration
            {
                ClientId = "YOUR_CLIENT_ID",
                ClientSecret = "YOUR_CLIENT_SECRET"
            };

// Initialize the ThreeD API client
            var threeDApi = new ThreeDApi(config);

// Input 3D model file (OBJ, STL, FBX, etc.)
            string inputFilePath = Path.Combine(Environment.CurrentDirectory, "model.obj");
            var inputFile = new FileInfo(inputFilePath);

// Desired output PDF file
            string outputFilePath = Path.Combine(Environment.CurrentDirectory, "model.pdf");

// Conversion parameters
            string targetFormat = "pdf";          // Output format
            string outPath = null;                // Let the API return the file stream
            string storage = null;                // Default storage
            string folder = null;                 // Root folder

try
            {
                // Perform conversion – the API returns a Stream containing the PDF data
                Stream pdfStream = threeDApi.PostConvert3D(
                    file: inputFile,
                    format: targetFormat,
                    outPath: outPath,
                    storage: storage,
                    folder: folder
                );

// Write the resulting PDF stream to disk
                using (var fileStream = new FileStream(outputFilePath, FileMode.Create, FileAccess.Write))
                {
                    pdfStream.CopyTo(fileStream);
                }

Console.WriteLine($"Conversion successful. PDF saved to: {outputFilePath}");
            }
            catch (ApiException apiEx)
            {
                Console.WriteLine($"API error: {apiEx.ErrorCode} - {apiEx.Message}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Unexpected error: {ex.Message}");
            }
        }
    }
}

注意: 此程式碼範例展示了核心功能。在將其用於您的專案之前,請確保更新所有檔案路徑和設定值以符合實際環境,驗證所有必要的相依項目已正確安裝,並在開發環境中徹底測試。如果遇到任何問題,請參閱官方文件或聯繫支援團隊尋求協助。

使用 cURL 和 REST API 進行 3D 轉 PDF 轉換

如果您偏好語言無關的方法,您可以使用原始 HTTP 呼叫執行相同的轉換。

  1. 取得存取權杖
curl -X POST "https://api.aspose.cloud/connect/token" \
     -H "Content-Type: application/x-www-form-urlencoded" \
     -d "grant_type=client_credentials&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET"
  1. 上傳來源 3D 檔案
   curl -X PUT "https://api.aspose.cloud/v3.0/3d/storage/file/model.obj" \
        -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
        -T "model.obj"
  1. 請求轉換
curl -X POST "https://api.aspose.cloud/v3.0/3d/convert?format=pdf&outPath=model.pdf" \
     -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
     -F "file=@model.obj"
  1. 下載生成的 PDF
   curl -X GET "https://api.aspose.cloud/v3.0/3d/storage/file/model.pdf" \
        -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
        -o "model.pdf"

這些指令鏡像 SDK 工作流程,對整合測試或自動化腳本很有用。欲了解更多細節,請參閱官方 API 文件

微調轉換設定

SDK 公开了多個參數,讓您能夠控制轉換過程:

  • targetFormat - 決定輸出格式。將其設定為 "pdf" 會觸發 3D 轉 PDF 的轉換。
  • outPath - 若保持 null,API 會回傳串流;提供路徑則讓服務直接將檔案儲存至雲端儲存。
  • storage - 若有多個容器,可選擇特定的雲端儲存位置。
  • folder - 在儲存空間中定義子資料夾以組織檔案。

調整這些選項可以提升 .NET 中的 3D 轉 PDF 轉換效能,尤其是在處理大型模型或批次操作時。

結論

您現在已掌握 C# 中的 3D 轉 PDF 轉換,使用 Aspose.3D Cloud SDK for .NET。本指南涵蓋了從環境設置、程式碼實作到基於 REST 的 cURL 命令以及針對速度與可靠性的微調選項。請記得取得正式授權以供生產環境使用;產品頁面上提供了價格資訊,且可從 臨時授權頁面 獲得臨時授權。將此工作流程整合至您的應用程式,以自動產生展示 3D 資產的高品質 PDF 文件。

常見問題

如何在 C# 中使用 Aspose 執行 3D 轉 PDF 轉換?
使用 ThreeDApi 類別的 PostConvert3D 方法。提供來源檔案,將 format 設為 "pdf",並處理返回的 Stream 以儲存 PDF。

我可以在批次處理中自動化 3D 轉 PDF 的轉換嗎?
是的。遍歷檔案路徑集合,對每個模型呼叫相同的轉換程式碼。SDK 的無狀態 API 使其易於整合到背景服務或 CI 管道中。

在處理大型 3D 模型的 C# 檔案時,有哪些重要考量?
建議使用串流方式讀寫輸入與輸出檔案,而不是將整個模型載入記憶體。範例使用 FileInfo 取得來源,並直接將 PDF 串流寫入磁碟,以降低記憶體壓力。

我可以在哪裡找到 Aspose.3D Cloud SDK for .NET 的授權資訊?
請造訪產品頁面以取得價格細節,並從臨時授權頁面取得試用或臨時授權。

Read More