下記のようなディレクトリがあったとして、「テスト」フォルダの配下にあるファイルをすべて印刷したいです。
下記コードだと、最終的に最後の一枚しか印刷が実行されません。
ファイルの数だけイベントハンドラーを回せばよいのかと思ったのですが、pd_PrintPageに印刷対象のファイルパスを渡せないため断念しております。
実現方法ご教授いただきたいです。
private void PrintButton_Click(object sender, EventArgs e) { //PrintDocumentオブジェクトの作成 System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument(); //指定したプリンタのプリンタ名取得 pd.PrinterSettings.PrinterName = "プリンタ名" pd.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(pd_PrintPage); //両面印刷機能の有無 //両面に設定 if (pd.PrinterSettings.CanDuplex) { pd.PrinterSettings.Duplex = Duplex.Horizontal; } //印刷開始 pd.Print(); } //PrintPageイベントハンドラ private void pd_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { //テストフォルダまでを追加 var folderPath = System.Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + @"\Downloads\" + "テスト"; // DirectoryInfoのインスタンスを生成する var di = new DirectoryInfo(folderPath); // テストフォルダ直下のすべてのディレクトリ一覧を取得する var diAlls = di.GetDirectories(); foreach (var d in diAlls) { //フォルダ内ファイルをすべて取得 string[] files = System.IO.Directory.GetFiles(di + "\\" + d.ToString(), "*", System.IO.SearchOption.AllDirectories); foreach (var fileName in files) { string inputFile = fileName; // PDFをロード。イメージに変換 PdfiumViewer.PdfDocument document = PdfiumViewer.PdfDocument.Load(inputFile); //PDFから画像データを作成 var img = document.Render(0, 4961, 7016, 350, 350, false); //画像の描画 e.Graphics.DrawImage(img, e.PageBounds); // 印刷継続を指定 e.HasMorePages = false; } }

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。