Exemplo n.º 1
0
        private int printShow(string url)
        {
            int             isOK     = 0;
            PDFFile         file     = PDFFile.Open(url);
            PrinterSettings settings = new PrinterSettings();

            System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument();
            settings.PrinterName = "\\\\192.168.2.93\\Brother DCP-7030 Printer";
            settings.PrintToFile = false;

            //设置纸张大小(可以不设置,取默认设置)3.90 in,  8.65 in
            //PaperSize ps = new PaperSize("test", 4, 9);
            //ps.RawKind = 9; //如果是自定义纸张,就要大于118,(A4值为9,详细纸张类型与值的对照请看http://msdn.microsoft.com/zh-tw/library/system.drawing.printing.papersize.rawkind(v=vs.85).aspx)

            O2S.Components.PDFRender4NET.Printing.PDFPrintSettings pdfPrintSettings = new O2S.Components.PDFRender4NET.Printing.PDFPrintSettings(settings);
            //pdfPrintSettings.PaperSize = ps;
            pdfPrintSettings.PageScaling            = O2S.Components.PDFRender4NET.Printing.PageScaling.FitToPrinterMarginsProportional;
            pdfPrintSettings.PrinterSettings.Copies = 1;

            try
            {
                file.Print(pdfPrintSettings);
                isOK = 1;
            }
            catch (Exception)
            {
                isOK = -1;
                throw;
            }
            finally
            {
                file.Dispose();
            }
            return(isOK);
        }
Exemplo n.º 2
0
        public string SilentPrint(string Url, string PostData, string cookieStr, string printerParams, string charset)
        {
            string fileName = null;

            try {
                Stream stream = HttpUtils.SendRequest(Url, PostData, charset, cookieStr);

                //创建临时目录
                string temp = System.Environment.GetEnvironmentVariable("TEMP");
                fileName = Guid.NewGuid().ToString();
                fileName = temp + "\\" + fileName + ".pdf";
                saveFile(stream, fileName);

                //读取PDF文件
                if (PrintEngine.Equals("exe"))
                {
                    string exePath = Assembly.GetExecutingAssembly().Location;
                    string exeFile = new FileInfo(exePath).DirectoryName + "\\FR.exe";
                    // MessageBox.Show(exeFile);
                    Process          p         = new Process();
                    ProcessStartInfo startInfo = new ProcessStartInfo();
                    startInfo.CreateNoWindow  = true;
                    startInfo.WindowStyle     = ProcessWindowStyle.Hidden;
                    startInfo.UseShellExecute = true;
                    startInfo.FileName        = exeFile;
                    //startInfo.Verb = "FR.exe";
                    string cmd = "/p  \"" + fileName + "\" \"" + GetPrinter() + "\"";

                    startInfo.Arguments = cmd;
                    p.StartInfo         = startInfo;
                    p.Start();
                    p.WaitForExit();
                }
                else
                {
                    PDFFile pdf = PDFFile.Open(fileName);
                    // Create a default printer settings to print on the default printer.
                    PrinterSettings  settings         = new PrinterSettings();
                    PDFPrintSettings pdfPrintSettings = new PDFPrintSettings(settings);
                    pdfPrintSettings.PageScaling = PageScaling.FitToPrinterMargins;
                    pdf.Print(pdfPrintSettings);
                    pdf.Dispose();
                }
            } catch (Exception ex) {
                return("{status:'error',msg:'" + ex.Message + "'}");
            } finally {
                try {
                    if (fileName != null)
                    {
                        new FileInfo(fileName).Delete();
                    }
                } catch (Exception ee) {
                }
            }
            return("{status:'success'}");
        }
Exemplo n.º 3
0
    /*打印pdf*/

    // <param name="url">要打印的PDF路径</param>
    private void printPDF(string url)
    {
        //打开pdf文件
        PDFFile         file     = PDFFile.Open(url);
        PrinterSettings settings = new PrinterSettings();

        System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument();
        //设置打印机的名称
        settings.PrinterName = "HP Officejet Pro X551dw Printer PCL 6 (网络)";
        settings.PrintToFile = false;


        //设置打印的颜色为彩色
        if (PrintColor.Equals("1"))
        {
            settings.DefaultPageSettings.Color = true;
        }
        else
        {
            settings.DefaultPageSettings.Color = false;
        }


        //设置打印的单双面
        if (PrintType.Equals("0"))
        {
            settings.Duplex = Duplex.Simplex;
        }
        else
        {   //双面
            settings.Duplex = Duplex.Vertical;
        }

        //设置纸张大小(可以不设置取,取默认设置)3.90 in,  8.65 in
        // PaperSize ps = new PaperSize("Your Paper Name", config.Width, config.Height);
        //  ps.RawKind = 150; //如果是自定义纸张,就要大于118,(A4值为9,详细纸张类型与值的对照请看http://msdn.microsoft.com/zh-tw/library/system.drawing.printing.papersize.rawkind(v=vs.85).aspx)

        O2S.Components.PDFRender4NET.Printing.PDFPrintSettings pdfPrintSettings = new O2S.Components.PDFRender4NET.Printing.PDFPrintSettings(settings);
        //设置打印纸张的大小
        //pdfPrintSettings.PaperSize = ps;
        pdfPrintSettings.PageScaling = O2S.Components.PDFRender4NET.Printing.PageScaling.FitToPrinterMarginsProportional;
        //设置打印份数
        pdfPrintSettings.PrinterSettings.Copies = 1;


        //打印pdf
        file.Print(pdfPrintSettings);
        //关闭文件
        file.Dispose();
    }
Exemplo n.º 4
0
        private bool pdf(String filename)
        {
            PDFFile file = PDFFile.Open(filename);

            try
            {
                file.Print(this.PrinterSettings());
            }
            catch
            {
                return(false);
            }
            finally
            {
                file.Dispose();
            }

            return(true);
        }
Exemplo n.º 5
0
        void PrintPDF(string pdfPath, int copies)
        {
            PDFFile         file    = PDFFile.Open(pdfPath);
            PrinterSettings setting = new PrinterSettings();
            PrintDocument   pd      = new PrintDocument();

            setting.PrinterName = "FUJI XEROX DocuCentre S2110";
            setting.Duplex      = Duplex.Vertical;
            setting.PrintToFile = false;
            setting.Copies      = (short)copies;
            setting.Collate     = true;
            PDFPrintSettings pdfps = new PDFPrintSettings(setting);

            try
            {
                file.Print(pdfps);
            }
            finally
            {
                file.Dispose();
            }
        }
