'How to print out documents on a list which is datasource of DGV C#
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.
Solution 1:[1]
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);
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | LFLJM |