PDF 生成模板编辑器
开源可视化 PDF 生成引擎,支持自定义模板,并提供面向开发者的 API,灵活构建文档生成流程。
“ComPDFKit_Tools” 文件夹包含了可引导快速集成 ComPDF SDK 的 UI 组件。我们还使用这个 UI 组件库构建了6个独立的功能程序,分别是 Viewer,Annotations,ContentEditor,Forms,DocsEditor 和 DigitalSignature。此外,我们还开发了一个叫做 PDFViewer 的程序,它集成了所有以上提到的示例功能供您参考。在本节中,我们会从以下几部分介绍如何使用它:
"ComPDFKit_Tools" 概述: 展示文件夹结构和对应的功能板块。
UI 组件:介绍 UI 组件以及如何轻松快速地使用它们。
使用 CPDFListView 类: 包含使用 CPDFListView的详细步骤。
了解更多:展示适用于 iOS 开发平台的特殊代码结构和功能。
如何使用默认 UI 制作 iOS 应用程序
ComPDF SDK 的 "ComPDFKit_Tools" 是示例工程的 UI 组件库,它包括应用程序的基本 UI。要使用 ComPDF SDK 的默认 UI 制作自定义的 iOS 应用程序 UI,可在 Lib/Tools/Objective-C/ 或 Lib/Tools/Swift/ 路径中找到对应开发语言的 "ComPDFKit_Tools" 文件,并引入到你的工程中。

"ComPDFKit_Tools" 中有九个模块: "Common"、"Viewer"、"Annotations"、"ContentEditor"、"Forms"、"DocsEditor"、"DigitalSignature"、"Security" 和 "Watermark"。如下表所示,每个模块都包含对应 PDF 功能的代码和 UI 组件。
| Folder | Subfolder | Description |
|---|---|---|
| Common | - | 包含 Viewer、Annotations、ContentEditor、Forms 和 DocsEditor 模块的可复用的 UI 组件以及视图控制器。 |
| Viewer | PDFBookmark | 包含实现文档书签列表和页面跳转的 UI 组件以及视图控制器。 |
| PDFOutline | 包含实现文档大纲和大纲跳转的 UI 组件以及视图控制器。 | |
| PDFSearch | 包含实现文档搜索功能和生成搜索列表的 UI 组件以及视图控制器。 | |
| PDFMoreMenu | 包含实现阅读设置、获取文档信息、文档分享、文档选择和文档页面编辑的 UI 组件以及视图控制器。 | |
| PDFThumbnail | 包含实现文档缩略图的 UI 组件以及视图控制器。 | |
| Annotations | PDFAnnotationBar | 包含实现修改注释属性和返回/撤销返回注释的操作的工具栏。 |
| PDFAnnatationList | 包含实现注释列表的 UI 组件以及视图控制器。 | |
| PDFAnnotationProperties | 包含实现修改注释属性的属性面板以及视图控制器。 | |
| ContentEditor | PDFEditBar | 包含实现 PDF 文字编辑、图片编辑和返回/撤销返回对文字/图片的操作的工具栏。 |
| PDFEditProperties | 包含实现文档文字编辑和图片编辑的属性面板以及视图控制器。 | |
| Forms | PDFFormBar | 包含实现修改表格属性和返回/撤销返回对表格属性的操作的工具栏。 |
| PDFFormProperties | 包含实现修改表格属性的属性面板以及视图控制器。 | |
| DocsEditor | PDFPageEdit | 包含实现文档页面编辑的 UI 控件以及视图控制器。 |
| PDFPageEditBar | 实现新页面创建、页面替换、旋转、提取和删除的工具栏。 | |
| PDFPageEditInsert | 实现空白页面创建和 PDF 文档页面插入的 UI 组件以及视图控制器。 | |
| DigitalSignature | PDFDigitalSignatureBar | 包含创建 Form 签名域、填写数字签名和验证数字签名的工具栏。 |
| PPDFDigitalSignatureProperties | 包含实现修改数字签名属性和外观的属性面板以及视图控制器。 | |
| Watermark | WatermarkSetting | 包含实现图片水印和文字水印属性的属性面板,以及实现水印平铺的视图。 |
| Security | - | 包含实现添加和移除文档开启密码和设置权限密码的属性面板。 |
本节主要介绍 "ComPDFKit_Tools" 的 UI 组件与 API 配置之间的联系,这不仅可以帮助你快速上手 ComPDFKit 默认的 UI,还可以帮助你查看相关的API配置。这些 UI 组件可以被使用和修改,以创建你自定义的 UI。
Part 1:

