예제 #1
0
        public static void PrintImagePage(PrinterData printerData)
        {
            var           eventType = (printerData.eventType).ToUpper();
            PrintDocument pd        = new PrintDocument();

            pd.PrinterSettings.PrinterName = printerData.printerName;
            pd.DefaultPageSettings.PrinterSettings.PrinterName = printerData.printerName;
            PrinterSettings.PaperSizeCollection paperSizes = pd.PrinterSettings.PaperSizes; //获取打印纸张
            if (printerData.customSizeFlag)
            {
                PaperSize ps = new PaperSize("用户选择模版(" + printerData.width + "*" + printerData.height + ")", (int)Millimeters2PixelsUtils.Millimeters2Pixels(printerData.width), (int)Millimeters2PixelsUtils.Millimeters2Pixels(printerData.height));
                pd.DefaultPageSettings.PaperSize = ps;
            }
            else
            {
                pd.DefaultPageSettings.PaperSize = getPaperSize(paperSizes, printerData.paperName) ?? pd.DefaultPageSettings.PaperSize;
            }
            pd.PrintPage += (o, e) =>
            {
                PageSettings settings    = e.PageSettings;
                var          paperHeight = settings.PaperSize.Height;
                var          paperWidth  = settings.PaperSize.Width;
                //Rectangle m = e.MarginBounds;
                int margin = 10, imgHeight = 0, imgWidth = 0;
                ImageScaling(printerData.image, paperHeight, paperWidth, margin, printerData.direction, ref imgHeight, ref imgWidth);
                if (printerData.direction == "2")                  //2:横向打印(html转pdf的有问题,html转pdf不考虑宽度,转出来标准的pdf页,分页也已经完成,在这部分就处理不了)
                {
                    e.Graphics.TranslateTransform(0, paperHeight); //旋转原点
                    e.Graphics.RotateTransform(-90.0F);            //旋转角度
                }
                e.Graphics.DrawImage(printerData.image, margin, margin, imgWidth, imgHeight);
            };
            if (eventType == "PRINT")
            {
                pd.Print();
            }
            else if (eventType == "PREVIEW")
            {
                PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog();
                printPreviewDialog.WindowState = FormWindowState.Maximized;
                printPreviewDialog.Document    = pd;
                printPreviewDialog.TopMost     = true;
                printPreviewDialog.ShowDialog();
            }
            (printerData.image).Dispose();
        }
예제 #2
0
        public static Stream Html2Pdf(PrinterData printerData)
        {
            string htmlText = printerData.data.Trim();

            if (string.IsNullOrEmpty(htmlText))
            {
                throw new Exception("传入的html无内容:" + htmlText);
            }
            MemoryStream outputStream = new MemoryStream();       //实例化MemoryStream,用于存PDF

            byte[]       data = Encoding.UTF8.GetBytes(htmlText); //字串转成byte[]
            MemoryStream msInput = new MemoryStream(data);
            int          width = 0, height = 0;

            width  = (int)Millimeters2PixelsUtils.Millimeters2Pixels(printerData.width);
            height = (int)Millimeters2PixelsUtils.Millimeters2Pixels(printerData.height);
            if (!printerData.customSizeFlag)
            {
                PrintUtils.getPaperSize(printerData.paperName ?? "A4", ref width, ref height);
            }
            Rectangle pageSize = new Rectangle(width, height); //设置pdf模板大小

            if (printerData.direction == "2")                  //2:横向打印
            {
                pageSize = new Rectangle(height, width);
            }
            Document  doc    = new Document(pageSize); //要写PDF的文件 document = new Document(PageSize.A4, 25, 25, 30, 30);
            PdfWriter writer = PdfWriter.GetInstance(doc, outputStream);
            //指定文件预设开档时的缩放为100%
            PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ, 0, doc.PageSize.Height, 1f);

            //开启Document文件
            doc.Open();

            //使用XMLWorkerHelper把Html parse到PDF档里
            XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, msInput, null, Encoding.UTF8, new UnicodeFontFactory());
            //XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, msInput, null, Encoding.UTF8);

            //将pdfDest设定的资料写到PDF档
            PdfAction action = PdfAction.GotoLocalPage(1, pdfDest, writer);

            writer.SetOpenAction(action);
            doc.Close();
            msInput.Close();
            outputStream.Close();
            //回传PDF档案
            var bytes = outputStream.ToArray();

            var ret = Convert.ToBase64String(bytes);

            try
            {
                string strbase64 = ret;
                strbase64 = strbase64.Replace(' ', '+');
                return(new MemoryStream(Convert.FromBase64String(strbase64)));
            }
            catch (Exception ex)
            {
                throw new Exception("html转pdf文件失败:" + ex.Message);
            }
        }
예제 #3
0
        public static string PreView(PrinterData printerData)
        {
            try
            {
                if (string.IsNullOrEmpty(printerData.printerName))
                {
                    return("请先设置默认打印机");
                }
                var eventType = (printerData.eventType).ToUpper();
                var filePath  = printerData.filePath;
                if (!Directory.Exists(filePath) && !File.Exists(filePath))
                {
                    return("要打印的数据不存在");
                }

                PrintDocument pd = new PrintDocument();
                pd.PrinterSettings.PrinterName = printerData.printerName;
                pd.DefaultPageSettings.PrinterSettings.PrinterName = printerData.printerName;

                if (printerData.customSizeFlag)
                {
                    PaperSize ps = new PaperSize("用户选择模版(" + printerData.width + "*" + printerData.height + ")", (int)Millimeters2PixelsUtils.Millimeters2Pixels(printerData.width), (int)Millimeters2PixelsUtils.Millimeters2Pixels(printerData.height));
                    pd.DefaultPageSettings.PaperSize = ps;
                }
                else
                {
                    pd.DefaultPageSettings.PaperSize = getPaperSize(pd.PrinterSettings.PaperSizes, printerData.paperName) ?? pd.DefaultPageSettings.PaperSize;
                }
                var fileListIndex = 0;

                List <string> fileList = new List <string>();
                if (File.Exists(filePath)) //路径是jpg图片
                {
                    fileList.Add(filePath);
                }
                else
                {
                    fileList = new List <string>(Directory.GetFiles(filePath).OrderBy(f => Convert.ToInt32(Path.GetFileNameWithoutExtension(f))));
                }
                if (fileList.Count == 0)
                {
                    return("要打印的数据不存在");
                }
                pd.PrintPage += (o, e) =>
                {
                    PageSettings settings = e.PageSettings;
                    var          paperHeight = settings.PaperSize.Height;
                    var          paperWidth = settings.PaperSize.Width;
                    int          margin = 10, imgHeight = 0, imgWidth = 0;

                    using (Image image = new Bitmap(fileList[fileListIndex]))
                    {
                        ImageScaling(image, paperHeight, paperWidth, margin, printerData.direction, ref imgHeight, ref imgWidth);
                        if (printerData.direction == "2")                  //2:横向打印(html转pdf的有问题,html转pdf不考虑宽度,转出来标准的pdf页,分页也已经完成,在这部分就处理不了)
                        {
                            e.Graphics.TranslateTransform(0, paperHeight); //旋转原点
                            e.Graphics.RotateTransform(-90.0F);            //旋转角度
                        }
                        e.Graphics.DrawImage(image, margin, margin, imgWidth, imgHeight);
                    }
                    if (fileListIndex < fileList.Count - 1)
                    {
                        e.HasMorePages = true;  //HaeMorePages属性为True时,PrintPage的回调函数就会被再次调用,打印一个页面。
                        fileListIndex++;
                    }
                    else
                    {
                        //预览界面点击打印需要把索引重新初始化
                        fileListIndex = 0;
                    }
                };

                //打印结束-删除一些创建的文件
                //pd.EndPrint += new PrintEventHandler();

                if (eventType == "PRINT")
                {
                    pd.Print();
                }
                else if (eventType == "PREVIEW")
                {
                    PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog();
                    printPreviewDialog.WindowState = FormWindowState.Maximized;
                    printPreviewDialog.Document    = pd;
                    printPreviewDialog.TopMost     = true;
                    printPreviewDialog.ShowDialog();
                }
                deleteTemFile();
                return("打印成功!");
            }
            catch (Exception e)
            {
                return(e.Message);
            }
        }
예제 #4
0
        public static void Html2Pdf(string htmlText, string tempPdfPath, string paperName, string direction, Boolean customSizeFlag, int customWidth, int customHeight)
        {
            if (string.IsNullOrEmpty(htmlText))
            {
                throw new Exception("传入的html无内容:" + htmlText);
            }
            MemoryStream outputStream = new MemoryStream();       //实例化MemoryStream,用于存PDF

            byte[]       data = Encoding.UTF8.GetBytes(htmlText); //字串转成byte[]
            MemoryStream msInput = new MemoryStream(data);
            int          width = 0, height = 0;

            width  = (int)Millimeters2PixelsUtils.Millimeters2Pixels(customWidth);
            height = (int)Millimeters2PixelsUtils.Millimeters2Pixels(customHeight);
            if (!customSizeFlag)
            {
                paperName = paperName ?? "A4";
                PrintUtils.getPaperSize(paperName, ref width, ref height);
            }
            Rectangle pageSize = new Rectangle(width, height); //设置pdf模板大小

            if (direction == "2")                              //2:横向打印
            {
                pageSize = new Rectangle(height, width);
            }
            Document  doc    = new Document(pageSize); //要写PDF的文件 document = new Document(PageSize.A4, 25, 25, 30, 30);
            PdfWriter writer = PdfWriter.GetInstance(doc, outputStream);
            //指定文件预设开档时的缩放为100%
            PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ, 0, doc.PageSize.Height, 1f);

            //开启Document文件
            doc.Open();

            //使用XMLWorkerHelper把Html parse到PDF档里
            XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, msInput, null, Encoding.UTF8, new UnicodeFontFactory());
            //XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, msInput, null, Encoding.UTF8);

            //将pdfDest设定的资料写到PDF档
            PdfAction action = PdfAction.GotoLocalPage(1, pdfDest, writer);

            writer.SetOpenAction(action);
            doc.Close();
            msInput.Close();
            outputStream.Close();
            //回传PDF档案
            var bytes = outputStream.ToArray();

            var ret = Convert.ToBase64String(bytes);

            try
            {
                string strbase64 = ret;
                strbase64 = strbase64.Replace(' ', '+');
                System.IO.MemoryStream stream = new System.IO.MemoryStream(Convert.FromBase64String(strbase64));
                System.IO.FileStream   fs     = new System.IO.FileStream(tempPdfPath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write);
                byte[] b = stream.ToArray();
                //byte[] b = stream.GetBuffer();
                fs.Write(b, 0, b.Length);
                fs.Close();
            }
            catch (Exception ex)
            {
                throw new Exception("html转pdf文件失败:" + ex.Message);
            }
        }