• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

全能PDF:Pdfium.Net SDK 2023-03-18 Crack

武飞扬头像
sdk大全
帮助1

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

  1.  
    /// <summary>
  2.  
    /// Create PDF Document on The Fly in C# using Pdfium.Net SDK Library
  3.  
    /// </summary>
  4.  
    public void CreatePdf()
  5.  
    {
  6.  
    // The PDF coordinate system origin is at the bottom left corner of the page.
  7.  
    // The X-axis is pointing to the right. The Y-axis is pointing in upward direction.
  8.  
    // The sizes and coordinates in this method are given in the inches.
  9.  
     
  10.  
    // Step 1: Initialize PDF library and create empty document
  11.  
    // Return value: PdfDocument main class
  12.  
    PdfCommon.Initialize();
  13.  
    var doc = PdfDocument.CreateNew(); // Create a PDF document
  14.  
     
  15.  
    // Step 2: Add new page
  16.  
    // Arguments: page width: 8.27", page height: 11.69", Unit of measure: inches
  17.  
    // The PDF unit of measure is point. There are 72 points in one inch.
  18.  
    var page = doc.Pages.InsertPageAt(doc.Pages.Count, 8.27f * 72, 11.69f * 72);
  19.  
     
  20.  
    // Step 3: Add graphics and text contents to the page
  21.  
    // Insert image from file using standart System.Drawing.Bitmap class
  22.  
    using (PdfBitmap logo = PdfBitmap.FromFile(@"e:\63\logo_square.png"))
  23.  
    {
  24.  
    PdfImageObject imageObject = PdfImageObject.Create(doc, logo, 0, 0);
  25.  
    //image resolution is 300 DPI and location is 1.69 x 10.0 inches.
  26.  
    imageObject.Matrix = new FS_MATRIX(logo.Width * 72 / 300, 0, 0, logo.Height * 72 / 300, 1.69 * 72, 10.0 * 72);
  27.  
    page.PageObjects.Add(imageObject);
  28.  
    }
  29.  
     
  30.  
    // Create fonts used for text objects
  31.  
    PdfFont calibryBold = PdfFont.CreateFont(doc, "CalibriBold");
  32.  
    // Insert text objects at 7.69"; 11.02" and font size is 25
  33.  
    PdfTextObject textObject = PdfTextObject.Create("Sample text", 1.69f * 72, 11.02f * 72, calibryBold, 25);
  34.  
    textObject.FillColor = FS_COLOR.Black;
  35.  
    page.PageObjects.Add(textObject);
  36.  
     
  37.  
    // Step 5: Generate page content and save pdf file
  38.  
    // argument: PDF file name
  39.  
    page.GenerateContent();
  40.  
    doc.Save(@"e:\63\sample_document.pdf", SaveFlags.NoIncremental);
  41.  
    }
学新通
 

Pdfium.Net SDK Library 允许开发人员在 C# 中轻松创建 PDF 文档。此示例显示可以使用页面对象动态创建 PDF 文档。

您可以创建多个页面对象并将它们放置在页面的任何位置。页面对象有几种类型:路径、表单、图像和文本对象。

如何使用 C# 以编程方式从一组图像生成 PDF

  1.  
    /// <summary>
  2.  
    /// Generate PDF document From Multiple Images in C# using PDF Library
  3.  
    /// </summary>
  4.  
    public void GeneratePdf()
  5.  
    {
  6.  
    //Initialize C# PDF Library
  7.  
    PdfCommon.Initialize();
  8.  
    //Create a PDF document
  9.  
    using (var doc = PdfDocument.CreateNew())
  10.  
    {
  11.  
    //Read images
  12.  
    var files = System.IO.Directory.GetFiles(@"c:\Images\", "*.*",
  13.  
    System.IO.SearchOption.AllDirectories);
  14.  
    foreach (var file in files)
  15.  
    {
  16.  
    //Create empty PdfBitmap
  17.  
    using (PdfBitmap pdfBitmap = PdfBitmap.FromFile(file))
  18.  
    {
  19.  
    //Create Image object
  20.  
    var imageObject = PdfImageObject.Create(doc, pdfBitmap, 0, 0);
  21.  
    //Calculate size of image in PDF points
  22.  
    var size = CalculateSize(pdfBitmap.Width, pdfBitmap.Height);
  23.  
    //Add empty page to PDF document
  24.  
    var page = doc.Pages.InsertPageAt(doc.Pages.Count, size);
  25.  
    //Insert image to newly created page
  26.  
    page.PageObjects.Add(imageObject);
  27.  
    //set image matrix
  28.  
    imageObject.Matrix = new FS_MATRIX(size.Width, 0, 0, size.Height, 0, 0);
  29.  
    //Generate PDF page content to content stream
  30.  
    page.GenerateContent();
  31.  
    }
  32.  
    }
  33.  
    // Save PDF document as "saved.pdf" in no incremental mode
  34.  
    doc.Save(@"c:\test.pdf", SaveFlags.NoIncremental);
  35.  
    }
  36.  
    }
  37.  
    /// <summary>
  38.  
    /// The function takes width and height of the bitmap in pixels as well as
  39.  
    /// horizontal and vertical DPI and calculates the size of the PDF page.
  40.  
    /// To understand the conversion you should know the following:
  41.  
    /// One inch contains exactly 72 PDF points;
  42.  
    /// DPI of the scanned image may vфry and depends on scanning resolution
  43.  
    /// <summary>
  44.  
    private FS_SIZEF CalculateSize(int width, int height, float dpiX=300, float dpiY=300)
  45.  
    {
  46.  
    return new FS_SIZEF()
  47.  
    {
  48.  
    Width = width * 72 / dpiX,
  49.  
    Height = height * 72 / dpiY
  50.  
    };
  51.  
    }
学新通
 

此示例展示了如何使用简单的 C# 代码和 PDF 库从一堆扫描图像生成 PDF 文档。

如何在 C# 中打印 PDF 文件

  1.  
    /// <summary>
  2.  
    /// Printing PDF Files in C# using PDF Library
  3.  
    /// </summary>
  4.  
    public void PrintPdf()
  5.  
    {
  6.  
    var doc = PdfDocument.Load("c:\test.pdf"); // Read PDF file
  7.  
    var printDoc = new PdfPrintDocument(doc);
  8.  
    printDoc.Print();
  9.  
    }
C#
复制

上面的代码将 PDF 文档打印到默认打印机。还显示带有打印进度的标准打印对话框。如果你想抑制进度窗口,请修改如下所示的代码。

  1.  
    public void PrintPdf()
  2.  
    {
  3.  
    var doc = PdfDocument.Load("c:\test.pdf");
  4.  
    var printDoc = new PdfPrintDocument(doc);
  5.  
    PrintController printController = new StandardPrintController();
  6.  
    printDoc.PrintController = printController;
  7.  
    printDoc.Print(); // C# Print PDF document
  8.  
    }
C#
复制

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

  1.  
    public void OnPrintClick()
  2.  
    {
  3.  
    if (PdfViewer.Document.FormFill != null)
  4.  
    PdfViewer.Document.FormFill.ForceToKillFocus();
  5.  
     
  6.  
    //create an instance of PrintDocument class
  7.  
    var printDoc = new PdfPrintDocument(PdfViewer.Document); // create an instance of Print document class that is used for printing PDF document.
  8.  
     
  9.  
    //Create a standard print dialog box
  10.  
    var dlg = new PrintDialog();
  11.  
    dlg.AllowCurrentPage = true;
  12.  
    dlg.AllowSomePages = true;
  13.  
    dlg.UseEXDialog = true;
  14.  
    //sets the PrintDocument used to obtain PrinterSettings.
  15.  
    dlg.Document = printDoc;
  16.  
    //show PrinterDialog and print pdf document
  17.  
    if (dlg.ShowDialog() == DialogResult.OK)
  18.  
    printDoc.Print(); // C# Print PDF
  19.  
    }
学新通

在 C# 中读取 PDF 文件并从中提取文本

  1.  
    /// <summary>
  2.  
    /// Read PDF File and Extract Text From it in C#
  3.  
    /// </summary>public void ExtractText()
  4.  
    {
  5.  
    //Initialize the SDK library
  6.  
    //You have to call this function before you can call any PDF processing functions.
  7.  
    PdfCommon.Initialize();
  8.  
     
  9.  
    //Open and load a PDF document from a file.
  10.  
    using (var doc = PdfDocument.Load(@"c:\test001.pdf")) // C# Read PDF File
  11.  
    {
  12.  
    foreach (var page in doc.Pages)
  13.  
    {
  14.  
    //Gets number of characters in a page or -1 for error.
  15.  
    //Generated characters, like additional space characters, new line characters, are also counted.
  16.  
    int totalCharCount = page.Text.CountChars;
  17.  
     
  18.  
    //Extract text from page to the string
  19.  
    string text = page.Text.GetText(0, totalCharCount);
  20.  
     
  21.  
    page.Dispose();
  22.  
    }
  23.  
    }
  24.  
    }
学新通
 

Pdfium.Net SDK 允许开发人员轻松地从几乎任何 PDF 文件中提取全文。

如何从 PDF 中提取文本坐标

  1.  
    /// <summary>
  2.  
    /// Extract Text Coordinates from Pdf in C# using PDF Library
  3.  
    /// </summary>
  4.  
    public void ExtractTextInfo()
  5.  
    {
  6.  
    //Initialize the SDK library
  7.  
    //You have to call this function before you can call any PDF processing functions.
  8.  
    PdfCommon.Initialize();
  9.  
     
  10.  
    //Open and load a PDF document from a file.
  11.  
    using (var doc = PdfDocument.Load(@"c:\test001.pdf")) // C# Read PDF File
  12.  
    {
  13.  
    //Get second page from document
  14.  
    using (var page = doc.Pages[1])
  15.  
    {
  16.  
    //Extract text information structure from the page
  17.  
    // 10 - Index for the start characters
  18.  
    // 25 - Number of characters to be extracted
  19.  
    var textInfo = page.Text.GetTextInfo(10, 25);
  20.  
     
  21.  
    //Gets text from textInfo strtucture
  22.  
    string text = textInfo.Text;
  23.  
     
  24.  
    //Gets a collection of rectangular areas bounding specified text.
  25.  
    var rects = textInfo.Rects;
  26.  
    }
  27.  
    }
  28.  
    }
学新通
 

Pdfium.Net SDK 还允许开发人员轻松地从任何 PDF 文件中提取文本坐标。

如何在 PDF 文件中搜索文本

  1.  
    /// <summary>
  2.  
    /// Search for a Text in a PDF File in C# With Pdfium.Net SDK Library
  3.  
    /// </summary>
  4.  
    public void Search()
  5.  
    {
  6.  
    //Open PDF document
  7.  
    using (var doc = PdfDocument.Load(@"d:\0\test_big.pdf")) // Read PDF document and enumerate pages
  8.  
    {
  9.  
    //Enumerate pages
  10.  
    foreach(var page in doc.Pages)
  11.  
    {
  12.  
    var found = page.Text.Find("text for search", FindFlags.MatchWholeWord, 0);
  13.  
    if (found == null)
  14.  
    return; //nothing found
  15.  
    do
  16.  
    {
  17.  
    var textInfo = found.FoundText;
  18.  
    foreach(var rect in textInfo.Rects)
  19.  
    {
  20.  
    float x = rect.left;
  21.  
    float y = rect.top;
  22.  
    //...
  23.  
    }
  24.  
    } while (found.FindNext());
  25.  
     
  26.  
    page.Dispose();
  27.  
    }
  28.  
    }
  29.  
    }
学新通
 

此示例说明如何使用简单的 C# 代码和 PDF 库在 PDF 文档中搜索文本。

