Aspose.Words for .NET是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。此外,API支持所有流行的Word处理文件格式,并允许将Word文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。
>>Aspose.Words for .NET已经更新至最新版,提供显示/隐藏语法和拼写错误的功能,引入了可在文档内部使用水印的新帮助程序类,添加了为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(); } }