Hello,
I’m using Aspose.Words .net 16.2.0.0 - Trying to get a barcode into the footer of a .docx that contains text like “1 of 3”, meaning page 1 of 3. Of course the barcode on each page needs to reflect the correct page number. The document is created from a template but has a variable number of pages depending on the amount of text that is inserted.
I tried using a DISPLAYBARCODE field in the template. Like this:
{ DISPLAYBARCODE "{ PAGE } of { NUMPAGES }" CODE128 }
This works fine while viewing the document with MS Word. However, if the document is saved in PDF format (i.e. doc.Save(“mydoc.pdf”, SaveFormat.Pdf)) the barcodes are missing from all pages.
I attempted to remedy this by implemented an IBarCodeGenerator. I have verified that GetBarcodeImage() is called once per page and the correct text value is passed in via the BarcodeParameters. For example “1 of 3”, “2 of 3”, “3 of 3”. My implementation of GetBarcodeImage() is passing back valid images but the images are not included in the saved PDF.
Is what I am attempting supported?
Code Sample (this should look familiar as it is mostly example code from the Aspose site):
using System;
using System.Drawing;
using Aspose.Words;
using Aspose.Words.Fields;
using Aspose.BarCode;
namespace ScratchConsole
{
class Program
{
private static void Main()
{
var wordlicense = new Aspose.Words.License();
wordlicense.SetLicense("Aspose.Words.lic");
var barcodelicense = new Aspose.BarCode.License();
barcodelicense.SetLicense("Aspose.BarCode.lic");
try
{
var template = "SampleDecTemplate.docx";
var doc = new Aspose.Words.Document(template);
var builder = new DocumentBuilder(doc);
for (int i = 1; i <= 30; i++)
builder.Writeln(i.ToString());
doc.FieldOptions.BarcodeGenerator = new CustomBarcodeGenerator();
doc.Save("SampleOut.pdf", SaveFormat.Pdf);
}
catch (Exception x)
{
}
}
}
public class CustomBarcodeGenerator : IBarcodeGenerator
{
public Image GetBarcodeImage(BarcodeParameters args)
{
if (args.BarcodeType == null || args.BarcodeValue == null)
return null;
var builder = new BarCodeBuilder();
builder.SymbologyType = ConvertBarcodeType(args.BarcodeType);
if (builder.SymbologyType == (Symbology)int.MinValue)
return null;
builder.CodeText = args.BarcodeValue;
if (builder.SymbologyType == Symbology.QR)
builder.Display2DText = args.BarcodeValue;
if (args.ForegroundColor != null)
builder.ForeColor = ConvertColor(args.ForegroundColor);
if (args.BackgroundColor != null)
builder.BackColor = ConvertColor(args.BackgroundColor);
if (args.SymbolHeight != null)
{
builder.ImageHeight = ConvertSymbolHeight(args.SymbolHeight);
builder.AutoSize = false;
}
builder.CodeLocation = CodeLocation.None;
if (args.DisplayText)
builder.CodeLocation = CodeLocation.Below;
builder.CaptionAbove.Text = "";
const float scale = 0.4f; // Empiric scaling factor for converting Word barcode to Aspose.BarCode
float xdim = 1.0f;
if (builder.SymbologyType == Symbology.QR)
{
builder.AutoSize = false;
builder.ImageWidth *= scale;
builder.ImageHeight = builder.ImageWidth;
xdim = builder.ImageHeight / 25;
builder.xDimension = builder.yDimension = xdim;
}
if (args.ScalingFactor != null)
{
float scalingFactor = ConvertScalingFactor(args.ScalingFactor);
builder.ImageHeight *= scalingFactor;
if (builder.SymbologyType == Symbology.QR)
{
builder.ImageWidth = builder.ImageHeight;
builder.xDimension = builder.yDimension = xdim *scalingFactor;
}
builder.AutoSize = false;
}
builder.ImageHeight = 16;
//builder.Save("Barcode.bmp");
return builder.BarCodeImage;
}
public Image GetOldBarcodeImage(BarcodeParameters parameters)
{
throw new NotImplementedException();
}
private static Symbology ConvertBarcodeType(string code)
{
if (code == null)
return (Symbology)int.MinValue;
string type = code.ToUpper();
switch (type)
{
case "QR":
return Symbology.QR;
case "CODE128":
return Symbology.Code128;
case "CODE39":
return Symbology.Code39Standard;
case "EAN8":
return Symbology.EAN8;
case "EAN13":
return Symbology.EAN13;
case "UPCA":
return Symbology.UPCA;
case "UPCE":
return Symbology.UPCE;
case "ITF14":
return Symbology.ITF14;
case "CASE":
break;
}
return (Symbology)int.MinValue;
}
// 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);
}
// 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;
}
}
}