上图的左图显示了与 Viewer 模块 API 相关的主要 UI 组件,这些也是 "ComPDFKit_Tools" 的基本 UI 组件。上图的右图显示了与 Text search 的 API 相关的主要 UI 组件。以下是 UI 组件和 APIs 之间对应的详细情况。
| 编号 | 功能模块 | 功能 | 描述 |
|---|---|---|---|
| 1-1 | PDFListView | CPDFListView | 见 2.5.3 使用 CPDFListView 类。 |
| 1-2 | Tools switcher | CPDFToolsViewController | 功能模式切换。 |
| 1-3 | Text search | CSearchToolbar | 进入搜索模式。 |
| 1-4 | BOTA | CPDFBOTAViewController | 进入大纲、书签和注释模块。 |
| 1-5 | More | CPDFPopMenuView | 进入 More 菜单。 |
| 1-6 | Thumbnails | CPDFThumbnailViewController | 进入文档缩略图. |
| 1-7 | Back | CNavBarButtonItem | 退出预览界面。 |
| 1-8 | Tools | CToolModel | 功能模式切换的枚举类型。 |
| 1-9 | Slider | CPDFSlider | 允许用户快速跳过页面。 |
| 1-10 | Search toolbar | CSearchToolbar | 允许关键字搜索。 |
| 1-11 | Search list | CPDFSearchResultsViewController | 搜索结果列表。 |
| 1-12 | Search setting | CSearchSettingViewController | 允许用户设置搜索属性。 |
| 1-13 | Replace toolbar | CSearchToolbar | 允许关键字搜索和替换。 |
| 1-14 | Replace menu | CSearchContentView | 允许替换单个关键字的上下文菜单。 |
以下是初始化 Tools switcher (编号2) 的方法:
// CPDFToolsViewController:初始化CToolModel类型的数组toolArrays
// toolArrays:保存Tools展示模式的类型和数量
let toolsVc = CPDFToolsViewController(customizeWithToolArrays: [NSNumber(value: CPDFToolFunctionTypeState.viewer.rawValue),
NSNumber(value: CPDFToolFunctionTypeState.edit.rawValue),
NSNumber(value: CPDFToolFunctionTypeState.annotation.rawValue),
NSNumber(value: CPDFToolFunctionTypeState.form.rawValue)])// CPDFToolsViewController:初始化CToolModel类型的数组toolArrays
// toolArrays:保存Tools展示模式的类型和数量
CPDFToolsViewController * toolsVc = [[CPDFToolsViewController alloc] initCustomizeWithToolArrays:@[@(CToolModelViewer),@(CToolModelEdit),@(CToolModelAnnotation),@(CToolModelForm)]];以下是初始化 BOTA(编号4)的方法:
// CPDFBOTAViewController:初始化CPDFListview对象 - pdfView,和CPDFBOTATypeState类型数组 - navArrays
// navArrays:保存BOTA展示的列表类型和数量
let navArrays: [CPDFBOTATypeState] = [.CPDFBOTATypeStateOutline,
.CPDFBOTATypeStateBookmark,
.CPDFBOTATypeStateAnnotation]
if(self.pdfListView != nil) {
let botaViewController = CPDFBOTAViewController(customizeWith: self.pdfListView!, navArrays: navArrays)
}// CPDFBOTAViewController:初始化CPDFListview对象 - pdfView,和CPDFBOTATypeState类型数组 - navArrays
// navArrays:保存BOTA展示的列表类型和数量
CPDFBOTAViewController *botaVC = [[CPDFBOTAViewController alloc] initCustomizeWithPDFView:pdfListView navArrays:@[@(CPDFBOTATypeStateOutline),@(CPDFBOTATypeStateBookmark),@(CPDFBOTATypeStateAnnotation)]];Part 2:

