Word格式處理控件Aspose.Words for .NET教程——為DISPLAYBARCODE字段生成自定義條形碼圖像
Aspose.Words for .NET是一種高級Word文檔處理API,用于執(zhí)行各種文檔管理和操作任務(wù)。API支持生成,修改,轉(zhuǎn)換,呈現(xiàn)和打印文檔,而無需在跨平臺應(yīng)用程序中直接使用Microsoft Word。此外,API支持所有流行的Word處理文件格式,并允許將Word文檔導(dǎo)出或轉(zhuǎn)換為固定布局文件格式和最常用的圖像/多媒體格式。
>>Aspose.Words for .NET已經(jīng)更新至v20.5,提供顯示/隱藏語法和拼寫錯誤的功能,引入了可在文檔內(nèi)部使用水印的新幫助程序類,添加了為OOXML文檔設(shè)置壓縮級別的功能,點擊下載體驗
Aspose.Words具有用于生成自定義條形碼的界面,這使得使用Aspose.Words和Aspose.BarCode一起使用非常容易,以在輸出文檔中呈現(xiàn)條形碼圖像。例如,您可以將包含DISPLAYBARCODE字段的DOC,OOXML或RTF文檔加載到Aspose.Words中,提供自定義條形碼生成器的實現(xiàn)并保存為固定頁面格式,例如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技術(shù)交流群(642018183),我們很高興為您提供查詢和咨詢。