Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create and read PDF documents by C#/VB.NET

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to create and read PDF documents in C#/VB.NET. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Create PDF document C#using Spire.Pdf;using Spire.Pdf.Graphics;using System.Drawing;namespace CreatePDF_PDF {class Program {static void Main (string [] args) {/ / initialize an instance of PdfDocument class PdfDocument document = new PdfDocument (); / / declare PdfUnitConvertor and PdfMargins class objects PdfUnitConvertor unitCvtr = new PdfUnitConvertor (); PdfMargins margins = new PdfMargins () / / set margins 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 / / add an A4-sized page PdfPageBase page = document.Pages.Add (PdfPageSize.A4, margins); / / customize PdfTrueTypeFont and PdfPen instances, and set font type, font size and font color PdfTrueTypeFont font = new PdfTrueTypeFont ("italics", 11f), true); PdfPen pen = new PdfPen (Color.Black) / / call the DrawString () method to write the text string text = ("Butterfly Love Flowers send Spring"\ nThousand thousands of strands of poplar hang outside the building. If you want to be young, live less spring and go away. Still floating willow catkins before the wind, let's see where to go with the spring. When the green mountains and rivers are full of du Yu, they become ruthless, and do not worry about people's suffering. Send wine to Chunchun without saying a word, but it rains Xiaoxiao at dusk. ") ; page.Canvas.DrawString (text, font, pen, 15,13); / / load the picture and call the DrawImage () method to draw the picture PdfImage image = PdfImage.FromFile ("image1.jpg") at the specified location; 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); / / Save and open the document document.SaveToFile ("PDF create .pdf"); System.Diagnostics.Process.Start ("PDF create .pdf");}}

Create the result:

VB.NETImports Spire.PdfImports Spire.Pdf.GraphicsImports System.DrawingNamespace 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 ("bold", 11F) True) Dim pen As PdfPen = New PdfPen (Color.Black) Dim text As String = ("" Butterfly loves flowers to send spring "& vbLf &" thousands of wisps of poplars hanging outside the building If you want to be young, live less in spring and go. Still floating willow catkins in front of the wind, let's see where to go with the spring. " & vbLf & "when the green mountains and rivers hear du Yu, they become ruthless, but do not worry about people's sufferings. Send wine to the spring without saying a word, but it rains Xiaoxiao at dusk.") 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 create .pdf") System.Diagnostics.Process.Start ("PDF create .pdf") End Sub End ClassEnd Namespace

PDF documents are created here. Spire.PDF supports generating PDF documents directly and adding text, images, graphics, watermarks, tables, headers, footers, page numbers and other operations at the same time. Here is an example of adding text and pictures. If necessary, please see the following blog:

C # add PDF watermark

C # create PDF form

Second, read PDF documents

Test documentation:

1. Read PDF text 1.1 read all text C#using Spire.Pdf;using System;using System.IO;using System.Text;namespace ExtractText_PDF {class Program {static void Main (string [] args) {/ / instantiate the PdfDocument class object and load the PDF document PdfDocument doc = new PdfDocument (); doc.LoadFromFile ("sample.pdf") / / instantiate a StringBuilder object StringBuilder content = new StringBuilder (); / / iterate through all the PDF pages of the document and extract the text foreach (PdfPageBase page in doc.Pages) {content.Append (page.ExtractText ()) } / / write the extracted text in .txt format and save it to the local path String fileName = "get text .txt"; File.WriteAllText (fileName, content.ToString ()); System.Diagnostics.Process.Start ("get text .txt");}

Read result:

VB.NETImports Spire.PdfImports SystemImports System.IOImports System.TextNamespace 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 = "get text .txt" File.WriteAllText (fileName Content.ToString () System.Diagnostics.Process.Start ("get text .txt") End Sub End ClassEnd Namespace1.2 reads the specified area text C#using Spire.Pdf Using System.IO;using System.Text;using System.Drawing;namespace ExtractText1_PDF {class Program {static void Main (string [] args) {/ / create an instance of the PdfDocument class and load the PDF document PdfDocument pdf = new PdfDocument (); pdf.LoadFromFile ("sample.pdf"); / / get the first page of PDF PdfPageBase page = pdf.Pages [0] / / extract the text string text = page.ExtractText (new RectangleF (50,50,500,170)) from the specified rectangular area of the first page; / / Save the text to a .txt file and open the document StringBuilder sb = new StringBuilder (); sb.AppendLine (text); File.WriteAllText ("Extract.txt", sb.ToString ()) System.Diagnostics.Process.Start ("Extract.txt");}

Read result:

(only the text in the specified area is read at this time)

VB.NETImports Spire.PdfImports System.IOImports System.TextImports System.DrawingNamespace 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) Dim sb As StringBuilder = New StringBuilder () sb.AppendLine (text) File.WriteAllText ("Extract.txt", sb.ToString ()) System.Diagnostics.Process.Start ("Extract.txt") End Sub End ClassEnd Namespace2. Read the PDF picture C#using Spire.Pdf;using System.Collections.Generic;using System.Drawing;namespace ExtractImages_PDF {class Program {static void Main (string [] args) {/ / create a PdfDocument class object and load the PDF test document PdfDocument doc = new PdfDocument (); doc.LoadFromFile ("sample.pdf") / / declare the List class object List ListImage = new List (); / / traverse all pages of the PDF document 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);}} / / Save the acquired image to the local path 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"); } } }} 读取结果: VB.NETImports Spire.PdfImports System.Collections.GenericImports System.DrawingNamespace 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 ClassEnd Namespace's article on "how C#/VB.NET creates and reads PDF documents" ends here. Hope that the above content can be helpful to you, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report