// // CPageEditToolBar.swift // ComPDFKit_Tools // // Copyright © 2014-2024 PDF Technologies, Inc. All Rights Reserved. // // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT. // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES. // This notice may not be removed from this file. // import UIKit import ComPDFKit public enum CPageEditToolBarType: Int { case insert = 0 case replace case extract case copy case rotate case delete } public protocol CPageEditToolBarDelegate: AnyObject { func pageEditToolBarBlankPageInsert(_ pageEditToolBar: CPageEditToolBar, pageModel: CBlankPageModel) func pageEditToolBarPDFInsert(_ pageEditToolBar: CPageEditToolBar, pageModel: CBlankPageModel, document: CPDFDocument) func pageEditToolBarExtract(_ pageEditToolBar: CPageEditToolBar) func pageEditToolBarRotate(_ pageEditToolBar: CPageEditToolBar) func pageEditToolBarDelete(_ pageEditToolBar: CPageEditToolBar) func pageEditToolBarCopy(_ pageEditToolBar: CPageEditToolBar) func pageEditToolBarReplace(_ pageEditToolBar: CPageEditToolBar, document: CPDFDocument) } public class CPageEditToolBar: UIView, UIDocumentPickerDelegate, CPDFPageInsertViewControllerDelegate, CPDFPDFInsertViewControllerDelegate, CDocumentPasswordViewControllerDelegate { public weak var delegate: CPageEditToolBarDelegate? public weak var parentVC: UIViewController? public var pdfView: CPDFView? public var currentPageIndex: Int = 0 public var isSelect: Bool = false public var pageEditBtns: [UIButton]? private var scrollView: UIScrollView? private var selectedIndex: Int = 0 private var insertDocument: CPDFDocument? private var replaceDocument: CPDFDocument? private var loadingView: CActivityIndicatorView? // MARK: - Initializers override init(frame: CGRect) { super.init(frame: frame) self.backgroundColor = CPDFColorUtils.CPDFViewControllerBackgroundColor() let line = UIView(frame: CGRect(x: 0, y: 0, width: self.frame.size.width, height: 1)) line.backgroundColor = UIColor(red: 210.0/255.0, green: 210.0/255.0, blue: 210.0/255.0, alpha: 1.0) line.autoresizingMask = .flexibleWidth self.addSubview(line) self.selectedIndex = -1 self.initSubview() } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } public override func layoutSubviews() { super.layoutSubviews() self.scrollView?.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height) } // MARK: - Public Methods func reloadData() { // Implementation if selectedIndex >= 0 && (selectedIndex < pageEditBtns?.count ?? 0) { var selectedButton = pageEditBtns?[selectedIndex] selectedButton?.backgroundColor = UIColor.clear } selectedIndex = -1 } // MARK: - Private Methods func initSubview() { scrollView = UIScrollView(frame: self.bounds) scrollView?.showsVerticalScrollIndicator = false scrollView?.showsHorizontalScrollIndicator = false self.addSubview(scrollView!) var offsetX: CGFloat = 20.0 let buttonOffset: CGFloat = 25 let buttonSize: CGFloat = 50 let topOffset = (60 - buttonSize) / 2 let images = ["CPageEditToolBarImageInsert", "CPageEditToolBarImageRepalce", "CPageEditToolBarImageExtract", "CPageEditToolBarImageCopy", "CPageEditToolBarImageRotate", "CPageEditToolBarImageRemove"] let names = [NSLocalizedString("Insert", comment: ""), NSLocalizedString("Replace", comment: ""), NSLocalizedString("Extract", comment: ""), NSLocalizedString("Copy", comment: ""), NSLocalizedString("Rotate", comment: ""), NSLocalizedString("Delete", comment: "")] var pageEditBtns = [UIButton]() for i in 0..= 0 && selectedIndex < (pageEditBtns?.count ?? 0) { let selectedButton = pageEditBtns![selectedIndex] selectedButton.backgroundColor = UIColor.clear } selectedIndex = button.tag switch button.tag { case CPageEditToolBarType.insert.rawValue: insertPage() break case CPageEditToolBarType.replace.rawValue: if isSelect { enterPDFAddFile() } else { popoverWarning() } break case CPageEditToolBarType.extract.rawValue: if isSelect { delegate?.pageEditToolBarExtract(self) } else { popoverWarning() } break case CPageEditToolBarType.rotate.rawValue: if isSelect { delegate?.pageEditToolBarRotate(self) } else { popoverWarning() } break case CPageEditToolBarType.copy.rawValue: if isSelect { delegate?.pageEditToolBarCopy(self) } else { popoverWarning() } break case CPageEditToolBarType.delete.rawValue: if isSelect { delegate?.pageEditToolBarDelete(self) } else { popoverWarning() } break default: break } } // MARK: - CPDFPageInsertViewControllerDelegate func pageInsertViewControllerSave(_ pageInsertViewController: CPDFPageInsertViewController, pageModel: CBlankPageModel) { delegate?.pageEditToolBarBlankPageInsert(self, pageModel: pageModel) reloadData() } func pageInsertViewControllerCancel(_ pageInsertViewController: CPDFPageInsertViewController) { reloadData() } // MARK: - CPDFPDFInsertViewControllerDelegate func pdfInsertViewControllerCancel(_ pageInsertViewController: CPDFPDFInsertViewController) { reloadData() } func pdfInsertViewControllerSave(_ pageInsertViewController: CPDFPDFInsertViewController, document: CPDFDocument, pageModel: CBlankPageModel) { insertDocument = document if(insertDocument != nil) { delegate?.pageEditToolBarPDFInsert(self, pageModel: pageModel, document: insertDocument!) } } // MARK: - UIDocumentPickerDelegate public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) { let fileUrlAuthozied = urls.first?.startAccessingSecurityScopedResource() ?? false if fileUrlAuthozied { let fileCoordinator = NSFileCoordinator() var error: NSError? fileCoordinator.coordinate(readingItemAt: urls.first!, options: [], error: &error) { newURL in let documentFolder = NSHomeDirectory() + "/Documents/Files" if !FileManager.default.fileExists(atPath: documentFolder) { try? FileManager.default.createDirectory(atPath: documentFolder, withIntermediateDirectories: true, attributes: nil) } let documentPath = documentFolder+"/"+newURL.lastPathComponent if !FileManager.default.fileExists(atPath: documentPath) { try? FileManager.default.copyItem(atPath: newURL.path, toPath: documentPath) } let url = URL(fileURLWithPath: documentPath) guard let document = CPDFDocument(url: url) else { print("Document is NULL") return } if let error = document.error, error._code != CPDFDocumentPasswordError { let okAction = UIAlertAction(title: NSLocalizedString("OK", comment: ""), style: .default) { _ in } let alert = UIAlertController(title: "", message: NSLocalizedString("Sorry PDF Reader Can't open this pdf file!", comment: ""), preferredStyle: .alert) alert.addAction(okAction) guard let tRootViewControl = self.parentVC else { return } tRootViewControl.present(alert, animated: true, completion: nil) } else { if document.isLocked { let documentPasswordVC = CDocumentPasswordViewController(document: document) documentPasswordVC.delegate = self documentPasswordVC.modalPresentationStyle = .fullScreen guard let tRootViewControl = self.parentVC else { return } tRootViewControl.present(documentPasswordVC, animated: true, completion: nil) } else { if CPageEditToolBarType(rawValue: selectedIndex) == .insert { insertDocument = document } else if CPageEditToolBarType(rawValue: selectedIndex) == .replace { replaceDocument = document } handleDocument() } } } urls.first?.stopAccessingSecurityScopedResource() } } public func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) { reloadData() } // MARK: - CDocumentPasswordViewControllerDelegate public func documentPasswordViewControllerCancel(_ documentPasswordViewController: CDocumentPasswordViewController) { reloadData() } public func documentPasswordViewControllerOpen(_ documentPasswordViewController: CDocumentPasswordViewController, document: CPDFDocument) { if CPageEditToolBarType(rawValue: selectedIndex) == .insert { insertDocument = document } else if CPageEditToolBarType(rawValue: selectedIndex) == .replace { replaceDocument = document } handleDocument() reloadData() } }