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 PDFNewPageViewController, which returns a PDFNewPageConfiguration via the PDFNewPageViewControllerDelegate.

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

Via the PDFNewPageConfiguration, you can configure how a single page in the new document should appear. This allows you to add a completely empty page, a page with a pattern, or a page from a different document. Additionally, ProcessorItem enables you to add an 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 = PDFNewPageConfiguration(pageTemplate: .blank) {
	$0.backgroundColor = backgroundColor
	$0.item = ProcessorItem(image: image, jpegCompressionQuality: 0.8) {
		$0.alignment = .bottom
		$0.transform = CGAffineTransform(scaleX: 0.3, y: 0.3)
	}
}
configuration.addNewPage(at: 0, configuration: imagePage)

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

// Add a page from a different document.
let document = Document(url: URL(fileURLWithPath: "example.pdf"))
let externalDocumentPageTemplate = PageTemplate(document: document, sourcePageIndex: 7)
let newPageFromDocument = PDFNewPageConfiguration(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 = PSPDFRectAlignmentBottom;
		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];