CPDFViewController.m 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //
  2. // CPDFViewController.m
  3. // PDFViewer
  4. //
  5. // Created by kdan on 2022/11/15.
  6. //
  7. #import <ComPDFKit/ComPDFKit.h>
  8. #import <ComPDFKit/CPDFWatermark.h>
  9. #import <ComPDFKit/CPDFHeaderFooter.h>
  10. #import "CPDFViewController.h"
  11. #import "CPDFTextPreview.h"
  12. #import "CPDFImagePreview.h"
  13. #import "CPDFTextView.h"
  14. #import "CPDFImageView.h"
  15. #import "CPDFDataModel.h"
  16. #import "Masonry.h"
  17. #import "CPDFAddViewController.h"
  18. @interface CPDFViewController () <UIActionSheetDelegate,WaterMarkModelDataDelegate>
  19. @property (nonatomic,strong) CPDFView *pdfView;
  20. @property (nonatomic,strong) CPDFDocument *pdfDocument;
  21. @property (nonatomic,strong) CPDFWatermark *textWatermark;
  22. @property (nonatomic,strong) CPDFWatermark *imageWatermark;
  23. @property (nonatomic,strong) CPDFAddViewController *addWaterMarkViewController;
  24. @property (nonatomic,strong) UIImage *image;
  25. @property (nonatomic,assign) CGSize imageSize;
  26. @end
  27. @implementation CPDFViewController
  28. - (instancetype)initWithPath:(NSString *)inPath {
  29. if (self = [super init]) {
  30. _path = inPath;
  31. }
  32. return self;
  33. }
  34. - (void)viewDidLoad {
  35. [super viewDidLoad];
  36. // Do any additional setup after loading the view.
  37. //add UIBarButtonItem in Navigation
  38. self.navigationController.toolbarHidden = YES;
  39. UIImage *btnMore = [UIImage imageNamed:@"btn_more"];
  40. UIBarButtonItem *ringhtBarItem = [[UIBarButtonItem alloc] initWithImage:btnMore style:UIBarButtonItemStylePlain target:self action:@selector(onClickedOkbtn)];
  41. self.navigationItem.rightBarButtonItem = ringhtBarItem;
  42. //setting URL
  43. NSURL *url = [NSURL fileURLWithPath:_path];
  44. _pdfDocument = [[CPDFDocument alloc] initWithURL:url];
  45. _pdfView = [[CPDFView alloc] initWithFrame:self.view.bounds];
  46. _pdfView.document = _pdfDocument;
  47. _pdfView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  48. [self.view addSubview:_pdfView];
  49. CPDFPage *pdfPage = [_pdfView.document pageAtIndex:0];
  50. _imageSize = [_pdfView.document pageSizeAtIndex:0];
  51. _image = [pdfPage thumbnailOfSize:_imageSize];
  52. }
  53. #pragma mark - Actions
  54. - (void)onClickedOkbtn {
  55. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  56. UIAlertAction *addWatermarkAction = [UIAlertAction actionWithTitle:@"Add Watermark" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  57. self.addWaterMarkViewController = [[CPDFAddViewController alloc] initWithImage:self.image];
  58. self.addWaterMarkViewController.delegate = self;
  59. [self.navigationController pushViewController:self.addWaterMarkViewController animated:YES];
  60. }];
  61. UIAlertAction *addHeaderfooterAction = [UIAlertAction actionWithTitle:@"Add Headerfooter" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  62. NSLog(@"addHeaderfooterAction");
  63. }];
  64. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  65. }];
  66. [alertController addAction:addWatermarkAction];
  67. [alertController addAction:addHeaderfooterAction];
  68. [alertController addAction:cancelAction];
  69. [self presentViewController:alertController animated:YES completion:nil];
  70. }
  71. #pragma mark - WaterMarkModelDataDelegate
  72. - (void)changeTextModel:(CPDFDataModel *)dataModel {
  73. _textWatermark = [[CPDFWatermark alloc] initWithDocument:_pdfDocument type:CPDFWatermarkTypeText];
  74. _textWatermark.text = dataModel.text;
  75. _textWatermark.textColor = dataModel.textColor;
  76. _textWatermark.opacity = dataModel.watermarkOpacity;
  77. _textWatermark.scale = dataModel.watermarkScale;
  78. _textWatermark.isTilePage = dataModel.isTile;
  79. _textWatermark.tx = dataModel.tx * _imageSize.width / self.addWaterMarkViewController.textViewController.textPreview.documentView.frame.size.width;
  80. _textWatermark.ty = dataModel.ty * _imageSize.height / self.addWaterMarkViewController.textViewController.textPreview.documentView.frame.size.height;
  81. _textWatermark.rotation = dataModel.watermarkRotation;
  82. if (_textWatermark.isTilePage) {
  83. _textWatermark.verticalSpacing = [self.addWaterMarkViewController.textViewController.textView.verticalField.text floatValue];
  84. _textWatermark.horizontalSpacing = [self.addWaterMarkViewController.textViewController.textView.horizontalField.text floatValue];
  85. }
  86. if (dataModel.pageString) {
  87. _textWatermark.pageString = dataModel.pageString;
  88. }
  89. [_pdfView.document addWatermark:_textWatermark];
  90. [_pdfView layoutDocumentView];
  91. NSURL *url = [NSURL fileURLWithPath:_path];
  92. [_pdfView.document writeToURL:url];
  93. }
  94. - (void)changeImageModel:(CPDFDataModel *)dataModel {
  95. _imageWatermark = [[CPDFWatermark alloc] initWithDocument:_pdfDocument type:CPDFWatermarkTypeImage];
  96. _imageWatermark.image = dataModel.image;
  97. _imageWatermark.opacity = dataModel.watermarkOpacity;
  98. _imageWatermark.scale = dataModel.watermarkScale;
  99. _imageWatermark.isTilePage = dataModel.isTile;
  100. _imageWatermark.tx = dataModel.tx * _imageSize.width / self.addWaterMarkViewController.imageViewController.imagePreview.documentView.frame.size.width;
  101. _imageWatermark.ty = dataModel.ty * _imageSize.height / self.addWaterMarkViewController.imageViewController.imagePreview.documentView.frame.size.height;
  102. _imageWatermark.rotation = dataModel.watermarkRotation;
  103. if (_imageWatermark.isTilePage) {
  104. _imageWatermark.verticalSpacing = [self.addWaterMarkViewController.imageViewController.imageView.verticalField.text floatValue];
  105. _imageWatermark.horizontalSpacing = [self.addWaterMarkViewController.imageViewController.imageView.horizontalField.text floatValue];
  106. }
  107. if (dataModel.pageString) {
  108. _imageWatermark.pageString = dataModel.pageString;
  109. }
  110. [_pdfView.document addWatermark:_imageWatermark];
  111. [_pdfView layoutDocumentView];
  112. // NSURL *url = [NSURL fileURLWithPath:_path];
  113. // [_pdfView.document writeContentToURL:url];
  114. }
  115. @end