Pdfium.Net SDK 是领先的 .Net 库,用于生成、操作和查看可移植文档格式的文件。我们提供高级 c# / VB.Net API,用于在 WEB 服务器或任何其他服务器系统上动态创建 pdf,并在现有桌面或 WEB 应用程序中实现“另存为 PDF”功能。

入门:C# 代码示例

  • 即时创建 PDF 文档
  • 从多个图像生成 PDF
  • 使用 C# 打印 PDF 文件
  • 在 C# 中从 PDF 中提取文本
  • 使用 C# 从 Pdf 中提取文本坐标
  • 使用 .Net C# 从 Pdf 文件中提取图像
  • 在 PDF 文件中搜索文本
  • 异步搜索文本
  • 在 C# 中拆分 PDF
  • 使用 C# 合并 PDF
  • 将 PDF 渲染为图像
  • 填写可编辑的 PDF 字段并从中提取数据

如何使用 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 classPdfCommon.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 classusing (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 objectsPdfFont calibryBold = PdfFont.CreateFont(doc, "CalibriBold");// Insert text objects at 7.69"; 11.02" and font size is 25PdfTextObject 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 namepage.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 LibraryPdfCommon.Initialize();//Create a PDF documentusing (var doc = PdfDocument.CreateNew()){//Read imagesvar files = System.IO.Directory.GetFiles(@"c:\Images\", "*.*", System.IO.SearchOption.AllDirectories);foreach (var file in files){//Create empty PdfBitmapusing (PdfBitmap pdfBitmap = PdfBitmap.FromFile(file)){//Create Image objectvar imageObject = PdfImageObject.Create(doc, pdfBitmap, 0, 0);//Calculate size of image in PDF pointsvar size = CalculateSize(pdfBitmap.Width, pdfBitmap.Height);//Add empty page to PDF documentvar page = doc.Pages.InsertPageAt(doc.Pages.Count, size);//Insert image to newly created pagepage.PageObjects.Add(imageObject);//set image matriximageObject.Matrix = new FS_MATRIX(size.Width, 0, 0, size.Height, 0, 0);//Generate PDF page content to content streampage.GenerateContent();}}// Save  PDF document as "saved.pdf" in no incremental modedoc.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 filevar printDoc = new PdfPrintDocument(doc);printDoc.Print();
}
C#
复制

上面的代码将 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
}
C#
复制

PdfPrintDocument派生自标准PrintDocument类,因此您可以使用 .Net Framework 的打印对话框 (PrinterDialog) 根据用户输入配置 PrintDocument。

public void OnPrintClick()
{if (PdfViewer.Document.FormFill != null)PdfViewer.Document.FormFill.ForceToKillFocus();//create an instance of PrintDocument classvar printDoc = new PdfPrintDocument(PdfViewer.Document); // create an instance of Print document class that is used for printing PDF document.//Create a standard print dialog boxvar 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 documentif (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 stringstring 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 documentusing (var page = doc.Pages[1]){//Extract text information structure from the page// 10 - Index for the start characters// 25 - Number of characters to be extractedvar textInfo = page.Text.GetTextInfo(10, 25);//Gets text from textInfo strtucturestring 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 documentusing (var doc = PdfDocument.Load(@"d:\0\test_big.pdf")) // Read PDF document and enumerate pages{//Enumerate pagesforeach(var page in doc.Pages){var found = page.Text.Find("text for search", FindFlags.MatchWholeWord, 0);if (found == null)return; //nothing founddo{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 documentdoc.Pages.ImportPages(sourceDoc, "1-5", 0);//And save it to doc1.pdfdoc.Save(@"c:\doc1.pdf", SaveFlags.Incremental);}//Create another PDF document for pages 5-10.using (var doc = PdfDocument.CreateNew()){//Also import pagesdoc.Pages.ImportPages(sourceDoc, "5-10", 0);//And save them toodoc.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 filesusing (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 documentmainDoc.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 documentmainDoc.Pages.ImportPages(doc,string.Format("1-{0}", doc.Pages.Count),mainDoc.Pages.Count);}mainDoc.Save(@"c:\ResultDocument.pdf", SaveFlags.NoIncremental);}
}
C#
复制

 

使用 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 文档中的所有可编辑表单。

