如何在列表上打印文档,即DGV C#的数据源

发布于 01-20 15:43 字数 2202 浏览 2 评论 0原文

我的应用程序有一个 DataGridView,其中列出了所有选定的文件并将其添加到作为 DGV 数据源的类中。

 public partial class Form1 : Form
    { 

 BindingList<Datei> dateienList = new BindingList<Datei>();



  private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Word(*.docx)| *.docx|PPT(*.pptx)|*.pptx|PDF(*.pdf)|*.pdf|Alle Dateien(*.*)|*.*";//filter is an attribute
            ofd.Multiselect = true;
           
                if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) //the next code executes only if ok is clicked
                {
                    foreach (String path in ofd.FileNames) 
                    {                                      
                        Datei datei = new Datei();  
                        datei.filePath = path;                 
                        datei.Dateiname = Path.GetFileName(path);
                        dateienList.Add(datei);
                    }
                }
            }
}

数据源类:

 public class Datei  
    {                     
        [DisplayName("Dateiname")] 
        public string Dateiname { get; set; }
        [DisplayName("Neue Dateiname")]
        public string NeueDateiname { get; set; }
        public string filePath { get; set; }
        public string newFilePath { get; set; }
       
     
    }

我想制作一个按钮来打印列表中的所有文档。这就是我尝试过的:

private void button2_Click(object sender, EventArgs e)
        {
            PrintDialog printDialog1 = new PrintDialog();
            
            PrintDocument printDocument = new PrintDocument();
            printDialog1.AllowSelection = true;
            printDialog1.AllowSomePages = true;
            printDialog1.AllowCurrentPage = true;
            
                if (printDialog1.ShowDialog() == DialogResult.OK)
                {   

                    foreach (Datei datei in dateienList)
                    {
                       printDocument.Print(); // guess it is totally wrong...
                    }
                }
            
           
        }

它没有按预期工作,打印出空白。任何帮助表示赞赏。

my app has a DataGridView where all selected files get listed and added to a class which is the datasource of the DGV.

 public partial class Form1 : Form
    { 

 BindingList<Datei> dateienList = new BindingList<Datei>();



  private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Word(*.docx)| *.docx|PPT(*.pptx)|*.pptx|PDF(*.pdf)|*.pdf|Alle Dateien(*.*)|*.*";//filter is an attribute
            ofd.Multiselect = true;
           
                if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) //the next code executes only if ok is clicked
                {
                    foreach (String path in ofd.FileNames) 
                    {                                      
                        Datei datei = new Datei();  
                        datei.filePath = path;                 
                        datei.Dateiname = Path.GetFileName(path);
                        dateienList.Add(datei);
                    }
                }
            }
}

datasource class:

 public class Datei  
    {                     
        [DisplayName("Dateiname")] 
        public string Dateiname { get; set; }
        [DisplayName("Neue Dateiname")]
        public string NeueDateiname { get; set; }
        public string filePath { get; set; }
        public string newFilePath { get; set; }
       
     
    }

I want to make a button to print out all the documents on the list. This is what I tried:

private void button2_Click(object sender, EventArgs e)
        {
            PrintDialog printDialog1 = new PrintDialog();
            
            PrintDocument printDocument = new PrintDocument();
            printDialog1.AllowSelection = true;
            printDialog1.AllowSomePages = true;
            printDialog1.AllowCurrentPage = true;
            
                if (printDialog1.ShowDialog() == DialogResult.OK)
                {   

                    foreach (Datei datei in dateienList)
                    {
                       printDocument.Print(); // guess it is totally wrong...
                    }
                }
            
           
        }

It did not work as expected, printing out blank. Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暮倦2025-01-27 15:43:32

我无法避免打开Word程序,但至少它可以工作。

 private void button2_Click(object sender, EventArgs e)
    {
         PrintDialog printDialog1 = new PrintDialog();
         if (printDialog1.ShowDialog() == DialogResult.OK)
         {
               foreach (Datei datei in dateienList) 
               {  
                    
                    Document doc = word.Documents.Open(datei.pfad); 
                    doc.Activate();
                    object copies = datei.AnzahlKopien;
                    object pages = "";
                    object range = Microsoft.Office.Interop.Word.WdPrintOutRange.wdPrintAllDocument;
                    object items = Microsoft.Office.Interop.Word.WdPrintOutItem.wdPrintDocumentContent;
                    object pageType = Microsoft.Office.Interop.Word.WdPrintOutPages.wdPrintAllPages;
                    object oTrue = true;
                    object oFalse = false;
                    object missing = Type.Missing;
                    object outputFileName = "";
                    doc.PrintOut(ref oTrue, ref oFalse, ref range, ref outputFileName, ref missing, ref missing,
                        ref items, ref copies, ref pages, ref pageType, ref oFalse, ref oTrue,
                        ref missing, ref oFalse, ref missing, ref missing, ref missing, ref missing);
               }
         }     
    }

I couldn't avoid opening Word program, but at least it works.

 private void button2_Click(object sender, EventArgs e)
    {
         PrintDialog printDialog1 = new PrintDialog();
         if (printDialog1.ShowDialog() == DialogResult.OK)
         {
               foreach (Datei datei in dateienList) 
               {  
                    
                    Document doc = word.Documents.Open(datei.pfad); 
                    doc.Activate();
                    object copies = datei.AnzahlKopien;
                    object pages = "";
                    object range = Microsoft.Office.Interop.Word.WdPrintOutRange.wdPrintAllDocument;
                    object items = Microsoft.Office.Interop.Word.WdPrintOutItem.wdPrintDocumentContent;
                    object pageType = Microsoft.Office.Interop.Word.WdPrintOutPages.wdPrintAllPages;
                    object oTrue = true;
                    object oFalse = false;
                    object missing = Type.Missing;
                    object outputFileName = "";
                    doc.PrintOut(ref oTrue, ref oFalse, ref range, ref outputFileName, ref missing, ref missing,
                        ref items, ref copies, ref pages, ref pageType, ref oFalse, ref oTrue,
                        ref missing, ref oFalse, ref missing, ref missing, ref missing, ref missing);
               }
         }     
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文