通過C#創建 PDF 檔

本機和高性能 PDF 檔創建,無需使用 C#安裝 Adobe 雜技演員。

如何通過C#生成PDF檔

為了創建PDF檔,我們將使用[Aspose.PDF用於.NET](https://products.aspose.com/pdf/net)API,這是一個功能豐富,功能強大且易於使用的文檔操作API,適用於 net 平臺。打開 [NuGet](https://www.nuget.org/packages/aspose.pdf) 包管理器,搜索“.PDF”並安裝。您也可以從程式包管理器主控台使用以下命令。

Package Manager Console

PM > Install-Package Aspose.PDF

如何通過C#創建 PDF


<% create.feature-section-col1.content-01 %>

  1. 在類檔中包含命名空間
  2. 初始化文件類物件。
  3. 使用頁面添加() 方法。
  4. 建立新的文字碎片物件並設定其文字。
  5. 將文字碎片添加到頁面的「段落」集合中。
  6. 使用保存(字串)方法保存 PDF。

以下原始碼顯示了如何使用 C#創建 PDF 檔案。

<% create.code-block.subtitle %>


    // Initialize document object
    Document pdf_doc = new Document();

    // Add page
    Page page = pdf_doc.Pages.Add();

    // Place the text of choice
    page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment("Text of choice"));

    // PDF file created at a specified location
    pdf_doc.Save("created_one.pdf");