問題描述
當我使用轉換時,我的打印輸出看起來不像打印預覽 (My printout doesn't look like the print preview when I use a Transform)
I'm trying to print out a DataGridView using a PrintDocument. In my PrintPage event handler, I use the e.Graphics object to draw some grid lines, then print some text in the "cells" created ‑‑ as an aside, this really should have been done by Microsoft, but I digress.
Anyway, this works just fine. The PrintDocument is tied to a PrintPreviewDialog, and when I open it, the DGV is rendered correctly. The user can click the Preview dialog's print button and get a printout. My problem is that sometimes the DGV is too wide for the page, so I'd like to be able to scale it (horizontally only) to fit. I'm calling
e.Graphics.ScaleTransform(printableWidth / gridWidth)
before I actually start drawing anything. This makes the preview look exactly the way I want it to, but for some reason, the actual printout looks exactly like it did before I added the code for scaling ‑‑ if the report is too wide, it just falls right off the side of the page!
Is there some difference between the way that a PrintPage event is handled for preview versus how it's sent to the printer? Would my run‑of‑the‑mill PostScript office laser printer not be able to handle the ScaleTransform function? I would think it should just blindly print what's sent to it, but maybe there's more processing involved than I thought.
‑‑‑‑‑
參考解法
方法 1:
OK, I found my problem ‑‑ I thought I was calling ScaleTransform for each page, but it turns out it was only actually executing on the first call to the PrintPage handler. Subsequent calls were skipping over the line due to a conditional it was wrapped in. So a word to the wise ‑‑ make sure that if you want to transform your printout, you do it independently for every page.