彩票走势图

logo Aspose.Words开发者指南
文档彩票走势图>>Aspose.Words开发者指南>>Word格式处理控件Aspose.Words for .NET教程——为DISPLAYBARCODE字段生成自定义条形码图像

Word格式处理控件Aspose.Words for .NET教程——为DISPLAYBARCODE字段生成自定义条形码图像


Aspose.Words for .NET是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。此外,API支持所有流行的Word处理文件格式,并允许将Word文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。

>>Aspose.Words for .NET已经更新至v20.5,提供显示/隐藏语法和拼写错误的功能,引入了可在文档内部使用水印的新帮助程序类,添加了为OOXML文档设置压缩级别的功能,点击下载体验


Aspose.Words具有用于生成自定义条形码的界面,这使得使用Aspose.Words和Aspose.BarCode一起使用非常容易,以在输出文档中呈现条形码图像。例如,您可以将包含DISPLAYBARCODE字段的DOC,OOXML或RTF文档加载到Aspose.Words中,提供自定义条形码生成器的实现并保存为固定页面格式,例如PDF,XPS等。典型的DISPLAYBARCODE字段具有以下语法:

{DISPLAYBARCODE“ SomeData” QR \ h 720}

下面是利用Aspose.BarCode API的示例代码生成器。本示例说明如何使用Aspose.Words和Aspose.BarCode API在Word文档中DISPLAYBARCODE字段的位置插入条形码图像。

// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithDocument();
Document doc = new Document(dataDir + @"GenerateACustomBarCodeImage.docx");

// Set custom barcode generator
doc.FieldOptions.BarcodeGenerator = new CustomBarcodeGenerator();
doc.Save(dataDir + @"GenerateACustomBarCodeImage_out.pdf");
public class CustomBarcodeGenerator : IBarcodeGenerator
{
    ////// Converts barcode image height from Word units to Aspose.BarCode units.
    /////////private static float ConvertSymbolHeight(string heightInTwipsString)
    {
        // Input value is in 1/1440 inches (twips)
        int heightInTwips = int.MinValue;
        int.TryParse(heightInTwipsString, out heightInTwips);

        if (heightInTwips == int.MinValue)
            throw new Exception("Error! Incorrect height - " + heightInTwipsString + ".");

        // Convert to mm
        return (float)(heightInTwips * 25.4 / 1440);
    }

    ////// Converts barcode image color from Word to Aspose.BarCode.
    /////////private static Color ConvertColor(string inputColor)
    {
        // Input should be from "0x000000" to "0xFFFFFF"
        int color = int.MinValue;
        int.TryParse(inputColor.Replace("0x", ""), out color);

        if (color == int.MinValue)
            throw new Exception("Error! Incorrect color - " + inputColor + ".");

        return Color.FromArgb(color >> 16, (color & 0xFF00) >> 8, color & 0xFF);

        // Backword conversion -
        //return string.Format("0x{0,6:X6}", mControl.ForeColor.ToArgb() & 0xFFFFFF);
    }

    ////// Converts bar code scaling factor from percents to float.
    /////////private static float ConvertScalingFactor(string scalingFactor)
    {
        bool isParsed = false;
        int percents = int.MinValue;
        int.TryParse(scalingFactor, out percents);

        if (percents != int.MinValue)
        {
            if (percents >= 10 && percents <= 10000) isParsed = true; } if (!isParsed) throw new Exception("Error! Incorrect scaling factor - " + scalingFactor + "."); return percents / 100.0f; } ////// Implementation of the GetBarCodeImage() method for IBarCodeGenerator interface.
    /////////public Image GetBarcodeImage(Fields.BarcodeParameters parameters)
    {
        if (parameters.BarcodeType == null || parameters.BarcodeValue == null)
            return null;

        string type = parameters.BarcodeType.ToUpper();
        var encodeType = EncodeTypes.None;

        switch (type)
        {
            case "QR":
                encodeType = EncodeTypes.QR;
                break;
            case "CODE128":
                encodeType = EncodeTypes.Code128;
                break;
            case "CODE39":
                encodeType = EncodeTypes.Code39Standard;
                break;
            case "EAN8":
                encodeType = EncodeTypes.EAN8;
                break;
            case "EAN13":
                encodeType = EncodeTypes.EAN13;
                break;
            case "UPCA":
                encodeType = EncodeTypes.UPCA;
                break;
            case "UPCE":
                encodeType = EncodeTypes.UPCE;
                break;
            case "ITF14":
                encodeType = EncodeTypes.ITF14;
                break;
            case "CASE":
                encodeType = EncodeTypes.None;
                break;
        }

        if (encodeType == EncodeTypes.None)
            return null;

        BarcodeGenerator generator = new BarcodeGenerator(encodeType);
        generator.CodeText = parameters.BarcodeValue;

        if (encodeType == EncodeTypes.QR)
            generator.Parameters.Barcode.CodeTextParameters.TwoDDisplayText = parameters.BarcodeValue;

        if (parameters.ForegroundColor != null)
            generator.Parameters.Barcode.BarColor = ConvertColor(parameters.ForegroundColor);

        if (parameters.BackgroundColor != null)
            generator.Parameters.BackColor = ConvertColor(parameters.BackgroundColor);

        if (parameters.SymbolHeight != null)
        {
            generator.Parameters.ImageHeight.Millimeters = ConvertSymbolHeight(parameters.SymbolHeight);
            generator.Parameters.AutoSizeMode = AutoSizeMode.Nearest;
        }

        generator.Parameters.Barcode.CodeTextParameters.Location = CodeLocation.None;

        if (parameters.DisplayText)
            generator.Parameters.Barcode.CodeTextParameters.Location = CodeLocation.Below;

        generator.Parameters.CaptionAbove.Text = "";

        const float scale = 1.0f; // Empiric scaling factor for converting Word barcode to Aspose.BarCode
        float xdim = 1.0f;

        if (encodeType == EncodeTypes.QR)
        {
            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 = generator.Parameters.Barcode.BarHeight.Millimeters = xdim;
        }

        if (parameters.ScalingFactor != null)
        {
            float scalingFactor = ConvertScalingFactor(parameters.ScalingFactor);
            generator.Parameters.ImageHeight.Millimeters *= scalingFactor;
            if (encodeType == EncodeTypes.QR)
            {
                generator.Parameters.ImageWidth.Millimeters = generator.Parameters.ImageHeight.Millimeters;
                generator.Parameters.Barcode.XDimension.Millimeters = generator.Parameters.Barcode.BarHeight.Millimeters = xdim * scalingFactor;
            }

            generator.Parameters.AutoSizeMode = AutoSizeMode.Nearest;
        }
        return generator.GenerateBarCodeImage();
    }
    
    public Image GetOldBarcodeImage(Fields.BarcodeParameters parameters)
    {
        throw new NotImplementedException();
    }
}

还想要更多吗?您可以点击阅读
【2019 · Aspose最新资源整合】查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(642018183),我们很高兴为您提供查询和咨询
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP