CPDFDocument.h 19 KB

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