Hello all.
My code for printing to specific pages has been working fine for over a year now. However, I got a bug report from one of my beta testers today that it wasn’t working. Sure enough, it’s not.
I did upgrade the solution to .NET 4.7.2 in this release cycle. However, the previous version of my software (.NET 4.6.1) doesn’t work either anymore. I’m not sure whether I’ll have to completely uninstall 4.7.2 to validate that the old version could still work; hopefully it won’t come to that. I did upgrade some other parts of the application that deal with .docx file generation; so, it could be that too.
Here’s my old code – short and simple:
public void Print(string path, PaperSource paperSource, short copies)
{
using (AsposeWordsPrintDocument printDoc = new AsposeWordsPrintDocument(new Document(path))
{
PrinterSettings = new PrinterSettings { PrinterName = FullName, Copies = copies },
PrintController = new StandardPrintController(),
})
{
printDoc.DefaultPageSettings.PaperSource = paperSource;
printDoc.Print();
}
}
Here’s the code I’m testing. The examples that I found online showed to set the paper source during the QueryPageSettings event. However, everything was still correct at that point in the workflow; so, I didn’t touch anything. It’s not until the PrintPage event when things go sideways, and the event argument’s PaperSource got set to Automatic. I tried to reset the PaperSource property at this point, but it didn’t help. Note that the event argument’s PageSettings property is read-only at this point.
I’d sure appreciate a silver bullet here… Thanks!
public void Print(string path, PaperSource paperSource, short copies)
{
PaperSource = paperSource;
using (printDoc = new AsposeWordsPrintDocument(new Document(path))
{
PrinterSettings = new PrinterSettings { PrinterName = FullName, Copies = copies },
PrintController = new StandardPrintController(),
DefaultPageSettings = new PageSettings { PaperSource = paperSource },
})
{
printDoc.BeginPrint += PrintDoc_BeginPrint;
printDoc.QueryPageSettings += PrintDoc_QueryPageSettings;
printDoc.PrintPage += PrintDoc_PrintPage;
printDoc.EndPrint += PrintDoc_EndPrint;
printDoc.Print();
printDoc.BeginPrint -= PrintDoc_BeginPrint;
printDoc.QueryPageSettings -= PrintDoc_QueryPageSettings;
printDoc.PrintPage -= PrintDoc_PrintPage;
printDoc.EndPrint -= PrintDoc_EndPrint;
}
}
PaperSource PaperSource = null;
AsposeWordsPrintDocument printDoc = null;
private void PrintDoc_BeginPrint(object sender, PrintEventArgs e)
{
string tray = printDoc.DefaultPageSettings.PaperSource.SourceName; // " Tray2"
int rawKind = printDoc.DefaultPageSettings.PaperSource.RawKind; // 260
}
private void PrintDoc_QueryPageSettings(object sender, QueryPageSettingsEventArgs e)
{
string tray0 = e.PageSettings.PaperSource.SourceName; // " Tray2"
int rawKind0 = e.PageSettings.PaperSource.RawKind; // 260
string tray = printDoc.DefaultPageSettings.PaperSource.SourceName; // " Tray2"
int rawKind = printDoc.DefaultPageSettings.PaperSource.RawKind;
}
private void PrintDoc_PrintPage(object sender, PrintPageEventArgs e)
{
string tray0 = e.PageSettings.PaperSource.SourceName; // " Automatically Select" !!!
int rawKind0 = e.PageSettings.PaperSource.RawKind; // 15 !!!
string tray = printDoc.DefaultPageSettings.PaperSource.SourceName;// " Tray2"
int rawKind = printDoc.DefaultPageSettings.PaperSource.RawKind; // 260
e.PageSettings.PaperSource = PaperSource; // Reset the paper source...
tray0 = e.PageSettings.PaperSource.SourceName; // " Tray2"
rawKind0 = e.PageSettings.PaperSource.RawKind; // 260
tray = printDoc.DefaultPageSettings.PaperSource.SourceName; // " Tray2"
rawKind = printDoc.DefaultPageSettings.PaperSource.RawKind; // 260
}
private void PrintDoc_EndPrint(object sender, PrintEventArgs e)
{
string tray = printDoc.DefaultPageSettings.PaperSource.SourceName; // " Tray2"
int rawKind = printDoc.DefaultPageSettings.PaperSource.RawKind; // 260
}
Edit… I’ve tried this with Aspose.Words versions: 17.10, 18.11 and 19.1. Same behavior in all three.
AJH