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

Free Support Forum - aspose.com

How open a printdialog to print an Aspose.words.Document?

Hi,
I’d like to open a printdialog when the user click on print. The document which must be print is an Aspose.words.Document.
Thanks for your help
Yohann

Hi,
Aspose.Words is not capable to print documents by itself yet. We are working on a rendering engine which would allow this. However, if your purpose is showing a print dialog, consider the System.Windows.Forms.PrintDialog class.

Like that ?
--------------------------------------------------------------------------------------
PrintDialog dlg = new PrintDialog();
Aspose.Words.Viewer.DocumentRenderer dr = new Aspose.Words.Viewer.DocumentRenderer();
dlg.Document = printDoc;
dr.Document = doc;
dlg.AllowSomePages = true;
dlg.PrinterSettings.MinimumPage = 1;
dlg.PrinterSettings.MaximumPage = 1;
dlg.PrinterSettings.FromPage = dlg.PrinterSettings.MinimumPage;
dlg.PrinterSettings.ToPage = dlg.PrinterSettings.MaximumPage;
if (dlg.ShowDialog().Equals(DialogResult.OK))
{
dr.Print();
}

--------------------------------------------------------------------------------------

It doesn’t work and I don’t know how to do. Could you help me to do it please?

Generally your code looks okay, what exactly does not work? Also note that DocumentRenderer is actually a part of Aspose.Words.Viewer beta and we only provide very limited technical support on it.

My code doesn’t work very well…
The print dialog is open and the user choose the options for printing but when he clicks on “print”, the options are not applied. The document to print is an Aspose.Words.Document.
My variable “dr” is a DocumentRenderer but the options of printing are for my other variable “dlg” which is a PrintDialog. My problem here is that I’d like to print my document by asking to the user choosing options for printing.
How could I do this?
Thanks for your help

I think you should just copy printer settings from PrintDialog to DocumentRenderer:

dr.PrinterSettings.MinimumPage = dlg.PrinterSettings.MinimumPage;
dr.PrinterSettings.MaximumPage = dlg.PrinterSettings.MaximumPage;
dr.PrinterSettings.FromPage = dlg.PrinterSettings.FromPage;
dr.PrinterSettings.ToPage = dlg.PrinterSettings.ToPage;

I would like to note again that the current renderer is beta and you should not expect stable work from it.

Yes thanks and how I could do to know if the user want it in color or not?

I can’t see such a property among PrintDialog properties. PrinterSettings contains the SupportsColor property but it is read-only and specifies printer capability, not the user’s choice. So probably there is no way to specify that in .NET.

My new code is :
----------------------------------------------------------------------------------------------------
private void buttonPrint_Click(object sender, EventArgs e)
{
PrintDialog dlg = new PrintDialog();
Aspose.Words.Viewer.DocumentRenderer dr = new Aspose.Words.Viewer.DocumentRenderer();
dr.Document = doc.Document;
dlg.Document = dr.PrintDocument;
dlg.PrinterSettings.MaximumPage = 1 ;
if (dlg.ShowDialog().Equals(DialogResult.OK))
{
dr.Print();
}
Console.Out.WriteLine(“Impression…”);
}

----------------------------------------------------------------------------------------------------
I looked too the code of the demo of Aspose.Editors and i found this :
----------------------------------------------------------------------------------------------------
private void FilePrint(object sender, System.EventArgs e)
{
// We use the standard .NET print document infrastructure.
PrintDialog dlg = new PrintDialog();
MyPrintDocument printDoc = new MyPrintDocument(docEditor.Document, docEditor.Options.IsShowFormattingMarks);
dlg.Document = printDoc;
dlg.AllowSomePages = true;
dlg.PrinterSettings.MinimumPage = 1;
dlg.PrinterSettings.MaximumPage = printDoc.PageCount;
dlg.PrinterSettings.FromPage = dlg.PrinterSettings.MinimumPage;
dlg.PrinterSettings.ToPage = dlg.PrinterSettings.MaximumPage;
if (dlg.ShowDialog().Equals(DialogResult.OK))
{
printDoc.Print();
}
}

----------------------------------------------------------------------------------------------------
My code is similar than this one and mine doesn’t work when I choose my options for printing whereas yours works very well.
Can you tell me why?
Thank you for your fast answers

Well notice your code is still different It omits some lines such as
dlg.PrinterSettings.FromPage = dlg.PrinterSettings.MinimumPage;
dlg.PrinterSettings.ToPage = dlg.PrinterSettings.MaximumPage;

Another cause might be the beta stage of the renderer (again). It may not obey all printing settings specified.

And the beta will be finish which day (or month) ?

We were planning to release it this month but due to many reasons we have had to postpone the release to an undetermined date somewhere in 2007.

Okay thanks for your good job
And could you tell me how is working the print of the demo of Aspose.Editor?
Because I don’t understand how the options are applied on the printDoc.
--------------------------------------------------------------------------------------
**// We use the standard .NET print document infrastructure.
PrintDialog dlg = new PrintDialog();

MyPrintDocument printDoc = new MyPrintDocument(docEditor.Document, docEditor.Options.IsShowFormattingMarks);
dlg.Document = printDoc;
dlg.AllowSomePages = true;
dlg.PrinterSettings.MinimumPage = 1;
dlg.PrinterSettings.MaximumPage = printDoc.PageCount;
dlg.PrinterSettings.FromPage = dlg.PrinterSettings.MinimumPage;
dlg.PrinterSettings.ToPage = dlg.PrinterSettings.MaximumPage;
if (dlg.ShowDialog().Equals(DialogResult.OK))
{
printDoc.Print();
}**

--------------------------------------------------------------------------------------
I see that your “MyPrintDocument” inherit the classe “PrintDocument” whereas in Aspose.Words, the “DocumentRenderer” has the class “PrintDocument” just in get :
--------------------------------------------------------------------------------------------------------
// Résumé :
// Renders or prints a Microsoft Word document.
//
// Notes :
// DocumentRenderer is the main class to render or print a document. It accepts
// a Document object to render. Use the GetPage and GetPages methods to get
// the rendered pages as bitmaps. Use the Print method to print current document.
public class DocumentRenderer
{
// Résumé :
// Constructs a new instance of the DocumentRenderer.
public DocumentRenderer();


// Résumé :
// Gets or sets print document.
public PrintDocument PrintDocument { get; }


}

-------------------------------------------------------------------------------------------------------
Is my problem come from here? :s

Probably yes. That is why you have to pass PrinterSettings properties from the dialog to the object possessed by DocumentRenderer as shown in this post. And if some properties do not work then - alas, they are just not supported by the beta

Thanks for you help DmitryV, I solve my problem
I insert just 2 lines :
----------------------------------------------------------------------------------------
PrintDialog dlg = new PrintDialog();
Aspose.Words.Viewer.DocumentRenderer dr = new Aspose.Words.Viewer.DocumentRenderer(doc);
dlg.AllowSomePages = true;
dlg.PrinterSettings.MinimumPage = 1;
dlg.PrinterSettings.MaximumPage = 1;
dlg.PrinterSettings.FromPage = dlg.PrinterSettings.MinimumPage;
dlg.PrinterSettings.ToPage = dlg.PrinterSettings.MaximumPage;
if (dlg.ShowDialog().Equals(DialogResult.OK))
{
dr.PrinterSettings.SetHdevmode(dlg.PrinterSettings.GetHdevmode());
dr.PrinterSettings.SetHdevnames(dlg.PrinterSettings.GetHdevnames());

dr.Print();
}
----------------------------------------------------------------------------------------

And it works !!!

Yohann

We are happy to tell you that the new Rendering Engine has replaced the “old Viewer Beta”. The Rendering Engine can print, save as images or draw onto .NET Graphics object any document page.

Please see Aspose.Words documentation to learn more about new features.

Saving to image

In additional, new Rendering engine allows you to convert Word document to PDF directly (without using Aspose.Pdf).

Saving to Pdf

The latest version of Aspose.Words is available for download from downloads section or from NuGet