We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Throw exception when font not embeded

Hello,

I’m using Aspose.PDF 18.5 for java on a Linux machine and trying to throw an exception when converting from PDF to PDFA-1A and a font is not embeded.
I’m using the following code snippet but it doesn’t throw an exception

	    for(Page page : document.getPages()) {
	if(page.getResources().getFonts() != null) {
	    for(com.aspose.pdf.Font pageFont : page.getResources().getFonts()) {
		pageFont.getFontOptions().setNotifyAboutFontEmbeddingError(true);
	    }
	}
	XFormCollection forms = page.getResources().getForms();
	for(Object object : forms) {
	    XForm form = (XForm) object;
	    if(form.getResources().getFonts() != null) {
		for(com.aspose.pdf.Font formFont : form.getResources().getFonts()) {
		    formFont.getFontOptions().setNotifyAboutFontEmbeddingError(true);
		}
	    }
	}
    }

Can you please help ?

@obouallou,

In the source code, after setting NotifyAboutFontEmbeddingError property to true, you are not embedding fonts like: pageFont.setEmbedded(true);

It is possible to read this property immediately after it was set, but it is not convenient approach. The NotifyAboutFontEmbeddingError property enforces exception mechanism for the cases where an attempt to embed fonts failed. If this flag is set, then an exception of type com.aspose.pdf.FontEmbeddingException will be thrown.

But I don’t want to embed fonts manually and all the time, they’re only embeded with certain PDFA norms like PDF-A-1a and it is done automatically by aspose I presume ?

Is there any way to throw an exception when I have an error like “Font ‘ArialNarrow’ is not embedded” in the conversion logs ?

Edit : I tried with the following code and I still don’t get an exception while I have “Font ‘ArialNarrow’ is not embedded” in the logs

    for(Page page : document.getPages()) {
	if(page.getResources().getFonts() != null) {
	    for(com.aspose.pdf.Font pageFont : page.getResources().getFonts()) {
		if(!pageFont.isEmbedded()) {
		    pageFont.getFontOptions().setNotifyAboutFontEmbeddingError(true);
		    pageFont.setEmbedded(true);
		}
	    }
	}
	XFormCollection forms = page.getResources().getForms();
	for(Object object : forms) {
	    XForm form = (XForm) object;
	    if(form.getResources().getFonts() != null) {
		for(com.aspose.pdf.Font formFont : form.getResources().getFonts()) {
			if(!formFont.isEmbedded()) {
			    formFont.getFontOptions().setNotifyAboutFontEmbeddingError(true);
			    formFont.setEmbedded(true);
			}
		}
	    }
	}
    }

@obouallou,

You can throw a custom conditional error in the code as follows:
Java

// Initialize new document object
Document doc = new Document(dataDir + "input.pdf");
// get all fonts from document
com.aspose.pdf.Font[] fonts = doc.getFontUtilities().getAllFonts();    	
String font = null;
for (com.aspose.pdf.Font f : fonts)
    if(f.getFontName() == "Ariel")
        font = f.getFontName();
if(font == null) throw new Exception ("Font not found error");