原創|行業資訊|編輯:胡濤|2024-05-09 11:27:32.097|閱讀 104 次
概述:在本節中,我將向您介紹如何創建 PDF 文件并將其保存到流中,以及如何從流中加載 PDF 文件。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
作為一個獨立組件,與所有 .NET 開發平臺兼容,Spire.PDF for .NET 使開發人員能夠創建、讀取、寫入、編輯和處理 PDF 文件,而無需任何外部 PDF 閱讀器或類似軟件。在本節中,我將向您介紹如何創建 PDF 文件并將其保存到流中,以及如何從流中加載 PDF 文件。
Spire.PDF for .NET 是一款獨立 PDF 控件,用于 .NET 程序中創建、編輯和操作 PDF 文檔。使用 Spire.PDF 類庫,開發人員可以新建一個 PDF 文檔或者對現有的 PDF 文檔進行處理,且無需安裝 Adobe Acrobat。
E-iceblue 功能類庫Spire 系列文檔處理組件均由中國本土團隊研發,不依賴第三方軟件,不受其他國家的技術或法律法規限制,同時適配國產操作系統如中科方德、中標麒麟等,兼容國產文檔處理軟件 WPS(如 .wps/.et/.dps 等格式
Spire.PDF for.net下載 Spire.PDF for java下載
第 1 步:新建一個 PDF 實例。
PdfDocument doc = new PdfDocument();
第 2 步:創建一頁。
PdfPageBase page = doc.Pages.Add();
第 3 步:向該頁面添加文本。
page.Canvas.DrawString("Hello, World!", new PdfFont(PdfFontFamily.Helvetica, 30f), new PdfSolidBrush(Color.Black), 10, 10);
第 4 步:將 PDF 文件保存到 Stream。
FileStream to_strem = new FileStream("To_stream.pdf", FileMode.Open); doc.SaveToStream(to_stream); to_stream.Close(); doc.Close();
第 1 步:新建一個 PDF 實例。
PdfDocument doc = new PdfDocument();
第 2 步:從流加載 PDF 文件。
FileStream from_stream = File.OpenRead("sample.pdf"); doc.LoadFromStream(from_stream);
步驟 3:保存 PDF 文檔。
doc.SaveToFile("From_stream.pdf",FileFormat.PDF); System.Diagnostics.Process.Start("From_stream.pdf");
完整代碼:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Spire.Pdf; using System.IO; using Spire.Pdf.Graphics; using System.Drawing; namespace PdfAndStream { class Program { static void Main(string[] args) { //A: create PDF file and save it to stream //create a pdf document. PdfDocument doc = new PdfDocument(); // create one page PdfPageBase page = doc.Pages.Add(); //draw the text page.Canvas.DrawString("Hello, World!", new PdfFont(PdfFontFamily.Helvetica, 30f), new PdfSolidBrush(Color.Black), 10, 10); //save pdf file to Stream FileStream to_stream = new FileStream("To_stream.pdf", FileMode.Open); doc.SaveToStream(to_stream); to_stream.Close(); doc.Close(); System.Diagnostics.Process.Start("To_stream.pdf"); //B: Load PDF file from Stream //create a pdf document. PdfDocument docFrom = new PdfDocument(); //load PDF file from stream FileStream from_stream = File.OpenRead("sample.pdf"); docFrom.LoadFromStream(from_stream); //save the pdf document docFrom.SaveToFile("From_stream.pdf",FileFormat.PDF); System.Diagnostics.Process.Start("From_stream.pdf"); } } }
以上便是如何將 PDF 文件保存到 Stream 并加載PDF文件,如果您有其他問題也可以繼續瀏覽本系列文章,獲取相關教程,你還可以給我留言或者加入我們的官方技術交流群。
歡迎下載|體驗更多E-iceblue產品
獲取更多信息請咨詢 ;技術交流Q群(767755948)
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn