c# - 打印成 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 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 文件的过程一无所知。在我的场景中,我需要在不使用 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”的引用。我花了比我想记住的时间更长的时间来找出我无法访问打印作业的原因。

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

关于c# - 打印成 XPS 文件,然后打印到打印机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13360720/

相关文章:

c# - 如何在虚拟模式下以编程方式编辑 datagridview 的单元格值?

c# - 将所有首字母转换为大写,每个单词的首字母小写

c# - 强制控制宽度或高度?

list - 只是试图递归地打印一个列表,但没有打印

perl - 排序多级 Perl 散列(基于算术动态)

c# - 如何在 ASP.Net C# 中区分 .mp4 音频文件和 .mp4 视频文件

c# - 为什么我的匿名方法不能在循环中工作?

c# - 只使用一次定时器

c# - 如何获取创建对象 'cells' 的范围?

python - 打印迭代次数而不换行