您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關C#/VB.NET如何創建、讀取PDF文檔,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
using Spire.Pdf; using Spire.Pdf.Graphics; using System.Drawing; namespace CreatePDF_PDF { class Program { static void Main(string[] args) { //初始化一個PdfDocument類實例 PdfDocument document = new PdfDocument(); //聲明 PdfUnitConvertor和PdfMargins類對象 PdfUnitConvertor unitCvtr = new PdfUnitConvertor(); PdfMargins margins = new PdfMargins(); //設置頁邊距 margins.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margins.Bottom = margins.Top; margins.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margins.Right = margins.Left; //新添加一個A4大小的頁面 PdfPageBase page = document.Pages.Add(PdfPageSize.A4, margins); //自定義PdfTrueTypeFont、PdfPen實例,設置字體類型、字號和字體顏色 PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("楷體", 11f),true); PdfPen pen = new PdfPen(Color.Black); //調用DrawString()方法在指定位置寫入文本 string text = ("《蝶戀花 送春》 \n 樓外垂楊千萬縷,欲系青春,少住春還去。猶自風前飄柳絮,隨春且看歸何處?\n 綠滿山川聞杜宇,便作無情,莫也愁人苦。把酒送春春不語,黃昏卻下瀟瀟雨。"); page.Canvas.DrawString(text, font, pen, 15, 13); //加載圖片,并調用DrawImage()方法在指定位置繪入圖片 PdfImage image = PdfImage.FromFile("image1.jpg"); float width = image.Width * 0.55f; float height = image.Height * 0.55f; float y = (page.Canvas.ClientSize.Width - width) / 3; page.Canvas.DrawImage(image, y, 60, width, height); //保存并打開文檔 document.SaveToFile("PDF創建.pdf"); System.Diagnostics.Process.Start("PDF創建.pdf"); } } }
創建結果:
Imports Spire.Pdf Imports Spire.Pdf.Graphics Imports System.Drawing Namespace CreatePDF_PDF Class Program Private Shared Sub Main(ByVal args As String()) Dim document As PdfDocument = New PdfDocument() Dim unitCvtr As PdfUnitConvertor = New PdfUnitConvertor() Dim margins As PdfMargins = New PdfMargins() margins.Top = unitCvtr.ConvertUnits(2.54F, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point) margins.Bottom = margins.Top margins.Left = unitCvtr.ConvertUnits(3.17F, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point) margins.Right = margins.Left Dim page As PdfPageBase = document.Pages.Add(PdfPageSize.A4, margins) Dim font As PdfTrueTypeFont = New PdfTrueTypeFont(New Font("楷體", 11F), True) Dim pen As PdfPen = New PdfPen(Color.Black) Dim text As String =("《蝶戀花 送春》 " & vbLf & " 樓外垂楊千萬縷,欲系青春,少住春還去。猶自風前飄柳絮,隨春且看歸何處?" & vbLf & " 綠滿山川聞杜宇,便作無情,莫也愁人苦。把酒送春春不語,黃昏卻下瀟瀟雨。") page.Canvas.DrawString(text, font, pen, 15, 13) Dim image As PdfImage = PdfImage.FromFile("image1.jpg") Dim width As Single = image.Width * 0.55F Dim height As Single = image.Height * 0.55F Dim y As Single =(page.Canvas.ClientSize.Width - width) / 3 page.Canvas.DrawImage(image, y, 60, width, height) document.SaveToFile("PDF創建.pdf") System.Diagnostics.Process.Start("PDF創建.pdf") End Sub End Class End Namespace
這里創建PDF文檔,Spire.PDF支持直接生成PDF文檔并同時實現添加文本、圖片、圖形、水印、表格、頁眉頁腳、頁碼等操作,這里示例代碼以添加文本、圖片為例,有需要可以參見以下博客:
C# 添加PDF水印
C# 創建PDF表格
測試文檔:
using Spire.Pdf; using System; using System.IO; using System.Text; namespace ExtractText_PDF { class Program { static void Main(string[] args) { //實例化PdfDocument類對象,并加載PDF文檔 PdfDocument doc = new PdfDocument(); doc.LoadFromFile("sample.pdf"); //實例化一個StringBuilder 對象 StringBuilder content = new StringBuilder(); //遍歷文檔所有PDF頁面,提取文本 foreach (PdfPageBase page in doc.Pages) { content.Append(page.ExtractText()); } //將提取到的文本寫為.txt格式并保存到本地路徑 String fileName = "獲取文本.txt"; File.WriteAllText(fileName, content.ToString()); System.Diagnostics.Process.Start("獲取文本.txt"); } } }
讀取結果:
Imports Spire.Pdf Imports System Imports System.IO Imports System.Text Namespace ExtractText_PDF Class Program Private Shared Sub Main(ByVal args As String()) Dim doc As PdfDocument = New PdfDocument() doc.LoadFromFile("sample.pdf") Dim content As StringBuilder = New StringBuilder() For Each page As PdfPageBase In doc.Pages content.Append(page.ExtractText()) Next Dim fileName As String = "獲取文本.txt" File.WriteAllText(fileName, content.ToString()) System.Diagnostics.Process.Start("獲取文本.txt") End Sub End Class End Namespace
using Spire.Pdf; using System.IO; using System.Text; using System.Drawing; namespace ExtractText1_PDF { class Program { static void Main(string[] args) { //創建PdfDocument類實例,并加載PDF文檔 PdfDocument pdf = new PdfDocument(); pdf.LoadFromFile("sample.pdf"); //獲取PDF第一頁 PdfPageBase page = pdf.Pages[0]; //從第一頁的指定矩形區域內提取文本 string text = page.ExtractText(new RectangleF(50, 50, 500, 170)); //保存文本到.txt文件,并打開文檔 StringBuilder sb = new StringBuilder(); sb.AppendLine(text); File.WriteAllText("Extract.txt", sb.ToString()); System.Diagnostics.Process.Start("Extract.txt"); } } }
讀取結果:
(此時讀取的就只是指定區域內的文本)
Imports Spire.Pdf Imports System.IO Imports System.Text Imports System.Drawing Namespace ExtractText1_PDF Class Program Private Shared Sub Main(ByVal args As String()) Dim pdf As PdfDocument = New PdfDocument() pdf.LoadFromFile("sample.pdf") Dim page As PdfPageBase = pdf.Pages(0) Dim text As String = page.ExtractText(New RectangleF(50, 50, 500, 170)) Dim sb As StringBuilder = New StringBuilder() sb.AppendLine(text) File.WriteAllText("Extract.txt", sb.ToString()) System.Diagnostics.Process.Start("Extract.txt") End Sub End Class End Namespace
using Spire.Pdf; using System.Collections.Generic; using System.Drawing; namespace ExtractImages_PDF { class Program { static void Main(string[] args) { //創建一個PdfDocument類對象,加載PDF測試文檔 PdfDocument doc = new PdfDocument(); doc.LoadFromFile("sample.pdf"); //聲明List類對象 List<Image> ListImage = new List<Image>(); //遍歷PDF文檔所有頁面 for (int i = 0; i < doc.Pages.Count; i++) { //獲取文檔所有頁,并提取頁面中的所有圖片 PdfPageBase page = doc.Pages[i]; Image[] images = page.ExtractImages(); if (images != null && images.Length > 0) { ListImage.AddRange(images); } } //將獲取到的圖片保存到本地路徑 if (ListImage.Count > 0) { for (int i = 0; i < ListImage.Count; i++) { Image image = ListImage[i]; image.Save("image" + (i + 1).ToString() + ".png", System.Drawing.Imaging.ImageFormat.Png); } //打開獲取到的圖片 System.Diagnostics.Process.Start("image1.png"); } } } }
讀取結果:
Imports Spire.Pdf Imports System.Collections.Generic Imports System.Drawing Namespace ExtractImages_PDF Class Program Private Shared Sub Main(ByVal args As String()) Dim doc As PdfDocument = New PdfDocument() doc.LoadFromFile("sample.pdf") Dim ListImage As List(Of Image) = New List(Of Image)() For i As Integer = 0 To doc.Pages.Count - 1 Dim page As PdfPageBase = doc.Pages(i) Dim images As Image() = page.ExtractImages() If images IsNot Nothing AndAlso images.Length > 0 Then ListImage.AddRange(images) End If Next If ListImage.Count > 0 Then For i As Integer = 0 To ListImage.Count - 1 Dim image As Image = ListImage(i) image.Save("image" & (i + 1).ToString() & ".png", System.Drawing.Imaging.ImageFormat.Png) Next System.Diagnostics.Process.Start("image1.png") End If End Sub End Class End Namespace
關于“C#/VB.NET如何創建、讀取PDF文檔”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。