如何将PDF文件分割成小文件

  1.  
    /// <summary>
  2.  
    /// Split PDF in C# using PDF Library
  3.  
    /// </summary>
  4.  
    public void SplitDocument()
  5.  
    {
  6.  
    //Initialize the SDK library
  7.  
    //You have to call this function before you can call any PDF processing functions.
  8.  
    PdfCommon.Initialize();
  9.  
     
  10.  
    //Open and load a PDF document from a file.
  11.  
    using (var sourceDoc = PdfDocument.Load(@"c:\test001.pdf")) // C# Read PDF File
  12.  
    {
  13.  
    //Create one PDF document for pages 1-5.
  14.  
    using (var doc = PdfDocument.CreateNew())
  15.  
    {
  16.  
    //Import pages from source document
  17.  
    doc.Pages.ImportPages(sourceDoc, "1-5", 0);
  18.  
    //And save it to doc1.pdf
  19.  
    doc.Save(@"c:\doc1.pdf", SaveFlags.Incremental);
  20.  
    }
  21.  
     
  22.  
    //Create another PDF document for pages 5-10.
  23.  
    using (var doc = PdfDocument.CreateNew())
  24.  
    {
  25.  
    //Also import pages
  26.  
    doc.Pages.ImportPages(sourceDoc, "5-10", 0);
  27.  
    //And save them too
  28.  
    doc.Save(@"c:\doc2.pdf", SaveFlags.Incremental);
  29.  
    }
  30.  
    }
  31.  
    }
学新通
 

下面的代码示例演示了如何使用 C# PDF 库来拆分 PDF 文档。

在 C# 中将多个 PDF 文件中的选定页面合并为一个

  1.  
    /// <summary>
  2.  
    /// Merge PDFs in C# using PDF Library
  3.  
    /// </summary>
  4.  
    public void MergePdf()
  5.  
    {
  6.  
    //Initialize the SDK library
  7.  
    //You have to call this function before you can call any PDF processing functions.
  8.  
    PdfCommon.Initialize();
  9.  
     
  10.  
    //Open and load a PDF document in which will be merged other files
  11.  
    using (var mainDoc = PdfDocument.Load(@"c:\test001.pdf")) // C# Read source PDF File #1
  12.  
    {
  13.  
    //Open one PDF document.
  14.  
    using (var doc = PdfDocument.Load(@"c:\doc1.pdf")) //Read PDF File #2
  15.  
    {
  16.  
    //Import all pages from document
  17.  
    mainDoc.Pages.ImportPages(
  18.  
    doc,
  19.  
    string.Format("1-{0}", doc.Pages.Count),
  20.  
    mainDoc.Pages.Count
  21.  
    );
  22.  
    }
  23.  
     
  24.  
    //Open another PDF document.
  25.  
    using (var doc = PdfDocument.Load(@"c:\doc2.pdf"))
  26.  
    {
  27.  
    //Import all pages from document
  28.  
    mainDoc.Pages.ImportPages(
  29.  
    doc,
  30.  
    string.Format("1-{0}", doc.Pages.Count),
  31.  
    mainDoc.Pages.Count
  32.  
    );
  33.  
    }
  34.  
     
  35.  
    mainDoc.Save(@"c:\ResultDocument.pdf", SaveFlags.NoIncremental);
  36.  
     
  37.  
     
  38.  
    }
  39.  
    }
学新通
C#
复制
 

使用 C# PDF 库,您不仅可以将多个 PDF 文件合并为一个文件,还可以从源文件中选择特定页面并将它们组合在一个 PDF 文档中。

上面的代码显示了如何使用ImportPages操作来完成它。

如何以编程方式从 PDF 字段填充和提取数据

  1.  
    /// <summary>
  2.  
    /// Filling Editable PDF Fields and Extracting Data From Them using .Net PDF Library
  3.  
    /// </summary>
  4.  
    private void btnTest_Click(object sender, RoutedEventArgs e)
  5.  
    {
  6.  
    var forms = new PdfForms();
  7.  
    var doc = PdfDocument.Load(@"c:\test.pdf", forms); // C# Read PDF Document
  8.  
    //doc.FormFill is equal to forms and can be used to get access to acro forms as well;
  9.  
     
  10.  
    int i = 0;
  11.  
    foreach(var field in forms.InterForm.Fields)
  12.  
    {
  13.  
    if(field.FieldType == Patagames.Pdf.Enums.FormFieldTypes.FPDF_FORMFIELD_TEXTFIELD)
  14.  
    {
  15.  
    field.Value = "This is a field #" ( i);
  16.  
    }
  17.  
    }
  18.  
    }
学新通
 

此示例代码演示了如何使用 .Net PDF 库以编程方式填写 pdf 文档中的所有可编辑表单。

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhggbfkc
系列文章
更多 icon
同类精品
更多 icon
继续加载