Exemplo n.º 6
0
        ///// <summary>
        ///// 将字符串发送到打印机方法
        ///// </summary>
        ///// <param name="szPrinterName">打印机名称</param>
        ///// <param name="szString">打印的字符串</param>
        ///// <returns></returns>
        //public static bool SendStringToPrinter(string szPrinterName, string szString)
        //{
        //    bool flag = false;
        //    try
        //    {
        //        IntPtr pBytes;
        //        Int32 dwCount;

        //        // 获取字符串长度
        //        dwCount = szString.Length;

        //        // 将字符串复制到非托管 COM 任务分配的内存非托管内存块,并转换为 ANSI 文本
        //        pBytes = Marshal.StringToCoTaskMemAnsi(szString);

        //        // 将已转换的 ANSI 字符串发送到打印机
        //        flag = SendBytesToPrinter(szPrinterName, pBytes, dwCount);

        //        // 释放先前分配的非托管内存
        //        Marshal.FreeCoTaskMem(pBytes);
        //    }
        //    catch (Win32Exception ex)
        //    {
        //        Helper.WriteLog(ex.Message);
        //        flag = false;
        //    }
        //    return flag;
        //}


        /// <summary>
        /// PDFRender4Net打印pdf
        /// </summary>
        /// <param name="filePath">pdf文件路径</param>
        /// <param name="printerName">打印机名称</param>
        /// <param name="paper">纸张大小</param>
        /// <param name="isVertical">是否竖打</param>
        /// <param name="copies">份数</param>
        public static string UsePDFRender4NetToPrintPdf(string filePath, string printerName, PaperSize paper, bool isVertical, int copies)
        {
            string errorStr = "";

            if (!File.Exists(filePath))
            {
                errorStr += "打印的文件不存在\r\n";
                return(errorStr);
            }

            PDFFile         file     = PDFFile.Open(filePath);
            PrinterSettings settings = new PrinterSettings();

            settings.PrinterName = printerName;
            settings.PrintToFile = false;
            settings.DefaultPageSettings.Landscape = !isVertical;

            PDFPrintSettings pdfPrintSettings = new PDFPrintSettings(settings);

            pdfPrintSettings.PaperSize              = paper;
            pdfPrintSettings.PageScaling            = PageScaling.FitToPrinterMarginsProportional;
            pdfPrintSettings.PrinterSettings.Copies = (short)copies;

            try
            {
                file.Print(pdfPrintSettings);
            }
            catch (Exception ex)
            {
                errorStr = ex.Message;
                Helper.WriteLog(ex.Message);
            }
            finally
            {
                file.Dispose();
            }
            return(errorStr);
        }
        private void PrintButton_Click(object sender, RoutedEventArgs e)
        {
            // Load the PDF file.
            PDFFile file = PDFFile.Open(this.pdfViewer.Source);

            file.SerialNumber = "PDFVW-6ATTA-DK6XD-A2XTO-AIQUM-3ECYE";
            // Create a default printer settings to print on the default printer.
            PrinterSettings  settings         = new PrinterSettings();
            PDFPrintSettings pdfPrintSettings = new PDFPrintSettings(settings);

            pdfPrintSettings.PageScaling = PageScaling.FitToPrinterMargins;

            // Create the print dialog object and set options
            PrintDialog pDialog = new PrintDialog();

            pDialog.PageRangeSelection   = PageRangeSelection.AllPages;
            pDialog.UserPageRangeEnabled = true;

            // Display the dialog. This returns true if the user presses the Print button.
            Nullable <Boolean> print = pDialog.ShowDialog();

            if (print == true)
            {
                // Get the settings from the PrintDialog and manually set each one.
                // If there are more option that need to be set the we need to identify them and set them manually.

                pdfPrintSettings.PrinterSettings.PrinterName = pDialog.PrintQueue.FullName;

                pdfPrintSettings.PrinterSettings.Copies   = (short)pDialog.PrintTicket.CopyCount;
                pdfPrintSettings.PrinterSettings.FromPage = pDialog.PageRange.PageFrom;
                pdfPrintSettings.PrinterSettings.ToPage   = pDialog.PageRange.PageTo;

                // Print the PDF file.
                file.Print(pdfPrintSettings);
            }

            file.Dispose();
        }
