Setting Printer Preference - Page Orientation to Landscape

和自甴很熟 提交于 2019-12-02 00:39:17

问题


I would like to set the Page orientation to LandScape for printing excel worksheet from my excel Vsto project. Manually page orientation is set from the Printer Preference window which is popped up from 'Print' form.

I need some automation which will set the orientation to LandScape every time user gives print command.

I have noticed that if i set the orientation to LandScape from my excel application, it stays same for if i want to give a print from a MS-word application & vice versa. So there has to be some kind of flag which can be changed from any simple winform application.

Is there any way i could manipulate the properties ?


回答1:


I could not find any way we could customize the printer settings of any individual printer. Here's the code which worked for me for EXCEL application.

CommonData._WORKBOOK is a static workbook object

Worksheet ws = CommonData._WORKBOOK.Application.ActiveSheet as Worksheet;

var _with1 = ws.PageSetup;

_with1.Orientation = Microsoft.Office.Interop.Excel.XlPageOrientation.xlLandscape;
CommonData._WORKBOOK.Application.Dialogs[Microsoft.Office.Interop.Excel.XlBuiltInDialog.xlDialogPrint].Show(Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                            Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                            Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                            Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                            Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                            Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);



回答2:


You can try something like the following below should work for you

public void CustPrinting() 
{
   try 
     {
       strPrint = new StreamReader (filePath);
     try 
       {
         printFont = new Font("Arial", 10);
         PrintDocument pd = new PrintDocument(); 
         pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
         pd.PrinterSettings.PrinterName = printer;
         // Set the page orientation to landscape.
         pd.DefaultPageSettings.Landscape = true;
         pd.Print();
       } 
     finally 
     {
       strPrint.Close() ;
     }
   } 
   catch(Exception ex)
   { 
     MessageBox.Show(ex.Message);
   }
 }


来源:https://stackoverflow.com/questions/13197606/setting-printer-preference-page-orientation-to-landscape

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