Error! Bar code generator is not set

Hello everybody,

When I try to print or convert a docm document to a PDF, all barecode aren’t printed.
I receive a text message instead of barcode. See examples.zip for more details

Thank you

examples.zip (182.8 KB)

@Paga16 Aspose.Words does not render DISPLAYBARCODE fields itself. To render such fields you should specify a custom barcode generator. For example see our documentation:
https://reference.aspose.com/words/net/aspose.words.fields/fieldoptions/barcodegenerator/

Hi @alexey.noskov

Thank you for your reply, I’m not sur to understand… I’ dont want to create a new barcode but just render the barcode in the document.

In your example you use CustomBarcodeGenrator() I find nothing about the class. Could you give more details please ?

Thanks you.

@Paga16 You can find an example of IBarcodeGenerator implementation here:
https://docs.aspose.com/words/net/how-to-generate-a-custom-barcode-image-for-displaybarcode-field/

The barcode in your document is not an image, it is represented by DISPLAYBARCODE field, to display such barcode it is required to render it. Aspose.Words itself does not render the DISPLAYBARCODE field. That is why a third-party tool is required for this task. In the above example Aspose.BarCode is used as such tool.

@alexey.noskov

Thank I understant better ! I have Aspose.Barcode, i will investigate the code.
Just by chance do you have the same exemple in vb, please ?

Regards.

@Paga16

Unfortunately, there are no VB examples. But there should not be difficulties in translation. In case of any issues, please let us know, we will be glad to help.

Hello Alexey,

I translated C# to VB (you can publish this code). However I have an error : Aspose.BarCode.BarCodeException : 'Can’t render barcode: not enough space’

When I try to convert the document in PDF. Could you assist me again ?

thank you.

    Public Class CustomBarcodeGenerator
        Implements IBarcodeGenerator

        Private Shared Function ConvertSymbolHeight(ByVal heightInTwipsString As String) As Single
            Dim heightInTwips As Integer = Integer.MinValue
            Integer.TryParse(heightInTwipsString, heightInTwips)
            If heightInTwips = Integer.MinValue Then Throw New Exception("Error! Incorrect height - " & heightInTwipsString & ".")
            Return CSng((heightInTwips * 25.4 / 1440))
        End Function

        Private Shared Function ConvertColor(ByVal inputColor As String) As System.Drawing.Color
            Dim color As Integer = Integer.MinValue
            Integer.TryParse(inputColor.Replace("0x", ""), color)
            If color = Integer.MinValue Then Throw New Exception("Error! Incorrect color - " & inputColor & ".")
            Return System.Drawing.Color.FromArgb((color + 16), ((color And 65280) + 8), (color And 255))

        End Function

        Private Shared Function ConvertScalingFactor(ByVal scalingFactor As String) As Single
            Dim isParsed As Boolean = False
            Dim percents As Integer = Integer.MinValue
            Integer.TryParse(scalingFactor, percents)

            If percents <> Integer.MinValue Then
                If percents >= 10 AndAlso percents <= 10000 Then isParsed = True
            End If

            If Not isParsed Then Throw New Exception("Error! Incorrect scaling factor - " & scalingFactor & ".")
            Return percents / 100.0F
        End Function

        Private Function IBarcodeGenerator_GetBarcodeImage(parameters As Aspose.Words.Fields.BarcodeParameters) As Image Implements IBarcodeGenerator.GetBarcodeImage
            If parameters.BarcodeType Is Nothing OrElse parameters.BarcodeValue Is Nothing Then Return Nothing
            Dim type As String = parameters.BarcodeType.ToUpper()
            Dim encodeType = Aspose.BarCode.Generation.EncodeTypes.None

            Select Case type
                Case "QR"
                    encodeType = Aspose.BarCode.Generation.EncodeTypes.QR
                Case "CODE128"
                    encodeType = Aspose.BarCode.Generation.EncodeTypes.Code128
                Case "CODE39"
                    encodeType = Aspose.BarCode.Generation.EncodeTypes.Code39Standard
                Case "EAN8"
                    encodeType = Aspose.BarCode.Generation.EncodeTypes.EAN8
                Case "EAN13"
                    encodeType = Aspose.BarCode.Generation.EncodeTypes.EAN13
                Case "UPCA"
                    encodeType = Aspose.BarCode.Generation.EncodeTypes.UPCA
                Case "UPCE"
                    encodeType = Aspose.BarCode.Generation.EncodeTypes.UPCE
                Case "ITF14"
                    encodeType = Aspose.BarCode.Generation.EncodeTypes.ITF14
                Case "CASE"
                    encodeType = Aspose.BarCode.Generation.EncodeTypes.None
            End Select

            If encodeType.Equals(Aspose.BarCode.Generation.EncodeTypes.None) Then Return Nothing

            Dim generator As New Aspose.BarCode.Generation.BarcodeGenerator(encodeType)
            generator.CodeText = parameters.BarcodeValue
            If encodeType.Equals(Aspose.BarCode.Generation.EncodeTypes.QR) Then generator.Parameters.Barcode.CodeTextParameters.TwoDDisplayText = parameters.BarcodeValue
            If parameters.ForegroundColor IsNot Nothing Then generator.Parameters.Barcode.BarColor = ConvertColor(parameters.ForegroundColor)
            If parameters.BackgroundColor IsNot Nothing Then generator.Parameters.BackColor = ConvertColor(parameters.BackgroundColor)

            If parameters.SymbolHeight IsNot Nothing Then
                generator.Parameters.ImageHeight.Millimeters = ConvertSymbolHeight(parameters.SymbolHeight)
                generator.Parameters.AutoSizeMode = AutoSizeMode.Nearest
            End If

            generator.Parameters.Barcode.CodeTextParameters.Location = CodeLocation.None
            If parameters.DisplayText Then generator.Parameters.Barcode.CodeTextParameters.Location = CodeLocation.Below
            generator.Parameters.CaptionAbove.Text = ""
            Const scale As Single = 1.0F
            Dim xdim As Single = 1.0F

            If encodeType.Equals(EncodeTypes.QR) Then
                generator.Parameters.AutoSizeMode = AutoSizeMode.Nearest
                generator.Parameters.ImageWidth.Millimeters *= scale
                generator.Parameters.ImageHeight.Millimeters = generator.Parameters.ImageWidth.Millimeters
                xdim = generator.Parameters.ImageHeight.Millimeters / 25
                generator.Parameters.Barcode.XDimension.Millimeters = 0.1 '15 ' xdim
            End If

            If parameters.ScalingFactor IsNot Nothing Then
                Dim scalingFactor As Single = ConvertScalingFactor(parameters.ScalingFactor)
                generator.Parameters.ImageHeight.Millimeters *= scalingFactor

                If encodeType.Equals(EncodeTypes.QR) Then
                    generator.Parameters.ImageWidth.Millimeters = generator.Parameters.ImageHeight.Millimeters
                    generator.Parameters.Barcode.XDimension.Millimeters = 0.1 ' (xdim * scalingFactor)
                End If

                generator.Parameters.AutoSizeMode = AutoSizeMode.Nearest
            End If

            Return generator.GenerateBarCodeImage()
        End Function


        Private Function IBarcodeGenerator_GetOldBarcodeImage(parameters As Aspose.Words.Fields.BarcodeParameters) As Image Implements IBarcodeGenerator.GetOldBarcodeImage
            Throw New NotImplementedException()
        End Function
    End Class

End Module

@Paga16 I cannot reproduce the problem on my side. I have used your VB implementation and the following code for testing:

Dim doc As Document = New Document("C:\Temp\in.doc")
' Specify custom barcode generator,
' which will render the barcode for Aspose.Words.
doc.FieldOptions.BarcodeGenerator = New CustomBarcodeGenerator()
doc.Save("C:\Temp\out.pdf")

The latest versions of Aspose.Words and Aspose.BarCode were used for testing. Here is the output PDF produced by the code: out.pdf (78.5 KB)