Pdfium.Net SDK 是领先的 .Net 库,用于生成、操作和查看可移植文档格式的文件。我们提供高级 c# / VB.Net API,用于在 WEB 服务器或任何其他服务器系统上动态创建 pdf,并在现有桌面或 WEB 应用程序中实现“另存为 PDF”功能。
入门:C# 代码示例
如何使用 C# 动态创建 PDF
/// <summary>
/// Create PDF Document on The Fly in C# using Pdfium.Net SDK Library
/// </summary>
public void CreatePdf()
{
// The PDF coordinate system origin is at the bottom left corner of the page.
// The X-axis is pointing to the right. The Y-axis is pointing in upward direction.
// The sizes and coordinates in this method are given in the inches.
// Step 1: Initialize PDF library and create empty document
// Return value: PdfDocument main class
PdfCommon.Initialize();
var doc = PdfDocument.CreateNew(); // Create a PDF document
// Step 2: Add new page
// Arguments: page width: 8.27", page height: 11.69", Unit of measure: inches
// The PDF unit of measure is point. There are 72 points in one inch.
var page = doc.Pages.InsertPageAt(doc.Pages.Count, 8.27f * 72, 11.69f * 72);
// Step 3: Add graphics and text contents to the page
// Insert image from file using standart System.Drawing.Bitmap class
using (PdfBitmap logo = PdfBitmap.FromFile(@"e:\63\logo_square.png"))
{
PdfImageObject imageObject = PdfImageObject.Create(doc, logo, 0, 0);
//image resolution is 300 DPI and location is 1.69 x 10.0 inches.
imageObject.Matrix = new FS_MATRIX(logo.Width * 72 / 300, 0, 0, logo.Height * 72 / 300, 1.69 * 72, 10.0 * 72);
page.PageObjects.Add(imageObject);
}
// Create fonts used for text objects
PdfFont calibryBold = PdfFont.CreateFont(doc, "CalibriBold");
// Insert text objects at 7.69"; 11.02" and font size is 25
PdfTextObject textObject = PdfTextObject.Create("Sample text", 1.69f * 72, 11.02f * 72, calibryBold, 25);
textObject.FillColor = FS_COLOR.Black;
page.PageObjects.Add(textObject);
// Step 5: Generate page content and save pdf file
// argument: PDF file name
page.GenerateContent();
doc.Save(@"e:\63\sample_document.pdf", SaveFlags.NoIncremental);
}
Pdfium.Net SDK Library 允许开发人员在 C# 中轻松创建 PDF 文档。此示例显示可以使用页面对象动态创建 PDF 文档。
您可以创建多个页面对象并将它们放置在页面的任何位置。页面对象有几种类型:路径、表单、图像和文本对象。
如何使用 C# 以编程方式从一组图像生成 PDF
/// <summary>
/// Generate PDF document From Multiple Images in C# using PDF Library
/// </summary>
public void GeneratePdf()
{
//Initialize C# PDF Library
PdfCommon.Initialize();
//Create a PDF document
using (var doc = PdfDocument.CreateNew())
{
//Read images
var files = System.IO.Directory.GetFiles(@"c:\Images\", "*.*",
System.IO.SearchOption.AllDirectories);
foreach (var file in files)
{
//Create empty PdfBitmap
using (PdfBitmap pdfBitmap = PdfBitmap.FromFile(file))
{
//Create Image object
var imageObject = PdfImageObject.Create(doc, pdfBitmap, 0, 0);
//Calculate size of image in PDF points
var size = CalculateSize(pdfBitmap.Width, pdfBitmap.Height);
//Add empty page to PDF document
var page = doc.Pages.InsertPageAt(doc.Pages.Count, size);
//Insert image to newly created page
page.PageObjects.Add(imageObject);
//set image matrix
imageObject.Matrix = new FS_MATRIX(size.Width, 0, 0, size.Height, 0, 0);
//Generate PDF page content to content stream
page.GenerateContent();
}
}
// Save PDF document as "saved.pdf" in no incremental mode
doc.Save(@"c:\test.pdf", SaveFlags.NoIncremental);
}
}
/// <summary>
/// The function takes width and height of the bitmap in pixels as well as
/// horizontal and vertical DPI and calculates the size of the PDF page.
/// To understand the conversion you should know the following:
/// One inch contains exactly 72 PDF points;
/// DPI of the scanned image may vфry and depends on scanning resolution
/// <summary>
private FS_SIZEF CalculateSize(int width, int height, float dpiX=300, float dpiY=300)
{
return new FS_SIZEF()
{
Width = width * 72 / dpiX,
Height = height * 72 / dpiY
};
}
此示例展示了如何使用简单的 C# 代码和 PDF 库从一堆扫描图像生成 PDF 文档。
如何在 C# 中打印 PDF 文件
/// <summary>
/// Printing PDF Files in C# using PDF Library
/// </summary>
public void PrintPdf()
{
var doc = PdfDocument.Load("c:\test.pdf"); // Read PDF file
var printDoc = new PdfPrintDocument(doc);
printDoc.Print();
}
上面的代码将 PDF 文档打印到默认打印机。还显示带有打印进度的标准打印对话框。如果你想抑制进度窗口,请修改如下所示的代码。
public void PrintPdf()
{
var doc = PdfDocument.Load("c:\test.pdf");
var printDoc = new PdfPrintDocument(doc);
PrintController printController = new StandardPrintController();
printDoc.PrintController = printController;
printDoc.Print(); // C# Print PDF document
}
PdfPrintDocument派生自标准PrintDocument类,因此您可以使用 .Net Framework 的打印对话框 (PrinterDialog) 根据用户输入配置 PrintDocument。
public void OnPrintClick()
{
if (PdfViewer.Document.FormFill != null)
PdfViewer.Document.FormFill.ForceToKillFocus();
//create an instance of PrintDocument class
var printDoc = new PdfPrintDocument(PdfViewer.Document); // create an instance of Print document class that is used for printing PDF document.
//Create a standard print dialog box
var dlg = new PrintDialog();
dlg.AllowCurrentPage = true;
dlg.AllowSomePages = true;
dlg.UseEXDialog = true;
//sets the PrintDocument used to obtain PrinterSettings.
dlg.Document = printDoc;
//show PrinterDialog and print pdf document
if (dlg.ShowDialog() == DialogResult.OK)
printDoc.Print(); // C# Print PDF
}
在 C# 中读取 PDF 文件并从中提取文本
/// <summary>
/// Read PDF File and Extract Text From it in C#
/// </summary>public void ExtractText()
{
//Initialize the SDK library
//You have to call this function before you can call any PDF processing functions.
PdfCommon.Initialize();
//Open and load a PDF document from a file.
using (var doc = PdfDocument.Load(@"c:\test001.pdf")) // C# Read PDF File
{
foreach (var page in doc.Pages)
{
//Gets number of characters in a page or -1 for error.
//Generated characters, like additional space characters, new line characters, are also counted.
int totalCharCount = page.Text.CountChars;
//Extract text from page to the string
string text = page.Text.GetText(0, totalCharCount);
page.Dispose();
}
}
}
Pdfium.Net SDK 允许开发人员轻松地从几乎任何 PDF 文件中提取全文。
如何从 PDF 中提取文本坐标
/// <summary>
/// Extract Text Coordinates from Pdf in C# using PDF Library
/// </summary>
public void ExtractTextInfo()
{
//Initialize the SDK library
//You have to call this function before you can call any PDF processing functions.
PdfCommon.Initialize();
//Open and load a PDF document from a file.
using (var doc = PdfDocument.Load(@"c:\test001.pdf")) // C# Read PDF File
{
//Get second page from document
using (var page = doc.Pages[1])
{
//Extract text information structure from the page
// 10 - Index for the start characters
// 25 - Number of characters to be extracted
var textInfo = page.Text.GetTextInfo(10, 25);
//Gets text from textInfo strtucture
string text = textInfo.Text;
//Gets a collection of rectangular areas bounding specified text.
var rects = textInfo.Rects;
}
}
}
Pdfium.Net SDK 还允许开发人员轻松地从任何 PDF 文件中提取文本坐标。
如何在 PDF 文件中搜索文本
/// <summary>
/// Search for a Text in a PDF File in C# With Pdfium.Net SDK Library
/// </summary>
public void Search()
{
//Open PDF document
using (var doc = PdfDocument.Load(@"d:\0\test_big.pdf")) // Read PDF document and enumerate pages
{
//Enumerate pages
foreach(var page in doc.Pages)
{
var found = page.Text.Find("text for search", FindFlags.MatchWholeWord, 0);
if (found == null)
return; //nothing found
do
{
var textInfo = found.FoundText;
foreach(var rect in textInfo.Rects)
{
float x = rect.left;
float y = rect.top;
//...
}
} while (found.FindNext());
page.Dispose();
}
}
}
此示例说明如何使用简单的 C# 代码和 PDF 库在 PDF 文档中搜索文本。
如何将PDF文件分割成小文件
/// <summary>
/// Split PDF in C# using PDF Library
/// </summary>
public void SplitDocument()
{
//Initialize the SDK library
//You have to call this function before you can call any PDF processing functions.
PdfCommon.Initialize();
//Open and load a PDF document from a file.
using (var sourceDoc = PdfDocument.Load(@"c:\test001.pdf")) // C# Read PDF File
{
//Create one PDF document for pages 1-5.
using (var doc = PdfDocument.CreateNew())
{
//Import pages from source document
doc.Pages.ImportPages(sourceDoc, "1-5", 0);
//And save it to doc1.pdf
doc.Save(@"c:\doc1.pdf", SaveFlags.Incremental);
}
//Create another PDF document for pages 5-10.
using (var doc = PdfDocument.CreateNew())
{
//Also import pages
doc.Pages.ImportPages(sourceDoc, "5-10", 0);
//And save them too
doc.Save(@"c:\doc2.pdf", SaveFlags.Incremental);
}
}
}
下面的代码示例演示了如何使用 C# PDF 库来拆分 PDF 文档。
在 C# 中将多个 PDF 文件中的选定页面合并为一个
/// <summary>
/// Merge PDFs in C# using PDF Library
/// </summary>
public void MergePdf()
{
//Initialize the SDK library
//You have to call this function before you can call any PDF processing functions.
PdfCommon.Initialize();
//Open and load a PDF document in which will be merged other files
using (var mainDoc = PdfDocument.Load(@"c:\test001.pdf")) // C# Read source PDF File #1
{
//Open one PDF document.
using (var doc = PdfDocument.Load(@"c:\doc1.pdf")) //Read PDF File #2
{
//Import all pages from document
mainDoc.Pages.ImportPages(
doc,
string.Format("1-{0}", doc.Pages.Count),
mainDoc.Pages.Count
);
}
//Open another PDF document.
using (var doc = PdfDocument.Load(@"c:\doc2.pdf"))
{
//Import all pages from document
mainDoc.Pages.ImportPages(
doc,
string.Format("1-{0}", doc.Pages.Count),
mainDoc.Pages.Count
);
}
mainDoc.Save(@"c:\ResultDocument.pdf", SaveFlags.NoIncremental);
}
}
使用 C# PDF 库,您不仅可以将多个 PDF 文件合并为一个文件,还可以从源文件中选择特定页面并将它们组合在一个 PDF 文档中。
上面的代码显示了如何使用ImportPages操作来完成它。
如何以编程方式从 PDF 字段填充和提取数据
/// <summary>
/// Filling Editable PDF Fields and Extracting Data From Them using .Net PDF Library
/// </summary>
private void btnTest_Click(object sender, RoutedEventArgs e)
{
var forms = new PdfForms();
var doc = PdfDocument.Load(@"c:\test.pdf", forms); // C# Read PDF Document
//doc.FormFill is equal to forms and can be used to get access to acro forms as well;
int i = 0;
foreach(var field in forms.InterForm.Fields)
{
if(field.FieldType == Patagames.Pdf.Enums.FormFieldTypes.FPDF_FORMFIELD_TEXTFIELD)
{
field.Value = "This is a field #" + (++i);
}
}
}
此示例代码演示了如何使用 .Net PDF 库以编程方式填写 pdf 文档中的所有可编辑表单。