C# 打印到XPS文件,然后打印到打印机

C# 打印到XPS文件,然后打印到打印机,c#,winforms,printing,richtextbox,xps,C#,Winforms,Printing,Richtextbox,Xps,我曾尝试打印RichTextBox的内容,但如果打印到打印机,则会出现太多错误。 但当我打印到XPS文件(通过windows中的XPS打印机),然后将此文件打印到打印机时,一切都正常 那么我可以通过编程来完成所有这些事情吗 以下是我的打印方法: public int PrintRotate(bool rotate, PrintPageEventArgs e, int charFrom, int charTo) { //Calculate the area to rend

我曾尝试打印RichTextBox的内容,但如果打印到打印机,则会出现太多错误。 但当我打印到XPS文件(通过windows中的XPS打印机),然后将此文件打印到打印机时,一切都正常

那么我可以通过编程来完成所有这些事情吗

以下是我的打印方法:

 public int PrintRotate(bool rotate, PrintPageEventArgs e, int charFrom, int charTo)
    {
        //Calculate the area to render and print
        RECT rectToPrint;
        rectToPrint.Top = (int)(e.MarginBounds.Top * anInch);
        rectToPrint.Bottom = (int)(e.MarginBounds.Bottom * anInch);
        rectToPrint.Left = (int)(e.MarginBounds.Left * anInch);
        rectToPrint.Right = (int)(e.MarginBounds.Right * anInch);

        //Calculate the size of the page
        RECT rectPage;
        rectPage.Top = (int)(e.PageBounds.Top * anInch);
        rectPage.Bottom = (int)(e.PageBounds.Bottom * anInch);
        rectPage.Left = (int)(e.PageBounds.Left * anInch);
        rectPage.Right = (int)(e.PageBounds.Right * anInch);

        IntPtr hdc = e.Graphics.GetHdc();

        FORMATRANGE fmtRange;
        fmtRange.chrg.cpMax = charTo;               //Indicate character from to character to 
        fmtRange.chrg.cpMin = charFrom;
        fmtRange.hdc = hdc;                    //Use the same DC for measuring and rendering
        fmtRange.hdcTarget = hdc;              //Point at printer hDC
        fmtRange.rc = rectToPrint;             //Indicate the area on page to print
        fmtRange.rcPage = rectPage;            //Indicate size of page


        SetGraphicsMode(fmtRange.hdc, GM_ADVANCED);

        XFORM par = new XFORM();

        par = new XFORM();
        par.eM11 = 1;
        par.eM12 = 0;
        par.eM21 = 0;
        par.eM22 = 1;
        par.eDx = -e.PageSettings.Margins.Left / 100 * e.PageSettings.PrinterResolution.X;
        par.eDy = -e.PageSettings.Margins.Top / 100 * e.PageSettings.PrinterResolution.Y;
        ModifyWorldTransform(fmtRange.hdc, ref par, MWT_LEFTMULTIPLY);

        IntPtr res = IntPtr.Zero;

        IntPtr wparam = IntPtr.Zero;
        wparam = new IntPtr(1);

        //Get the pointer to the FORMATRANGE structure in memory
        IntPtr lparam = IntPtr.Zero;
        lparam = Marshal.AllocCoTaskMem(Marshal.SizeOf(fmtRange));
        Marshal.StructureToPtr(fmtRange, lparam, false);

        //Send the rendered data for printing 
        res = SendMessage(Handle, EM_FORMATRANGE, wparam, lparam);

        //Free the block of memory allocated
        Marshal.FreeCoTaskMem(lparam);

        //Release the device context handle obtained by a previous call
        e.Graphics.ReleaseHdc(hdc);

        //Return last + 1 character printer
        return res.ToInt32();
    }

我遇到过这样的问题,最终生成了一个.XPS文件,然后将其发送到打印机

从您的问题来看,听起来您已经有了“打印”到xps文件的过程,这很好,因为我对将富文本框打印到xps文件的过程一无所知。在我的scenaria中,我需要在不使用ms office的情况下打印文档,因此我最终制作了一个XPS文件,用代码编辑它,然后将其发送到打印机

这是我用来将xps文件直接发送到打印机的代码:

LocalPrintServer localPrintServer = new LocalPrintServer();
var queue = localPrintServer.GetPrintQueue("NameOfPrinter");
PrintSystemJobInfo xpsPrintJob = queue.AddJob("name of print job", "my/xps/path.xps",false);
还请记住,要使此代码正常工作,您需要添加对System.Printing和“ReachFramework”的引用。我花了很长时间才找到为什么我不能访问printjob


根据我的经验,大多数打印机都应该支持这一点。常见的条形码打印机,甚至可以在我们存储部门的古怪“条形码打印机”上使用。

您有没有找到解决方案?我希望做同样的事情。不幸的是没有。这项任务现在对我来说优先级较低。我也想解决这个问题。