Papersource Section not working when printing

Hallo,

we want to use Aspose.Word for printing files. We have to print differnt pages of the document to diffrent papertrays containing different kinds of paper.

The problem is that the papersource selection is not working at all. Everything is always printed to the first/default papertray.

This is an exanmple from our code:
wordDoc.Sections[0].PageSetup.FirstPageTray =260;
wordDoc.Sections[0].PageSetup.OtherPagesTray = 260;
System.Drawing.Printing.PrinterSettings objPrinterSettings = new System.Drawing.Printing.PrinterSettings();
objPrinterSettings.PrinterName = ConfigurationManager.AppSettings[“Drucker”];
objPrinterSettings.DefaultPageSettings.PaperSource= objPrinterSettings.PaperSources[2];
wordDoc.Save(strDateiNameWordDatei);
wordDoc.Print(objPrinterSettings,strDrucker);

Either the gloabl papersource settings when calling the print-function as well as the papersource settings for the sections in the wordfile are ignored, when aspose prints the document.

Our example code saves the word-dokument. If I open the saved file with MS-Word and print it, the doument is printed to the correct papertray.

This image shows our papertrays for our printer:
image.png (31.3 KB)

We use a Brother HL4150CDN printer for our tests. The oserve the same behavior with the Minolta Printer of our customer.

And ideas how we can solve out this realy frustrating issue?

Sven

@tgjas,

Have you tried the latest version of Aspose.Words for .NET i.e. 18.12 on your end?

In case the problem still remains, please ZIP and upload your sample input Word document you are getting this problem with here for testing.

Please also run the following code on your end and share the output here for our reference.

PrinterSettings settings = new PrinterSettings();
settings.PrinterName = "name of your multi-tray printer";

foreach (PaperSource source in settings.PaperSources)
    Console.WriteLine(source.RawKind);

Please also create a standalone simple console application (source code without compilation errors) that helps us to reproduce your current problem on our end and attach it here for testing

We will then investigate the issue on our end and provide you more information.

I have investigated a bit further. Aspose seams to ignore the papersource-settings when printing. We now have experienced the same behavior in Apose.Words and Apose.Pdf! We need a solution for Word and Pdf.
I made the tests with the newest version of Apose.Word and Apose.Pdf (18.12.0.0). I am using .NET 4.7 (newest version), Visual Studio 2012 and a Brother HL-4150CDN series Printer. We are developing console-applications

This is the enumeration the papersources of out printer:
0: Papersouce ‘Automatisch auswählen’, RawKind 15
1: Papersouce ‘Automatische Schachtaus’, RawKind 261
2: Papersouce ‘Fach 1’, RawKind 260
3: Papersouce ‘Fach 2’, RawKind 259
4: Papersouce ‘MP-Fach’, RawKind 258
5: Papersouce ‘Manuell’, RawKind 257

If have written two small test programs called “wordtest” and “pdftest” to test the printing to different papersources for Apose.Words and Apose.Pdf. You will find the full C#-projects in the attached ZIP-file. They take 3 arguments (filename, printername, papersource (0-5 for my printer). In both cases the papersource is ignored. Files are always printed on paper from the default source.

This is the test program for Apose.Word:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aspose.Words ;

namespace wordtest
{
class Program
{
static void Main(string[] args)
{
if (args.Count() != 3)
{
Console.WriteLine("Usage: wordtest <wordfile_to_print> ");
}
else
{
Aspose.Words.Document myDoc = new Aspose.Words.Document(args[0]);
System.Drawing.Printing.PrinterSettings objPrinterSettings = new System.Drawing.Printing.PrinterSettings();
objPrinterSettings.PrinterName = args[1];
objPrinterSettings.DefaultPageSettings.PaperSource = objPrinterSettings.PaperSources[System.Convert.ToInt32(args[2])];
myDoc.Print(objPrinterSettings);

        }
    }
}

}

This is the test program for Apose.Pdf:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aspose.Pdf;

namespace pdftest
{
class Program
{
static void Main(string[] args)
{
if (args.Count() != 3)
{
Console.WriteLine("Usage: pdftest <pdffile_to_print> ");
}
else
{
Aspose.Pdf.Document myDoc = new Aspose.Pdf.Document(args[0]);
System.Drawing.Printing.PrinterSettings objPrinterSettings = new System.Drawing.Printing.PrinterSettings();
objPrinterSettings.PrinterName = args[1];
objPrinterSettings.DefaultPageSettings.PaperSource = objPrinterSettings.PaperSources[System.Convert.ToInt32(args[2])];

            Aspose.Pdf.Facades.PdfViewer viewer = new Aspose.Pdf.Facades.PdfViewer();
            viewer.BindPdf(myDoc );
            viewer.PrintDocumentWithSettings(objPrinterSettings);
        }
    }
}

}

To proof that it is not a general problem of my printer,.NET or windows, I have also tried to print something with the plain printing-functions from .NET to paper from different papersources. This is working fine!!! In the following example the text is first printed on paper from the “2: Papersouce ‘Fach 1’, RawKind 260” then the text is printed to paper from then “5: “Papersouce ‘Manuell’, RawKind 257”

printFont = new Font("Arial", 10);
PrintDocument pd = new PrintDocument();
System.Drawing.Printing.PrinterSettings objPrinterSettings = new System.Drawing.Printing.PrinterSettings();
objPrinterSettings.PrinterName = "Brother HL-4150CDN series Printer";
objPrinterSettings.DefaultPageSettings.PaperSource= objPrinterSettings.PaperSources[2];
pd.PrinterSettings = objPrinterSettings;
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
pd.Print ();
objPrinterSettings.DefaultPageSettings.PaperSource = objPrinterSettings.PaperSources[5];
pd.PrinterSettings = objPrinterSettings;
pd.Print();

