0

I want to print out a list in my App.

However if the list is too large to fit on one page there is a problem with the last and the first line on a page.

e.g. the last line on a page is cropped and some parts are shown on top of next page.

Screenshot of bottom of page 1 Screenshot of bottom of page 1

Screenshot of top of page 2

Screenshot of top of page 2

How can I manage to set up my print job that the text is set up correctly on every page?

Currently I have the following code snippet:

   func makePDF(markup: String, amount: Int) {
    let directoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
    let printOpts: [NSPrintInfo.AttributeKey: Any] = [NSPrintInfo.AttributeKey.jobDisposition: NSPrintInfo.JobDisposition.save, NSPrintInfo.AttributeKey.jobSavingURL: directoryURL]
    let printInfo = NSPrintInfo(dictionary: printOpts)
    printInfo.horizontalPagination = NSPrintInfo.PaginationMode.autoPagination
    printInfo.verticalPagination = NSPrintInfo.PaginationMode.autoPagination
    printInfo.topMargin = 20.0
    printInfo.leftMargin = 20.0
    printInfo.rightMargin = 20.0
    printInfo.bottomMargin = 20.0

    let view = NSView(frame: NSRect(x: 0, y: 0, width: 560, height: (60 + 36 * amount)))

    if let htmlData = markup.data(using: String.Encoding.utf8) {
        let attrStr = NSAttributedString(html: htmlData, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding:
            String.Encoding.utf8.rawValue], documentAttributes: nil)

        let frameRect = NSRect(x: 0, y: 0, width: 560, height: (60 + 36 * amount))
        let textField = NSTextField(frame: frameRect)
        textField.attributedStringValue = attrStr!
        view.addSubview(textField)

        let printOperation = NSPrintOperation(view: view, printInfo: printInfo)
        printOperation.showsPrintPanel = true
        printOperation.showsProgressPanel = true
        printOperation.run()

    }
}
Oliver Koehler
  • 711
  • 2
  • 8
  • 24
  • What is "correctly"? Does [Selecting the Page Bounds for Content That Exceed a Single Page](https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Printing/osxp_pagination/osxp_pagination.html#//apple_ref/doc/uid/20001051-119037) help? – Willeke Mar 14 '18 at 23:49
  • @Willeke: "correctly" would mean that a cropped line like above can be displayed uncropped on the next page. – Oliver Koehler Mar 15 '18 at 10:54
  • Does `width: 560, height: (60 + 36 * amount)` fit on one page? Do you need `view` or will printing a text view do? – Willeke Mar 15 '18 at 11:53

0 Answers0