GroupDocs.Assembly for Java 25.6 Release Notes

Major Features

Feature - Support for custom font directories

Full List of Features Covering all Changes in this Release

KeySummaryCategory
ASSEMBLYJAVA-256Support for custom font directoriesFeature

Support for custom font directories

To set custom font directory, use the following code:

    String inFile = "Arial.docx";
	String outFile = "Arial.pdf";
	String customFontsDir = "custom_fonts_dir";

	FolderFontSource fontSource = new FolderFontSource(customFontsDir, SearchOption.TOP_FOLDER_ONLY);
	FontSettings.setFontSources(fontSource);

	try (InputStream modelContent = new FileInputStream(inFile);
		OutputStream mFileStream = new FileOutputStream(outFile)) {

		DocumentAssemblerUtil.assemble(modelContent, "", mFileStream, SupportedType.Pdf);

	} catch (IOException e) {
		e.printStackTrace();
	}
	
	public enum SupportedType {
		Unspecified,
		Docx,
		Xlsx,
		Pdf,
		Text
	}

    public static class DocumentAssemblerUtil {
		private static final DocumentAssembler assembler = new DocumentAssembler();
		private static final String DEFAULT_DATA = "{}";

		public static void assemble(
				InputStream modelContent,
				String data,
				OutputStream output,
				SupportedType outputFormat
		) {
			DataSourceInfo dataSourceInfo = initDataSourceInfo(data);
			LoadSaveOptions loadSaveOptions = new LoadSaveOptions(supportedTypeToFileFormat(outputFormat));

			boolean result;
			try {
				result = assembler.assembleDocument(modelContent, output, loadSaveOptions, dataSourceInfo);
			} catch (Exception ex) {
				throw new RuntimeException("Erreur lors de la génération du document", ex);
			}

			if (!result) {
				throw new RuntimeException("La génération du document avec Assembly est en erreur");
			}
		}

		private static int supportedTypeToFileFormat(SupportedType type) {
			if (type == null) {
				return FileFormat.UNSPECIFIED;
			}

			switch (type) {
				case Docx:
					return FileFormat.DOCX;
				case Pdf:
					return FileFormat.PDF;
				case Xlsx:
					return FileFormat.XLSX;
				case Text:
					return FileFormat.TEXT;
				default:
					return FileFormat.UNSPECIFIED;
			}
		}

		private static DataSourceInfo initDataSourceInfo(String data) {
			try {
				String dataValue = (data != null && !data.isEmpty()) ? data : DEFAULT_DATA;
				byte[] dataBytes = dataValue.getBytes(StandardCharsets.UTF_8);

				try (InputStream dataStream = new ByteArrayInputStream(dataBytes)) {
					JsonDataLoadOptions options = new JsonDataLoadOptions();
					options.setAlwaysGenerateRootObject(true);
					options.setSimpleValueParseMode(JsonSimpleValueParseMode.STRICT);

					return new DataSourceInfo(new JsonDataSource(dataStream, options));
				}
			} catch (Exception ex) {
				throw new RuntimeException("Erreur lors de l'initialisation de la source de données", ex);
			}
		}
	}