In the past we have been using the Libre Office java API in order to generate PDF-files from ODT (OpenOffice) templates. Some time ago we have switched to Aspose.Word, but we are experiencing some trouble getting barcodes to work in our specific use case. We are using Aspose.Word version 14.2 in our product, but also tested with version 15.12.
Use-case;
> We offer customers the functionality to upload OTD-templates with some predefined variables to use.
> On generating PDF-files we call the getRange() on the Document, and fill the predefined variables using com.aspose.words.Range.replace()
An example would be to place address or order information in template before saving the output
So far it works well for us. One of our customers wants to add barcodes to the document in the following use-case;
> The barcode added should be based on the content of a variable we replace in the document. Barcodes in general are working in ODT-templates by using the IDAutomationHC39M font. Furthermore, barcodes based on a variable should be work using fields.
> The customer only wishes have the barcodes optionally printed, only when some variable contains a certain value. This functionality was possible with Libre Office using Fields / Other / Functions / Conditional Text.
Is it possible to tell me if these functionality/use-cases are supported by Aspose.Word? And if so, how would we configure this in ODT-templates? If its not supported out of the box, what would be the best practice to implement optional barcodes based on a variable?
Hi Mark,
Inserting Document Elements
Hi Awais,
Thank you for your prompt response on my question. We have implemented the IReplacingCallBack interface, and are able to replace static variables in templates for dynamic values. Although, we experience some trouble implementing the barcode feature.
As pointed out in de documentation on Inserting Document Elements, we set the barcode font on the DocumentBuilder object as following;
builder.moveTo(matchNode);
String replacement = currentNodeText.replace(replacingArgs.getMatch().group(), replaceText);
Font font = builder.getFont();
String fontName = new String(font.getName());
if (replacingArgs.getMatch().group().equals(“[client.pasNummer]”))
{
font.setName(“IDAutomationHC39M”);
}
builder.write(replacement);
font.setName(fontName);
Problem
When converting documented to PDF, Aspose.Word produces the wollowing warning;
11:58:43.027 [qtp711772275-17] WARN n.t.d.c.i.DocumentWarningsCallback - Template convert font substitution warning: Font ‘IDAutomationHC39M’ has not been found. Using ‘Times New Roman’ font instead.
I’m developing on OSX, and I have the font installed on my system (under ~/Library/Fonts/), therefore, Aspose should be able to find the selected font.
Attempt 1 (font as resource)
As pointed out in the following forum topic, it should be possible to add another font from byte array. I have implemented that feature the way as posed below. Horever, Aspose.Word is still not able to use the selected font for barcodes.
List fontSources = new ArrayList(Arrays.asList(FontSettings.getFontsSources()));
try
{
final URL source = Thread.currentThread().getContextClassLoader().getResource(“fonts/IDAutomationHC39M.ttf”);
MemoryFontSource memoryFontSource = new MemoryFontSource(IOUtils.toString(source, “UTF-8”).getBytes());
fontSources.add(memoryFontSource);
}
catch (IOException e)
{
LOGGER.error(“Niet mogelijk om font te laden”, e);
}
finally
{
FontSourceBase[] updatedFontSources = (FontSourceBase[]) fontSources.toArray(new FontSourceBase[fontSources.size()]);
FontSettings.setFontsSources(updatedFontSources);
LOGGER.info(“Barcode font geregistreerd”);
}
Attempt 2 (font on system)
…
FolderFontSource folderFontSource = new FolderFontSource(“~/Library/Fonts/”, true);
fontSources.add(folderFontSource);
…
…
String[] folders = { “~/Library/Fonts/” };
FontSettings.setFontsFolders(folders, false);
…
The warning keeps persistent, and I’m not able to use write text using barcode font. Any suggestions regarding this issue?
Hi Mark,
- Required Font file(s)
- Your input Word document
- Aspose.Words generated output document showing the undesired behavior
- Your expected document which shows the correct output. Please create this document using Microsoft Word application.
- Please create a standalone Java application (source code without compilation errors) that helps us reproduce your problem on our end and attach it here for testing.
Hi Awais,
- testDocument, shows that loading template from resources, and saving as PDF works.
- testDocumentReplace, shows that replacing a variable works.
- testDocumentReplaceBarcode, tries to replace a variable using barcode font as suggested in this topic. Unfortunately the output PDF does not contain a barcode.
Hi Mark,
Hi Awais,
Thank you. After replacing the font-file, we successfully generated an PDF-file containing a barcode with variable value.
For now we need to do some more testing ourself regarding displaying barcodes in certain cases only.
Regards,
Mark