上面的左图展示了与CPDFBOTAViewController API 关联的主要 UI 组件。上面的右图展示了与CPDFPopMenuView API 关联的主要UI组件。请参阅以下 UI 组件与 API 的对应情况。
| 编号 | 功能模块 | 功能 | 描述 |
|---|---|---|---|
| 2-1 | Outlines | CPDFOutlineViewController | 进入文档大纲。 |
| 2-2 | Bookmarks | CPDFBookmarkViewController | 进入文档书签列表。 |
| 2-3 | Annotations | CPDFAnnotationViewController | 进入文档注释列表。 |
| 2-4 | View setting | CPDFDisplayViewController | 进入文档页面设置。 |
| 2-5 | Page edit | CPDFPageEditViewController | 进入文档页面编辑。 |
| 2-6 | Document info | CPDFInfoViewController | 进入文档信息列表。 |
| 2-7 | Save | - | 文档保存。 |
| 2-8 | Save as Flattened | - | 文档另存为Flattened副本。 |
| 2-9 | Share | - | 文档分享。 |
| 2-10 | Open | - | 选择文档。 |
Part 3:

上面的左图展示了与 Annotations API 关联的主要 UI 组件。上面的右图展示了与ContentEditor API 关联的主要 UI 组件。请参阅以下 UI 组件与 API 的对应情况。
| Number | Name | Functionality | Description |
|---|---|---|---|
| 3-1 | Annotation toolbar | CPDFAnnotationToolbar | 修改各类注释属性和返回/撤销返回对注释属性的操作。 |
| 3-2 | Properties | - | 打开属性面板。 |
| 3-3 | Edit toolbar | CPDFEditToolBar | 修改 PDF 文本/图片属性和返回/撤销返回对 PDF 文字/图片属性的操作。 |
以下是初始化CAnnotationManage(编号19)的方法:
// 初始化CAnnotationManage对象。
var annotationManage = CAnnotationManage.init(pdfListView: self.pdfListView!)
// 设置CAnnotationManage对象管理当前选中的注释。
annotationManage.setAnnotStyle(from: pdfListView.activeAnnotations)
// 设置CAnnotationManage对象管理默认的注释。
annotationManage.setAnnotStyle(from: pdfListView.annotationMode)
// 设置CAnnotationManage对象去修改当前选中的注释的属性。
annotationManage.refreshPage(with: annotStyle.annotations)
// CPDFAnnotationToolbar:初始化一个CAnnotationManage对象annotationManage。
// annotationManage:管理注释属性单独model数据。
let annotationBar = CPDFAnnotationToolBar.init(annotationManage: annotationManage)// 初始化CAnnotationManage对象。
CAnnotationManage *annotationManage = [[CAnnotationManage alloc] initWithPDFView:pdfListView];
// 设置CAnnotationManage对象管理当前选中的注释。
[annotManage setAnnotStyleFromAnnotations:pdfListView.activeAnnotations];
// 设置CAnnotationManage对象管理默认的注释。
[annotManage setAnnotStyleFromMode:pdfListView.annotationMode];
// 设置CAnnotationManage对象去修改当前选中的注释的属性。
[annotManage refreshPageWithAnnotations:annotStyle.annotations];
// CPDFAnnotationToolbar:初始化一个CAnnotationManage对象annotationManage。
// annotationManage:管理注释属性单独model数据。
CPDFAnnotationToolbar *annotationToolbar = [[CPDFAnnotationToolbar alloc] initAnnotationManage:annotationManage];Part 4:

上面的左图展示了与 Forms API 关联的主要 UI 组件。上面的右图展示了与DocsEditor API 关联的主要 UI 组件。请参阅以下 UI 组件与 API 的对应情况。
| Number | Name | Functionality | Description |
|---|---|---|---|
| 4-1 | Form toolbar | CPDFFormToolbar | 修改 forms 属性和返回/撤销返回对 forms 属性的操作。 |
| 4-2 | PageEdit toolbar | CPageEditToolBar | 对页面进行编辑。 |
Part 5:

上面的左图展示了与CPDFNoteViewController API 关联的主要 UI 组件。上面的右图展示了与CPDFFormCheckBoxViewController API 关联的主要 UI 组件。请参阅以下 UI 组件与 API 的对应情况。
| Number | Name | Functionality | Description |
|---|---|---|---|
| 5-1 | Note | CPDFNoteViewController | 修改便签注释的属性。 |
| 5-2 | Check box | CPDFFormCheckBoxViewController | 修改 Check box 表单的属性。 |
Part 6:

