I’ve found posts that say that printing is not supported under .NET Standard (.net5 too?).
https://stackoverflow.com/questions/63482761/aspose-word-document-dont-want-to-use-the-print-method
Can I upgrade to .net6 to get printing?
Where are the Aspose Docs that say printing is supported and not supported?
@sludwig.freepoint Unfortunately, printing is not supported in .NET6 too. I have linked your request to the appropriate issue WORDSNET-17369.
As a workaround you can use System.Drawing.Common
for printing documents in .NET Standard and .NET6. The following simple code can be used to print document by using the System.Drawing.Common
. For printing, the document is first converted to image and then printed. Please note that in such approach there might be quality degradation because of conversion to image, also this solution is acceptable only for Windows.
DocumentPrinter printer = new DocumentPrinter();
Document doc = new Document(@"C:\Temp\TestRendering.doc");
printer.Print(doc);
public class DocumentPrinter
{
/// <summary>
/// Prints the specified document using default printer.
/// </summary>
public void Print(Document doc)
{
mDoc = doc;
mPageIndex = 0;
PrintDocument pd = new PrintDocument();
pd.PrintPage += PrintPage;
pd.Print();
}
private void PrintPage(object o, PrintPageEventArgs e)
{
try
{
using (MemoryStream pageImageStream = new MemoryStream())
{
ImageSaveOptions options = new ImageSaveOptions(SaveFormat.Png);
options.PageSet = new PageSet(mPageIndex);
mDoc.Save(pageImageStream, options);
pageImageStream.Position = 0;
using (Image pageImage = Image.FromStream(pageImageStream))
{
e.Graphics.DrawImage(pageImage, new Point(0, 0));
}
// Increase page index and check if there are more pages.
mPageIndex++;
e.HasMorePages = (mPageIndex < mDoc.PageCount);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
// Stop printing in case of errors.
e.HasMorePages = false;
}
}
private int mPageIndex;
private Document mDoc;
}