静默使用 Microsoft XPS Document Writer 打印机创建 XPS

2023-12-06

几天来,我一直在努力在没有对话框的情况下将 XPS 打印到文件。 我读过 CodeGuru 和 Feng Yuan (MSDN) 中有关此事的帖子,以及这里的许多讨论主题,但我仍然迷失。

具体来说,我的场景是我必须使用一个第三方 API,它会打印到默认打印机(例如 Microsoft XPS Document Writer)。我希望能够在打印过程之前“应用”文件名,当然没有对话框。

我尝试过使用 WinDDK - XPSDRV 和 LOCALMON 示例,但无法确切地弄清楚如何操作代码来实现我的目标。 (甚至完全了解我是否需要新的打印机驱动程序或新的端口类型)


我遇到了同样的需求。以下是为我提供所需功能的一些逻辑:

 // 
 // PrintDocument_inst
 // 
 this.PrintDocument_inst.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.k_line_PrintPage);

  private void Print( string align_file_name )
  {
     if ( plot_metafile == null )
     {
        MessageBox.Show( "you need to load offset data before printing a plot" );
        return;
     }

     try
     {
        PrintDocument_inst.DefaultPageSettings = PageSettings_inst;

        PrintDialog_inst = new PrintDialog( );
        PrintDialog_inst.Document = PrintDocument_inst;
        PrintDialog_inst.UseEXDialog = true;  // this must be set true or dialog won't show on 64 bit Vista 
        PrintDialog_inst.PrinterSettings.PrinterName = "Microsoft XPS Document Writer";
        PrintDialog_inst.PrinterSettings.PrintToFile = true;
        PrintDialog_inst.PrinterSettings.PrintFileName = align_file_name;

        i_page_to_print_next = 1;
        n_pages_still_to_print = 1;
        PrintDocument_inst.Print( );
     }
     catch ( Exception e )
     {
        MessageBox.Show( e.ToString( ) );
     }
     finally
     {
     }

  }  //    end of function   Print( string align_file_name )

  //PrintPage event handler
  private void k_line_PrintPage(object sender,PrintPageEventArgs ppea)
  {         
     int leftMargin = ppea.MarginBounds.Left;
     int topMargin = ppea.MarginBounds.Top ;

     try
     {
        float _scale_f;

        if ( PrintDialog_inst != null )
        {
           string str_printer_name = PrintDialog_inst.PrinterSettings.PrinterName.ToString ( );
           if ( str_printer_name.CompareTo ( "Adobe PDF" ) == 0 )
           {
              _scale_f = 0.61F; //  0.85F;
           }
           else
           {
              _scale_f = 0.59F; //  0.82F;
           }
        }
        else  // case of print preview
        {
           _scale_f = 0.59F; // 0.82F;
        }
        if ( _scale_f != 1.0F ) ppea.Graphics.ScaleTransform ( _scale_f, _scale_f );
        ppea.Graphics.DrawImage ( plot_metafile, leftMargin, topMargin );
        ppea.HasMorePages = ( --n_pages_still_to_print > 0 ? true : false );
     }
     finally
     {
     }
  } //  end of  private void k_line_PrintPage(object sender,PrintPageEventArgs ppea)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

静默使用 Microsoft XPS Document Writer 打印机创建 XPS 的相关文章

随机推荐