Document Creation

PSPDFKit can create new PDF documents or single PDF pages from various sources such as images or existing PDF patterns. This feature requires the Document Editor component.

The Document Editor component already offers a convenient UI for inserting blank pages or pages with predefined patterns into existing documents – this is perfect for adding new pages for more scratch space for drawing or adding textual notes. You can also use PSPDFNewPageViewController, which returns a PSPDFNewPageConfiguration via the PSPDFNewPageViewControllerDelegate.

Note: The below requires PSPDFKit 5.3.6 for iOS or later.

Via the PSPDFNewPageConfiguration you can configure how a single page in the new document should appear. This allows you to either add a completely empty page, a page with a pattern, or a page from a different document. Additionally, PSPDFProcessorItem enables you to add a image or logo aligned to the page border:

Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Set up a processor configuration with no document
let configuration = PSPDFProcessorConfiguration()
// Add an empty page with the image in the bottom center
let backgroundColor = UIColor(red:0.965, green:0.953, blue:0.906, alpha: 1)
let image = UIImage(named: "exampleimage.jpg")!
configuration.addNewPage(at: 0, configuration: PSPDFNewPageConfiguration(emptyPageBuilder: { builder in
    builder.backgroundColor = backgroundColor
    builder.item = PSPDFProcessorItem(image: image, jpegCompressionQuality: 0.8, builderBlock: { itemBuilder in
        itemBuilder.alignment = .alignBottom
        itemBuilder.transform = CGAffineTransform(scaleX: 0.3, y: 0.3)
    })
}))
// Add a page with a pattern grid
configuration.addNewPage(at: 1, configuration: PSPDFNewPageConfiguration(tiledPattern: PSPDFNewPagePatternGrid5mm, builderBlock: nil)
// Add a page from a different document
let document = PSPDFDocument(url: URL(fileURLWithPath: "example.pdf"))
configuration.addNewPage(at: 2, configuration: PSPDFNewPageConfiguration(document: document, sourcePageIndex: 7, builderBlock: nil))
Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Set up a processor configuration with no document
PSPDFProcessorConfiguration *configuration = [[PSPDFProcessorConfiguration alloc] init];
// Add an empty page with the image in the bottom center
UIColor *backgroundColor = [UIColor colorWithRed:0.965f green:0.953f blue:0.906f alpha:1];
UIImage *image = [UIImage imageNamed:@"exampleimage.jpg"];
[configuration addNewPageAtIndex:0 configuration:[PSPDFNewPageConfiguration newPageConfigurationWithEmptyPageBuilder:^(PSPDFNewPageConfigurationBuilder *builder) {
    builder.backgroundColor = backgroundColor;
    builder.item = [PSPDFProcessorItem processorItemWithImage:image jpegCompressionQuality:0.8f builderBlock:^(PSPDFProcessorItemBuilder *itemBuilder) {
        itemBuilder.alignment = PSPDFRectAlignBottom;
        itemBuilder.transform = CGAffineTransformMakeScale(0.3f, 0.3f);
    }];
}]];
// Add a page with a pattern grid
[configuration addNewPageAtIndex:1 configuration:[PSPDFNewPageConfiguration newPageConfigurationWithTiledPattern:PSPDFNewPagePatternLines5mm builderBlock:nil]];
// Add a page from a different document
PSPDFDocument *document = [[PSPDFDocument alloc] initWithURL:[NSURL fileURLWithPath:@"example.pdf"]];
[configuration addNewPageAtIndex:2 configuration:[PSPDFNewPageConfiguration newPageConfigurationWithDocument:document sourcePageIndex:7 builderBlock:nil]];