'PDF Generator that utilizes text fields instead of images | Swift / Xcode

My app creates a PDF, but the problem is the PDF is uneditable because the text is created as images, I believe.

How could I accomplish the same thing (it automatically continuing to the next page and everything) being done in the following code I've written, but instead the PDF text be editable in something like Adobe Acrobat Pro, etc.?

private func addText(context : UIGraphicsPDFRendererContext) {
    let layoutManager = NSLayoutManager()
    let textStorage = NSTextStorage()
    var text = NSAttributedString()
    if reportController.userDidEditReport {
        // the user edited the report, so use the temp report array
        text = combineTextsHeaders(array: reportController.tempReportDataToBePrinted)
    } else {
        // the user didn't edit the report, so use the preset report array
        text = combineTextsHeaders(array: reportController.presetReportDataToBePrinted)
    }
    textStorage.append(text)
    textStorage.addLayoutManager(layoutManager)
    var textContainerSize = CGSize()
    var textContainer : NSTextContainer
    var textViews = [UITextView]()
    // keep adding text containers/views
    repeat {
        if isFirstTextView {
            textContainerSize = CGSize(width: pageWidth - marginSize.width, height: pageHeight - theGreetingBottom - 10 - marginSize.height)
            isFirstTextView = false
        } else {
            textContainerSize = CGSize(width: pageWidth - marginSize.width, height: pageHeight - marginSize.height)
        }
        textContainer = NSTextContainer(size: textContainerSize)
        layoutManager.addTextContainer(textContainer)
        textViews.append(UITextView(frame: CGRect(origin: marginPoint, size: textContainerSize), textContainer: textContainer))
        // while the last glyph is not in a text container
    } while layoutManager.textContainer(forGlyphAt: layoutManager.numberOfGlyphs - 1, effectiveRange: nil) == nil
    // draw each text view
    var number = 0
    for textView in textViews {
        if number == 0 {
            // it's the first textView in the array
            context.cgContext.translateBy(x: marginPoint.x, y: theGreetingBottom + 10)
        } else {
            context.beginPage()
            addPageNumber(pageRect: pageRect)
            context.cgContext.translateBy(x: marginPoint.x, y: marginPoint.y)
        }
        textView.textContainerInset = .zero
        textView.backgroundColor = .white
        textView.textColor = .black
        textView.layer.render(in: context.cgContext)
        number = number + 1
    }
}

func combineTextsHeaders(array: [reportStruct]) -> NSMutableAttributedString {
    // create variables
    let attributedString = NSMutableAttributedString()
    let paraHeaderFont = Constants.Fonts.reportParaHeaderFont
    let paragraphFont = Constants.Fonts.reportParaBodyFont
    // run through each item in the proper reportController array to be printed
    for data in array {
        // first, append the Header
        let paraHeaderAttributes: [NSAttributedString.Key: Any] = [NSAttributedString.Key.font: paraHeaderFont as Any, NSAttributedString.Key.underlineStyle: NSUnderlineStyle.thick.rawValue]
        let paraHeaderString = NSMutableAttributedString(string: "\(data.header.uppercased())\n", attributes: paraHeaderAttributes)
        attributedString.append(paraHeaderString)
        
        // second, append the paragraph
        let paragraphAttributes: [NSAttributedString.Key: Any] = [NSAttributedString.Key.font: paragraphFont as Any]
        let paragraphString = NSMutableAttributedString(string: "\(data.body)\n\n", attributes: paragraphAttributes)
        attributedString.append(paragraphString)
    }
    return attributedString
}

PLEASE explain your answers. I'm self-taught mostly and want to understand the concepts – not copy code.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source