Creating a new tool

Because the Tools framework is open source, it is easy to modify and extend it to meet your app's unique needs. This article will show how to add an entirely new tool.

Creating a redaction tool

Apryse includes the ability to redact information from a PDF using the PTRedactor class. It has a single method, which takes a document, a page number, and series of rectangles to redact.

The common flow in redacting a document is that the user creates one or more redaction region annotations (PTRedactionAnnot, rectangles indicating the region(s) to be redacted), and then instructs the system to redact them.

In the name of simplicity, this tool will allow the user to create a single rectangle, which is then immediately redacted.

Defining the interface

The class PTTool is an abstract base class from which all tools derive. It implements the required functionality to fit into the tool system (as defined by the PTToolSwitching protocol), as well as some common helper methods, but does do anything useful for the user on its own.

For the redact tool, we could choose to extend from PTTool, but a better choice is a more closely related tool, the abstract PTCreateToolBase tool. It already implements some useful behaviour, and is the base class for a number of similar concrete tools such as PTRectangleCreate, PTLineCreate.

Like the PTRectangleCreate tool, our simple redact tool will not need to expose any public API, so we can define its interface simply as follows:

Obj-C

1@interface PTRedactTool : PTCreateToolBase {
2
3}

Implementing tool specific methods

Tools contain a number of identifying methods, which we can copy from the PTRectangleCreate tool, replacing what the methods return to make it specific to our redact tool.

Obj-C

1- (instancetype)initWithPDFViewCtrl:(PTPDFViewCtrl*)in_pdfViewCtrl
2{
3 self = [super initWithPDFViewCtrl:in_pdfViewCtrl];
4 if (self) {
5
6 }
7 return self;
8}
9
10+(BOOL)createsAnnotation
11{
12 return YES;
13}
14
15-(Class)annotClass
16{
17 return [PTRedactionAnnot class];
18}
19
20-(PTAnnotType)annotType
21{
22 return e_ptRedact;
23}

You will notice that it returns YES in response to the method createsAnnotation. This is because it is, as you will see, creating an annotation (that is immediately deleted), and because this information is used further up the class chain to determine if scrolling should take place when two fingers are used rather than zooming (which is true for tools which create annotations and false for tools that do not).

Behaviour while touches are moving

The create tool base class keeps track of where the user touched down, where they are dragging their finger, and when they release. The only thing we need to do while the user drags their finger is define what the rectangle should look like, which is done it the tool's drawRect: method. What follows is a modified version of PTRectangleCreate's drawRect: method.

Obj-C

1- (void)drawRect:(CGRect)rect
2{
3 CGContextRef currentContext = UIGraphicsGetCurrentContext();
4
5 if( self.pageNumber >= 1 && !self.allowScrolling)
6 {
7
8 [super setupContext:currentContext];
9
10 CGContextSetLineWidth(currentContext, 0);
11 CGContextSetStrokeColorWithColor(currentContext, [UIColor clearColor].CGColor);
12 CGContextSetFillColorWithColor(currentContext, [UIColor blackColor].CGColor);
13 CGContextSetAlpha(currentContext, 0.5);
14
15 CGContextFillRect(currentContext, self.drawArea);
16 CGContextStrokeRect(currentContext, self.drawArea);
17
18 CGContextStrokePath(currentContext);
19 }
20}

Redacting when touches end

Lastly, we need to implement what happens when the user stops dragging. Here, immediately redact the area, and delete the annotation.

Obj-C

1- (BOOL)pdfViewCtrl:(PTPDFViewCtrl*)pdfViewCtrl onTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
2{
3 BOOL ret = [super onTouchesEnded:touches withEvent:event];
4
5 @try
6 {
7 // we will be modifying the document, so obtain a write lock.
8 // yes cancels any rendering that may be occuring
9 [self.pdfViewCtrl DocLock:YES];
10
11 // get the rectangle of the PTRedactAnnot we just made
12 PTPDFRect* annotRect = [self.currentAnnotation GetRect];
13
14 // set this rectangle as the area to be deleted
15 PTVectorRedaction *vec = [[PTVectorRedaction alloc] init];
16 [vec add: [[PTRedaction alloc] initWithPage_num: self.annotationPageNumber bbox: annotRect negative: NO text:@""]];
17
18 // define the appearance of the redacted area
19 PTAppearance *app = [[PTAppearance alloc] init];
20 [app setRedactionOverlay: YES];
21 [app setBorder: NO];
22
23 // redact the area
24 [PTRedactor Redact: [self.pdfViewCtrl GetDoc] red_arr: vec app: app ext_neg_mode: NO page_coord_sys: YES];
25
26 // delete the PTRedactAnnot we just made
27 [self deleteSelectedAnnotation];
28
29 }
30 @catch (NSException *exception) {
31 NSLog(@"Exception: %@: %@",exception.name, exception.reason);
32 }
33 @finally {
34 [self.pdfViewCtrl DocUnlock];
35 }
36
37 return ret;
38}

How to use the tool

To activate the PTRedactTool, you can set it as the PTToolManager's active tool. This could, for example, be executed when the user presses the "Redact" button in a toolbar.

Obj-C

1self.toolManager.tool = [[PTRedactTool alloc] initWithPDFViewCtrl:self.pdfViewCtrl];

To add a "Redact" option to the long press menu that occurs when the user long-presses on the document, open the PTPanTool source file, find the method attachInitialMenuItems:, and add a new option for the Redact tool.

Adding functionality to PTAnnotEditTool

Instead of redacting immediately, a "Redact" option could be added to the redaction annotation when selected. To do this, remove the entire onTouchesEnded:withEvent: method from the PTRedactTool, and add everything that was in the try/catch/finally portion to a new method, redact, in PTAnnotEditTool. Then in the PTAnnotEditTool's method attachInitialMenuItemsForAnnotType:, add the following to the e_ptRedact case in the switch statement:

Obj-C

1case e_ptRedact:
2 menuItem = [[UIMenuItem alloc] initWithTitle:@"Redact" action:@selector(redact)];
3 [menuItems addObject:menuItem];
4 break;

Apryse Docs Image

Redacting a portion of a page with the annot edit tool

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales