We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

User-defined paper source for printing Words document?

Hi!

I am using Aspose.Words (V 9.4 with .NET 2.0) very successfully for the following scenario:

My users create their own Word documents which are then read in by my application (using Aspose.Words, modified (replacing fields with values) and saved again as Word or PDF file. This works very well.

I now want to add a printing functionality and got stuck.

What I want seems quite simple:

Users define in MS Word the page setup properties including the printer trays for first and other pages. These settings are saved in the output documents. This works. But also I want to print the documents directly from my application and the printing should also use the paper sources defined in the document. And this does not work.

My first approach was to use Document.Print. But this always uses the default printer tray and ignores the settings in the document. If I print the same document after saving from MS Word, everything is ok and custom trays are used.

As a second approach I tried to use AsposeWordsPrintDocument to adjust the paper source in the QueryPageSettings event. This has no effect, though. After chaning in the QueryPageSettingsEventArgs, I monitor what happens in the PrintPage event and the setting is changed back to the default tray and this is the one being used. No chance to receive any printout using any other tray.

Probably I am doing something wrong. So, my question is: What is the standard procedure for printing an Aspose.Words document directly from the application using user-defined paper sources?

Just one more note: I am aware that saving the settings for paper sources within the Word document may cause difficulties if different printers are used. But this is not the cause for my problem. I am doing all this with my default Windows printer and there is no need in my application to print the document using a different printer.

Thank you,

Wolfgang

Hi Wolfgang,

Thanks for your query. There is no direct way in Aspose.Words to set user-defined paper sources. You can do this by using System.Drawing.Printing.PrinterSettings. Please read the following documentation links for your kind reference.
https://docs.microsoft.com/en-us/dotnet/api/system.drawing.printing.pagesettings.papersource
https://docs.microsoft.com/en-us/dotnet/api/system.drawing.printing.papersource

Document doc = new Document();
System.Drawing.Printing.PrinterSettings ps = new System.Drawing.Printing.PrinterSettings();
//your code
doc.Print(ps);

Hi Tahir,

thank you for response!

Two comments:

It seems a bit strange to me that Aspose.Words ignores settings for the paper source made in the Word document when printing. As I mentioned above, these settings are present in the Word file saved by Aspose.Words (so when printing this file from MS Word everything is ok). Just the Print method of Aspose.Words ignores these settings. Sounds like a deficiency of Aspose.Words to me which you should address at some point.

Secondly, your suggestion to use the PrinterSettings was already amongst those things I attempted to solve the issue. Which also was not successful. But after some further experiments I now do know why this did not work:

Apparently, selecting the Paper Source using the PrinterSettings only works, if the Word document does NOT contain any definition for printer trays in the Word page setup. This only works if in the Word document ‘automatic selection’ is enabled (which is the default). In that case, I can manipulate the paper source which Document.Print uses by manipulating the PrinterSettings as you suggest. If, however, any non-default paper source is specified in the Word document, Aspose.Words ignores the setting in the document as well as the setting in PrinterSettings. This really seems odd to me.

I wonder if you can reproduce this behaviour?

But anyway, at least I have some way now to select paper sources by making sure that the Word document does not specify a paper source in the Word page settings and then using the PrinterSettings object.

Although this works, it is still not ideal (for example I cannot specify different paper trays for first and other pages this way). It would be much easier if Aspose.Words just did obey the settings made in the Word document in the Document.Print method.

Best regards,

Wolfgang

Hi Wolfgang,

Thanks for your request. As you may know, 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 above-mentioned printer. So first of all you should make sure that paper trays in your document are specified correctly. You can specify paper trays programmatically, using Aspose.Words:
https://reference.aspose.com/words/net/aspose.words/pagesetup/

Hope this helps.

Best regards,

Hi Alexey,

thank you for your response!

I am aware of the specificity of the tray settings to certain printers. But this is not the reason for my issue. Please refer to the note at the end of my original post. The whole process of creating the document, manipulating it from Aspose.Words and printing it has been done using my default Windows printer.

To clarify this, here again my basic scenario (using “Manual Paper” tray as example, would be any of the installed trays in production environment):

  1. Create template document using MS Word and setting tray for first and other pages in the first section (it only has one) to “Manual Paper”. Printing the document from MS Word uses “Manual Paper”. Ok.

  2. Reading the template into my application using Aspose.Words. Doing some manipulation (replacing of some values).

  3. Printing the document using Document.Print(): Does NOT use “Manual Paper”, but uses the “Automatic” tray. Not Ok.

  4. Saving the Aspose.Words document. Opening this document with MS Word and printing: Uses “Manual Paper” (and also can see the setting in the Word Page Setup). Ok.

So, in conclusion, my issue is that Step #3 ignores the setting in the Word document when printing, but correctly writes this setting into the Word file.

This is not consistent behaviour and it makes it impossible for me to achieve what I want to achieve: Users should be able to specify trays for first and other pages for each section in the template document and my application should then print each page to the appropriate tray.

My second scenario is a workaround: I am trying to find to at least be able to offer some selection option to my users for paper trays. This workaround defines a PrinterSettings object, sets its PaperSource to “Manual Paper” and uses Document.Print(MyPrinterSettings).

Doing this instead of Step #3 above also fails (see my above post)! Whatever I specify in MyPrinterSettings is ignored and printing is to the “Automatic” tray!

But as I also pointed out above, there is a way to make this work! And this is to not specify any tray in the template document. If I define “Standard Tray” in the template document, MyPrinterSettings are used and printing is to “Manual Paper” using Document.Print(MyPrinterSettings) as it should.

This is what I was referring to above as really not consistent behaviour of Aspose.Words.

In order to get some more information on what is going on interally, I have used the AsposeWordsPrintDocument object. Key elements of my code look like this (with MyWorkDocument being the template document after reading in and doing some manipulations):

Dim MyWorkDocument As Aspose.Words.Document

Dim MyPrinterSettings As System.Drawing.Printing.PrinterSettings
Dim MyPaperSource As New System.Drawing.Printing.PaperSource
Dim MyPrintDialog As System.Windows.Forms.PrintDialog = New System.Windows.Forms.PrintDialog

MyPrintDialog.ShowDialog()
MyPrinterSettings = MyPrintDialog.PrinterSettings
MyPaperSource = MyPrinterSettings.PaperSources(5)
MyPrinterSettings.DefaultPageSettings.PaperSource = MyPaperSource

MyPrintDocument As Aspose.Words.Rendering.AsposeWordsPrintDocument
MyPrintDocument = New Aspose.Words.Rendering.AsposeWordsPrintDocument(MyWorkDocument)
MyPrintDocument.PrinterSettings = MyPrinterSettings

AddHandler MyPrintDocument.PrintPage, AddressOf PrintPage
MyPrintDocument.Print()

...

Private Sub PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
Console.WriteLine(e.PageSettings.PaperSource.RawKind)
End Sub

With this code I get the following results:

Paper sources in MyPrinterSettings.PaperSource are:

MyPrinterSettings.PaperSources(0)
{System.Drawing.Printing.PaperSource}
Kind: FormSource {15}
RawKind: FormSource {15}
SourceName: "Automatisch auswählen"

MyPrinterSettings.PaperSources(1)
{System.Drawing.Printing.PaperSource}
Kind: Custom {257}
RawKind: Custom {257}
SourceName: "Auto Select"

MyPrinterSettings.PaperSources(2)
{System.Drawing.Printing.PaperSource}
Kind: Custom {257}
RawKind: 258
SourceName: "Tray 1"

...

MyPrinterSettings.PaperSources(5)
{System.Drawing.Printing.PaperSource}
Kind: Custom {257}
RawKind: 261
SourceName: "Manual Paper"

...

Case 1 (Template document has set the tray to "Manual Paper", i.e. the scenario described in the beginning):

Before executing MyPrintDocument.Print() I have the following:

MyWorkDocument.Sections(0).PageSetup.FirstPageTray
261

MyPrinterSettings.DefaultPageSettings.PaperSource 
{System.Drawing.Printing.PaperSource}
Kind: Custom {257}
RawKind: 261
SourceName: "Manual Paper"

MyPrintDocument.PrinterSettings.DefaultPageSettings.PaperSource 
{System.Drawing.Printing.PaperSource}
Kind: Custom {257}
RawKind: 261
SourceName: "Manual Paper"

After executing MyPrintDocument.Print(), monitoring the MyPrintDocument.PrintPage event yields:

e.PageSettings.PaperSource
{System.Drawing.Printing.PaperSource}
Kind: Custom {257}
RawKind: Custom {257}
SourceName: "Auto Select"

> Print goes to “Automatic tray”. Not Ok.

Case 2 (Template document has set the tray to “Standard tray”):

Before executing MyPrintDocument.Print() I have the following:

MyWorkDocument.Sections(0).PageSetup.FirstPageTray
DefaultBin {0}

MyPrinterSettings.DefaultPageSettings.PaperSource 
{System.Drawing.Printing.PaperSource}
Kind: Custom {257}
RawKind: 261
SourceName: "Manual Paper"

MyPrintDocument.PrinterSettings.DefaultPageSettings.PaperSource 
{System.Drawing.Printing.PaperSource}
Kind: Custom {257}
RawKind: 261
SourceName: "Manual Paper"

After executing MyPrintDocument.Print(), monitoring the MyPrintDocument.PrintPage Event yields:

e.PageSettings.PaperSource
{System.Drawing.Printing.PaperSource}
Kind: Custom {257}
RawKind: 261
SourceName: "Manual Paper"

> Print goes - as it should - to “Manual Paper”. Ok.

I hope this clarifies the situation. My suspicion is that Aspose.Words has a problem if the PageSetup of the Word document specifies a tray (in my example 261) which is not contained in the System.Drawing.Printing.PaperSource Enumeration. If this happens, RawKind is set to 257 irrespective of the settings in the PrinterSettings. These are only taken account of if the Word document specifies a tray contained in the System.Drawing.Printing.PaperSource Enumeration (or even only if it is set to DefaultBin).

So, in essence I am reporting two issues here:

  1. Document.Print ignores page setup in the Word document (at least for trays with RawKind above 257).

  2. Document.Print (MyPrinterSettings) ignores the defined paper source if the page setup in the Word document specifies a non-standard tray (or at least a tray with RawKind above 257).

For Issue #2 there is a workaround by not making any selection of paper sources in my template document. So, I can get this to work (but I still would see this as some kind of bug).

But this workaround still does not give me the full possibilities which MS Words offers in printing (defining different paper sources for different sections and for first and other pages in a section). This would only be possible by Issue #1 being solved.

The only workaround here would be to save the document with Aspose.Words and then open it through Word Automation and print it from there. But this is not really an approach I would like to take because the whole issue of buying a Aspose.Words license was to get rid of Word Automation which I was using before …

Best regards,

Wolfgang

Hi Wolfgang,

Please accept my apology for late response. We will investigate this issue and will share our findings with you soon.

Hi Tahir,

is still anybody taking care of this issue? Any progress so far?

Thank you,

Wolfgang

Hi Wolfgang,

I have verified from our issue tracking system and like to share with you that this issue under analysis phase. You will be updated via this forum thread once this issue is resolved.

Hi Tahir,

do you have any indication as to how much more time it will take to at least find out whether the reported issuess actually are bugs and whether there are any workarounds? My clients are waiting for the implementation of this feature and I have to let them know at least a schedule for doing this!

Thank you,

Wolfgang

Hi Wolfgang,

Sorry for the delay.

I was the one communicating with the developer, however I missed his last reply. We have been working to reproduce your issue however we are unable to.

Could you please let us know what version of .NET you are targeting and what DLL of Aspose.Words you are using? e.g 1.1, 2.0, 3.5 client profile?

Thanks,

Hi Adam,

as I wrote in my initial post, I am using Aspose.Words V 9.4 with .NET 2.0 (SP 2).

Please let me know if there is anything else you need to know!

Best regards,

Wolfgang

Hi Wolfgang,

Thanks for your reply.

That’s good. I also need to know the DLL you are using (not the version, but the build). For example are you using the dll from the Aspose.Words for .NET\bin\net1.1\ folder or the Aspose.Words for .NET\bin\net2.0\ folder etc.

BTW it’s a long shot, but have you tried using the latest version of Aspose.Words (even in evaluation mode is fine). Does the issue still occur?

Thanks,

Hi Adam,

I am using the DLL from the bin/net2.0 folder. So far I have not tried to use latest version.

Best regards,

Wolfgang

Hi Wolfgang,

Thanks for this information.

Hmm well it seems we were unable to reproduce any issue on our side. If you were using the .NET1.1 DLL then that would have been the problem.

I will consult with another developer and get back to you later. Please tell us how things go with the latest version.

Thanks,

Hi Adam,

I have now tested the issue using an evaluation version of your latest version 11.1.0. The issue appears to be fixed in this version, i.e. printing uses the paper tray defined in the Word document.

So, I have fix for the problem now. Just have to contemplate whether fixing this one issue is worth the investment for buying the new version, since everything else is working as it should with the older version …

Best regards,

Wolfgang

Hi Wolfgang,

Thanks for this additional information.

It’s great everything is working with the most recent version of Aspose.Words. I have taken a look through our database and found three possible fixes made between 9.4 and 11.1 which might account for the fix to the issue you were having. I have listed the issues below.

  • WORDSNET-3021 - Check whether PaperTray options works correctly upon
    printing document. (Fixed in 9.5).
  • WORDSNET-2191 - Aspose.Words ignores PaperSize during
    printing. (Fixed in 9.5)
  • WORDSNET-4119 - Problem with printing to non-AutoSelect
    tray. (Fixed in 9.6)

You may wish to try each of the 9.5 and 9.6 versions one by one to see which one provides the fix. This means if the release was made during your subscription period then you won’t need to upgrade at all.

However, if the issue was fixed after your subscription expires then I’m afraid you will need to upgrade in order to use the fix.

Thanks,

Hi Adam,

thanks a lot for the additional information!

I have checked now with Version 10.5 (the last one covered by my license period so far). And it works! So it looks like the fixes in Versions 9.5 and 9.6 which you mentioned solved the issue.

So, problem solved, thank you very much!

Best regards,

Wolfgang

Hi Wolfgang,

It’s great your issue is resolved. Please feel free to ask anytime you need any assistance.

Thanks,

The issues you have found earlier (filed as WORDSNET-5872) have been fixed in this .NET update and this Java update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.