Export (PDF, TIFF, JPEG, PNG) Pixelnetica™ Document Scanning SDK for Apple iOS
Once a page is cropped, enhanced, and (optionally) recognised, you write it to a file with PxImageWriter. The same API produces single images, multi-page PDFs, and searchable PDFs with an embedded invisible text layer.
Output formats
PxImageWriter_Type selects the format: PxImageWriter_Type_JPEG, PxImageWriter_Type_PNG, PxImageWriter_Type_PNG_EXT, PxImageWriter_Type_TIFF, and PxImageWriter_Type_PDF. PNG, TIFF, and PDF are licensed features.
Write a page
Create a writer for the format, open an output path, write the picture, and close. Engine calls can raise exceptions, so wrap them with PxCatchExceptions:
import DocScanningSDK
let writer = PxImageWriter.new(PxImageWriter_Type_PDF)
var writtenPath: String?
try PxCatchExceptions.do {
writer.open(outputPath)
writtenPath = writer.write(picture) // write throws PxException for license-gated formats
}
writer.close()
Searchable PDF
To embed the OCR result as an invisible, selectable text layer behind the image, pass the page’s PxTextResult to write(_:with:) — the text layer is hidden by default:
let writer = PxImageWriter.new(PxImageWriter_Type_PDF)
var writtenPath: String?
try PxCatchExceptions.do {
writer.open(outputPath)
writtenPath = writer.write(picture, with: picture.scanResult)
}
writer.close()
Use setFonts(_:) to supply the fonts for the embedded text layer. See OCR & text recognition for producing the PxTextResult.
Multi-page PDFs
For a multi-page document, open the writer once, write each page, then close — every write call appends a page:
let writer = PxImageWriter.new(PxImageWriter_Type_PDF)
try PxCatchExceptions.do {
writer.open(outputPath)
for page in pages { _ = writer.write(page, with: page.scanResult) }
}
writer.close()
Compression
PDF (and TIFF) output is compressed per page. Call setCompressionLevel(_:) before writing, passing one of the PxImageWriter_CompressionLevel presets — or any custom rate >= 1. Higher means a smaller file at lower quality:
let writer = PxImageWriter.new(PxImageWriter_Type_PDF)
writer.setCompressionLevel(Float(PxImageWriter_CompressionLevel_Medium.rawValue))
| Preset | Use when |
|---|---|
PxImageWriter_CompressionLevel_Lossless | Archival quality — no quality loss, largest files. |
PxImageWriter_CompressionLevel_Low | Default. Balanced size and quality for everyday documents. |
PxImageWriter_CompressionLevel_Medium | Noticeably smaller, for sharing or upload, with slight quality loss. |
PxImageWriter_CompressionLevel_High | Aggressive size reduction when storage or bandwidth is tight. |
PxImageWriter_CompressionLevel_Extreme | Smallest files, most visible quality loss. |
The level tunes the lossy compression of colour and greyscale pages only. Black-and-white pages are always stored losslessly, whatever the level — see Image enhancement. If you never call setCompressionLevel(_:), the writer uses Low.
Viewing the PDFs. To keep files small, the SDK uses modern PDF image compression — JBIG2 for black-and-white pages and JPEG 2000 for colour and greyscale. Both are part of the PDF standard and render correctly in current mainstream viewers, including Apple’s built-in Preview and Quick Look on recent macOS and iOS, and the previews in Google Docs and Google Drive. Some older or lightweight third-party readers don’t fully implement these codecs, though, and may show such a page blank or render it slowly. If you need a viewer that fully supports both across the board, Adobe Acrobat and Adobe Reader do.
More controls
- Paper size & orientation: configure the PDF page geometry with the writer’s paper-configuration API (see
PxPaperand the API reference). - Footer: add a footer with text (and an optional URL) via the writer’s footer configuration.
Tips
- Always
close()the writer — for PDFs this finalises the file. - Confirm the format’s feature is enabled on your license before exporting (PNG / TIFF / PDF are gated).