Exemplo n.º 8
0
        private void CreatePDF(string hphm, string vin, string business, string carType, string chekItem, string weight, string makeDate, byte[] qrcode)
        {
            PdfHelper    pdfHelper    = new PdfHelper();
            Document     doc          = pdfHelper.CreateDocument(500, 500);
            MemoryStream memoryStream = new MemoryStream();
            PdfWriter    writer       = PdfWriter.GetInstance(doc, memoryStream);

            doc.Open();
            doc.NewPage();

            BaseFont ArialFont = BaseFont.CreateFont("STZHONGS.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

            iTextSharp.text.Font BoldFont = new iTextSharp.text.Font(ArialFont, 11, iTextSharp.text.Font.NORMAL, new BaseColor(System.Drawing.Color.Black));
            iTextSharp.text.Font HeadFont = new iTextSharp.text.Font(ArialFont, 11, iTextSharp.text.Font.BOLD, new BaseColor(System.Drawing.Color.Red));
            PdfPTable            tableVIN = pdfHelper.CreateTable(new float[] { 1, 1, 1, 1, 1 }, doc);

            tableVIN.AddCell(pdfHelper.CreateCell("号牌号码", BoldFont, Element.ALIGN_RIGHT));
            tableVIN.AddCell(pdfHelper.CreateCell(hphm, HeadFont, Element.ALIGN_CENTER));
            tableVIN.AddCell(pdfHelper.CreateCell("VIN", BoldFont, Element.ALIGN_RIGHT));
            tableVIN.AddCell(pdfHelper.CreateCell(vin, HeadFont, Element.ALIGN_CENTER, 2));
            doc.Add(tableVIN);

            PdfPTable tableMsg = pdfHelper.CreateTable(new float[] { 1, 1, 1, 1 }, doc);

            tableMsg.AddCell(pdfHelper.CreateCell("业务类型", BoldFont, Element.ALIGN_RIGHT));
            tableMsg.AddCell(pdfHelper.CreateCell(business, BoldFont, Element.ALIGN_CENTER));
            tableMsg.AddCell(pdfHelper.CreateCell("车辆类型", BoldFont, Element.ALIGN_RIGHT));
            tableMsg.AddCell(pdfHelper.CreateCell(carType, BoldFont, Element.ALIGN_CENTER));
            tableMsg.AddCell(pdfHelper.CreateCell("检测项目", BoldFont, Element.ALIGN_RIGHT));
            tableMsg.AddCell(pdfHelper.CreateCell(chekItem, BoldFont, Element.ALIGN_CENTER));
            tableMsg.AddCell(pdfHelper.CreateCell("总质量", BoldFont, Element.ALIGN_RIGHT));
            tableMsg.AddCell(pdfHelper.CreateCell(weight, BoldFont, Element.ALIGN_CENTER));
            tableMsg.AddCell(pdfHelper.CreateCell("环保登记", BoldFont, Element.ALIGN_RIGHT));
            tableMsg.AddCell(pdfHelper.CreateCell("", BoldFont, Element.ALIGN_CENTER));
            tableMsg.AddCell(pdfHelper.CreateCell("出厂日期", BoldFont, Element.ALIGN_RIGHT));
            tableMsg.AddCell(pdfHelper.CreateCell(makeDate, BoldFont, Element.ALIGN_CENTER));

            PdfPCell qrCell = null;

            if (qrcode != null)
            {
                qrCell = new PdfPCell(pdfHelper.CreateImage(qrcode, 100f, 100f));
            }
            else
            {
                qrCell = pdfHelper.CreateCell("", BoldFont, Element.ALIGN_CENTER);
            }
            //qrCell.Rowspan = 3;
            qrCell.Padding = 2;
            tableMsg.AddCell(qrCell);
            //tableMsg.AddCell(pdfHelper.CreateCell(" ", BoldFont, Element.ALIGN_LEFT, false, 3, false));
            //tableMsg.AddCell(pdfHelper.CreateCell("Admin", BoldFont, Element.ALIGN_LEFT,false,3,false));
            tableMsg.AddCell(pdfHelper.CreateCell("Admin\r\n\r\n" + DateTime.Now.ToString("yyyy年MM月dd日  HH:mm:ss"), BoldFont, Element.ALIGN_LEFT, false, 3, false));

            doc.Add(tableMsg);
            doc.Close();
            MemoryStream pdf     = new MemoryStream(memoryStream.ToArray());
            PDFFile      pdfFile = PDFFile.Open(pdf);
            //Bitmap pageImage  = pdfFile.GetPageImage(0,56 * (int)PdfHelper.Definition.Six);

            PrinterSettings settings = new PrinterSettings();
            PrintDocument   pd       = new PrintDocument();

            settings.PrinterName = "Adobe PDF";
            settings.PrintToFile = false;

            //设置纸张大小(可以不设置,取默认设置)3.90 in,  8.65 in
            //PaperSize ps = new PaperSize("test", 4, 9);
            // ps.RawKind = 9; //如果是自定义纸张,就要大于118,(A4值为9,详细纸张类型与值的对照请看http://msdn.microsoft.com/zh-tw/library/system.drawing.printing.papersize.rawkind(v=vs.85).aspx)

            PDFPrintSettings pdfPrintSettings = new PDFPrintSettings(settings);

            //pdfPrintSettings.PaperSize = ps;
            pdfPrintSettings.PageScaling            = O2S.Components.PDFRender4NET.Printing.PageScaling.MultiplePagesPerSheetProportional;
            pdfPrintSettings.PrinterSettings.Copies = 1;
            pdfFile.Print(pdfPrintSettings);

            // MemoryStream pic = new MemoryStream();
            //pageImage.Save(pic, ImageFormat.Png);
            //pageImage.Save("测试.png", ImageFormat.Png);
            pdf.Close();
            pdf.Dispose();
            //pageImage.Dispose();
            pdfFile.Dispose();
            // picQRCode.BackgroundImage = System.Drawing.Image.FromStream(pic);
        }
Exemplo n.º 9
0
        private void CreatePDF(X18J52 x18J52, string hpzl, byte[] qrcode)
        {
            PdfHelper    pdfHelper    = new PdfHelper();
            Document     doc          = pdfHelper.CreateDocumentA4();
            MemoryStream memoryStream = new MemoryStream();
            PdfWriter    writer       = PdfWriter.GetInstance(doc, memoryStream);

            try
            {
                doc.Open();
                doc.NewPage();

                BaseFont             ArialFont = BaseFont.CreateFont("STZHONGS.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
                iTextSharp.text.Font BoldFont  = new iTextSharp.text.Font(ArialFont, 11, iTextSharp.text.Font.NORMAL, new BaseColor(System.Drawing.Color.Black));

                //二维码
                iTextSharp.text.Image img = pdfHelper.CreateImage(qrcode, 100, 100f);
                img.SetAbsolutePosition(AppHelper.PointSetting.X_QRCode, AppHelper.PointSetting.Y_QRCode);
                doc.Add(img);

                PrintPoint     print = AppHelper.PointSetting;
                PdfContentByte pb    = writer.DirectContent;
                pb.BeginText();
                pb.SetFontAndSize(ArialFont, 9);
                pb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, DateTime.Now.ToString("yyyy"), print.X_Year, print.Y_Year, 0);
                pb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, DateTime.Now.ToString("MM"), print.X_Month, print.Y_Month, 0);
                pb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, DateTime.Now.ToString("dd"), print.X_Day, print.Y_Day, 0);
                pb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, x18J52.hphm, print.X_PlateNo, print.Y_PlateNo, 0);
                pb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, x18J52.syr, print.X_Owner, print.Y_Owner, 0);
                pb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, hpzl, print.X_PlateType, print.Y_PlateType, 0);
                pb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "", print.X_PlateColor, print.Y_PlateColor, 0);
                pb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, x18J52.cllx, print.X_CarType, print.Y_CarType, 0);
                pb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, x18J52.clpp1, print.X_CarBrand, print.Y_CarBrand, 0);
                pb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, x18J52.clxh, print.X_CarMold, print.Y_CarMold, 0);
                pb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, x18J52.ccdjrq, print.X_RegisterDate, print.Y_RegisterDate, 0);
                pb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, x18J52.ccrq, print.X_MakeDate, print.Y_MakeDate, 0);
                pb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, x18J52.zzl, print.X_Weight, print.Y_Weight, 0);
                pb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, x18J52.zbzl, print.X_KerbWeight, print.Y_KerbWeight, 0);

                pb.EndText();

                doc.Close();
                MemoryStream pdf     = new MemoryStream(memoryStream.ToArray());
                PDFFile      pdfFile = PDFFile.Open(pdf);

                PrinterSettings settings = new PrinterSettings();
                PrintDocument   pd       = new PrintDocument();
                settings.PrinterName = AppHelper.AppSetting.PrinterName;
                settings.PrintToFile = false;

                PDFPrintSettings pdfPrintSettings = new PDFPrintSettings(settings);
                pdfPrintSettings.PageScaling            = PageScaling.MultiplePagesPerSheetProportional;
                pdfPrintSettings.PrinterSettings.Copies = 1;
                pdfFile.Print(pdfPrintSettings);
                pdf.Close();
                pdf.Dispose();
                pdfFile.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show("打印异常" + ex.Message);
            }
            finally
            {
                memoryStream.Dispose();
                memoryStream.Close();
                writer.Dispose();
                doc.Dispose();
                doc.Close();
            }
        }
