UiPath Documentation
activities
latest
false
Document Understanding 活动
重要 :
请注意,此内容已使用机器翻译进行了部分本地化。 新发布内容的本地化可能需要 1-2 周的时间才能完成。

PDF 编码自动化 API(预览版)

适用于 PDF 活动包的编码自动化 API,涵盖读取、合并、转换和操作 PDF 和 XPS 文件。

UiPath.PDF.Activities

编码工作流 API,用于读取、合并、转换和操作 PDF 和 XPS 文件。这些 API 在设计编码自动化时可用。有关编码自动化以及如何使用 API 设计自动化的简介,请参阅编码自动化

  • 服务访问者: pdf (类型IPdfService
  • 所需的包: "UiPath.PDF.Activities": "*"依赖项中的project.json

自动导入的命名空间

安装 PDF 包后,以下命名空间将自动在编码工作流中可用:

  • UiPath.PDF.Activities.Api
  • UiPath.PDF
  • UiPath.PDF.Activities.PDF.Enums
  • UiPath.Platform.ResourceHandling
  • System.Net.Mail

服务概述

pdf 服务将每个操作公开为直接方法调用。没有可打开的连接或作用域。大多数操作提供两种形式:一种接受文件路径作为string ,另一种接受IResource (例如,存在路径获取本地文件或文件夹活动的输出)。直接在服务访问器上调用方法:

string text = pdf.ReadPdfText(@"C:\docs\report.pdf");
string text = pdf.ReadPdfText(@"C:\docs\report.pdf");

创建或转换文件的方法会返回指向输出的 ILocalResource。当 outputFileNamenull 时,服务将自动生成名称。受密码保护的文件可通过传递 password 参数打开。range 参数接受 "All" 或明确的范围,例如 "1-3,5"

读取文本

string ReadPdfText(string fileName, string password = null, string range = "All", bool preserveFormatting = false)

读取 PDF 文件中的文本。将 preserveFormatting 设置为 true,以保留文本布局。也提供了接受 IResource 的重载。

string ReadPdfWithOcr(string fileName, IOCRActivity ocrEngine, string range = "All", int degreeOfParallelism = 1, string password = null, ImageDpi imageDpi = ImageDpi.Medium)

使用提供的 OCR 引擎读取扫描 PDF 中的文本。从 OCR 服务中获取 OCR 引擎(例如 IOcrService.GetUiPathDocumentOcr)。degreeOfParallelism 设置一次处理的页面数量。仅限 Windows。

string ReadXpsText(string fileName, string range = "All")

读取 XPS 文件中的文本。也提供了接受 IResource 的重载。仅限 Windows。

string ReadXpsWithOcr(string fileName, IOCRActivity ocrEngine, string range = "All", int degreeOfParallelism = 1, string password = null)

使用提供的 OCR 引擎读取 XPS 文件中的文本。仅限 Windows。

页面和文档操作

int GetPdfPageCount(string fileName, string password = null)

返回 PDF 文件的页数。也提供了接受 IResource 的重载。

ILocalResource JoinPdf(string[] fileList, string outputFileName = null)

按给定顺序将多个 PDF 文件合并为一个 PDF。也提供了接受 IResource[] 的重载。

ILocalResource ExtractPdfPageRange(string fileName, string range, string password = null, string outputFileName = null)

将 PDF 中一系列页面(例如"1-3,5")提取到新 PDF 中。也提供了接受 IResource 的重载。

ILocalResource ExportPdfPageAsImage(string fileName, int pageNumber, string outputFileName, string password = null, ImageDpi imageDpi = ImageDpi.Medium)

将单个页面(从 1 开始的 pageNumber)导出为图像。图像格式从 outputFileName 扩展名中推断出来。也提供了接受 IResource 的重载。

创建和转换

ILocalResource CreatePdfFromImages(string[] fileList, string outputFileName = null)

从图像文件列表创建 PDF,每页一个图像。支持的输入格式:PNG、JPG、JPEG、TIF、TIFF、BMP。也提供了接受 IResource[] 的重载。

ILocalResource ConvertHtmlToPdf(string html, string outputFileName = null, bool keepBackground = true, ConvertToPdfOptions options = null)

将 HTML 内容转换为 PDF。将 keepBackground 设置为 true 可保留背景颜色和图形。

ILocalResource ConvertEmailToPdf(MailMessage email, string outputFileName = null, bool keepBackground = true, ConvertToPdfOptions options = null)

将电子邮件消息转换为 PDF。

ILocalResource ConvertTextToPdf(string text, string outputFileName = null, int fontSize = 12, TextAlignment textAlignment = TextAlignment.Justify, ConvertToPdfOptions options = null)

将纯文本转换为 PDF,可配置字体大小和文本对齐方式。

提取图像和附件

IEnumerable<ILocalResource> ExtractImagesFromPdf(string fileName, string password = null, ImageExtension imageExtension = ImageExtension.PNG, string outputFolderName = null)

从 PDF 中提取所有图像,并以选定的格式保存这些图像。也提供了接受 IResource 的重载。

IEnumerable<ILocalResource> ExtractAttachmentsFromPdf(string fileName, string password = null, string[] filter = null, string outputFolderName = null)

提取 PDF 中嵌入的文件。设置 filter,以限制为特定扩展名(例如 new[] { ".docx", ".xlsx" })。也提供了接受 IResource 的重载。

管理密码

ILocalResource ManagePdfPassword(string fileName, string oldUserPassword, string newUserPassword, string oldOwnerPassword, string newOwnerPassword, string outputFileName = null)

设置或删除 PDF 的用户和所有者密码。传递 null 或空字符串作为新密码以将其删除。也提供了接受 IResource 的重载。

选项

ConvertToPdfOptions

转换方法的共享布局和内容选项。

  • HeaderHtml String - 用作页标头的 HTML 代码片段。默认为 null(无标头)。
  • FooterHtml String - 用作页脚的 HTML 代码片段。默认为 null(无页脚)。
  • PaperSize PaperSize - 输出 PDF 的纸张大小。默认为 A4
  • Margin Int - 以点为单位的页边距。默认为 0
  • Scale Double - 页面呈现比例,介于 0.12 之间(含)。默认为 1.0

枚举引用

ImageExtension

ExtractImagesFromPdf使用。

  • PNG - PNG 图像格式。
  • JPEG - JPEG 图像格式。
  • TIFF - TIFF 图像格式。
  • BMP - BMP 图像格式。

ImageDpi

ReadPdfWithOcrExportPdfPageAsImage使用。

  • Low - 96 DPI。
  • Medium - 150 DPI。默认。
  • High - 270 DPI。

TextAlignment

ConvertTextToPdf使用。

  • Justify - 两端对齐。默认。
  • Left - 左对齐。
  • Right - 右对齐。
  • Center - 居中对齐。

PaperSize

ConvertToPdfOptions使用。

  • A4(默认)、A3A5A2A6 - ISO A 系列规格。
  • LetterLegalTabloidLedgerExecutiveStatement - 北美尺寸。
  • B4B5 - ISO B 系列规格。
  • Number10EnvelopeDLEnvelopeC5EnvelopeC4Envelope - 信封尺寸。

与活动的关系

编码 API 和 PDF XAML 活动在同一 Runtime 上运行,因此编码工作流可实现与PDF 活动包中的活动相同的读取、转换和操作行为。区别在于调用形式:服务接受纯文件路径或IResource输入,并直接返回stringintILocalResource结果,并且您使用普通try / catch而不是活动“出错时继续”选项。

常见模式

读取 PDF 的文本

此模式从 PDF 文件中读取某个页面范围的文本。ReadPdfText 提取的文本以 string 形式返回。

[Workflow]
public void Execute()
{
    string text = pdf.ReadPdfText(@"C:\docs\report.pdf", range: "1-3");
    Log(text);
}
[Workflow]
public void Execute()
{
    string text = pdf.ReadPdfText(@"C:\docs\report.pdf", range: "1-3");
    Log(text);
}

合并 PDF 并提取页面范围

此模式将两个 PDF 文件合并为一个 PDF 文件,然后从合并结果中提取页面范围。每个步骤都会返回一个指向输出文件的 ILocalResource

[Workflow]
public void Execute()
{
    var merged = pdf.JoinPdf(new[] { @"C:\docs\a.pdf", @"C:\docs\b.pdf" }, @"C:\docs\merged.pdf");
    var firstTwoPages = pdf.ExtractPdfPageRange(merged.LocalPath, "1-2", outputFileName: @"C:\docs\excerpt.pdf");
}
[Workflow]
public void Execute()
{
    var merged = pdf.JoinPdf(new[] { @"C:\docs\a.pdf", @"C:\docs\b.pdf" }, @"C:\docs\merged.pdf");
    var firstTwoPages = pdf.ExtractPdfPageRange(merged.LocalPath, "1-2", outputFileName: @"C:\docs\excerpt.pdf");
}

将 HTML 转换为带有边距的信特尺寸 PDF

此模式使用 ConvertToPdfOptions 设置信纸纸张大小和页边距,将 HTML 字符串转换为 PDF。ConvertHtmlToPdf 会返回指向已创建 PDF 的 ILocalResource

[Workflow]
public void Execute()
{
    var options = new ConvertToPdfOptions { PaperSize = PaperSize.Letter, Margin = 20 };
    var pdfFile = pdf.ConvertHtmlToPdf("<h1>Report</h1><p>Generated by a coded workflow.</p>", @"C:\docs\report.pdf", options: options);
}
[Workflow]
public void Execute()
{
    var options = new ConvertToPdfOptions { PaperSize = PaperSize.Letter, Margin = 20 };
    var pdfFile = pdf.ConvertHtmlToPdf("<h1>Report</h1><p>Generated by a coded workflow.</p>", @"C:\docs\report.pdf", options: options);
}

此页面有帮助吗?

连接

需要帮助? 支持

想要了解详细内容? UiPath Academy

有问题? UiPath 论坛

保持更新