Loading

RichEditDocumentServer打印记录

RichEditDocumentServer srv = new RichEditDocumentServer();
srv.LoadDocument("test.docx");

FixedDocument document = RichEditDocumentXpfPrinter.CreateFixedDocument(srv);

PrintDialog pDialog = new PrintDialog();
PrintQueueCollection queues = new PrintServer().GetPrintQueues(new[] { EnumeratedPrintQueueTypes.Local,
    EnumeratedPrintQueueTypes.Connections});
System.Collections.IEnumerator localPrinterEnumerator = queues.GetEnumerator();
PrintQueue printQueue = null;

do
{
    if (!localPrinterEnumerator.MoveNext())
        break;
    printQueue = (PrintQueue)localPrinterEnumerator.Current;
}
while (!printQueue.FullName.Contains("Canon"));
if (printQueue != null)
{
    pDialog.PrintQueue = printQueue;
    pDialog.PrintDocument(document.DocumentPaginator, string.Empty);
}
using System.Printing;
using System.Windows;
using System.Windows.Controls;
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.Printing;
using System.Windows.Documents;
using System.Drawing.Printing;
    public class CustomXpfRichEditPrinter : XpfRichEditPrinter
    {
        public CustomXpfRichEditPrinter(IRichEditControl control)
            : base(control) {}

        public void PrintToMyPrinter()
        {
            PrintDialog pDialog = new PrintDialog();
            PrintQueueCollection queues = new PrintServer().GetPrintQueues(new[] { EnumeratedPrintQueueTypes.Local,
                EnumeratedPrintQueueTypes.Connections });
            System.Collections.IEnumerator localPrinterEnumerator = queues.GetEnumerator();
            PrintQueue printQueue = null;

            if (localPrinterEnumerator.MoveNext()) {
                printQueue = (PrintQueue)localPrinterEnumerator.Current;
            }

            if (printQueue != null) {
                pDialog.PrintQueue = printQueue;
                FixedDocument document = this.CreateFixedDocument();
                pDialog.PrintDocument(document.DocumentPaginator, string.Empty);
            }
        }
    }
using DevExpress.Xpf.Printing;
using DevExpress.Xpf.RichEdit;
using DevExpress.XtraPrinting;
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.Commands;
using DevExpress.XtraRichEdit.Internal;
using DevExpress.XtraRichEdit.Services;

using HealthHappy.Configs;

using Microsoft.Win32;

using System;
using System.IO;


using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

using DevExpress.XtraRichEdit.Utils;
using DevExpress.XtraRichEdit.API.Native;

using System.Windows.Controls;
using System.Printing;
using DevExpress.XtraRichEdit.Printing;
using System.Windows.Documents;


namespace HealthHappy.UI.UReport.Base
{
    public static class MyFactoryHelper
    {
        public static void SetMyNewCommandFactory(RichEditControl control,string paID)
        {
            try
            {
                IRichEditCommandFactoryService service = control.GetService<IRichEditCommandFactoryService>();
                if (service == null)
                {
                    service = new XpfRichEditCommandFactoryService(control);
                    control.AddService(typeof(IRichEditCommandFactoryService), service);

                }
                CustomRichEditCommandFactoryService myCommandFactory = new CustomRichEditCommandFactoryService(control, service,paID);
                control.ReplaceService<IRichEditCommandFactoryService>(myCommandFactory);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }
    }
    public class CustomRichEditCommandFactoryService : IRichEditCommandFactoryService
    {
        private string paID = string.Empty;
        private readonly IRichEditCommandFactoryService service;
        private readonly IRichEditControl control;
        public CustomRichEditCommandFactoryService(IRichEditControl control, IRichEditCommandFactoryService service,string _paID)
        {
            DevExpress.Utils.Guard.ArgumentNotNull(control, "control");
            DevExpress.Utils.Guard.ArgumentNotNull(service, "service");
            this.control = control;
            this.service = service;
            paID = _paID;
        }
        public RichEditCommand CreateCommand(RichEditCommandId id)
        {
            //if (id == RichEditCommandId.FileSaveAs)
            //{
            //    return new CustomSaveDocumentAsCommand(control);
            //}
            switch(id.ToString())
            {
                case "85":                  //RichEditCommandId.FileSaveAs
                    return new CustomSaveDocumentAsCommand(control);
                case "96":                  //RichEditCommandId.Print
                    return new CustomPrintDocumentCommand(control, paID);
            }

            return service.CreateCommand(id);
        }
    }

    public class CustomPrintDocumentCommand: PrintCommand
    {
        string paID = string.Empty;
        public CustomPrintDocumentCommand(IRichEditControl control,string _paID)
            : base(control) {
            paID = _paID;
        }

