C#からOpenOfficeを操作して両面印刷を実行する方法を教えてください。
よろしくお願いいたします。
環境は、VS2019でMicrosoft Officeはないです。
「uno」と呼ばれるAPIを使用。
ClosedXMLからエクセルのファイルを作成しています。
そのファイルにはシートが5つあります。
印刷実行時にシートをすべて選択して、両面印刷で実行したいです。
シートを1つ選択したときの印刷処理はできています。
わからないことは、
1シート全体を選択する方法
2両面印刷に設定する方法
// OpenOffice用 using uno.util; using unoidl.com.sun.star.frame; using unoidl.com.sun.star.lang; using unoidl.com.sun.star.sheet; using unoidl.com.sun.star.table; using unoidl.com.sun.star.uno; using unoidl.com.sun.star.beans; using unoidl.com.sun.star.util; using unoidl.com.sun.star.view; using unoidl.com.sun.star.drawing; using System.ComponentModel; public void PrintTest(BackgroundWorker bgw, string xlPath, int waitMain = 20000, int waitSub = 2000, int page = 1) { try { string imgDir = ""; if (bgw != null) bgw.ReportProgress(10); //コンポーネントコンテキストオブジェクト取得(OpenOfficeの基本プロセス) XComponentContext context = Bootstrap.bootstrap(); //サービスマネージャ取得 XMultiServiceFactory factory = (XMultiServiceFactory)context.getServiceManager(); //コンポーネントローダオブジェクト取得 XComponentLoader loader = (XComponentLoader)factory.createInstance("com.sun.star.frame.Desktop"); if (bgw != null) bgw.ReportProgress(20); //非表示で実行 プロパティ指定 PropertyValue[] args1 = new PropertyValue[1]; args1[0] = new PropertyValue(); args1[0].Name = "Hidden"; args1[0].Value = new uno.Any((Boolean)true); if (bgw != null) bgw.ReportProgress(30); //ファイルを開きドキュメントオブジェクトを生成 string docFile = "file:///" + xlPath; doc = (XSpreadsheetDocument)loader.loadComponentFromURL(docFile, "_blank", 0, args1); if (bgw != null) bgw.ReportProgress(40); //わからない部分1 シート全体を選択する方法 //シートを取得 sheets = doc.getSheets(); //Sheet1を取得 sheet = (XSpreadsheet)sheets.getByName("sheet").Value; //わからない部分2 両面印刷の設定方法 // 印刷 XPrintable xPrintable = (XPrintable)doc; // 印刷時のプロパティ設定 PropertyValue[] printDesc = new PropertyValue[1]; printDesc[0] = new PropertyValue(); printDesc[0].Name = "Name"; printDesc[0].Value = new uno.Any(""); xPrintable.setPrinter(printDesc); if (bgw != null) bgw.ReportProgress(70); // 印刷オプション PropertyValue[] printProps = new PropertyValue[1]; printProps[0] = new PropertyValue(); printProps[0].Name = "Pages"; string ppPages = string.Format("1-{0}", page); printProps[0].Value = new uno.Any(ppPages); if (bgw != null) bgw.ReportProgress(80); try { // 印刷 xPrintable.print(printProps); Thread.Sleep(waitSub); } catch (unoidl.com.sun.star.uno.Exception ex) { MessageBox.Show(ex.Message); } Thread.Sleep(waitMain); if (bgw != null) bgw.ReportProgress(90); // 閉じる if (doc != null) { XCloseable xCloseable = (XCloseable)doc; xCloseable.close(true); doc = null; } if (bgw != null) bgw.ReportProgress(95); } catch (System.Exception ex) { } if (bgw != null) bgw.ReportProgress(100); }
回答1件
あなたの回答
tips
プレビュー