Exemplo n.º 10
0
        public bool pdf_Printer_fast(printerParam param, int copys, out string resultString)
        {
            resultString = "";
            PDFFile pdf = PDFFile.Open(param.FileName);

            try
            {
                PrinterSettings settings = new PrinterSettings();
                settings.PrinterName = param.printerName;
                settings.PrintToFile = false;
                if (!settings.IsValid)
                {
                    throw new Exception("打印机:" + param.printerName + "无效");
                }
                //设置纸张大小(可以不设置,取默认设置)3.90 in,  8.65 in
                PaperSize ps = new PaperSize("test", 4, 9);
                ps.RawKind = 9; //如果是自定义纸张,就要大于118,(A4值为9,详细纸张类型与值的对照请看http://msdn.microsoft.com/zh-tw/library/system.drawing.printing.papersize.rawkind(v=vs.85).aspx)
                O2S.Components.PDFRender4NET.Printing.PDFPrintSettings pdfPrintSettings = new O2S.Components.PDFRender4NET.Printing.PDFPrintSettings(settings);
                pdfPrintSettings.PaperSize               = ps;
                pdfPrintSettings.PageScaling             = O2S.Components.PDFRender4NET.Printing.PageScaling.FitToPrinterMarginsProportional;
                pdfPrintSettings.PrinterSettings.Collate = true;
                if (pdf.GetPageSize(0).Width > pdf.GetPageSize(0).Height)
                {
                    pdfPrintSettings.PrinterSettings.DefaultPageSettings.Landscape = true;
                }
                else
                {
                    pdfPrintSettings.PrinterSettings.DefaultPageSettings.Landscape = false;
                }
                pdfPrintSettings.PrinterSettings.Copies = Convert.ToInt16(copys);
                if (!param.isColor)
                {
                    if (pdfPrintSettings.PrinterSettings.PaperSources.Count <= param.PaperSource)
                    {
                        //MessageBox.Show("纸盒参数错误!");
                        throw new Exception("纸盒参数错误!");
                    }
                    else
                    {
                        pdfPrintSettings.PaperSource = pdfPrintSettings.PrinterSettings.PaperSources[param.PaperSource];
                    }
                }
                else
                {
                    for (int i = 0; i < pdfPrintSettings.PrinterSettings.PaperSources.Count; i++)
                    {
                        if (pdfPrintSettings.PrinterSettings.PaperSources[i].SourceName.ToString().Contains("纸盘 " + (param.PaperSource + 1).ToString()))
                        {
                            pdfPrintSettings.PaperSource = pdfPrintSettings.PrinterSettings.PaperSources[i];
                        }
                    }
                    if (pdfPrintSettings.PaperSource == null)
                    {
                        throw new Exception("纸盒参数错误!");
                    }
                }
                pdfPrintSettings.PrinterSettings.DefaultPageSettings.Margins.Left = Math.Max(0, (int)(pdfPrintSettings.PrinterSettings.DefaultPageSettings.PaperSize.Width - pdf.GetPageSize(0).Width) / 2);
                pdfPrintSettings.PrinterSettings.DefaultPageSettings.Margins.Top  = Math.Max(0, (int)(pdfPrintSettings.PrinterSettings.DefaultPageSettings.PaperSize.Height - pdf.GetPageSize(0).Height) / 2);
                if (param.isColor)
                {
                    pdfPrintSettings.PrinterSettings.DefaultPageSettings.Color = true;
                }
                bool isDuplex = pdfPrintSettings.PrinterSettings.CanDuplex;
                if (!isDuplex)
                {
                    if (param.DoubleForm)
                    {
                        throw new Exception("该打印机不支持双面打印!");
                    }
                    else
                    {
                        pdfPrintSettings.PrinterSettings.Duplex = System.Drawing.Printing.Duplex.Simplex;
                    }
                }
                else
                {
                    if (param.DoubleForm)
                    {
                        if (pdf.GetPageSize(0).Width > pdf.GetPageSize(0).Height)
                        {
                            pdfPrintSettings.PrinterSettings.Duplex = System.Drawing.Printing.Duplex.Horizontal;
                        }
                        else
                        {
                            pdfPrintSettings.PrinterSettings.Duplex = System.Drawing.Printing.Duplex.Vertical;
                        }
                    }
                    else
                    {
                        pdfPrintSettings.PrinterSettings.Duplex = System.Drawing.Printing.Duplex.Simplex;
                    }
                }
                pdf.Print(pdfPrintSettings);
                pdf.Dispose();
                resultString = "";
                return(true);
            }
            catch (Exception ex)
            {
                resultString += ex.Message + ";";
                pdf.Dispose();
                return(false);
            }
        }