上面的上图图展示了与Digital Signature API 关联的主要 UI 组件。请参阅以下 UI 组件与 API 的对应情况。
| Number | Name | Functionality | Description |
|---|---|---|---|
| 6-1 | Digital Signature Toolbar | CPDFDigitalSignatureToolBar | 增加和验证数字签名的属性。 |
| 6-2 | Customize Signature Preview | CAddSignatureViewController | 自定义数字签名外观的属性。 |
| 6-3 | Sign Info | CAddSignatureViewController | 验证签名后的信息显示属性 |
CPDFListView 是 CPDFView的一个子类,它包含预览、注释、表单、PDF 内容编辑等功能的操作。
ToolModel 功能模式切换
要切换到不同的功能模式,只需通过CPDFListView::toolModel设置相应的功能模式。然后,CPDFListView将显示不同的功能的 UI 界面。下表展示的是,在各功能模式下,对注释和表单的操作的支持。
| CToolModel | Description |
|---|---|
| CToolModelViewer | - 支持文本选取、文本复制。 - 支持表单填写和签署。 - 不支持添加、选取、移动、删除注释,且不支持编辑注释的属性。 - 不支持表单的添加、选取、移动、和删除,且不支持编辑表单的属性。 |
| CToolModelEdit | - 不支持添加、选取、移动、删除注释,且不支持编辑注释的属性。 - 不支持表单填写。 - 不支持表单的添加、选取、移动、和删除,且不支持编辑表单的属性。 |
| CToolModelAnnotation | - 支持文本选取、文本复制。 - 支持添加、选取、移动、删除注释,且支持编辑注释的属性。 - 不支持表单填写 - 不支持表单的添加、选取、移动、和删除,且不支持编辑表单的属性。 |
| CToolModelForm | - 支持表单的添加、选取、移动、和删除,且不支持编辑表单的属性。 - 不支持文本选取、文本复制。 - 不支持添加、选取、移动、删除注释,且不支持编辑注释的属性。 - 不支持表单填写。 |
| CToolModelPageEdit | - 不支持注释和表单的任何操作。 |
添加注释或表单模式:CPDFViewAnnotationMode
当进入注释模式或表单模式时,只需在CPDFListView::annotationMode中设置相应的注释模式(表单是一种注释),在CPDFListView中创建指定的注释和表单。下表展示了每个值代表了什么注释类型。
| CPDFViewAnnotationMode | 描述 |
|---|---|
| CPDFViewAnnotationModeNone | 进入注释模式,但未选中注释类型。 |
| CPDFViewAnnotationModeNote | 便签注释。 |
| CPDFViewAnnotationModeHighlight | 标记类注释:高亮。 |
| CPDFViewAnnotationModeUnderline | 标记类注释:下划线。 |
| CPDFViewAnnotationModeStrikeout | 标记类注释:删除线。 |
| CPDFViewAnnotationModeSquiggly | 标记类注释:波浪线。 |
| CPDFViewAnnotationModeCircle | 形状类注释:圆形。 |
| CPDFViewAnnotationModeSquare | 形状类注释:矩形。 |
| CPDFViewAnnotationModeArrow | 形状类注释:箭头。 |
| CPDFViewAnnotationModeLine | 形状类注释:直线。 |
| CPDFViewAnnotationModeInk | 手绘注释。 |
| CPDFViewAnnotationModePencilDrawing | Apple Pencil 注释。 |
| CPDFViewAnnotationModeFreeText | 文字注释。 |
| CPDFViewAnnotationModeSignature | 签名。 |
| CPDFViewAnnotationModeStamp | 图章注释。 |
| CPDFViewAnnotationModeImage | 图片注释。 |
| CPDFViewAnnotationModeLink | 链接注释。 |
| CPDFViewAnnotationModeSound | 音频注释。 |
| CPDFViewFormModeText | 文本表单域。 |
| CPDFViewFormModeCheckBox | 复选框表单域。 |
| CPDFViewFormModeRadioButton | 单选框表单域。 |
| CPDFViewFormModeCombox | 列表框表单域。 |
| CPDFViewFormModeList | 下拉菜单表单域。 |
| CPDFViewFormModeButton | 动作按钮表单域。 |
| CPDFViewFormModeSign | 签名表单域。 |
注:
在选中注释模式或选中某一注释类型时,不支持滚动PDFCPDFListView视图。
只有在 iOS 13.0 版本以上的设备才支持CPDFViewAnnotationModePencilDrawing模式。
返回/撤销返回 PDF 注释的操作
表单和CPDFListView的注释的返回/撤销返回操作,这些操作实际上涉及到使用 KVO 观察注释属性。在初始化CPDFListView时,只需调用CPDFListView::registerAsObserver。NSUndoManager对象将使用keysForValuesToObserveForUndo方法来观察已添加的注释和表单的相关属性。
func startObservingNotes(newNotes: [CPDFAnnotation]) {
if self.notes == nil {
self.notes = []
}
for note in newNotes {
if self.notes?.contains(note) == false {
self.notes?.append(note)
}
let keys: Set<String> = note.keysForValuesToObserveForUndo()
for key in keys {
note.addObserver(self, forKeyPath: key, options: [.new, .old], context: &CPDFAnnotationPropertiesObservationContext)
}
}- (void)startObservingNotes:(NSArray *)newNotes {
if (!self.notes) {
self.notes = [NSMutableArray array];
}
for (CPDFAnnotation *note in newNotes) {
if (![self.notes containsObject:note]) {
[self.notes addObject:note];
}
for (NSString *key in [note keysForValuesToObserveForUndo]) {
[note addObserver:self forKeyPath:key options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:&CPDFAnnotationPropertiesObservationContext];
}
}
}注: 切换文档以及移除对应的注释或表单时,一定要留意移除注释属性的 KVO 监听。
下面的代码展示了如何撤消和重做注释和表单的编辑
@objc func buttonItemClicked_undo(_ button: UIButton) {
if self.annotManage?.pdfListView?.undoPDFManager != nil && ((self.annotManage?.pdfListView?.canUndo()) == true) {
self.annotManage?.pdfListView?.undoPDFManager?.undo()
}
}
@objc func buttonItemClicked_redo(_ button: UIButton) {
if self.annotManage?.pdfListView?.undoPDFManager != nil && (self.annotManage?.pdfListView?.canRedo()) == true {
self.annotManage?.pdfListView?.undoPDFManager?.redo()
}
}- (void)buttonItemClicked_undo:(UIButton *)button {
if(self.annotManage.pdfListView.undoPDFManager && [self.annotManage.pdfListView canUndo]) {
[self.annotManage.pdfListView.undoPDFManager undo];
}
}
- (void)buttonItemClicked_redo:(UIButton *)button {
if(self.annotManage.pdfListView.undoPDFManager && [self.annotManage.pdfListView canRedo]) {
[self.annotManage.pdfListView.undoPDFManager redo];
}
}实现委托函数
关于实现委托函数,请参考 “CPDFViewController” 文件中的以下方法.
@optional
// 上下文菜单选项。
@objc optional func PDFListView(_ pdfListView: CPDFListView, customizeMenuItems menuItems: [UIMenuItem], forPage page: CPDFPage, forPagePoint pagePoint: CGPoint) -> [UIMenuItem]
// 空白区域点击
@objc optional func PDFListViewPerformTouchEnded(_ pdfListView: CPDFListView)
// 功能模式切换。
@objc optional func PDFListViewChangedToolMode(_ pdfListView: CPDFListView, forToolMode toolMode: Int)
// 注释类型切换。
@objc optional func PDFListViewChangedToolMode(_ pdfListView: CPDFListView, forToolMode toolMode: Int)
// 选中注释发生变化
@objc optional func PDFListViewChangeatioActiveAnnotations(_ pdfListView: CPDFListView, forActiveAnnotations annotations: [CPDFAnnotation])
// 文档注释有发生变化。
@objc optional func PDFListViewAnnotationsOperationChange(_ pdfListView: CPDFListView)
// 编辑注释内容。
@objc optional func PDFListViewEditNote(_ pdfListView: CPDFListView, forAnnotation annotation: CPDFAnnotation)
// 修改注释属性。
@objc optional func PDFListViewEditProperties(_ pdfListView: CPDFListView, forAnnotation annotation: CPDFAnnotation)
// 取消音频添加。
@objc optional func PDFListViewPerformPlay(_ pdfView: CPDFListView, forAnnotation annotation: CPDFSoundAnnotation)
// 取消音频添加。
@objc optional func PDFListViewPerformCancelMedia(_ pdfView: CPDFListView, atPoint point: CGPoint, forPage page: CPDFPage)
// 音频是否正在录音
@objc optional func PDFListViewPerformRecordMedia(_ pdfView: CPDFListView, atPoint point: CGPoint, forPage page: CPDFPage)
// Sound annotations: If it's recording
@objc optional func PDFListViewerTouchEndedIsAudioRecordMedia(_ pdfListView: CPDFListView) -> Bool
// 添加图章注释。
@objc optional func PDFListViewPerformAddStamp(_ pdfView: CPDFListView, atPoint point: CGPoint, forPage page: CPDFPage)
// 添加图片。
@objc optional func PDFListViewPerformAddImage(_ pdfView: CPDFListView, atPoint point: CGPoint, forPage page: CPDFPage)
// 添加签名。
@objc optional func PDFListViewPerformSignatureWidget(_ pdfView: CPDFListView, forAnnotation annotation: CPDFSignatureWidgetAnnotation)
// 修改PDF文字属性。
@objc optional func PDFListViewContentEditProperty(_ pdfListView: CPDFListView, point: CGPoint)@optional
// 上下文菜单选项。
- (NSArray<UIMenuItem *> *)PDFListView:(CPDFListView *)pdfListView customizeMenuItems:(NSArray *)MenuItems forPage:(CPDFPage *)page forPagePoint:(CGPoint)pagePoint;
// 空白区域点击。
- (void)PDFListViewPerformTouchEnded:(CPDFListView *)pdfListView;
// 功能模式切换。
- (void)PDFListViewChangedToolMode:(CPDFListView *)pdfListView forToolMode:(CToolModel)toolMode;
// 注释类型切换。
- (void)PDFListViewChangedAnnotationType:(CPDFListView *)pdfListView forAnnotationMode:(CPDFViewAnnotationMode)annotationMode;
// 选中注释发生变化。
- (void)PDFListViewChangeatioActiveAnnotations:(CPDFListView *)pdfListView forActiveAnnotations:(NSArray<CPDFAnnotation *> *)annotations;
// 文档注释有发生变化。
- (void)PDFListViewAnnotationsOperationChange:(CPDFListView *)pdfListView;
// 编辑注释内容。
- (void)PDFListViewEditNote:(CPDFListView *)pdfListView forAnnotation:(CPDFAnnotation *)annotation;
// 修改注释属性。
- (void)PDFListViewEditProperties:(CPDFListView *)pdfListView forAnnotation:(CPDFAnnotation *)annotation;
// 播放音频注释。
- (void)PDFListViewPerformPlay:(CPDFListView *)pdfView forAnnotation:(CPDFSoundAnnotation *)annotation;
// 取消音频添加。
- (void)PDFListViewPerformCancelMedia:(CPDFListView *)pdfView atPoint:(CGPoint)point forPage:(CPDFPage *)page;
// 音频注释开始录音。
- (void)PDFListViewPerformRecordMedia:(CPDFListView *)pdfView atPoint:(CGPoint)point forPage:(CPDFPage *)page;
// 音频是否正在录音。
- (BOOL)PDFListViewerTouchEndedIsAudioRecordMedia:(CPDFListView *)pdfListView;
// 添加图章注释。
- (void)PDFListViewPerformAddStamp:(CPDFListView *)pdfView atPoint:(CGPoint)point forPage:(CPDFPage *)page;
// 添加图片注释。
- (void)PDFListViewPerformAddImage:(CPDFListView *)pdfView atPoint:(CGPoint)point forPage:(CPDFPage *)page;
// 添加签名。
- (void)PDFListViewPerformSignatureWidget:(CPDFListView *)pdfView forAnnotation:(CPDFSignatureWidgetAnnotation *)annotation;
// 修改PDF文字属性。
- (void)PDFListViewContentEditProperty:(CPDFListView *)pdfListView point:(CGPoint)point;注: 如果需要切换或取消选择注释,需要调用CPDFListView::updateActiveAnnotations才能让CPDFListView::PDFListViewChangeatioActiveAnnotations: forActiveAnnotations:得到响应。
自 iOS 版本 10.1 起,ComPDF 公开了特定的 API,以便开箱即用地将 ComPDF 与 SwiftUI 结合使用。CPDFViewController.swift这使得在 SwiftUI 应用程序中处理 ComPDF 变得更容易,并且如果您想走 SwiftUI 路线,它不需要您自己包装。
导入资源文件,ComPDFKit_Tools iOS样例项目自带默认UI设计,包括应用程序的基本UI和功能模块UI,使用ComPDF SDK实现,并已发布。还包括一个 CPDFViewController 视图控制器,它包含随时可用的UI模块实现。

通过将PDF文档拖放到项目中,将它们添加到应用程序中。在显示的对话框中,选择Finish以接受默认的集成选项。您可以使用TestFile文件夹中的文档作为示例。
桥接CPDFSwiftViewController到SwiftUI
首先,声明' CPDFSwiftViewController '结构-它符合' UIViewControllerRepresentable'协议-这样你就可以从UIKit桥接到SwiftUI。
struct CPDFSwiftViewController: UIViewControllerRepresentable {
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
class Coordinator: NSObject,CPDFViewBaseControllerDelete {
var myview: CPDFSwiftViewController
init(_ myview: CPDFSwiftViewController) {
self.myview = myview
}
// MARK: - CPDFViewBaseControllerDelete
func PDFViewBaseControllerDissmiss(_ baseControllerDelete: CPDFViewBaseController) {
baseControllerDelete.dismiss(animated: true)
}
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func makeUIViewController(context: Context) -> CNavigationController {
CPDFKit.setLicenseKey("", secret: "")
let filePath = Bundle.main.path(forResource: "developer_guide_ios", ofType: "pdf")
let documentFolder = NSHomeDirectory().appending("/Documents/Samples")
if !FileManager.default.fileExists(atPath: documentFolder) {
try? FileManager.default.createDirectory(atPath: documentFolder, withIntermediateDirectories: true, attributes: nil)
}
let documentURL = URL(fileURLWithPath: documentFolder).appendingPathComponent((filePath as NSString?)?.lastPathComponent ?? "")
if !FileManager.default.fileExists(atPath: documentURL.path) {
try? FileManager.default.copyItem(at: URL(fileURLWithPath: filePath ?? ""), to: documentURL)
}
let configuration = CPDFConfiguration()
let thumbnail = CNavBarButtonItem(viewLeftBarButtonItem: .thumbnail)
let back = CNavBarButtonItem(viewLeftBarButtonItem: .back)
let search = CNavBarButtonItem(viewRightBarButtonItem: .search)
let bota = CNavBarButtonItem(viewRightBarButtonItem: .bota)
let more = CNavBarButtonItem(viewRightBarButtonItem: .more)
configuration.showleftItems = [back, thumbnail]
configuration.showRightItems = [search, bota, more]
let vc = CPDFViewController(filePath: documentURL.path, password: nil, configuration: configuration)
vc.delegate = context.coordinator;
let navController = CNavigationController(rootViewController: vc)
return navController
}
func updateUIViewController(_ uiViewController: CNavigationController, context: UIViewControllerRepresentableContext<CPDFSwiftViewController>) {
// Update the view controller.
}
}在SwiftUI中使用CPDFSwiftViewController
最后,在你的SwiftUI内容视图中使用'CPDFSwiftViewController :
struct ContentView: View {
@State var isPresented: Bool = false
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundColor(.accentColor)
Text("Hello, world!")
.toolbar {
ToolbarItem(placement: .bottomBar) {
Button("Click to open the sample PDF") {
isPresented = true
}
}
}
.sheet(isPresented: $isPresented) {
CPDFSwiftViewController()
}
}
.padding()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}如何处理 PDF 文件加载
要处理 PDF 文档加载,请参考***“CPDFViewBaseController.m”***文件中的以下方法。
- (void)reloadDocumentWithFilePath:(NSString *)filePath password:(nullable NSString *)password completion:(void (^)(BOOL result))completion;
<key>NSCameraUsageDescription</key>
<string>Your consent is required before you could access the function.</string>
<key>NSMicrophoneUsageDescription</key>
<string>Your consent is required before you could access the function.</string>
<key>NSPhotoLibraryAddUsageDescription</key>
<string>Your consent is required before you could access the function.</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>Your consent is required before you could access the function.</string>