Thank you for ready. Hoping for a solution – our customer is waiting…
Sven

@tgjas,

Thanks for the details. But, I am afraid, we do not see any attachments (sample documents + console app) in this thread.

If your ZIP file size is big then you can upload the ZIP file to Dropbox and share the Download link here for testing.

The ZIP-file is big. So I uploaded it as public to my google-drive

https://drive.google.com/file/d/109bD3_uUoU5n9QwtmIdv-AEs0gNbramV/view?usp=sharing

Sven

@tgjas,

Please also provide the Word and PDF documents (you are getting this problem with during printing) here for testing. Thanks for your cooperation.

The problem occurs with every PDF- or Word-document. But I provide to example-dcouments as attachment,

Svena_anschreiben.pdf (12.8 KB)
documents.zip (77.3 KB)

@tgjas

Thank you for sharing requested data.

We have logged an investigation ticket with ID PDFNET-45820 to further investigate it with Aspose.PDF perspective. We will let you know as soon as some significant updates will be available.

About Aspose.Words for .NET API, we are checking the scenario and will share our findings with you soon.

@tgjas,

Regarding the issue you are facing with Aspose.Words for .NET API, we have logged your problem in our issue tracking system. Your ticket number is WORDSNET-17912. We will further look into the details of this problem and will keep you updated on the status of the linked issue.

@tgjas,

It looks like WORDSNET-17912 is the same problem as reported by another customer (i.e. WORDSNET-17348).

Regarding WORDSNET-17348, the problem occurs because PageTray is set to the document but its layout is already cached (during PageCount calculating) and will be used for printing. To get correct result we need to invalidate/rebuild layout (call UpdatePageLayout)

According to UpdatePageLayout description. – “if you modify the document after rendering and then attempt to render it again - Aspose.Words will not update the page layout automatically. In this case you should call UpdatePageLayout before rendering (printing) again.”

So, we do not think that we are going to fix this as this seems to be an expected behavior.

Please try using the following workaround:

PrinterSettings printerSettings = new PrinterSettings();
Document doc = new Document("TestDoc.docx");
 
if (doc.PageCount < 2) { } // highlighted line
 
foreach (Aspose.Words.Section section in doc.Sections)
{
section.PageSetup.OtherPagesTray = section.PageSetup.FirstPageTray;
}
 
// Just call UpdatePageLayout to fix the problem.
doc.UpdatePageLayout();
 
 
doc.Print(printerSettings);
Document newDoc = doc.Clone();
newDoc.Print(printerSettings);

Just call UpdatePageLayout after setting new PagesTray to fix the problem. Hope, this helps.

I am having the same problem with Aspose PDF. If i use separate print jobs for the cover sheet and the document itself, it works fine, but I didn’t want to risk getting the cover sheet put on the wrong document so i put them together and tried using the following code.

            PrintingJobSettings printJob = new PrintingJobSettings();
        printJob.FromPage = 1;
        printJob.ToPage = documentToPrint.Pages.Count;
        printingJobs.Add(printJob);

        {
            for (var printingJobIndex = 0; printingJobIndex < printingJobs.Count; printingJobIndex++)
            {
                PageSettings pgs = new PageSettings();
                PrinterSettings ps = new PrinterSettings();

                ps.PrintRange = PrintRange.SomePages;
                ps.FromPage = printingJobs[printingJobIndex].FromPage;
                ps.ToPage = printingJobs[printingJobIndex].ToPage;
                ps.PrinterName = _printerName;
                //ps.PrinterName = "Microsoft Print to PDF";
                System.Console.WriteLine(ps.FromPage);
                System.Console.WriteLine(ps.ToPage);
                System.Console.WriteLine(printingJobIndex);

                prtDoc.PrinterSettings.PrinterName = ps.PrinterName;
                //TODO: Put this back in for tray source if needed: pgs.PaperSource = prtDoc.PrinterSettings.PaperSources[4];
                
                using (var theViewer = new Aspose.Pdf.Facades.PdfViewer())
                {
                    // Print document using printer and page settings                    
                    theViewer.BindPdf(documentToPrint);
                    theViewer.AutoResize = true;
                    theViewer.AutoRotate = true;
                    theViewer.PrintPageDialog = false;
                    theViewer.PdfQueryPageSettings += PdfvOnPdfQueryPageSettings;
                    theViewer.PrintDocumentWithSettings(pgs, ps);
                    if (theViewer.PrintStatus != null)

                    {
                        Exception ex = (Exception)theViewer.PrintStatus;
                        success = false;
                        WriteLog(string.Format("After PrintDocumentWithSettings(): (PrintQueue) Aspose PDF print document encountered exception: {0} : inner exception: {1},", ex.Message, ex.InnerException), ex);

                    }
                    else
                    {
                        success = true;
                    }
                    theViewer.Close();
                }
            }
    private static void PdfvOnPdfQueryPageSettings(object sender, QueryPageSettingsEventArgs queryPageSettingsEventArgs, PdfPrintPageInfo currentPageInfo)
    {
        bool isCoverPage = currentPageInfo.PageNumber == 1;

        System.Drawing.Printing.PrinterSettings.PaperSourceCollection paperSources = queryPageSettingsEventArgs.PageSettings.PrinterSettings.PaperSources;

        if (isCoverPage)
            queryPageSettingsEventArgs.PageSettings.PaperSource = paperSources[3];
        else
            queryPageSettingsEventArgs.PageSettings.PaperSource = paperSources[0];

    }

@jwquinn

Thank you for verifying your problem to be the same as PDFNET-45820. We have logged your concerns and have replied you in respective thread*. Kindly follow up there for future references.


* https://forum.aspose.com/t/evaluating-your-software-for-printing-from-backend-console/192524/23