3

I am working on a pdf reader but I am facing problem in creating night mode for a PDF Document. I have successfully created the new PDF Document with Black background and white foreground. but can not do for an already created document I am doing for new PDF Document is

func show(text:NSAttributedString) {


       // let attributedString = NSAttributedString(string: text, attributes: [NSAttributedString.Key.foregroundColor: UIColor.white])
   // text.attribute(NSAttributedString.Key.foregroundColor, at: 0, effectiveRange: NSRange(location: 0, length: text.length))

        let printFormatter = UISimpleTextPrintFormatter(attributedText: text)
        let renderer = UIPrintPageRenderer()
        renderer.addPrintFormatter(printFormatter, startingAtPageAt: 0)
        // A4 size
        let pageSize = CGSize(width: 595.2, height: 841.8)
        // Use this to get US Letter size instead
        // let pageSize = CGSize(width: 612, height: 792)

        // create some sensible margins
        let pageMargins = UIEdgeInsets(top: 72, left: 72, bottom: 72, right: 72)

        // calculate the printable rect from the above two
        let printableRect = CGRect(x: pageMargins.left, y: pageMargins.top, width: pageSize.width - pageMargins.left - pageMargins.right, height: pageSize.height - pageMargins.top - pageMargins.bottom)

        // and here's the overall paper rectangle
        let paperRect = CGRect(x: 0, y: 0, width: pageSize.width, height: pageSize.height)

        renderer.setValue(NSValue(cgRect: paperRect), forKey: "paperRect")
        renderer.setValue(NSValue(cgRect: printableRect), forKey: "printableRect")
        let pdfData = NSMutableData()

        UIGraphicsBeginPDFContextToData(pdfData, paperRect, nil)
        renderer.prepare(forDrawingPages: NSMakeRange(0, renderer.numberOfPages))

        let bounds = UIGraphicsGetPDFContextBounds()
        for i in 0  ..< renderer.numberOfPages {
            UIGraphicsBeginPDFPage()
            let currentContext = UIGraphicsGetCurrentContext()
            currentContext?.setFillColor(UIColor.yellow.cgColor)
            currentContext?.fill(bounds)
            renderer.drawPage(at: i, in: bounds)

        }

        UIGraphicsEndPDFContext()

        var pdfUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
        pdfUrl?.appendPathComponent("text")
        pdfUrl?.appendPathExtension("pdf")
        do {
            try pdfData.write(to: pdfUrl!)
        } catch {
            print(error.localizedDescription)
        }
    let pdfdocemrnt = PDFDocument(url: pdfUrl!)
    self.baseView.document = pdfdocemrnt!
    }

Please help if someone knows the issue.

Awais Mobeen
  • 733
  • 11
  • 19
  • 1
    I was looking for the same thing. I don't think this exists right now. I saw a Twitter post about the things devs want most from Swift, and one posted "dark mode for PDFKit", which confirms my suspicion it's not doable. There's a 3rd-party commercial lib called PSPDF that seems to have this capability somewhat, as they address it in their documentation. – James Toomey Aug 03 '22 at 17:26

0 Answers0