全能PDF:Pdfium.Net SDK 2023-03-18 Crack相关推荐

  1. Pdfium.Net SDK增强您的功能快速生成PDF

    Pdfium.Net SDK增强您的功能快速生成PDF PDF领导者是否想在Windows和Mac上仅用几行代码来创建,渲染,打印,保护,合并,拆分和操作PDF文件?使用 Pdfium.Net SDK ...

  2. English Learning - L2-10 英音地道语音语调 鼻辅音 [m] [n] [ŋ] 舌边音 [l] [r] 2023.03.23 周四

    English Learning - L2-10 英音地道语音语调 鼻辅音 [m] [n] [ŋ] 舌边音 [l] [r] 2023.03.23 周四 课前准备活动和回顾 鼻辅音 鼻辅音 [m] 鼻辅 ...

  3. Go C画图 CSP-J 信息学奥赛 2023.03。13、14、15测试题

    2023.03.09.10.11.12 测试题(选做3-5题) 请将每1道题做完后,将AC截图发到QQ群里 请测试完成后,统计一下AC题目的个数 379.场记板 难度:1 登录 385.身高 难度:1 ...

  4. 2020.03.18模拟赛17(第二题)

    2.[GDKOI训练]音乐节拍(mnotes) 题目描述 FJ准备教他的奶牛弹奏一首歌曲,歌曲由N(1<=N<=50,000)种音节组成,编号为1到N,而且一定按照从1到N的顺序进行弹奏, ...

  5. 2018.03.18 临汾市游记

    2018.03.18 临汾市游记 写在前面 首先,Capella 极其擅长记流水账,包括本文. 其次,本文中所有并列关系的人名,均按字典序排列. 背景 临汾一中 Mr_Wolfram 和 poorpo ...

  6. 2020.03.18模拟赛17(第三题)

    3.[GDKOI训练]电视游戏问题(vidgame) 题目描述 农夫约翰的奶牛们游戏成瘾!本来FJ是想要按照陶教授的做法拿她们去电击戒瘾的,可是后来他发现奶牛们玩游戏之后比原先产更多的奶.很明显,这是 ...

  7. 美团校招-2023.3.18.10点-第四题-商店-困难

    商店 Problem Description 在商店里有N个物品,每个物品有原价和折扣价小美相要购买商品.小美拥有X元,一共Y张折扣券.小美需要最大化购买商品的数量,并在所购商品数量尽量多的前提下,尽 ...

  8. 全能pdf转换器注册码

    选择PDF转换器注册码哪个好?需要注意的是,普通的PDF转换器缺乏优秀的转换核心,经常会出现转换质量不佳的情况,尤其在软件本身的PDF文件识别技术不成熟的情况下,其转换出来的文件内容常常会存在文字丢失 ...

  9. # Day3 2023.3.18

    Day3 2023.3.18 一.查询命令帮助信息 1.command --help 2.man command 二.文件和目录常用命令 ls ls 类似于DOS下的dir 以. 开头的文件为隐藏文件 ...

最新文章

  1. how to add one row in the dataframe?
  2. 2013年2月最后一周
  3. 迁移至Android3.0遇到一些问题
  4. web安全_皮卡丘_xss
  5. 【APICloud系列|30】苹果MAC电脑取消辅助功能-语音识别
  6. windowsGHO镜像系统winXPwin7win8win10下载
  7. linux vim 安装失败,ubuntu安装vim失败怎么办
  8. 【2】输入俩个数m,n,字符串st1为1-m组成,输出字符串的倒数第n个字符
  9. 判断无线网卡是否支持监听模式
  10. openoffic+java+spring 多线程 转换doc,ppt,xls - html/pdf
  11. 未明学院:产品经理到底在职场中过得怎么样?
  12. 【掌上齐齐哈尔】市民网上换领驾驶证方便快捷
  13. echarts地图生成
  14. iPhone/iPad屏幕投屏镜像到PC或Mac上面教程分享
  15. 数字图像处理八:图像分割
  16. Linux配置PHP环境
  17. 计算机硬件知识比赛策划,计算机硬件知识讲座活动策划案.doc
  18. 计算机毕业设计PHP汽车4S店保养在线预约系统
  19. Joshua Bloch
  20. macbook 定时提醒发送邮件

热门文章

  1. 面向过程与面向对象编程实例
  2. Ubuntu14.04 屏幕截图快捷键
  3. CString::TrimLeft和CString::TrimRight
  4. Python都纳入新课标了,你还不知道它是什么?
  5. XorDDos木马清除
  6. Leonard ai 画明代皇帝肖像
  7. 第一个作业-1-1-温度转换
  8. 【集群监控——Cacti、Nagios、Zabbix安装配置过程】
  9. backtrack工具列表及介绍
  10. 线程池ThreadPool