Printing
A program prints by answering an event: the PrintPage event runs once for each page, and the program draws the page. This lesson prints a report of 100 rows on several pages with a header, a page number and a footer, changes the page to landscape, and looks at the pages as pictures without a printer.
What you will learn#
PrintDocumentand the PrintPage event,- drawing a page on
e.Graphicsinside the margins (e.MarginBounds), - paging: how many rows fit, and
e.HasMorePages, - paper size, margins and landscape (
DefaultPageSettings), - the preview controller: pages as pictures, no printer needed,
- why a page of the preview is a metafile, and how to look at its pixels.
Printing is drawing: the tutorial Images used Graphics to draw on a bitmap, and here the same calls draw on a page. These classes are part of Windows Forms and need Windows.
The program#
# printing - PrintDocument and the PrintPage event: a report on several pages, margins, page numbers, landscape, and a preview without a printer
load "dotnet.ring"
using {
"System"
"System.IO"
"System.Drawing"
"System.Drawing.Imaging"
"System.Drawing.Printing"
"System.Windows.Forms" # (it also loads the helpers uiFont and uiColor)
}
aRows = [] # the report: [number, name, price]
nPage = 0 # the page that is being printed
nNextRow = 1 # the first row that has not been printed yet
aOnPage = [] # how many rows each page got
func main
# 1. A report with 100 rows
for i = 1 to 100
aRows + [ i, "Item " + right("000" + i,3), 2.5 + i * 0.75 ]
next
? "1. the report has " + len(aRows) + " rows"
# 2. A PrintDocument is a document that can be sent to a printer. The program does not draw on the printer: it answers the event
# PrintPage, once for each page, and draws on e.Graphics. When the page is full it sets e.HasMorePages = True and the document
# asks for the next page.
oDocument = new PrintDocument()
oDocument.DocumentName = "Price list"
# 3. The PREVIEW controller: the pages are made as pictures in memory, and no printer and no dialog are used. The program (and the
# test) can then look at the pages. A real print job leaves this line out. The paper is set explicitly (A4, in hundredths of an
# inch, with margins of half an inch), so the pages are the same on every computer.
oPreview = new PreviewPrintController()
oDocument.PrintController = oPreview
setupPaper(oDocument,False)
? nl + "2. paper: " + oDocument.DefaultPageSettings.PaperSize.Width + " x " + oDocument.DefaultPageSettings.PaperSize.Height + " hundredths of an inch"
# 4. Print: the event runs for each page
oDocument.setPrintPageEvent("drawPage(aDotNetArgs[2])")
nPage = 0
nNextRow = 1
aOnPage = []
oDocument.Print()
? nl + "3. the report needed " + nPage + " pages; rows on each page: " + joinList(aOnPage)
# 5. The preview pages are pictures, but not bitmaps: each one is a METAFILE, a list of the drawing commands that the program gave
# (it can be drawn at any size). To look at the pixels the program draws the metafile on a white bitmap of 827 x 1169 pixels.
aPages = oPreview.GetPreviewPageInfo().toList()
oBitmap = pageBitmap(aPages[1].Image,827,1169)
? nl + "4. the preview has " + len(aPages) + " pages; the first as a bitmap is " + oBitmap.Width + " x " + oBitmap.Height + " pixels"
? " a pixel in the margin is " + oBitmap.GetPixel(10,10).Name + ", and the page has black pixels: " + yesNo(countDark(oBitmap) > 0)
cFile = new Path().Combine([ new Path().GetTempPath(), "ringdotnet-page1.png" ])
oBitmap.Save([ :filename = cFile, :format = new ImageFormat().Png ])
? " the first page was saved as a picture: " + yesNo(new FileInfo(cFile).Length > 0)
new File().Delete(cFile)
oLast = pageBitmap(aPages[len(aPages)].Image,827,1169)
? " the last page has less on it than the first: " + yesNo(countDark(oLast) < countDark(oBitmap))
# 6. Landscape: the page is wide and low, so fewer rows fit and more pages are needed
nPortrait = nPage
setupPaper(oDocument,True)
oPreview = new PreviewPrintController()
oDocument.PrintController = oPreview
nPage = 0
nNextRow = 1
aOnPage = []
oDocument.Print()
? nl + "5. in landscape: " + nPage + " pages instead of " + nPortrait + "; rows on each page: " + joinList(aOnPage)
# A4 is 827 x 1169 hundredths of an inch; the margins are 50 (half an inch). Landscape turns the page.
func setupPaper oDocument,lLandscape
oPaperSize = new PaperSize().@New([ :name = "A4", :width = 827, :height = 1169 ])
oDocument.DefaultPageSettings.PaperSize = oPaperSize
oMargins = new Margins().@New([ :left = 50, :right = 50, :top = 50, :bottom = 50 ])
oDocument.DefaultPageSettings.Margins = oMargins
oDocument.DefaultPageSettings.Landscape = lLandscape
# The PrintPage event: draw one page. e.MarginBounds is the rectangle inside the margins (in hundredths of an inch).
func drawPage oArgs
nPage++
oGraphics = oArgs.Graphics
oBounds = oArgs.MarginBounds
oFont = uiFont("Arial",10,False)
oBold = uiFont("Arial",10,True)
oBrush = new SolidBrush(uiColor("Black"))
nLine = oFont.@GetHeight(oGraphics) # the height of a line of text in the unit of the page (hundredths of an inch)
nY = oBounds.Top
# the header: the title and the page number
oGraphics.DrawString([ :s = "Price list", :font = oBold, :brush = oBrush, :point = new PointF(oBounds.Left,nY) ])
oGraphics.DrawString([ :s = "Page " + nPage, :font = oBold, :brush = oBrush, :point = new PointF(oBounds.Right - 60,nY) ])
nY += nLine * 2
# the rows: as many as fit above the bottom margin (one line is kept for the footer)
nCount = 0
while nNextRow <= len(aRows) and nY + nLine * 2 <= oBounds.Bottom
aRow = aRows[nNextRow]
oGraphics.DrawString([ :s = "" + aRow[1], :font = oFont, :brush = oBrush, :point = new PointF(oBounds.Left,nY) ])
oGraphics.DrawString([ :s = aRow[2], :font = oFont, :brush = oBrush, :point = new PointF(oBounds.Left + 80,nY) ])
oGraphics.DrawString([ :s = "" + aRow[3], :font = oFont, :brush = oBrush, :point = new PointF(oBounds.Left + 260,nY) ])
nY += nLine
nNextRow++
nCount++
end
aOnPage + nCount
# the footer, and the answer to the document: are there more pages?
oGraphics.DrawString([ :s = "(" + (nNextRow - 1) + " of " + len(aRows) + " rows printed)", :font = oFont, :brush = oBrush, :point = new PointF(oBounds.Left,oBounds.Bottom - nLine) ])
oArgs.HasMorePages = (nNextRow <= len(aRows))
# a metafile drawn on a white bitmap of the given size
func pageBitmap oMetafile,nWidth,nHeight
oBitmap = new Bitmap(nWidth,nHeight)
oGraphics = new Graphics().FromImage(oBitmap)
oGraphics.Clear(uiColor("White"))
oGraphics.DrawImage([ :image = oMetafile, :rect = new Rectangle(0,0,nWidth,nHeight) ])
oGraphics.Dispose()
return oBitmap
# how many pixels of a picture are dark (a step of 4 keeps it quick)
func countDark oImage
nDark = 0
for x = 0 to oImage.Width - 1 step 4
for y = 0 to oImage.Height - 1 step 4
oColor = oImage.GetPixel(x,y)
if oColor.R < 100 and oColor.G < 100 and oColor.B < 100
nDark++
ok
next
next
return nDark
func joinList aList
cText = ""
for item in aList
if cText != ""
cText += ", "
ok
cText += "" + item
next
return cText
func yesNo lValue
if lValue
return "yes"
ok
return "no"1. the report has 100 rows
2. paper: 827 x 1169 hundredths of an inch
3. the report needed 2 pages; rows on each page: 57, 43
4. the preview has 2 pages; the first as a bitmap is 827 x 1169 pixels
a pixel in the margin is ffffffff, and the page has black pixels: yes
the first page was saved as a picture: yes
the last page has less on it than the first: yes
5. in landscape: 3 pages instead of 2; rows on each page: 36, 36, 28How it works#
A document and its event#
A PrintDocument does not draw by itself. Print() starts the job, and for each page the document raises PrintPage; the program draws on e.Graphics and, if the page is full, sets e.HasMorePages = True. The document then asks for the next page, and so on until the program answers False. The state of the job (the number of the page, the next row to print) is kept in global variables, because each call of the event is separate.
The page#
The unit of the page is a hundredth of an inch. e.MarginBounds is the rectangle inside the margins (Left, Top, Right, Bottom), so the program never draws into the margin. The height of a line comes from the font: Font.@GetHeight(graphics) gives it in the unit of the graphics. The header has the title and the page number, the rows follow, and the loop stops when the next line would not fit above the bottom margin (one line is kept for the footer). In the output the first page has 57 rows and the second 43: 100 rows in all.
Paper and margins#
DefaultPageSettings has the PaperSize, the Margins and Landscape. The program sets the paper explicitly (A4 is 827 by 1169 hundredths of an inch), so the pages are the same on every computer; without it the size comes from the default printer, which can be A4 or Letter. In landscape the page is wide and low, so fewer rows fit and the same report needs three pages instead of two.
The row counts depend on the font (Arial here): a computer without that font would use another one and could fit a different number of rows.
Preview without a printer#
PrintController = new PreviewPrintController() makes the pages as pictures in memory and sends nothing to a printer. A real job leaves this line out and the default controller sends the pages to the printer (add PrintDialog before it if the user should choose a printer). GetPreviewPageInfo() returns the pages, and Image is the picture of a page.
A page is a metafile#
The preview image is a metafile: the list of the drawing commands, not pixels (its size is in the units of the printer, 4963 by 7015 at 600 dots per inch). A metafile has no GetPixel. The function pageBitmap draws it on a white Bitmap of 827 by 1169 pixels, and then the program can read pixels: the margin is white, the page has black text, and the last page has less on it than the first. The bitmap can be saved as a PNG.
Printing needs an installed printer for the page settings, even for a preview. On Windows the Microsoft Print to PDF printer is always there; a computer without any printer raises an error when the document asks for its settings.
What you learned#
- the PrintPage event and
HasMorePages, - drawing inside
MarginBoundsand paging by the height of a line, - paper, margins and landscape,
- the preview controller and looking at a page as a bitmap.
Exercises#
- Print a table with lines between the rows and the columns (
DrawLine). - Print the number of the page as Page 2 of 3: first count the pages with the preview controller, then print.
- Add a button to a form that prints the report with
PrintDialog. - Print a picture on its own page and scale it to fit inside the margins.