UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Draw text on all pages of PDF using PDFKit

Forums > macOS

I'm using the following code to draw text on a PDF Document.This seems to draw the text only on a single page.I'm trying to iterate through each page,draw string on it and finally display the PDF document from the MutableData. How do I draw the string on all pages?

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

3      

Does the PDF contain only the last page of text? That's what I expect to see from the code you showed.

You are creating a new context each time you go through the for loop.

let context = CGContext(consumer: consumer, mediaBox: &mediaBox, nil)!

When you call beginPDFPage and endPDFPage to create a new PDF page, you end up adding only one page to the context because there's a new context each time you go through the loop. Instead of creating X number of pages, you create X number of contexts with one page each.

Declare the context before the for loop. Now the new pages you add inside the for loop will be added to the same context.

You should also call closePDF after the for loop. With your current code, you're saving a PDF every time through the loop. Wait until you create all the pages before saving the PDF.

There may be other problems with the code. If you are still getting only a single page, set a breakpoint and step through the code line by line, making sure the variables contain the data you expect. The following article may help you if you still have problems:

Creating PDFs with Core Text and Quartz

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.