Pdfium.Net SDK 将基础的查看引擎扩展 pages 为功能全面的文档处理器。其强大的 C# / VB.NET API 支持深度的内容编辑、交互式表单填充、合规的 PDF/A 创建,以及您所能想象的几乎所有功能。
通过将结构化的 XML 元数据直接嵌入到您的流水线中,轻松实现电子发票(Factur-X / ZUGFeRD)自动化。轻松应对任何文档挑战,在数天而非数月内部署生产级工作流。
/// <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 standard System.Drawing.Bitmap class
using (PdfBitmap logo = PdfBitmap.FromFile(@"c:\examples\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(@"c:\examples\sample_document.pdf", SaveFlags.NoIncremental);
}
Pdfium.Net SDK库允许开发者在C#中轻松创建PDF文档。这个例子表明,可以使用页面对象动态地创建一个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:\Examples\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:\Examples\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 vary 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文档。
/// <summary>
/// Printing PDF Files in C# using PDF Library
/// </summary>
public void PrintPdf()
{
var doc = PdfDocument.Load(@"c:\Examples\test.pdf"); // Read PDF file
var printDoc = new PdfPrintDocument(doc);
printDoc.Print();
}
上面的代码将 PDF 文档打印到默认打印机。 还显示了带有打印进度的标准打印对话框。 如果你想抑制进度窗口,请修改如下所示的代码。
public void PrintPdf()
{
var doc = PdfDocument.Load(@"c:\Examples\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
}
/// <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:\Examples\test.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 文件中提取全文。
/// <summary>
/// Search for a Text in a PDF File in C# With Pdfium.Net SDK Library
/// </summary>
public void Search()
{
// Read PDF document and enumerate pages
using (var doc = PdfDocument.Load(@"c:\Examples\test.pdf"))
{
//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 文档中搜索文本。
/// <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:\Examples\test.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 structure
string text = textInfo.Text;
//Gets a collection of rectangular areas bounding specified text.
var rects = textInfo.Rects;
}
}
}
Pdfium.Net SDK 还允许开发人员轻松地从任何 PDF 文件中提取文本坐标。
/// <summary>
/// Extracting Images from Pdf File With .Net C# and PDF Library
/// </summary>
private int _writtenImageIndex = 0;
public void ExtractAllImages()
{
//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:\Examples\test.pdf")) // C# Read PDF File
{
//Enumerate all pages sequentially in a given document
foreach (var page in doc.Pages)
{
//Extract and save images
ExtractImagesFromPage(page);
//dipose page object to unload it from memory
page.Dispose();
}
}
}
private void ExtractImagesFromPage(PdfPage page)
{
//Enumerate all objects on a page
foreach (var obj in page.PageObjects)
{
var imageObject = obj as PdfImageObject;
if (imageObject == null)
continue; //if not an image object then nothing do
//Save image to disk
var path = string.Format(@"c:\Images\image_{0}.png", ++_writtenImageIndex);
imageObject.Bitmap.Image.Save(path, ImageFormat.Png);
}
}
该示例演示了从 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:\Examples\test.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:\Examples\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:\Examples\doc2.pdf", SaveFlags.Incremental);
}
}
}
下面的代码示例演示了如何使用 C# PDF 库来拆分 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:\Examples\test.pdf")) // C# Read source PDF File #1
{
//Open one PDF document.
using (var doc = PdfDocument.Load(@"c:\Examples\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:\Examples\doc2.pdf"))
{
//Import all pages from document
mainDoc.Pages.ImportPages(
doc,
string.Format("1-{0}", doc.Pages.Count),
mainDoc.Pages.Count
);
}
mainDoc.Save(@"c:\Examples\ResultDocument.pdf", SaveFlags.NoIncremental);
}
}
使用 C# PDF 库,您不仅可以将多个 PDF 文件合并为一个文件,还可以从源文件中选择特定页面并将它们组合成一个 PDF 文档。
上面的代码展示了如何使用 ImportPages 操作来完成。
/// <summary>
/// Render whole PDF document using C# PDF Library
/// </summary>
public void RenderPages()
{
using (var doc = PdfDocument.Load(@"c:\Examples\test.pdf")) // C# Read PDF Document
{
int idx = 0;
foreach (var page in doc.Pages)
{
int width = (int)(page.Width / 72.0 * 96);
int height = (int)(page.Height / 72.0 * 96);
using (var bitmap = new PdfBitmap(width, height, true))
{
bitmap.FillRect(0, 0, width, height, FS_COLOR.White);
page.Render(bitmap, 0, 0, width, height, PageRotate.Normal, RenderFlags.FPDF_LCD_TEXT);
bitmap.Image.Save($"c:\\Examples\\page_{idx++}", ImageFormat.Png);
}
}
}
}
In this example we create a bitmap of each page, so we calculate the required width and height of the bitmap in pixels converted from the dimensions of the PDF page in Points. Each Point is 1/72 of an inch, so we basically take the vertical or horizontal DPI of the image (96 in our example), multiply it to corresponding dimension and divide by 72.
Next we create a new PdfBitmap using the dimensions we just computed. The last parameter of the constructor tells to use the true color mode.
Then we fill the entire bitmap with white and render the page to it. That's all!
/// <summary>
/// Filling Editable PDF Fields and Extracting Data From Them using .Net PDF Library
/// </summary>
public void FillFields()
{
var doc = PdfDocument.Load(@"c:\Examples\test.pdf", new PdfForms()); // C# Read PDF Document
int i = 0;
foreach (var field in doc.FormFill.InterForm.Fields)
{
if (field.FieldType == Patagames.Pdf.Enums.FormFieldTypes.FPDF_FORMFIELD_TEXTFIELD)
{
field.Value = "This is a field #" + (++i);
}
}
}
此示例代码演示了如何使用 .Net PDF 库以编程方式填充 pdf 文档中的所有可编辑表单。
Patgames 支持服务就是您所说的“为开发者服务的开发者”。 当您获得产品许可时,您收到的不仅仅是一个很棒的库。 为了充分利用这一功能,您还可以直接从我们的 .Net 开发团队获得 12 个月的技术支持。
我已经尝试了你的最新版本,它运行良好——解决了我的两个问题! 感谢您满足我的要求——非常感谢。 我们仍将 [其他产品] 用于一些遗留项目,但我们已将其用于所有新项目。 尽管 [其他产品] 库本身很好,但我们发现它们的支持很差 - 毫无疑问,Pdfium.NET 支持已被证明要好得多,无论是速度还是质量! 再次感谢您提供一流的服务和优质的产品。 -TerryThe link is hidden due to ethical concerns.
Pdfium.Net SDK 的主要设计目标是使开发人员的事情变得简单直观。 类库抽象了使用底层 pdfium 库的所有细节,并提供了一个基于世界对象和其他直观类的接口。
此外,Pdfium.Net SDK 是有据可查的 SDK! 我们提供了丰富的文档,旨在让初学者和高级用户大致了解 Pdfium.Net SDK 的功能。
One of our targets is to provide developers with high quality tool for dynamic PDF creation on any server system like Microsoft Azure.
我已经验证您的 SDK 在部署到 Azure 时确实可以工作(我测试的几个产品没有 - 您可能想宣传这一点)。 我已经购买了。
再次感谢! -Cyberghttps://forum.patagames.com/posts/m1105-Workability-in-various-environments#post1105
赋予您的 .Net 应用查看 pdf 文件的能力! 借助多种视图模式、细致入微的自定义和对 acroforms pdf 查看器的完整支持,可无缝融入您的应用程序并根据您的设计量身定制。