生成自定义BarCode图像
条形码是平行线或图案形式的数据的视觉表示。 条码广泛应用于零售、物流、医疗、银行等行业。
Microsoft Word允许用户使用字段将条形码直接嵌入到文档中。 用户可以插入特定类型的条形码,例如QR代码或线性条形码,使用 BARCODE 场。
在本文中,我们将介绍BARCODE字段是如何在Aspose.Words中实现的,以及Aspose.Words如何允许用户使用已添加条形码的Word文档。
Aspose.Words支持的条形码类型
Aspose.Words支持各种类型的条形码。 条形码类型在BarcodeType属性中作为字符串值传递。
当保存为支持条形码的Word格式时,您可以使用任何类型的条形码 支持Microsoft Word. 如果传递了错误类型的条形码,Word将显示错误。
当保存为其他格式(如PDF)时,Aspose.Words将条形码呈现委托给用户代码,因此用户仅限于其实现或使用的库的条形码类型。
将条形码插入文档或加载添加了条形码的文档
Aspose.Words提供以下功能::
- 使用 DisplayBarcode 和 MergeBarcode 字段代码
- 或者加载已插入条形码的Word文档以进行进一步的工作
Aspose.Words具有用于生成自定义条形码的界面,使其易于使用 Aspose.Words 和 阿斯波斯。BarCode 一起在输出文档中呈现条形码图像。 例如,您可以创建DOC、OOXML或RTF文档,并使用Aspose.Words向其添加DISPLAYBARCODE字段。 或者您可以加载DOC,OOXML或RTF文档,其中已存在DISPLAYBARCODE字段,并提供自定义条形码生成器的实现。
典型的DISPLAYBARCODE字段具有以下语法:
{ DISPLAYBARCODE "SomeData" QR \h 720 }
下面是使用Aspose.Words和Aspose的示例代码生成器。BarCode APIs. 此示例演示如何在Word文档中的DISPLAYBARCODE字段位置插入条形码图像:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
Document doc = new Document(getMyDir() + "Field sample - BARCODE.docx"); | |
doc.getFieldOptions().setBarcodeGenerator(new CustomBarcodeGenerator()); | |
doc.save(getArtifactsDir() + "WorkingWithBarcodeGenerator.GenerateACustomBarCodeImage.pdf"); |
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
public class CustomBarcodeGenerator extends DocsExamplesBase implements IBarcodeGenerator | |
{ | |
/// <summary> | |
/// Converts barcode image height from Word units to Aspose.BarCode units. | |
/// </summary> | |
/// <param name="heightInTwipsString"></param> | |
/// <returns></returns> | |
private static float convertSymbolHeight(String heightInTwipsString) throws Exception { | |
// Input value is in 1/1440 inches (twips). | |
int heightInTwips = tryParseInt(heightInTwipsString); | |
if (heightInTwips == Integer.MIN_VALUE) | |
throw new Exception("Error! Incorrect height - " + heightInTwipsString + "."); | |
// Convert to mm. | |
return (float) (heightInTwips * 25.4 / 1440.0); | |
} | |
/// <summary> | |
/// Converts barcode image color from Word to Aspose.BarCode. | |
/// </summary> | |
/// <param name="inputColor"></param> | |
/// <returns></returns> | |
private static Color convertColor(String inputColor) throws Exception { | |
// Input should be from "0x000000" to "0xFFFFFF". | |
int color = tryParseHex(inputColor.replace("0x", "")); | |
if (color == Integer.MIN_VALUE) | |
throw new Exception("Error! Incorrect color - " + inputColor + "."); | |
return new Color((color >> 16), ((color & 0xFF00) >> 8), (color & 0xFF)); | |
// Backward conversion - | |
// return string.Format("0x{0,6:X6}", mControl.ForeColor.ToArgb() & 0xFFFFFF); | |
} | |
/// <summary> | |
/// Converts bar code scaling factor from percent to float. | |
/// </summary> | |
/// <param name="scalingFactor"></param> | |
/// <returns></returns> | |
private static float convertScalingFactor(String scalingFactor) throws Exception { | |
boolean isParsed = false; | |
int percent = tryParseInt(scalingFactor); | |
if (percent != Integer.MIN_VALUE && percent >= 10 && percent <= 10000) | |
isParsed = true; | |
if (!isParsed) | |
throw new Exception("Error! Incorrect scaling factor - " + scalingFactor + "."); | |
return percent / 100.0f; | |
} | |
/// <summary> | |
/// Implementation of the GetBarCodeImage() method for IBarCodeGenerator interface. | |
/// </summary> | |
/// <param name="parameters"></param> | |
/// <returns></returns> | |
public BufferedImage getBarcodeImage(BarcodeParameters parameters) throws Exception { | |
if (parameters.getBarcodeType() == null || parameters.getBarcodeValue() == null) | |
return null; | |
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.QR); | |
String type = parameters.getBarcodeType().toUpperCase(); | |
switch (type) | |
{ | |
case "QR": | |
generator = new BarcodeGenerator(EncodeTypes.QR); | |
break; | |
case "CODE128": | |
generator = new BarcodeGenerator(EncodeTypes.CODE_128); | |
break; | |
case "CODE39": | |
generator = new BarcodeGenerator(EncodeTypes.CODE_39_STANDARD); | |
break; | |
case "EAN8": | |
generator = new BarcodeGenerator(EncodeTypes.EAN_8); | |
break; | |
case "EAN13": | |
generator = new BarcodeGenerator(EncodeTypes.EAN_13); | |
break; | |
case "UPCA": | |
generator = new BarcodeGenerator(EncodeTypes.UPCA); | |
break; | |
case "UPCE": | |
generator = new BarcodeGenerator(EncodeTypes.UPCE); | |
break; | |
case "ITF14": | |
generator = new BarcodeGenerator(EncodeTypes.ITF_14); | |
break; | |
case "CASE": | |
generator = new BarcodeGenerator(EncodeTypes.NONE); | |
break; | |
} | |
if (generator.getBarcodeType().equals(EncodeTypes.NONE)) | |
return null; | |
generator.setCodeText(parameters.getBarcodeValue()); | |
if (generator.getBarcodeType().equals(EncodeTypes.QR)) | |
generator.getParameters().getBarcode().getCodeTextParameters().setTwoDDisplayText(parameters.getBarcodeValue()); | |
if (parameters.getForegroundColor() != null) | |
generator.getParameters().getBarcode().setBarColor(convertColor(parameters.getForegroundColor())); | |
if (parameters.getBackgroundColor() != null) | |
generator.getParameters().setBackColor(convertColor(parameters.getBackgroundColor())); | |
if (parameters.getSymbolHeight() != null) | |
{ | |
generator.getParameters().getImageHeight().setPixels(convertSymbolHeight(parameters.getSymbolHeight())); | |
generator.getParameters().setAutoSizeMode(AutoSizeMode.NONE); | |
} | |
generator.getParameters().getBarcode().getCodeTextParameters().setLocation(CodeLocation.NONE); | |
if (parameters.getDisplayText()) | |
generator.getParameters().getBarcode().getCodeTextParameters().setLocation(CodeLocation.BELOW); | |
generator.getParameters().getCaptionAbove().setText(""); | |
// Empiric scaling factor for converting Word barcode to Aspose.BarCode. | |
final float SCALE = 2.4f; | |
float xdim = 1.0f; | |
if (generator.getBarcodeType().equals(EncodeTypes.QR)) | |
{ | |
generator.getParameters().setAutoSizeMode(AutoSizeMode.NEAREST); | |
generator.getParameters().getImageWidth().setInches(generator.getParameters().getImageWidth().getInches() * SCALE); | |
generator.getParameters().getImageHeight().setInches(generator.getParameters().getImageWidth().getInches()); | |
xdim = generator.getParameters().getImageHeight().getInches() / 25; | |
generator.getParameters().getBarcode().getXDimension().setInches(xdim); | |
generator.getParameters().getBarcode().getBarHeight().setInches(xdim); | |
} | |
if (parameters.getScalingFactor() != null) | |
{ | |
float scalingFactor = convertScalingFactor(parameters.getScalingFactor()); | |
generator.getParameters().getImageHeight().setInches(generator.getParameters().getImageHeight().getInches() * scalingFactor); | |
if (generator.getBarcodeType().equals(EncodeTypes.QR)) | |
{ | |
generator.getParameters().getImageWidth().setInches(generator.getParameters().getImageHeight().getInches()); | |
generator.getParameters().getBarcode().getXDimension().setInches(xdim * scalingFactor); | |
generator.getParameters().getBarcode().getBarHeight().setInches(xdim * scalingFactor); | |
} | |
generator.getParameters().setAutoSizeMode(AutoSizeMode.NONE); | |
} | |
return generator.generateBarCodeImage(); | |
} | |
/// <summary> | |
/// Implementation of the GetOldBarcodeImage() method for IBarCodeGenerator interface. | |
/// </summary> | |
/// <param name="parameters"></param> | |
/// <returns></returns> | |
public BufferedImage getOldBarcodeImage(BarcodeParameters parameters) | |
{ | |
if (parameters.getPostalAddress() == null) | |
return null; | |
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.POSTNET); | |
{ | |
generator.setCodeText(parameters.getPostalAddress()); | |
} | |
return generator.generateBarCodeImage(); | |
} | |
/// <summary> | |
/// Parses an integer using the invariant culture. Returns Int.MinValue if cannot parse. | |
/// | |
/// Allows leading sign. | |
/// Allows leading and trailing spaces. | |
/// </summary> | |
public static int tryParseInt(String s) { | |
try { | |
double value = Double.parseDouble(s); | |
return castDoubleToInt(value); | |
} catch (NumberFormatException e) { | |
return Integer.MIN_VALUE; | |
} | |
} | |
/// <summary> | |
/// Casts a double to int32 in a way that uint32 are "correctly" casted too (they become negative numbers). | |
/// </summary> | |
public static int castDoubleToInt(double value) | |
{ | |
long temp = (long) value; | |
return (int) temp; | |
} | |
/// <summary> | |
/// Try parses a hex String into an integer value. | |
/// on error return int.MinValue | |
/// </summary> | |
public static int tryParseHex(String s) | |
{ | |
try { | |
return Integer.parseInt(s); | |
} catch (NumberFormatException e) { | |
return Integer.MIN_VALUE; | |
} | |
} | |
} |
您还可以使用加载或新插入的条形码以固定页面格式(如PDF,XPS等)保存文档。 下面的代码示例演示如何将Word文档保存为PDF格式:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
Document doc = new Document(getMyDir() + "Document.docx"); | |
doc.save(getArtifactsDir() + "BaseConversions.DocxToPdf.pdf"); |
指定条码选项
使用条形码时,您可以设置一些其他属性。 Aspose.Words为您提供BarcodeParameters类–用于条形码参数传递到BarcodeGenerator的类。
Aspose.Words支持使用IBarcodeGenerator生成的图像的嵌入式96ppi分辨率,这限制了条形码图像的最小尺寸。 为了解决这个问题,开发人员可以手动将目标分辨率的条形码图像插入到Word文档中,并将其保存为所需的格式。 有关使用条形码的更多详细信息和示例,请参阅文章 从Word文档读取条形码.