Silently use Microsoft XPS Document Writer printer to create XPS

陌路散爱 提交于 2019-12-31 03:23:06

问题


For some days now I've been battling with printing XPS to file without the dialog. I've read posts on the matter in CodeGuru and by Feng Yuan (MSDN), along with many discussion topics here and I am still lost.

Specifically my scenario is that I have a 3rd party API that I must use, and it prints to the default printer (say Microsoft XPS Document Writer). I want to be able to "apply" a filename prior to the printing procedure, and of course not to have dialog.

I've tried working with WinDDK - XPSDRV and LOCALMON samples but wasn't able to figure out exactly how to manipulate the code to achieve my goals. (or even fully understand if I need a new printer driver or a new port type)


回答1:


You will delete filters in pipeline xml and also related dll's in inf file. But yet, as I did, i guess you will face problem of printing canvas (graphics). I wasn't able to convert / transform this canvas to glyphs to get the contents of it.

If you had further issues, let me know

Kind Regards




回答2:


I ran into the same need. Following is some of the logic that provides the desired functionality for me:

 // 
 // 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)


来源:https://stackoverflow.com/questions/5182696/silently-use-microsoft-xps-document-writer-printer-to-create-xps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!