Exemplo n.º 11
0
        public void PrintMsg()
        {
            PdfHelper    pdfHelper    = new PdfHelper();
            Document     doc          = pdfHelper.CreateDocumentA4();
            MemoryStream memoryStream = new MemoryStream();
            PdfWriter    writer       = PdfWriter.GetInstance(doc, memoryStream);

            try
            {
                doc.Open();
                doc.NewPage();

                BaseFont             ArialFont = BaseFont.CreateFont("STZHONGS.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
                iTextSharp.text.Font BoldFont  = new iTextSharp.text.Font(ArialFont, 17, iTextSharp.text.Font.BOLD, new BaseColor(System.Drawing.Color.Black));

                ApplyTableSetting print = AppHelper.ApplyPointSetting;
                PdfContentByte    pb    = writer.DirectContent;
                pb.BeginText();
                pb.SetFontAndSize(ArialFont, 15);
                pb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, DateTime.Now.ToString("yyyy"), print.X_Year, print.Y_Year, 0);
                pb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, DateTime.Now.ToString("MM"), print.X_Month, print.Y_Month, 0);
                pb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, DateTime.Now.ToString("dd"), print.X_Day, print.Y_Day, 0);

                pb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, _carInfo.Owner, print.X_Owner, print.Y_Owner, 0);
                pb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "100021", print.X_Post, print.Y_Post, 0);
                pb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, _carInfo?.Addr ?? "", print.X_Addr, print.X_Addr, 0);
                pb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, _carInfo?.MobilePhone ?? "", print.X_MobilePhone, print.Y_MobilePhone, 0);
                pb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, _carInfo.PlateNo, print.X_PlateNo, print.Y_PlateNo, 0);
                pb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, _carInfo.PlateType, print.X_PlateType, print.Y_PlateType, 0);

                pb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, _ticketNo, print.X_Ticket, print.Y_Ticket, 0);
                if (_carInfo.PlateNo.Contains("京"))
                {
                    pb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "√", print.X_Apply, print.Y_Apply, 0);
                }
                else
                {
                    pb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "√", print.X_Apply1, print.Y_Apply1, 0);
                }

                pb.EndText();

                doc.Close();
                MemoryStream pdf     = new MemoryStream(memoryStream.ToArray());
                PDFFile      pdfFile = PDFFile.Open(pdf);

                PrinterSettings settings = new PrinterSettings();
                PrintDocument   pd       = new PrintDocument();
                settings.PrinterName = AppHelper.AppSetting.PrinterName;
                settings.PrintToFile = false;

                PDFPrintSettings pdfPrintSettings = new PDFPrintSettings(settings);
                pdfPrintSettings.PageScaling            = PageScaling.MultiplePagesPerSheetProportional;
                pdfPrintSettings.PrinterSettings.Copies = 1;
                pdfFile.Print(pdfPrintSettings);
                pdf.Close();
                pdf.Dispose();
                pdfFile.Dispose();
            }
            catch (Exception ex)
            {
                LogHelper.Error("打印异常:" + ex.Message);
            }
            finally
            {
                memoryStream.Dispose();
                memoryStream.Close();
                writer.Dispose();
                doc.Dispose();
                doc.Close();
            }
        }