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

Free Support Forum - aspose.com

Barcode image inserted into Aspose word is not printing

Hello Team,

I have created a barcode using BarcodeGenerator and saved it as png image, then inserted the image to the aspose word document using builder.InsertImage. The document when saved to the computer is showing the barcode but when printing the document, the barcode is not printing on the paper.

Below is the code, I am using:out.docx (30.9 KB)

BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.Code39Standard, $"*{reportParameters?.BarTextLine1}*");
generator.Parameters.Barcode.XDimension.Pixels = 1;

DocumentBuilder builder = new DocumentBuilder(doc);
generator.Save(ms, BarCodeImageFormat.Png);
builder.InsertImage(ms,
    RelativeHorizontalPosition.Margin,
    150,
    RelativeVerticalPosition.Margin,
    650,
    200,
    100,
    WrapType.Square);
doc.Save(ms, _saveOptions.Invoke(reportParameters.Format));
Printing(ms, doc, ps.PrinterName);
public void Printing(MemoryStream ms, Aspose.Words.Document doc, String PrinterName)
{
    try
    {
        streamToPrint = new StreamReader(ms);

        try
        {
            mDoc = doc;
            mPageIndex = 0;
            PrintDocument pd = new PrintDocument();
            pd.PrinterSettings.PrinterName = PrinterName;
            pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);

            // Print the document.
            pd.Print();
        }
        finally
        {
            streamToPrint.Close();
        }
    }
    catch (Exception ex)
    {
        _logger.LogError("Error PrintDocumentWithSettings", ex.Message);
    }
}

private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
    try
    {
        using (MemoryStream pageImageStream = new MemoryStream())
        {
            ImageSaveOptions options = new ImageSaveOptions(Aspose.Words.SaveFormat.Emf);
            options.PageSet = new PageSet(mPageIndex);
            // Change the image's brightness and contrast.
            // Both are on a 0-1 scale and are at 0.5 by default.
            options.ImageBrightness = 0.3f;
            options.ImageContrast = 0.7f;

            // Change the horizontal resolution.
            // The default value for these properties is 96.0, for a resolution of 96dpi.
            options.HorizontalResolution = 72f;

            mDoc.Save(pageImageStream, options);
            //mDoc.Save(@"C:\temp\testing.docx");
            pageImageStream.Position = 0;
            using (System.Drawing.Image pageImage = System.Drawing.Image.FromStream(pageImageStream))
            {
                ev.Graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                ev.Graphics.DrawImage(pageImage, new System.Drawing.Point(0, 0));
            }

            // Increase page index and check if there are more pages.
            mPageIndex++;
            ev.HasMorePages = (mPageIndex < mDoc.PageCount);
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
        // Stop printing in case of errors.
        ev.HasMorePages = false;
    }
}

@manikantakondepati Most likely you are using .NET Standard version of Aspose.Words and your project is running on Linux or in Linux Docker container. Aspose.Words for .NET Standard uses SkiaSharp to deal with graphics, to make it work on Linux you have to add reference either to SkiaSharp.NativeAssets.Linux or to SkiaSharp.NativeAssets.Linux.NoDependencies

If you add reference to SkiaSharp.NativeAssets.Linux, you should also install libfontconfig1 in your system. SkiaSharp.NativeAssets.Linux depends on this library. You can use the following command to install it:

apt-get update && apt-get install -y libfontconfig1

If you do not have rights to install packages, or other reasons not to install libfontconfig1, you can simply use SkiaSharp.NativeAssets.Linux.NoDependencies, which does not require installation of libfontconfig1.

Please let me know if the problem is still there. Also for testing purposes you can try saving your document as EMF to file and check whether barcode image is there.

@alexey.noskov
Our solution is a .net core framework project running on Windows server. We are using Aspose.barcode (21.11.0), Aspose.Words(21.12.0), Aspose.PDF(21.11.0) nuget packages. When I am generating a aspose PDF and insert barcode as image and print it using PrintDocumentWithSettings then the barcode is printed on the document. If I use aspose word and insert barcode as image and use windows native libraries to print the document, then print out is not having Image.

@manikantakondepati Thank you for additional information. I have just notices that in your code you changed horizontal resolution but do not change vertical resolution in image save options. Could you please try setting the same vertical and horizontal resolution and let me know if this fixes the problem:

options.HorizontalResolution = 72f;
options.VerticalResolution = 72f;

If VerticalResolution and HorizontalResolution have different valuea the image is not proportional and barcode image might go outside the page boundaries upon printing.

@alexey.noskov
I tried your suggestion but no luck. You could be able to reproduce the issue on your end by creating a word document and then inserting an barcode image. Then use the Printing method I shared above. Please let me know if you have any questions.

@manikantakondepati Thank you for additional information. Unfortunately, I was not able to reproduce the problem on my side. I have tried printing the document you have attached and document generated from scratch using your code and in both case the document is printed properly.
In your code you are rendering each page of the document to Emf image, Could you please check whether the Emf images produced by Aspose.Words have the barcode image?

@alexey.noskov

ImageSaveOptions options = new ImageSaveOptions(Aspose.Words.SaveFormat.Emf);
options.PageSet = new PageSet(mPageIndex);
// Change the image's brightness and contrast.
// Both are on a 0-1 scale and are at 0.5 by default.
options.ImageBrightness = 0.3f;
options.ImageContrast = 0.7f;

// Change the horizontal resolution.
// The default value for these properties is 96.0, for a resolution of 96dpi.
options.HorizontalResolution = 72f;
options.VerticalResolution = 72f;

mDoc.Save(pageImageStream, options);
mDoc.Save(@"C:\temp\testing.docx");

The document testing.docx got saved and it has barcode imagetesting.docx (30.9 KB)

@manikantakondepati Thank you for additional information. But you have attached DOCX document. Could you please attach EMF file generated by your code on your side. Use the following code:

ImageSaveOptions options = new ImageSaveOptions(Aspose.Words.SaveFormat.Emf);
options.PageSet = new PageSet(mPageIndex);
// Change the image's brightness and contrast.
// Both are on a 0-1 scale and are at 0.5 by default.
options.ImageBrightness = 0.3f;
options.ImageContrast = 0.7f;

// Change the horizontal resolution.
// The default value for these properties is 96.0, for a resolution of 96dpi.
options.HorizontalResolution = 72f;
options.VerticalResolution = 72f;

mDoc.Save(@"C:\Temp\page.emf", options);

@alexey.noskov
Thanks for the info, the emf file generated is not having the barcode. Unfortunately, I couldn’t upload the EMF file as the portal is restricting it.
What do you suggest to resolve the issue.
Thank you…

@manikantakondepati Could you please run the following code in your environment and post the string returned by it:

string osVersion = System.Environment.OSVersion.VersionString;

@alexey.noskov
“Microsoft Windows NT 10.0.18363.0”

@manikantakondepati Thank you for additional information. Please make sure libSkiaSharp.dll is published with your application.
If it is published, try calling doc.UpdatePageLayout() method before calling Printing method. The problem might occur if document layout was cached before inserting the image.

@alexey.noskov
Calling doc.UpdatePageLayout() method fixed the issue, thank you…

@manikantakondepati It is perfect that the issue is already resolved. Please feel free to ask in case of any further issues. We will be glad to assist.

A post was split to a new topic: Move the current line to next page on condition. LINQ Reporting Engine