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

Free Support Forum - aspose.com

C# Code to Print Word Document over .NET 5.0 or .NET Core Frameworks

Hi.

I’m evaluating Aspose.words for a project, but I’ve just struck a rock. I find that the Document.Print() methods are missing from the API, though I can see that they exist in the .NET framework version of the API.

Is it not possible to print documents in .NET 5/Core?

Many thanks

@LukeWebber,

I am afraid, as mentioned in the following page, the Document.Print Method is not available in .NET Core.

Your forum thread has been linked to the appropriate issue (WORDSNET-17369) and you will be notified here as soon as printing in .NET Core environment may be supported in future. Sorry for the inconvenience.

However, the following simple code can be used to print document by using the System.Drawing.Common. For printing, the document is first converted to images and then printed. Please note that in such approach there might be quality degradation because of conversion to images.

DocumentPrinter printer = new DocumentPrinter();
Document doc = new Document(@"C:\Temp\input.docx");
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.PrinterSettings.PrinterName = "Microsoft XPS Document Writer";
        // pd.PrinterSettings.PrintToFile = true;
        // pd.PrinterSettings.PrintFileName = "C:\\Temp\\.net core printing.xps";
        pd.Print();
    }

    private void PrintPage(object o, PrintPageEventArgs e)
    {
        try
        {
            using (MemoryStream pageImageStream = new MemoryStream())
            {
                ImageSaveOptions options = new ImageSaveOptions(SaveFormat.Png);

                PageRange pageRange = new PageRange(mPageIndex, mPageIndex); // Specify Range
                options.PageSet = new PageSet(pageRange);

                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;
}

Thanks for that, Hafeez.

I’ve implemented that, with one slight tweak…

ImageSaveOptions options = new ImageSaveOptions(SaveFormat.Png)
{
    UseAntiAliasing = true,
    UseHighQualityRendering = true,
    Resolution = 300 //TODO: Include in configuration
};

… because the print quality was otherwise awful - 96dpi by default.

However, I’m left with one further problem. The vertical margins on the printed documents are not the same as in the Word document and, as a result, I’m getting two pages rather than one from my test document, and wasted space in the header section at the top. Any help on how I can make the output conform to the way that Word prints the document?

Never mind. I’ve got it, I think. I need to set the PrinterSettings.DefaultPageSettings to match the document’s settings. There are a few bones in this, but I think I’m getting there. I will say, though, that I wish you’d implemented the Print method in .NET before this. It’s coming of for five years since .NET Core version 1, after all.

@LukeWebber,

We will inform you here as soon as WORDSNET-17369 will get resolved in future.

Have you tried the latest (21.6) version of Aspose.Words for .NET on your end? In case the problem still remains, then can you please ZIP and upload a sample Word document you are getting this problem with here for testing? We will then investigate the issue on our end and provide you more information.

Thanks Awais, but I believe I’ve got this sorted out now. The document was trying to print to Letter stationery, and I needed to set every section to A4.

@LukeWebber,

It is great that you were able to resolve the problem on your end. In case you have further inquiries or may need any help in future, please let us know by posting a new thread in Aspose.Words’ forum.