Re: User-defined paper source for printing Words document?

Hi,


I seem to have a similar problem. I can’t specify the papersource and have my document print from the correct tray. I can specify number of copies fine every time. I am using net3.5_ClientProfile_AuthenticodeSigned and the latest .dll (15.3.0.0).

Here’s my code:
doc = new Aspose.Words.Document(stream);
System.Drawing.Printing.PrinterSettings ps = new System.Drawing.Printing.PrinterSettings();
ps.PrinterName = form.Printer_Name;
foreach (PaperSource paperSource in ps.PaperSources)
{
if (paperSource.SourceName.Contains(form.Printer_Tray))
{
ps.DefaultPageSettings.PaperSource = paperSource;
}
}
ps.Copies = (short)form.Copies;
doc.Print(ps);

In this code I actually see how the ps.DefaultPageSettings.PaperSource gets correctly assigned to the wanted paperSource, however the printer still prints to the wrong one. Copies are correct. This code setup works in Cells.

Any ideas?
Thanks!


Hi edgar,


Thanks
for sharing the detail. Please note that Aspose.Words print document pages using the standard .NET printing infrastructure.

Aspose.Words reads values from document in Raw
format (i.e. as int) and after Print method is called, a function
compares the Raw value of PaperTray of current page and values in
PrinterSettings.PaperSources and if it doesn’t find coincidence,
function returns “Automatic” value. Then it passes the value for
PaperTray to underlying Windows function that actually sends document to
printer.

You
should also note that paper tray numbers are printer specific. I
suppose the problem might occur because paper trays numbers specified in
your document do not match paper tray numbers of the printer running on
your local environment. So, first of all you should make sure that
paper trays in your document are specified correctly.

Hope this answers your query. Please let us know if you have any more queries.

Hi Tahir,


As you can see in the my code snippet. I loop through all the papersources, and once I get to the desired papersource (based on Sourcename comparison), I assign the papersource to be used. When I debug this, I see how the If statement is entered and the DefaultaPageSettings.PaperSource is assigned to the proper paperSource. I check the values of the variable PrinterSettings ps right when it’s passed to doc.Print(ps) and it contains the proper Papersource, however it actually doesn’t print from the correct tray. I am not sure what else I can do here…

Looking forward to your reply!

Kevin

Also, I would like to reiterate that the exact same code in terms of assigning PaperSource and printing works in Cells.

It appears that I have got it working, however I still do believe that my earlier code should work on its own. In order to make it work, I had to add the following red code:



System.Drawing.Printing.PrinterSettings ps = new System.Drawing.Printing.PrinterSettings();
ps.PrinterName = form.Printer_Name;
foreach (PaperSource paperSource in ps.PaperSources)
{
if (paperSource.SourceName.Contains(form.Printer_Tray))
{
ps.DefaultPageSettings.PaperSource = paperSource;
}
}

foreach (Section section in doc.Sections)
{
section.PageSetup.FirstPageTray = ps.DefaultPageSettings.PaperSource.RawKind;
section.PageSetup.OtherPagesTray = ps.DefaultPageSettings.PaperSource.RawKind;
}

ps.Copies = (short)form.Copies;
doc.Print(ps);

Hope this will save someone that runs into this issue a lot of time!
Cheers,

Kevin

Hi edgar,


Thanks for your feedback. It is nice to hear from you that you have found the solution of your issue.

Please note that Microsoft
Word stores printer specific values for paper trays in a Word document
and therefore, only printing on the same printer model as the one that
was selected when the user specified the paper trays will result in
printing from the correct trays. If you print a document on a different
printer, then most likely the default paper tray will be used, not the
trays specified in the document.

Following code example changes all sections in a document to use the default paper tray of the selected printer.

Document doc = new
Document(MyDir + “in.docx”);

// Find the printer that will be used for printing this document. In this case it is the default printer.

// You can define a specific printer using PrinterName.

System.Drawing.Printing.PrinterSettings settings = new System.Drawing.Printing.PrinterSettings();

// The paper tray value stored in documents is completely printer specific. This means

// The code below resets all page tray values to use the current printers default tray.

// You can enumerate PrinterSettings.PaperSources to find the other valid paper tray values of the selected printer.

foreach (Section section in doc.Sections)

{

section.PageSetup.FirstPageTray = settings.DefaultPageSettings.PaperSource.RawKind;

section.PageSetup.OtherPagesTray = settings.DefaultPageSettings.PaperSource.RawKind;

}

Yeah, notice, I set the printer name based on what I have stored in a local DB, same with the tray. This is of course presuming, as you rightly alluded to, that the printer model with that name was present when the data was stored in the DB and is still present when documents need to be printed.


I still believe that the last foreach loop, in theory, is superfluous and shouldn’t be used if the previous foreach loop does its job properly.

Thanks anyways,

Kevin

Hi edgar,


Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.