文檔半島外圍網上直營>>E-iceblue中文文檔>>將 PDF 文件保存到數據流并從數據流中加載 PDF 文件
                將 PDF 文件保存到數據流并從數據流中加載 PDF 文件
Spire.PDF for .NET 是一款專門對 Word 文檔進行操作的 .NET 類庫。致力于在于幫助開發人員輕松快捷高效地創建、編輯、轉換和打印 Microsoft Word 文檔,而無需安裝 Microsoft Word。
行號用于在每行文本旁邊顯示 Word 自動計算的行數。當我們需要參考合同或法律文件等文檔中的特定行時,它非常有用。word中的行號功能允許我們設置起始值、編號間隔、與文本的距離以及行號的編號方式。使用 Spire.Doc,我們可以實現上述所有功能。本文將介紹如何將 HTML 轉換為 PDF。
歡迎加入spire技術交流群:767755948
作為一個與所有.NET開發平臺兼容的獨立組件,Spire.PDF for .NET使開發人員能夠創建、讀取、寫入、編輯和處理PDF文件,而無需任何外部PDF閱讀器或類似軟件。在本節中,我將向您介紹如何創建 PDF 文件并將其保存到數據流中,以及如何從數據流中加載 PDF 文件。
第一部分:創建 PDF 文件并將其保存為流
第 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 文件保存為流。 
FileStream to_strem = new FileStream("To_stream.pdf", FileMode.Open);
doc.SaveToStream(to_stream);
to_stream.Close();
doc.Close();
 第二部分從流中加載 PDF 文件第 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 文檔外,Spire.PDF 還能輕松地從文件和字節數組中加載 PDF 文檔。請參閱 Spire.PDF 程序指南,獲取更多有關在 C#、VB.NET 中處理 PDF 的信息。                
            
 QQ交談
QQ交談 在線咨詢
在線咨詢 
                 
                
 渝公網安備
            50010702500608號
渝公網安備
            50010702500608號
             
            
 客服熱線
客服熱線