C# PrintDocument 列印

來源:互聯網
上載者:User

進來搞Window列印,發現還是很好弄的,就是在PrintDocument上畫,跟在Canvas畫一樣,要什麼樣子就畫什麼樣子,先上範例程式碼

private void button1_Click(object sender, RoutedEventArgs e)        {            var printDocument = new PrintDocument();            //指定印表機            printDocument.PrinterSettings.PrinterName = "Microsoft XPS Document Writer";            //設定頁面邊界            printDocument.PrinterSettings.DefaultPageSettings.Margins.Left = 0;            printDocument.PrinterSettings.DefaultPageSettings.Margins.Top = 0;            printDocument.PrinterSettings.DefaultPageSettings.Margins.Right = 0;            printDocument.PrinterSettings.DefaultPageSettings.Margins.Bottom = 0;            //設定尺寸大小,如不設定預設是A4紙            //A4紙的尺寸是210mm×297mm,            //當你設定的解析度是72像素/英寸時,A4紙的尺寸的映像的像素是595×842            //當你設定的解析度是150像素/英寸時,A4紙的尺寸的映像的像素是1240×1754            //當你設定的解析度是300像素/英寸時,A4紙的尺寸的映像的像素是2479×3508,            printDocument.DefaultPageSettings.PaperSize = new PaperSize("A4", 595, 842);            printDocument.PrintPage += new PrintPageEventHandler(printDocument_PrintPage);            try            {                printDocument.Print();            }            catch (InvalidPrinterException)            {            }            finally            {                printDocument.Dispose();            }        } //列印方法        void printDocument_PrintPage(object sender, PrintPageEventArgs e)        {            var printContent ="列印測試";            var printFont = new Font("宋體", 12, System.Drawing.FontStyle.Regular);            var printColor = System.Drawing.Brushes.Black;            var pointY = 10f;            //畫字串            e.Graphics.DrawString(printContent, printFont, printColor, 10f, pointY);                        //如何列印帶粗體,傾斜,字型中帶橫線,底線的字串,設定字型的FontStyle(粗體,傾斜,字型中帶橫線,底線)            printFont = 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);            //畫映像            //e.Graphics.DrawImage(Image, 10, 50);            //設定座標系縮放            //設定列印座標系X值為原值的0.6倍列印            e.Graphics.ScaleTransform(0.6f, 1.0f);            e.Graphics.DrawString(printContent, printFont, printColor, 10f, pointY += 20f);            //恢複座標系縮放            e.Graphics.ScaleTransform(1 / 0.6f, 1.0f);            e.Graphics.DrawString(printContent, printFont, printColor, 10f, pointY += 20f);            //繪畫的設定儲存與恢複            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);            //如果列印還有下一頁,將HasMorePages值置為true;            e.HasMorePages = false;        }

其中Graphics中的方法非常多,大家可以去參考文檔,這裡只是列出幾個常用的,

以片就是以上代碼列印後的效果

其中的縮放的列印方式不適合針式印表機,縮放後的文字在針式印表機上基本無法查看,後來查看產生的XPS文檔,在文檔裡是圖片格式儲存的

XPS文檔是GZip壓縮格式可以使用RAR解壓後查看

以下是產生xps文檔的目錄結構

其中1.fpage就是產生的文檔結構,其它目錄都是一些相關的資源檔,比如Resources中的Fonts字型檔夾,Images影像檔夾等等

下面是1.fpage中產生的內容,大家可以看出裡面的格式就是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="12.32"           OriginY="23.36"           Indices="6275;2464;9083;16901"          UnicodeString="列印測試" />    <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,409,93" TileMode="None" ViewboxUnits="Absolute"                     ViewportUnits="Absolute" Viewport="13.92,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="0,0,235,93" TileMode="None" ViewboxUnits="Absolute"                    ViewportUnits="Absolute" Viewport="7.84,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="列印測試" />    <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="0,0,235,93" TileMode="None" ViewboxUnits="Absolute"                  ViewportUnits="Absolute" Viewport="7.84,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="12.32" OriginY="119.36" Indices="6275;2464;9083;16901" UnicodeString="列印測試" /></FixedPage>

大家如果想操作xps文檔的話可以引用ReachFramework.dll, System.Windows.Xps.Packaging命名空間
裡面封裝了xps文檔的操作方法,這裡拋磚引玉順便介紹了xps的一些知識

順便貼一個比較詳細介紹列印的檔案供大家參考

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

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.