CPDFDocument.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. //
  2. // CPDFDocument.h
  3. // ComPDFKit
  4. //
  5. // Copyright © 2014-2022 PDF Technologies, Inc. All Rights Reserved.
  6. //
  7. // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  8. // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  9. // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  10. // This notice may not be removed from this file.
  11. //
  12. #import <ComPDFKit/CPDFKitPlatform.h>
  13. extern NSNotificationName const CPDFDocumentDidUnlockNotification;
  14. extern NSErrorDomain const CPDFDocumentErrorDomain;
  15. NS_ERROR_ENUM(CPDFDocumentErrorDomain) {
  16. CPDFDocumentUnknownError = 1, // Unknown error.
  17. CPDFDocumentFileError = 2, // File not found or could not be opened.
  18. CPDFDocumentFormatError = 3, // File not in PDF format or corrupted.
  19. CPDFDocumentPasswordError = 4, // Password required or incorrect password.
  20. CPDFDocumentSecurityError = 5, // Unsupported security scheme.
  21. CPDFDocumentPageError = 6 // Page not found or content error.
  22. };
  23. /**
  24. * An enumeration that specifies document permissions status.
  25. */
  26. typedef NS_ENUM(NSInteger, CPDFDocumentPermissions) {
  27. /** The status that indicates no document permissions. */
  28. CPDFDocumentPermissionsNone = 0,
  29. /** The status that indicates user document permissions. */
  30. CPDFDocumentPermissionsUser,
  31. /** The status that indicates owner document permissions. */
  32. CPDFDocumentPermissionsOwner
  33. };
  34. typedef NS_OPTIONS(NSInteger, CPDFSearchOptions) {
  35. CPDFSearchCaseInsensitive = 0,
  36. /** If not set, it will not match case by default. */
  37. CPDFSearchCaseSensitive = 1,
  38. /** If not set, it will not match the whole word by default. */
  39. CPDFSearchMatchWholeWord = 2,
  40. /** If not set, it will skip past the current match to look for the next match. */
  41. CPDFSearchConsecutive = 4
  42. };
  43. /**
  44. * PDF/A conformance levels.
  45. */
  46. typedef NS_ENUM(NSInteger, CPDFType) {
  47. CPDFTypePDF = 0,
  48. /** PDF/A-1a */
  49. CPDFTypePDFA1a,
  50. /** PDF/A-1b */
  51. CPDFTypePDFA1b,
  52. /** PDF/A-2a */
  53. CPDFTypePDFA2a,
  54. /** PDF/A-2u */
  55. CPDFTypePDFA2u,
  56. /** PDF/A-2b */
  57. CPDFTypePDFA2b,
  58. /** PDF/A-3a */
  59. CPDFTypePDFA3a,
  60. /** PDF/A-3u */
  61. CPDFTypePDFA3u,
  62. /** PDF/A-3b */
  63. CPDFTypePDFA3b
  64. };
  65. typedef NSString *CPDFDocumentAttribute NS_STRING_ENUM;
  66. extern CPDFDocumentAttribute const CPDFDocumentTitleAttribute; // NSString containing document title.
  67. extern CPDFDocumentAttribute const CPDFDocumentAuthorAttribute; // NSString containing document author.
  68. extern CPDFDocumentAttribute const CPDFDocumentSubjectAttribute; // NSString containing document title.
  69. extern CPDFDocumentAttribute const CPDFDocumentCreatorAttribute; // NSString containing name of app that created document.
  70. extern CPDFDocumentAttribute const CPDFDocumentProducerAttribute; // NSString containing name of app that produced PDF data.
  71. extern CPDFDocumentAttribute const CPDFDocumentKeywordsAttribute; // NSString containing document keywords.
  72. extern CPDFDocumentAttribute const CPDFDocumentCreationDateAttribute; // NSString representing document creation date.
  73. extern CPDFDocumentAttribute const CPDFDocumentModificationDateAttribute; // NSString representing last document modification date.
  74. typedef NSString *CPDFDocumentWriteOption NS_STRING_ENUM;
  75. extern CPDFDocumentWriteOption const CPDFDocumentOwnerPasswordOption; // NSString for the owner's password.
  76. extern CPDFDocumentWriteOption const CPDFDocumentUserPasswordOption; // NSString for the user's password.
  77. extern CPDFDocumentWriteOption const CPDFDocumentAllowsPrintingOption;
  78. extern CPDFDocumentWriteOption const CPDFDocumentAllowsHighQualityPrintingOption;
  79. extern CPDFDocumentWriteOption const CPDFDocumentAllowsCopyingOption;
  80. extern CPDFDocumentWriteOption const CPDFDocumentAllowsDocumentChangesOption;
  81. extern CPDFDocumentWriteOption const CPDFDocumentAllowsDocumentAssemblyOption;
  82. extern CPDFDocumentWriteOption const CPDFDocumentAllowsCommentingOption;
  83. extern CPDFDocumentWriteOption const CPDFDocumentAllowsFormFieldEntryOption;
  84. @class CPDFPage, CPDFOutline, CPDFBookmark, CPDFWatermark, CPDFHeaderFooter, CPDFBates, CPDFBackground, CPDFSelection;
  85. @protocol CPDFDocumentDelegate;
  86. /**
  87. * An object that represents a PDF file and defines methods for writing, searching, and selecting PDF data.
  88. *
  89. * @discussion The other utility classes are either instantiated from methods in CPDFDocument, as are CPDFPage and CPDFOutline; or support it, as do CPDFWatermark and CPDFDestination.
  90. * You initialize a CPDFDocument object with a URL to a PDF file. You can then ask for the page count, add or delete pages, perform a find, or parse selected content into an NSString object.
  91. */
  92. @interface CPDFDocument : NSObject
  93. #pragma mark - Initializers
  94. /**
  95. * Initializes a CPDFDocument object with new PDF.
  96. */
  97. - (instancetype)init;
  98. /**
  99. * Initializes a CPDFDocument object with the contents at the specified URL.
  100. */
  101. - (instancetype)initWithURL:(NSURL *)url;
  102. #pragma mark - Accessors
  103. /**
  104. * The object acting as the delegate for the CPDFDocument object.
  105. *
  106. * @see CPDFDocumentDelegate
  107. */
  108. @property (nonatomic,assign) id<CPDFDocumentDelegate> delegate;
  109. /**
  110. * The URL for the document.
  111. *
  112. * @discussion May return NULL if the document was created from an new object.
  113. */
  114. @property (nonatomic,readonly) NSURL *documentURL;
  115. /**
  116. * The password for the document.
  117. *
  118. * @discussion May return NULL if the document is not an encrypted document.
  119. */
  120. @property (nonatomic,readonly) NSString *password;
  121. /**
  122. * Error message for the document loading.
  123. *
  124. * @discussion To determine the error type, use the code to obtain error of type CPDFDocumentErrorDomain.
  125. * Except for the CPDFDocumentPasswordError error, other errors cannot open the document.
  126. * If CPDFDocumentPasswordError, reading the document requires a password.
  127. */
  128. @property (nonatomic,readonly) NSError *error;
  129. /**
  130. * A Boolean value specifying whether the document is encrypted.
  131. *
  132. * @discussion Whether the PDF is encrypted. With the right password, a PDF can be unlocked - nonetheless, the PDF still indicates that it is encrypted - just no longer locked.
  133. * Some PDF's may be encrypted but can be unlocked with the empty string. These are unlocked automatically.
  134. */
  135. @property (nonatomic,readonly) BOOL isEncrypted;
  136. /**
  137. * A Boolean value indicating whether the document is locked.
  138. *
  139. * @discussion Only encrypted documents can be locked. Use the unlockWithPassword: method to unlock a document using a password.
  140. */
  141. @property (nonatomic,readonly) BOOL isLocked;
  142. /**
  143. * Attempts to unlock an encrypted document.
  144. *
  145. * @discussion If the password is correct, this method returns YES, and a CPDFDocumentDidUnlockNotification notification is sent.
  146. * Once unlocked, you cannot use this function to relock the document.
  147. * @param password The password to unlock an encrypted document (you cannot lock an unlocked PDF document by using an incorrect password).
  148. */
  149. - (BOOL)unlockWithPassword:(NSString *)password;
  150. /**
  151. * The major version of the document.
  152. *
  153. * @discussion PDF version of the PDF file (example: major version = 1; PDF v1.4).
  154. */
  155. @property (nonatomic,readonly) NSInteger majorVersion;
  156. /**
  157. * The minor version of the document.
  158. *
  159. * @discussion PDF version of the PDF file (example: minor version = 4; PDF v1.4).
  160. */
  161. @property (nonatomic,readonly) NSInteger minorVersion;
  162. /**
  163. * The permissions status of the PDF document.
  164. *
  165. * @discussion Returns the permissions status of the PDF document. You have CPDFDocumentPermissionsNone status for an encrypted
  166. * document that you have not supplied either a valid user or owner password. For a document with no encryption, you automatically have CPDFDocumentPermissionsUser status.
  167. * @see CPDFDocumentPermissions
  168. */
  169. @property (nonatomic,readonly) CPDFDocumentPermissions permissionsStatus;
  170. /**
  171. * A Boolean value indicating whether the document allows printing.
  172. */
  173. @property (nonatomic,readonly) BOOL allowsPrinting;
  174. /**
  175. * A Boolean value indicating whether the document allows printing in high fidelity.
  176. */
  177. @property (nonatomic,readonly) BOOL allowsHighQualityPrinting;
  178. /**
  179. * A Boolean value indicating whether the document allows copying of content to the Pasteboard.
  180. */
  181. @property (nonatomic,readonly) BOOL allowsCopying;
  182. /**
  183. * A Boolean value indicating whether you can modify the document contents except for document attributes.
  184. */
  185. @property (nonatomic,readonly) BOOL allowsDocumentChanges;
  186. /**
  187. * A Boolean value indicating whether you can manage a document by inserting, deleting, and rotating pages.
  188. */
  189. @property (nonatomic,readonly) BOOL allowsDocumentAssembly;
  190. /**
  191. * A Boolean value indicating whether you can create or modify document annotations, including form field entries.
  192. */
  193. @property (nonatomic,readonly) BOOL allowsCommenting;
  194. /**
  195. * A Boolean value indicating whether you can modify form field entries even if you can't edit document annotations.
  196. */
  197. @property (nonatomic,readonly) BOOL allowsFormFieldEntry;
  198. #pragma mark - Save
  199. /**
  200. * A Boolean value indicating whether the document has been modified.
  201. */
  202. - (BOOL)isModified;
  203. /**
  204. * Writes the document to a location specified by the passed-in URL.
  205. */
  206. - (BOOL)writeToURL:(NSURL *)url;
  207. /**
  208. * Writes the document to the specified URL with the specified options.
  209. *
  210. * @discussion Set the password to the document by setting options.
  211. * @param options CPDFDocumentOwnerPasswordOption, CPDFDocumentUserPasswordOption.
  212. */
  213. - (BOOL)writeToURL:(NSURL *)url withOptions:(NSDictionary<CPDFDocumentWriteOption, id> *)options;
  214. /**
  215. * Writes the document to the specified URL after flattening.
  216. */
  217. - (BOOL)writeFlattenToURL:(NSURL *)url;
  218. /**
  219. * Writes the document to the specified URL after removing annotions and form field entries.
  220. */
  221. - (BOOL)writeContentToURL:(NSURL *)url;
  222. /**
  223. * Writes the document to the specified URL after removing permissions.
  224. */
  225. - (BOOL)writeDecryptToURL:(NSURL *)url;
  226. #pragma mark - Attributes
  227. /**
  228. * A dictionary of document metadata.
  229. *
  230. * @discussion Metadata is optional for PDF documents. The dictionary may be empty, or only some of the keys may have associated values.
  231. */
  232. - (NSDictionary<CPDFDocumentAttribute, id> *)documentAttributes;
  233. - (void)setDocumentAttributes:(NSDictionary<CPDFDocumentAttribute, id> *)documentAttributes;
  234. #pragma mark - Outline
  235. /**
  236. * The document’s root outline to a PDF outline object.
  237. *
  238. * @discussion When a PDF document is saved, the outline tree structure is written out to the destination PDF file.
  239. */
  240. - (CPDFOutline *)outlineRoot;
  241. /**
  242. * Create a root outline for the document.
  243. */
  244. - (CPDFOutline *)setNewOutlineRoot;
  245. #pragma mark - Bookmark
  246. /**
  247. * A array of document’s bookmarks.
  248. */
  249. - (NSArray<CPDFBookmark *> *)bookmarks;
  250. /**
  251. * Add a bookmark at the specified index number.
  252. *
  253. * @discussion Indexes are zero based. This method raises an exception if index is out of bounds.
  254. */
  255. - (BOOL)addBookmark:(NSString *)label forPageIndex:(NSUInteger)pageIndex;
  256. /**
  257. * Remove the bookmark at the specified index number.
  258. *
  259. * @discussion Indexes are zero based. This method raises an exception if index is out of bounds.
  260. */
  261. - (BOOL)removeBookmarkForPageIndex:(NSUInteger)pageIndex;
  262. /**
  263. * Returns the bookmark at the specified index number.
  264. *
  265. * @discussion Indexes are zero based. This method raises an exception if index is out of bounds.
  266. */
  267. - (CPDFBookmark *)bookmarkForPageIndex:(NSUInteger)pageIndex;
  268. #pragma mark - Watermark
  269. /**
  270. * A array of document’s watermarks.
  271. */
  272. - (NSArray<CPDFWatermark *> *)watermarks;
  273. /**
  274. * Add a watermark.
  275. *
  276. * @param watermark A PDF watermark object.
  277. */
  278. - (BOOL)addWatermark:(CPDFWatermark *)watermark;
  279. /**
  280. * Remove the watermark.
  281. *
  282. * @param watermark The PDF watermark object from document’s watermarks.
  283. */
  284. - (BOOL)removeWatermark:(CPDFWatermark *)watermark;
  285. /**
  286. * Update the watermark.
  287. *
  288. * @param watermark The PDF watermark object from document’s watermarks.
  289. */
  290. - (BOOL)updateWatermark:(CPDFWatermark *)watermark;
  291. #pragma mark - HeaderFooter
  292. /**
  293. * Gets the header & footer of the document.
  294. */
  295. - (CPDFHeaderFooter *)headerFooter;
  296. #pragma mark - Bates
  297. /**
  298. * Gets the bates numbers of the document.
  299. */
  300. - (CPDFBates *)bates;
  301. #pragma mark - Background
  302. /**
  303. * Gets the background of the document.
  304. */
  305. - (CPDFBackground *)background;
  306. #pragma mark - Pages
  307. /**
  308. * The number of pages in the document.
  309. */
  310. @property (nonatomic,readonly) NSUInteger pageCount;
  311. /**
  312. * Returns the page size at the specified index number.
  313. *
  314. * @discussion Indexes are zero based. This method raises an exception if index is out of bounds.
  315. */
  316. - (CGSize)pageSizeAtIndex:(NSUInteger)index;
  317. /**
  318. * Returns a CPDFPage object representing the page at index.
  319. *
  320. * @discussion Indexes are zero based. This method raises an exception if index is out of bounds.
  321. */
  322. - (CPDFPage *)pageAtIndex:(NSUInteger)index;
  323. /**
  324. * Gets the index number for the specified page.
  325. *
  326. * @discussion Given a CPDFPage, returns the pages index within the document. Indices are zero-based.
  327. */
  328. - (NSUInteger)indexForPage:(CPDFPage *)page;
  329. /**
  330. * Inserts a blank page at the specified index point.
  331. *
  332. * @discussion Indexes are zero based. The index must lie within bounds, or be equal to the length of bounds.
  333. */
  334. - (BOOL)insertPage:(CGSize)pageSize atIndex:(NSUInteger)index;
  335. /**
  336. * Inserts a blank page with image at the specified index point.
  337. *
  338. * @discussion Indexes are zero based. The index must lie within bounds, or be equal to the length of bounds.
  339. */
  340. - (BOOL)insertPage:(CGSize)pageSize withImage:(NSString *)imagePath atIndex:(NSUInteger)index;
  341. /**
  342. * Removes page(s) at the specified index set.
  343. */
  344. - (BOOL)removePageAtIndexSet:(NSIndexSet *)indexSet;
  345. /**
  346. * Move one page to another.
  347. *
  348. * @discussion This method raises an exception if either index value is out of bounds.
  349. */
  350. - (BOOL)movePageAtIndex:(NSUInteger)indexA withPageAtIndex:(NSUInteger)indexB;
  351. /**
  352. * Exchanges one page with another.
  353. *
  354. * @discussion This method raises an exception if either index value is out of bounds.
  355. */
  356. - (BOOL)exchangePageAtIndex:(NSUInteger)indexA withPageAtIndex:(NSUInteger)indexB;
  357. /**
  358. * Import page(s) from another document at the specified index point.
  359. *
  360. * @discussion This method raises an exception if either index value is out of bounds.
  361. */
  362. - (BOOL)importPages:(NSIndexSet *)indexSet fromDocument:(CPDFDocument *)document atIndex:(NSUInteger)index;
  363. #pragma mark - Annotations
  364. /**
  365. * Export annotation to XFDF document.
  366. */
  367. - (BOOL)exportAnnotationToXFDFPath:(NSString *)xfdfPath;
  368. /**
  369. * Import annotations from XFDF document.
  370. */
  371. - (BOOL)importAnnotationFromXFDFPath:(NSString *)xfdfPath;
  372. /**
  373. * Remove all signatures.
  374. */
  375. - (BOOL)removeAllSignature;
  376. /**
  377. * A Boolean value indicating whether the document contains annotations.
  378. */
  379. - (BOOL)hasAnnotations;
  380. /**
  381. * Reset all forms in PDF document.
  382. */
  383. - (void)resetForm;
  384. #pragma mark - Extract
  385. /**
  386. * Extract image at the specified index set.
  387. */
  388. - (NSUInteger)extractImageFromPages:(NSIndexSet *)indexSet toPath:(NSString *)path;
  389. /**
  390. * Cancels a extract initiated with extractImageFromPages:toPath:.
  391. */
  392. - (void)cancelExtractImage;
  393. #pragma mark - Summary
  394. - (NSString *)summaryHTML;
  395. #pragma mark - PDF/A
  396. /**
  397. * Gets PDF/A conformance levels.
  398. *
  399. * @see CPDFType
  400. */
  401. - (CPDFType)type;
  402. /**
  403. * Converts existing PDF files to PDF/A compliant documents, including PDF/A-1a and PDF/A-1b only.
  404. *
  405. * @discussion The conversion option analyzes the content of existing PDF files and performs a sequence of modifications in order to produce a PDF/A compliant document.
  406. * Features that are not suitable for long-term archiving (such as encryption, obsolete compression schemes, missing fonts, or device-dependent color) are replaced with their PDF/A compliant equivalents.
  407. * Because the conversion process applies only necessary changes to the source file, the information loss is minimal.
  408. * @see CPDFType
  409. */
  410. - (BOOL)writePDFAToURL:(NSURL *)url withType:(CPDFType)type;
  411. #pragma mark - Find
  412. /**
  413. * Returns a Boolean value indicating whether an asynchronous find operation is in progress.
  414. */
  415. @property (nonatomic,readonly) BOOL isFinding;
  416. /**
  417. * Synchronously finds all instances of the specified string in the document.
  418. *
  419. * @discussion Each hit gets added to an NSArray object as a CPDFSelection object. If there are no hits, this method returns an empty array.
  420. * Use this method when the complete search process will be brief and when you don’t need the flexibility or control offered by beginFindString:withOptions:.
  421. * @see CPDFSearchOptions
  422. */
  423. - (NSArray<NSArray<CPDFSelection *> *> *)findString:(NSString *)string withOptions:(CPDFSearchOptions)options;
  424. /**
  425. * Asynchronously finds all instances of the specified string in the document.
  426. *
  427. * @discussion This method returns immediately. It causes delegate methods to be called when searching begins and ends, on each search hit, and when the search proceeds to a new page.
  428. * @see CPDFDocumentDelegate
  429. */
  430. - (void)beginFindString:(NSString *)string withOptions:(CPDFSearchOptions)options;
  431. /**
  432. * Cancels a search initiated with beginFindString:withOptions:.
  433. */
  434. - (void)cancelFindString;
  435. #pragma mark - Redact
  436. /**
  437. * Applies redaction annotations in the document.
  438. */
  439. - (void)applyRedactions;
  440. @end
  441. /**
  442. * The delegate for the CPDFDocument object.
  443. */
  444. @protocol CPDFDocumentDelegate <NSObject>
  445. @optional
  446. /**
  447. * Called when the beginFindString:withOptions: or findString:withOptions: method begins finding.
  448. */
  449. - (void)documentDidBeginDocumentFind:(CPDFDocument *)document;
  450. /**
  451. * Called when the beginFindString:withOptions: or findString:withOptions: method returns.
  452. */
  453. - (void)documentDidEndDocumentFind:(CPDFDocument *)document;
  454. /**
  455. * Called when a find operation begins working on a new page of a document.
  456. */
  457. - (void)documentDidBeginPageFind:(CPDFDocument *)document pageAtIndex:(NSUInteger)index;
  458. /**
  459. * Called when a find operation finishes working on a page in a document.
  460. */
  461. - (void)documentDidEndPageFind:(CPDFDocument *)document pageAtIndex:(NSUInteger)index;
  462. /**
  463. * Called when a string match is found in a document.
  464. *
  465. * @discussion To determine the string selection found, use the selection.
  466. */
  467. - (void)documentDidFindMatch:(CPDFSelection *)selection;
  468. @end