【发布时间】:2015-11-06 15:48:18
【问题描述】:
我正在通过改编以前编写的桌面程序来打印拼图,为一位家庭成员制作了一本打印拼图的书。我需要每个拼图都以特定尺寸打印,在这种情况下为 5" 正方形。我似乎无法找到一种可靠的方式以编程方式进行打印。
这是一个实现,它使用通过反复试验确定的缩放因子来缩放绘制拼图的画布。这是用 Clojure 编写的,但我认为任何使用 Java 的人都可以理解。
(defn create-print-button-click-handler
"Handle a click on the 'Print...' button."
[canvas stage]
(reify EventHandler
(handle [this event]
(if false
(batch-print)
;; else
(let [job (PrinterJob/createPrinterJob)]
(if (.showPrintDialog job stage)
(let [printer (.getPrinter job)
job-settings (.getJobSettings job)
;; Margin settings are in points. Set to half inch left margin,
;; 3/4 inch for the rest.
layout (.createPageLayout printer Paper/NA_LETTER PageOrientation/PORTRAIT
36.0 54.0 54.0 54.0)
printable-width (.getPrintableWidth layout)
printable-height (.getPrintableHeight layout)
printer-dpi (.getFeedResolution (.getPrintResolution job-settings))
dots-across (* printer-dpi 5) ;; five inches
cnvs (Canvas. dots-across dots-across)
scale 0.25]
(.setPrintColor job-settings PrintColor/MONOCHROME)
(.setPageLayout job-settings layout)
;; Scale by the same amount along both axes.
(.add (.getTransforms cnvs) (Scale. scale scale))
;; This ugliness is because I want to print the background completely white.
;; Since we are using the same function to draw the board to the screen and
;; to the canvas for printing, we need to change the background before
;; drawing then back afterwards.
(def board-color (Color/web "#ffffff"))
(redraw-board cnvs)
(def board-color (Color/web board-web-color))
(.printPage job cnvs)
(.endJob job))))))))
我见过一些使用可打印宽度和高度的示例,但我没有得到可以理解的结果(太大而无法容纳页面)。
就像我说的,这可行,但我希望程序在使用其他可能具有不同分辨率、不同水平和垂直分辨率等的打印机时正确响应。
【问题讨论】: