C # print printdocument

Source: Internet
Author: User

In the window printing process, we found that the image was still very good, that is, painting on printdocument. Just like painting on canvas, what would you like? First, let's look at the sample code.

Private void button#click (Object sender, routedeventargs e) {var printdocument = new printdocument (); // specifies the printer printdocument. printersettings. printername = "Microsoft XPS document writer"; // sets the page margin printdocument. printersettings. defaultpagesettings. margins. left = 0; printdocument. printersettings. defaultpagesettings. margins. top = 0; printdocument. printersettings. defaultpagesettings. margins. right = 0; printdocument. printersettings. defaultpagesettings. margins. bottom = 0; // set the size. If this parameter is not set, the default size is A4 paper. // the size of A4 paper is 210mm × 297mm, // when the resolution you set is 72 pixels/inch, the size of the A4 paper image pixel is 595x842 // when the resolution you set is 150 pixels/inch, a4 paper size image pixels are 1240x1754 // when you set the resolution to 300 pixels/inch, A4 paper size image pixels are 2479x3508, printdocument. defaultpagesettings. papersize = new papersize ("A4", 595,842); printdocument. printpage + = new printpageeventhandler (printdocument_printpage); try {printdocument. print ();} catch (invalidprinterexception) {} finally {printdocument. dispose () ;}// print method void printdocument_printpage (Object sender, printpageeventargs e) {var printcontent = "Print test"; var printfont = new font ("", 12, system. drawing. fontstyle. regular); var printcolor = system. drawing. brushes. black; var pointy = 10f; // draw the string E. graphics. drawstring (printcontent, printfont, printcolor, 10f, pointy); // you can specify the fontstyle (bold, skewed, font = new font ("", 12, system. drawing. fontstyle. bold | system. drawing. fontstyle. italic); E. graphics. drawstring (printcontent, printfont, printcolor, 10f, pointy + = 20f); printfont = new font ("", 12, system. drawing. fontstyle. regular); // draw an image // E. graphics. drawimage (image, 10, 50); // sets the coordinate system scaling // sets the printing coordinate system x to 0.6 times the original value to print E. graphics. scaletransform (0.6f, 1.0f); E. graphics. drawstring (printcontent, printfont, printcolor, 10f, pointy + = 20f); // restore coordinate system scaling E. graphics. scaletransform (1/0.6f, 1.0f); E. graphics. drawstring (printcontent, printfont, printcolor, 10f, pointy ++ = 20f); // save and restore the settings of painting var status = E. graphics. save (); E. graphics. scaletransform (0.6f, 1.0f); E. graphics. drawstring (printcontent, printfont, printcolor, 10f, pointy + = 20f); E. graphics. restore (Status); E. graphics. drawstring (printcontent, printfont, printcolor, 10f, pointy + = 20f); // set the hasmorepages value to true if the next page is displayed. E. hasmorepages = false ;}

There are many methods in graphics. You can refer to this document. Here we only list several common methods,

The effect of printing the above Code is as follows:

The zoom printing method is not suitable for the dot matrix printer. The scaled text cannot be viewed on the dot matrix printer. Later, the generated XPS document is saved in the image format.

The XPs document is in gzip compression format. You can use RAR to decompress it and view it.

The directory structure for generating XPS documents is as follows:

1. fpage is the generated document structure. Other directories are related resource files, such as the fonts font folder and images image folder in resources.

The following is the content generated in 1. fpage. You can see that the format is XML.

<Fixedpage width = "571.04" Height = "808.48" xmlns = "http://schemas.microsoft.com/xps/2005/06" XML: lang = "und"> <! -- Microsoft XPS document converter (mxdc) generated! Version: 0.3.7600.16385 --> <glyphs fill = "# ff000000" fonturi = "/documents/1/resources/fonts/FBB4FB52-34A4-45CC-836A-F18952F112F9.odttf" fontrenderingemsize = "16.016" stylesimulations = "NONE" originx INX = "12.32" originy = "23.36" indices = "6275; 2464; 9083; 16901 "unicodestring =" Print test "/> <canvas renderoptions. edgemode = "aliased"> <path data = "m 13.92, 29.6 l 79.36, 29.6 79.36, 44.48 13.92, 44.48 Z"> <path. fill> <imagebrush imagesource = "/documents/1/resources/images/1.png" viewbox =" 0, 0, 13.92 "tilemode =" NONE "viewboxunits =" absolute "viewportunits =" absolute "viewport =" 29.6, 65.44, 14.88 "/> </path. fill> </path> <path data = "m 7.84, 48.8 l 45.44, 48.8 45.44, 63.68 7.84, 63.68 Z"> <path. fill> <imagebrush imagesource = "/documents/1/resources/images/2.png" viewbox =, 7.84 "tilemode =" NONE "viewboxunits =" absolute "viewportunits =" absolute "viewport =" 48.8, 37.6, 14.88 "/> </path. fill> </path> </canvas> <glyphs fill = "# ff000000" fonturi = "/documents/1/resources/fonts/FBB4FB52-34A4-45CC-836A-F18952F112F9.odttf" fontrenderingemsize = "16.016" stylesimulations =" none "originx =" 12.32 "originy =" 80.96 "indices =" 6275; 2464; 9083; 16901 "unicodestring =" Print test "/> <path data =" m 7.84, 87.2 l 45.44, 87.2 45.44, 102.08 7.84, 102.08 Z "> <path. fill> <imagebrush imagesource = "/documents/1/resources/images/2.png" viewbox =, 7.84 "tilemode =" NONE "viewboxunits =" absolute "viewportunits =" absolute "viewport =" 87.2, 37.6, 14.88 "/> </path. fill> </path> <glyphs fill = "# ff000000" fonturi = "/documents/1/resources/fonts/FBB4FB52-34A4-45CC-836A-F18952F112F9.odttf" fontrenderingemsize = "16.016" stylesimulations = "NONE" originx ations = ""12.32" originy = "119.36" indices = "6275; 2464; 9083; 16901 "unicodestring =" Print test "/> </fixedpage>

If you want to operate the XPS document, you can reference the reachframework. dll, system. Windows. XPS. Packaging namespace.
It encapsulates the operation methods of the XPS document. Here we will introduce some knowledge of XPS.

By the way, paste a detailed document for your reference.

Http://blog.csdn.net/gkq8124372/article/details/7494272

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.