CPDFDocument.h 19 KB

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