Ik ben bezig met een print functie te bouwen voor mijn programma.
De printfunctie werkt als ik print naar een fysieke printer, maar op het moment dat ik probeer te printen naar een virtuele printer als PDF995 of de MS document image writer krijg ik vage problemen.
De ene keer krijg ik een AccesViolationException, de andere keer loopt het programma gewoon compleet vast, en als ik echt een flinke portie geluk heb kan ik 1 keer printen, maar als ik het dan de 2de keer probeer gaat het alsnog fout.
Na flink wat zoeken op google kan ik de oplossing niet vinden, dus hoop ik dat er hier een C#-goeroe rondhangt die mij weer de juiste weg op kan duwen.
De code:
De printfunctie werkt als ik print naar een fysieke printer, maar op het moment dat ik probeer te printen naar een virtuele printer als PDF995 of de MS document image writer krijg ik vage problemen.
De ene keer krijg ik een AccesViolationException, de andere keer loopt het programma gewoon compleet vast, en als ik echt een flinke portie geluk heb kan ik 1 keer printen, maar als ik het dan de 2de keer probeer gaat het alsnog fout.
Na flink wat zoeken op google kan ik de oplossing niet vinden, dus hoop ik dat er hier een C#-goeroe rondhangt die mij weer de juiste weg op kan duwen.
De code:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
| class Printer : IDisposable
{
private PrintDocument printDoc = new PrintDocument();
private PageSettings pgSettings = new PageSettings();
private PrinterSettings prtSettings = new PrinterSettings();
private StringReader Tekst;
public Printer()
{
printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
Tekst.Close();
printDoc.Dispose();
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public void print(string teksttoprint)
{
Tekst = new StringReader(teksttoprint);
printDoc.DefaultPageSettings = pgSettings;
PrintDialog dlg = new PrintDialog();
dlg.Document = printDoc;
if (dlg.ShowDialog() == DialogResult.OK)
{
printDoc.Print();
}
}
void printDoc_PrintPage(object sender, PrintPageEventArgs e)
{
float linesPerPage = 0;
float yPosition = 0;
int count = 0;
float leftMargin = e.MarginBounds.Left;
float topMargin = e.MarginBounds.Top;
string line = null;
Font printFont = new Font("Times New Roman", 6f, FontStyle.Regular);
SolidBrush myBrush = new SolidBrush(Color.Black);
// Work out the number of lines per page, using the MarginBounds.
linesPerPage = e.MarginBounds.Height / printFont.GetHeight(e.Graphics);
// Iterate over the string using the StringReader, printing each line.
while (count < linesPerPage && ((line = Tekst.ReadLine()) != null))
{
// calculate the next line position based on the height of the font according to the printing device
yPosition = topMargin + (count * printFont.GetHeight(e.Graphics));
// draw the next line
e.Graphics.DrawString(line, printFont, myBrush, leftMargin, yPosition, new StringFormat());
count++;
}
// If there are more lines, print another page.
if (line != null)
{
e.HasMorePages = true;
}
else
{
e.HasMorePages = false;
}
myBrush.Dispose();
}
} |