【发布时间】:2022-08-23 20:54:43
【问题描述】:
我正在使用以下代码在 PDF 文档上绘制文本。这似乎只在单个页面上绘制文本。我试图遍历每一页,在其上绘制字符串,最后显示 PDF 文档可变数据。如何在所有页面上绘制字符串?
var pdffile=PDFDocument(url: input)
let data = NSMutableData()
let consumer = CGDataConsumer(data: data as CFMutableData)!
for y in stride(from: 0, to: pdffile!.pageCount, by: 1)
{
let page: PDFPage = pdffile!.page(at: y)!
let outputBounds = page.bounds(for: PDFDisplayBox.mediaBox)
var mediaBox = CGRect(x: 0, y: 0, width: outputBounds.size.width, height: outputBounds.size.height)
let context = CGContext(consumer: consumer, mediaBox: &mediaBox, nil)!
NSGraphicsContext.current = NSGraphicsContext(cgContext: context, flipped: false)
context.beginPDFPage(nil)
page.draw(with: .mediaBox, to: context)
text.draw(in:drawrect,withAttributes:textFontAttributes);
context.endPDFPage()
context.closePDF()
}
let anotherDocument = PDFDocument(data:data as Data)
pdfview.document=anotherDocument
-
这回答了你的问题了吗? Generate multi-page PDF in Swift