Example on how to print a NSTableView from your app but then also add text to the print-out.

Overview

NSTableView Printing Test

This is a demo project so you can check out how printing a table could work.

The goal here is to show tabular data on screen, and add text for print-outs.

Screenshot of the print dialog
Print dialog
Screenshot Preview.app showing the print result
PDF result

Discussion

NSView-based printing is a bit odd since it prints whatever NSView.draw(_:) produces. We can customzie that by overriding the draw method. But for a table view? Embedding a stock standard table view in a custom view hierarchy sounds more promising.

To add a multi-line label on page, we cannot use the stock view-based printing options since the label is not part of the view hierarchy. We need to set up our own view hierarchy with all the to-be-printed views.

For the simple page layout used here, I picked a vertical NSStackView. This can be printed via NSPrintOperation(view: stackView) and then showing the print dialog for that.

For complex floating layouts, a NSTextView with an attributed string and maybe even a custom NSLayoutManager might be necessary. That's how you could, in theory, get text wrapped around images or small tables like Pages does it.

Actually recommended by other devs: use WKWebView and HTML. WebKit does a great job with complex layouts; just look at Safari. Then you can export the web view's content as PDF, and then use the PDF data for the print dialog directly.

See my accompanying blog post for details and a recap:
https://christiantietze.de/posts/2021/06/printing-nstableview-with-label/

License

This code is in the Public Domain. Do what you want with it.

You might also like...
This is an Instagram clone with a custom Parse backend that allows a user to post photos, view a global photos feed, and add comments!
This is an Instagram clone with a custom Parse backend that allows a user to post photos, view a global photos feed, and add comments!

Parstagram - Part II This is an Instagram clone with a custom Parse backend that allows a user to post photos, view a global photos feed, and add comm

Advanced Catalyst Example with sidebar, list view, SwiftUI detail view, toolbar & AppKit bundle
Advanced Catalyst Example with sidebar, list view, SwiftUI detail view, toolbar & AppKit bundle

Advanced Catalyst Example This is an example of a Catalyst app using a three-column layout, with a primary toolbar. It includes topics such as: Drag &

Example for RxFeedback

RxFeedback Sample This is a repository for the RxFeedback code sample. Running Install bazel (This project is tested with Bazel 4.0.0) bazel run //App

An example project of using the new Character Controller component in RealityKit 2.0
An example project of using the new Character Controller component in RealityKit 2.0

CharacterController in RealityKit 2.0 An example project of using the new Character Controller component in RealityKit 2.0. Demo Tweet Usage Install a

This is example project for my presentation in iOSDC JAPAN 2021

Swift PM Project Example This is example project for my presentation in iOSDC JAPAN 2021. Package.swift based project management Multi Modules Multi P

Example Xcode swift iOS project for Core Data + iCloud syncing
Example Xcode swift iOS project for Core Data + iCloud syncing

iCloudCoreDataStarter Hello, I'm Chad. For the last several months I have been working on Sticker Doodle, an app you should go download right now! In

An example of adding a faux notch using AppKit + SwiftUI
An example of adding a faux notch using AppKit + SwiftUI

faux-notch An example of adding a faux notch using AppKit + SwiftUI What is this? It's a bare-bones example that renders a fake MacBook notch in macOS

A repository of example plugins for Delta Client

Example Plugins for Delta Client This repository contains a few example plugins to help developers get a practical understanding of how to use the plu

CustomPod Example Axon With Swift

CustomPod_Example_Axon Example To run the example project, clone the repo, and run pod install from the Example directory first. Requirements Installa

Owner
Clean Cocoa
On the way to write better iOS and macOS apps!
Clean Cocoa
Code Challenge - Using Alamofire is a Swift-based, HTTP networking library, also Codable for Data Model and Combine Framework .

Code Challenge ##Using Alamofire is a Swift-based, HTTP networking library, also Codable for Data Model and Combine Framework . Alamofire is one of th

Eng Angelo Saber 0 Nov 24, 2021
GLWalkthrough is an easily configurable plug-and-play tool to add walkthrough or coachmarker functionality to your app with ease.

GLWalkthrough Example To run the example project, clone the repo, and run pod install from the Example directory first. Requirements Installation GLWa

Gokul 40 Dec 17, 2022
Example project guide you schedules multiple thread for network requests in RxSwift, which is optimize your app's performance better.

RxSwift-Multi-Threading-Example Example project guide you schedules multiple thread for network requests in RxSwift, which is optimize your app's perf

Huy Trinh Duc 6 Nov 4, 2022
An example app with using ShazamKit

ShazamKitExample An example app with using ShazamKit. Check out a live demo on Twitter. Related Resources Introducing ShazamKit WWDC21 — Explore Shaza

Artem Novichkov 10 May 25, 2022
Example App for playing around with

BookStore ?? 한글 버전 See new releases and search for programming books from IT Bookstore API This is a sample app to practice using Result type, stubbin

Danny Gilbert 0 Nov 3, 2021
MVVM example app with RxSwift & RxDataSources & Dependency Injection & UnitTests

Hi there, This is MVVM example app with RxSwift & RxDataSources & Dependency Injection & UnitTests and more ?? MVVM with RxSwift Example Features: Rea

Ali Fayed 9 Aug 30, 2022
SwiftUI Todo app example using a React/Redux monolithic state store with flux like dispatch/reduce actions

SwiftUI-Todo-Redux SwiftUI Todo Redux app example using a React/Redux monolithic state store with flux like dispatch/reduce actions Background SwiftUI

moflo 12 Nov 29, 2022
SwiftUI Navigation example app

SwiftUINavigationExample SwiftUI Navigation example app. Navigation is managed by a single class called NavSwitches. This contains all the booleans th

Luke Smith 4 Feb 18, 2022
Example app source code developed by swift language from apple

AboutMe Example app source code developed by swift language from apple. Display data from a central source in multiple views. Welcome to the About Me

yuezht 0 Mar 29, 2022