Since iOS/iPadOS 27 after adding .freeText annotations and exporting with PDFDocument.dataRepresentation(), the exported PDF contains embedded font streams missing the required /Length entry.
Reproduction steps:
Load a one-page PDF that passes qpdf --check into a PDFDocument.
Add.freeText annotations to the first page.
Save the bytes returned by PDFDocument.dataRepresentation() as freetext.pdf.
Run qpdf --check freetext.pdf on macOS.
Expected: The exported PDF passes validation.
Actual: qpdf reports stream dictionary lacks /Length key and attempts to recover the affected stream lengths.
Has anyone seen the same behavior on iOS/iPadOS 27? Is there a supported workaround or fix that keeps the annotation text selectable and searchable?
The code:
import Foundation
import PDFKit
import SwiftUI
import UIKit
struct ContentView: View {
@State private var status = "Ready"
var body: some View {
VStack(spacing: 16) {
Button("Run PDFKit Repro", action: runRepro)
Text(status)
}
.padding()
}
private func runRepro() {
guard let inputURL = Bundle.main.url(forResource: "input", withExtension: "pdf") else {
status = "input.pdf was not found in the app bundle."
return
}
do {
let inputData = try Data(contentsOf: inputURL)
guard let baseline = PDFDocument(data: inputData),
let annotated = PDFDocument(data: inputData),
let page = annotated.page(at: 0) else {
status = "PDFKit could not open input.pdf or its first page."
return
}
let bounds = page.bounds(for: .cropBox)
let offsets: [CGFloat] = [36, 72]
for offset in offsets {
let annotation = PDFAnnotation(
bounds: CGRect(
x: bounds.minX + 36,
y: bounds.minY + offset,
width: 120,
height: 24
),
forType: .freeText,
withProperties: nil
)
annotation.contents = "Custom Annotation"
page.addAnnotation(annotation)
}
guard let baselineData = baseline.dataRepresentation() else {
status = "PDFKit could not serialize the baseline PDF."
return
}
guard let annotatedData = annotated.dataRepresentation() else {
status = "PDFKit could not serialize the FreeText PDF."
return
}
let folder = FileManager.default.temporaryDirectory
let baselineURL = folder.appendingPathComponent("no-new-annotations.pdf")
let annotatedURL = folder.appendingPathComponent("freetext.pdf")
try baselineData.write(to: baselineURL, options: .atomic)
try annotatedData.write(to: annotatedURL, options: .atomic)
print("PDF_REPRO input: \(inputURL.path)")
print("PDF_REPRO baseline: \(baselineURL.path)")
print("PDF_REPRO FreeText: \(annotatedURL.path)")
status = "Exported both PDFs. Search for PDF_REPRO in the Xcode console."
} catch {
status = "PDF test failed: \(error.localizedDescription)"
}
}
}This issue also filed as FBxxxxxxxx.
0
0
23