Printer settings are ignored

Hi,

I’m trying to use Aspose.Words to print out Word and HTML documents.
To do this I use the following piece of code:

====================================================================

PrintDialog printDlg = new PrintDialog();
printDlg.PrinterSettings.PrinterName = "PRINTER NAME";

if (!printDlg.ShowDialog().Equals(DialogResult.OK))
    return;

Aspose.Words.Document doc = new Aspose.Words.Document(srcFile);

Aspose.Words.Rendering.AsposeWordsPrintDocument awPrintDoc = new Aspose.Words.Rendering.AsposeWordsPrintDocument(doc);
awPrintDoc.PrinterSettings = printDlg.PrinterSettings;

awPrintDoc.Print();

====================================================================
Choosing the printer and entering/retrieving all the settings appears to work fine. I get the printer dialog, and after changing some settings I can read them again from the returned printer settings.
The settings however are not used by the printer itself. I selected to print duplex AND orientate horizontally but neither settings was used during printing.

When I look in the printing queue I find my document and opening its properties also shows me the correct parameters (as you can see in the attached image).

I also tried not using the printer dialog screen and just setting the parameters manually like so:

====================================================================

PrinterSettings printSettings = new PrinterSettings();
printSettings.PrinterName = "PRINTER NAME";
printSettings.Duplex = Duplex.Horizontal;

doc.Print(printSettings);

====================================================================
This gives me the same result as the other code piece, the settings are visible in the printer queue, but the printer refuses to use them.

When printing straight from Word I can use these settings without problems so the printer is working correctly.

I’ve run out of ideas to try, could you maybe guide me in a new direction?

Hi Nick,

Thanks for your inquiry. In case you are using an older version of Aspose.Words, I would suggest you please upgrade to the latest version (v14.2.0) from here and let us know how it goes on your side. If the problem still remains, please attach your input Word document here for testing. I will investigate the issue on my side and provide you more information.

Moreover, please note that the Aspose.Words Print method uses System.Drawing and standard .NET printing classes. Please try following code snippet for testing at your end. This code does not use Aspose.Words at all. So if it will not work, the problem is somewhere on your side.

string[] printText = new string[101];
for (int i = 0; i <101; i++)
{
    printText[i] = i.ToString();
    printText[i] += ": The quick brown fox jumps over the lazy dog.";
}
PrintDocument doc = new TextDocument(printText);
doc.PrintPage += this.Doc_PrintPage;
PrintDialog dlgSettings = new PrintDialog();
dlgSettings.Document = doc;
// If the user clicked OK, print the document.
if (dlgSettings.ShowDialog() == DialogResult.OK)
{
    doc.Print();
}
public class TextDocument: PrintDocument
{
    private string[] text;
    private int pageNumber;
    private int offset;
    public string[] Text
    {
        get
        {
            return text;
        }
        set
        {
            text = value;
        }
    }
    public int PageNumber
    {
        get
        {
            return pageNumber;
        }
        set
        {
            pageNumber = value;
        }
    }
    public int Offset
    {
        get
        {
            return offset;
        }
        set
        {
            offset = value;
        }
    }
    public TextDocument(string[] text)
    {
        this.Text = text;
    }
}
private void Doc_PrintPage(object sender, PrintPageEventArgs e)
{
    // Retrieve the document that sent this event.
    TextDocument doc = (TextDocument) sender;
    // Define the font and determine the line height.
    using(System.Drawing.Font font = new System.Drawing.Font("Arial", 10))
    {
        float lineHeight = font.GetHeight(e.Graphics);
        // Create variables to hold position on page.
        float x = e.MarginBounds.Left;
        float y = e.MarginBounds.Top;
        // Increment the page counter (to reflect the page that
        // is about to be printed).
        doc.PageNumber += 1;
        // Print all the information that can fit on the page.
        // This loop ends when the next line would go over the
        // margin bounds, or there are no more lines to print.
        while ((y + lineHeight) <e.MarginBounds.Bottom &&
            doc.Offset <= doc.Text.GetUpperBound(0))
        {
            e.Graphics.DrawString(doc.Text[doc.Offset], font,
                Brushes.Black, x, y);
            // Move to the next line of data.
            doc.Offset += 1;
            // Move the equivalent of one line down the page.
            y += lineHeight;
        }
        if (doc.Offset <doc.Text.GetUpperBound(0))
        {
            // There is still at least one more page.
            // Signal this event to fire again.
            e.HasMorePages = true;
        }
        else
        {
            // Printing is complete.
            doc.Offset = 0;
        }
    }
}

Hi Tahir,

Sorry for the slow response but I’ve been pretty busy the past weeks and finally got around to testing this out.
Using the code you supplied, all settings are correctly applied and I can print duplex and pick the document orientation without a problem.
Using the following code, with the latest Aspose.Words DLL (14.2.0 OR 13.11.0) I can print duplex, but the orientation setting is ignored.

string[] printText = new string[101];

for (int i = 0; i <101; i++)

{

    printText[i] = i.ToString();

    printText[i] +=
        ": Testing Duplex printing + Landscape";

}
PrintDialog printDlg = new PrintDialog();

if (!printDlg.ShowDialog().Equals(DialogResult.OK))

    return "STOPPED";

Aspose.Words.Document doc = new Aspose.Words.Document();

DocumentBuilder builder = new DocumentBuilder(doc);

foreach(string txt in printText)

builder.Writeln(txt);

doc.Print(printDlg.PrinterSettings);

Testing with this limited code with a clean word document (see attachement), the result is the same (for 14.2.0 and 13.11). Duplex settings are used but orientation is ignored.

PrintDialog printDlg = new PrintDialog();

if (!printDlg.ShowDialog().Equals(DialogResult.OK))

    return "STOPPED";
Aspose.Words.Document doc = new Aspose.Words.Document(".../.../data/print.docx");

doc.Print(printDlg.PrinterSettings);

This makes me wonder why my original implementation would not print Duplex, but it seems that that is not an Aspose issue.
As for the orientation, is this expected behaviour and should I set the orientation on the document itself instead of in the print settings maybe? Or is this actually a problem?

Hi Nick,

Thanks for your feedback. Yes, this issue seems not to be related with Aspose.Words. Please note that Aspose.Words Print method uses System.Drawing and standard .NET printing classes.

A single Word document can consist of multiple sections that specify pages with different sizes, orientation and paper trays. AsposeWordsPrintDocument overrides OnQueryPageSettings to properly select paper size, orientation and paper source when printing a Word document.

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.

In your case, I suggest you please set the page orientation at document level. You may use the following code to set the orientation of the page. Hope this helps you.

Document doc = new Document(MyDir + "in.docx");
foreach(Section section in doc.Sections)
{
    section.PageSetup.Orientation = Aspose.Words.Orientation.Landscape;
}

A post was split to a new topic: Facing issue with my Epson printer