打印帮助类

代码

 /// <summary>
    /// 打印帮助类
    /// </summary>
    public class PrintHelper
    {  private int m_currentPageIndex;
        private IList<Stream> m_streams;

        /// <summary>
        /// 报表直接打印
        /// </summary>
        /// <param name="reportPath">报表文件路径</param>
        /// <param name="printerName">打印机名称</param>
        /// <param name="dt">DataTable</param>
        /// <param name="sourceName">rdlc的数据集名称</param>
        /// <param name="paraList">参数列表</param>
        public void Run(string reportPath, string printerName, DataTable dt, string sourceName, List<ReportParameter> paraList)
        {
            LocalReport report = new LocalReport();
            report.ReportPath = reportPath;
            report.DataSources.Add(new ReportDataSource(sourceName,dt));
            report.EnableExternalImages = true;
            report.SetParameters(paraList);
            Export(report);
            m_currentPageIndex = 0;
            Print(printerName);
        }

        private void Export(LocalReport report)
        {
            string deviceInfo =
                "<DeviceInfo>" +
                " <OutputFormat>EMF</OutputFormat>"+
                "</DeviceInfo>";
            Warning[] warnings;
            m_streams = new List<Stream>();
            try
            {
                report.Render("Image", deviceInfo, CreateStream, out warnings);
            }
            catch (Exception ex)
            {
                Exception innerEx = ex.InnerException;
                while (innerEx != null)
                {
                    string errmessage = innerEx.Message;
                    innerEx = innerEx.InnerException;

                }
            }

            foreach (Stream stream in m_streams)
            {
                stream.Position = 0;
            }
        }

        private Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek)
        {
            Stream stream = new FileStream(name + DateTime.Now.Millisecond + "." + fileNameExtension, FileMode.Create);
            m_streams.Add(stream);
            return stream;
        }
        private void Print(string printerName)
        {
            if (m_streams == null || m_streams.Count == 0) return;
            PrintDocument printDoc = new PrintDocument();
            if (printerName.Length > 0)
            {
                printDoc.PrinterSettings.PrinterName = printerName;
            }
            foreach (PaperSize ps in printDoc.PrinterSettings.PaperSizes)
            {
                if (ps.PaperName == "A4")
                {
                    printDoc.PrinterSettings.DefaultPageSettings.PaperSize = ps;
                    printDoc.DefaultPageSettings.PaperSize = ps;
                }
            }
            if (!printDoc.PrinterSettings.IsValid)
            {
                string msg = string.Format("找不到打印机:{0}",printerName);
                LogUtil.Log(msg);
                return;
            }
            printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
            printDoc.Print();
        }

        private void PrintPage(object sender, PrintPageEventArgs ev)
        {
            Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]);
            ev.Graphics.DrawImage(pageImage, 0, 0, 827, 1169);//像素
            m_currentPageIndex++;
            ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
        }
    }

说明

打印格式

string deviceInfo =
            "<DeviceInfo>" +
            "  <OutputFormat>EMF</OutputFormat>" +
            "  <PageWidth>210mm</PageWidth>" +
            "  <PageHeight>297mm</PageHeight>" +
            "  <MarginTop>5mm</MarginTop>" +
            "  <MarginLeft>10mm</MarginLeft>" +
            "  <MarginRight>10mm</MarginRight>" +
            "  <MarginBottom>5mm</MarginBottom>" +
            "</DeviceInfo>";//这里是设置打印的格式 边距什么的

关于OutputFormat:

http://support.supermap.com.cn/DataWarehouse/WebDocHelp/6.1.3/iserverOnlineHelp/mergedProjects/iServerJavadoc/com/supermap/services/components/commontypes/OutputFormat.html

参考文章

http://www.cnblogs.com/bfyx/p/3279385.html (详细)

http://blog.csdn.net/moshuchao/article/details/2607017

http://www.cnblogs.com/qiuweiguo/archive/2011/08/26/2154706.html

时间: 2024-10-18 13:32:17

打印帮助类的相关文章

自写打印日历类

在论坛上看到有人在问打印日历的程序,今天下午空闲的时候写了一个日历类,简单的写了几个方法. calendar.h #include <string> using namespace std; #ifndef CALENDAR_H #define CALENDAR_H class Calendar{ public: Calendar(){} void printAllMonth(const int &year); void printOneMonth(const int &yea

反射---输入一个类,打印出类中所有元素

package com.sadhu; import java.util.*; import java.lang.reflect.*; /** 反射---输入一个类,打印出类中所有元素 reflect包中有三个描述类元素的类:Field.Method.Constructor */ public class Sample {     public static void main(String[] args)     {         String name;//收集用户输入的类         

PHP打印日志类

PHP简单封装个打印日志类,方便查看日志: <?php /** * Created by PhpStorm. * User: zenkilan * Date: 2019/9/26 * Time: 11:36 */ class ZenkiLog { private $rootDir; private $fileName; private $folder; private $dir; public function __construct($fileName, $folder) { $this->

关于C语言打印string类字符串的问题

首先因为printf函数输出字符串是针对char *的,即printf只能输出c语言的内置数据,而string不是c语言的内置数据. 其次string类型的对象不止包含字符串,还包含了许多用于操作的函数,所以&str并非字符串的首地址 因此该怎么打印string字符串呢? 1.利用string的成员函数c_str()打印,如下: string q = "123ABCD"; printf("%s", q.c_str()); 2.强转,将string类型强转成c

【iOS】NSLog 打印 BOOL 类型值

这个问题以前没在意,刚偶然打印,发现有些问题,上网查了下,发现是这么搞的: NSLog(@"%@", isEqual?@"YES":@"NO");

windows下 打印机打印操作类 VS2015

#include "stdafx.h" #include "CPrinter.h" BOOL CPrinter::GetPrinterDevice(LPTSTR pszPrinterName, HGLOBAL* phDevNames, HGLOBAL* phDevMode) { // if NULL is passed, then assume we are setting app object's // devmode and devnames if (phDev

java.lang.reflection打印一个类的全部信息

package com.ljy.chapter5; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.Scanner; /** * This program uses reflection to print all features of

iOS 随笔小技巧 弱self 打印当前类行数列数,多人开发自动适配pch地址,获取设备uid的信息

$(SRCROOT)/PrefixHeader.pch自动适配pch地址 __weak __block typeof(self) weakself = self; __weak typeof(self)weakSelf = self; #define DN_DEBUG_LOG(fmt, ...) {NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__); } NSDictionary *if

打印日志类

<?php /* * To change this template, choose Tools | Templates * and open the template in the editor. */ class WriteLog{ public static function setLog($file,$content){ $flag = "testwin"; try{ $file = preg_replace('#[^A-z0-9]#','',$file); $_ospa