        protected override void ExecuteCore()
        {
            RichEditControl rich = (RichEditControl)this.Control;
            //base.ExecuteCore();

            var window = new DocumentPreviewWindow();
            window.PreviewControl.DocumentSource = rich;
            window.print


            if (string.IsNullOrEmpty(paID))
                Console.WriteLine(paID);
            return;

            PrintDialog pDialog = new PrintDialog();
            pDialog.UserPageRangeEnabled = true;
            PrintQueueCollection queues = new PrintServer().GetPrintQueues(new[] { EnumeratedPrintQueueTypes.Local,
                EnumeratedPrintQueueTypes.Connections });
            System.Collections.IEnumerator localPrinterEnumerator = queues.GetEnumerator();
            PrintQueue printQueue = null;

            //if (localPrinterEnumerator.MoveNext())
            //{
            //    printQueue = (PrintQueue)localPrinterEnumerator.Current;
            //}
            string printer = UserConfigManager.Instance.Root.Printer.Bg;

            do
            {
                if (!localPrinterEnumerator.MoveNext())
                    break;
                printQueue = (PrintQueue)localPrinterEnumerator.Current;
            }
            while (printQueue.FullName != printer);

            if (printQueue != null)
            {
                pDialog.PrintQueue = printQueue;
                
                FixedDocument document = DevExpress.Xpf.RichEdit.RichEditDocumentXpfPrinter.CreateFixedDocument(rich);

                if (pDialog.ShowDialog() == true)
                {
                    if (!string.IsNullOrEmpty(paID))
                    {
                        Console.WriteLine("1");
                        ///修改打印状态
                        //HHTechDB.SqlHelper.ExecuteNonQuery();
                    }
                    string duplexType = CommonConfigManager.Instance.Root.Report.DuplexType;
                    if (string.IsNullOrEmpty(duplexType))
                        duplexType = "OneSided";
                    if (duplexType == "OneSided")
                    {
                        pDialog.PrintTicket.Duplexing = Duplexing.OneSided;
                    }
                    else if (duplexType == "TwoSidedLongEdge")
                    {
                        pDialog.PrintTicket.Duplexing = Duplexing.TwoSidedLongEdge;
                    }
                    else if (duplexType == "TwoSidedShortEdge")
                    {
                        pDialog.PrintTicket.Duplexing = Duplexing.TwoSidedShortEdge;
                    }
                    else
                    {
                        pDialog.PrintTicket.Duplexing = Duplexing.OneSided;
                    }

                    pDialog.PrintTicket.PageMediaSize = new System.Printing.PageMediaSize(System.Printing.PageMediaSizeName.ISOA4);
                    pDialog.PrintTicket.OutputQuality = OutputQuality.High;

                    
                    pDialog.PrintDocument(document.DocumentPaginator, string.Empty);
                }
                //else
                //    MessageBox.Show("Printing was cancelled");
            }
        }
    }

    /// <summary>
    /// 另存为文件自定义
    /// </summary>
    public class CustomSaveDocumentAsCommand : SaveDocumentAsCommand
    {
        public CustomSaveDocumentAsCommand(IRichEditControl control)
            : base(control) { }

        protected override void ExecuteCore()
        {
            SaveFileDialog dialog = new SaveFileDialog
            {
                Filter = "Microsoft Word文件(*.docx)|*.docx|RTF文件(*.rtf)|*.rtf|PDF文件(*.pdf)|*.pdf|All Files (*.*)|*.*",
                FileName = "report",
                DefaultExt = "docx",
                RestoreDirectory = true,
                CheckFileExists = false,
                CheckPathExists = true,
                OverwritePrompt = true,
                DereferenceLinks = true,
                ValidateNames = true,
                AddExtension = false,
                FilterIndex = 1
            };
            try
            {
                UserConfigRootElement root = UserConfigManager.Instance.Root;
                dialog.InitialDirectory = root.Report.SavePath;
                bool? result = dialog.ShowDialog();
                if (result == true)
                {
                    if (dialog.FilterIndex == 3)
                    {
                        RichEditControl rich = (RichEditControl)this.Control;

                        IRichEditDocumentServer server = rich.CreateDocumentServer();
                        MemoryStream ms = new MemoryStream();
                        rich.SaveDocument(ms, DocumentFormat.Rtf);
                        ms.Seek(0, SeekOrigin.Begin);
                        server.LoadDocument(ms, DocumentFormat.Rtf);
                        LegacyPrintableComponentLink link = new LegacyPrintableComponentLink(server as IPrintable);
                        link.CreateDocument(false);
                        link.ExportToPdf(dialog.FileName);
                    }
                    else
                    {
                        DocumentFormat docFormat = DocumentFormat.OpenXml;
                        if (dialog.FilterIndex == 2)
                        {
                            docFormat = DocumentFormat.Rtf;
                        } ((RichEditControl)this.Control).SaveDocument(dialog.FileName, docFormat);
                    }
                }
            }
            catch (Exception ex)
            {
                HHTech.Log.Error("报告另存为崩溃", ex);
            }
        }
    }
}

 

posted @ 2021-04-27 20:24  jevan  阅读(249)  评论(0编辑  收藏  举报