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
18
19
20
21
22
// 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")!
let imagePage = PSPDFNewPageConfiguration(pageTemplate: .blank) { (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)
	})
}
configuration.addNewPage(at: 0, configuration: imagePage)

// Add a page with a pattern grid
let grid5mmPageTemplate = PSPDFPageTemplate(pageType: .tiledPatternPage, identifier: .grid5mm)
let newGridPage = PSPDFNewPageConfiguration(pageTemplate: grid5mmPageTemplate, builderBlock: nil)
configuration.addNewPage(at: 1, configuration: newGridPage)

// Add a page from a different document
let document = PSPDFDocument(url: URL(fileURLWithPath: "example.pdf"))
let externalDocumentPageTemplate = PSPDFPageTemplate(document: document, sourcePageIndex: 7)
let newPageFromDocument = PSPDFNewPageConfiguration(pageTemplate: externalDocumentPageTemplate, builderBlock: nil)
configuration.addNewPage(at: 2, configuration: newPageFromDocument)
Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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"];
PSPDFNewPageConfiguration *imagePage = [PSPDFNewPageConfiguration newPageConfigurationWithPageTemplate:[PSPDFPageTemplate blankTemplate] builderBlock:^(PSPDFNewPageConfigurationBuilder * _Nonnull 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);
	}];
}];
[configuration addNewPageAtIndex:0 configuration:imagePage];

// Add a page with a pattern grid
PSPDFPageTemplate *grid5mmTemplate = [[PSPDFPageTemplate alloc] initWithPageType:PSPDFNewPageTypeTiledPatternPage identifier:PSPDFTemplateIdentifierGrid5mm];
[configuration addNewPageAtIndex:1 configuration:[PSPDFNewPageConfiguration newPageConfigurationWithPageTemplate:grid5mmTemplate builderBlock:nil]];

// Add a page from a different document
PSPDFDocument *document = [[PSPDFDocument alloc] initWithURL:[NSURL fileURLWithPath:@"example.pdf"]];
PSPDFPageTemplate *externalDocumentPageTemplate = [[PSPDFPageTemplate alloc] initWithDocument:document sourcePageIndex:7];
PSPDFNewPageConfiguration *newPageFromDocument = [PSPDFNewPageConfiguration newPageConfigurationWithPageTemplate:externalDocumentPageTemplate builderBlock:nil];
[configuration addNewPageAtIndex:2 configuration:newPageFromDocument];