Convert a PDF without any font on PDF with font

Dear support,

I’m trying to convert a PDF which haven’t any font (removed by Aspose), and after that, i want to add a font on this PDF. But when I try to save it, Aspose rise a exception.

Please see it below :

class com.aspose.pdf.internal.ms.System.z10: Value cannot be null.
Parameter name: key
com.aspose.pdf.internal.p554.z2.tryGetValue(Unknown Source)
com.aspose.pdf.internal.p9.z1.m1(Unknown Source)
com.aspose.pdf.internal.p14.z5$z1.m1(Unknown Source)
com.aspose.pdf.internal.p66.z2.m1(Unknown Source)
com.aspose.pdf.internal.p93.z6.m3(Unknown Source)
com.aspose.pdf.internal.p93.z6.m1(Unknown Source)
com.aspose.pdf.internal.p93.z6.m1(Unknown Source)
com.aspose.pdf.internal.p93.z6.m17(Unknown Source)
com.aspose.pdf.internal.p93.z6.m2(Unknown Source)
com.aspose.pdf.internal.p93.z7.m12(Unknown Source)
com.aspose.pdf.internal.p93.z6.m1(Unknown Source)
com.aspose.pdf.ADocument.m1(Unknown Source)
com.aspose.pdf.Document.m1(Unknown Source)
com.aspose.pdf.ADocument.convertInternal(Unknown Source)
com.aspose.pdf.Document.convertInternal(Unknown Source)
com.aspose.pdf.ADocument.convert(Unknown Source)
com.aspose.pdf.Document.convert(Unknown Source)
com.ideosante.document.PDFDocument.convertByAspose(PDFDocument.java:65)
com.ideosante.document.AsposeDocumentImpl.convert(AsposeDocumentImpl.java:65)
com.ideosante.Converter.convert(Converter.java:35)
com.ideosante.ConverterTest.allPdfToPDF(ConverterTest.java:592)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:606)
org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:69)
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:48)
org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
org.junit.runners.ParentRunner.run(ParentRunner.java:292)
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
-2146233087
class com.aspose.pdf.internal.ms.System.z10: Value cannot be null.
Parameter name: key
com.aspose.pdf.internal.p554.z2.tryGetValue(Unknown Source)
com.aspose.pdf.internal.p9.z1.m1(Unknown Source)
com.aspose.pdf.internal.p14.z5$z1.m1(Unknown Source)
com.aspose.pdf.internal.p66.z2.m1(Unknown Source)
com.aspose.pdf.internal.p93.z6.m3(Unknown Source)
com.aspose.pdf.internal.p93.z6.m1(Unknown Source)
com.aspose.pdf.internal.p93.z6.m1(Unknown Source)
com.aspose.pdf.internal.p93.z6.m17(Unknown Source)
com.aspose.pdf.internal.p93.z6.m2(Unknown Source)
com.aspose.pdf.internal.p93.z7.m12(Unknown Source)
com.aspose.pdf.internal.p93.z6.m1(Unknown Source)
com.aspose.pdf.ADocument.m1(Unknown Source)
com.aspose.pdf.Document.m1(Unknown Source)
com.aspose.pdf.ADocument.convertInternal(Unknown Source)
com.aspose.pdf.Document.convertInternal(Unknown Source)
com.aspose.pdf.ADocument.convert(Unknown Source)
com.aspose.pdf.Document.convert(Unknown Source)
com.ideosante.document.PDFDocument.convertByAspose(PDFDocument.java:65)
com.ideosante.document.AsposeDocumentImpl.convert(AsposeDocumentImpl.java:65)
com.ideosante.Converter.convert(Converter.java:35)
com.ideosante.ConverterTest.allPdfToPDF(ConverterTest.java:592)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:606)
org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:69)
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:48)
org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
org.junit.runners.ParentRunner.run(ParentRunner.java:292)
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Value cannot be null.
key
[com.aspose.pdf.internal.p554.z2.tryGetValue(Unknown Source), com.aspose.pdf.internal.p9.z1.m1(Unknown Source), com.aspose.pdf.internal.p14.z5$z1.m1(Unknown Source), com.aspose.pdf.internal.p66.z2.m1(Unknown Source), com.aspose.pdf.internal.p93.z6.m3(Unknown Source), com.aspose.pdf.internal.p93.z6.m1(Unknown Source), com.aspose.pdf.internal.p93.z6.m1(Unknown Source), com.aspose.pdf.internal.p93.z6.m17(Unknown Source), com.aspose.pdf.internal.p93.z6.m2(Unknown Source), com.aspose.pdf.internal.p93.z7.m12(Unknown Source), com.aspose.pdf.internal.p93.z6.m1(Unknown Source), com.aspose.pdf.ADocument.m1(Unknown Source), com.aspose.pdf.Document.m1(Unknown Source), com.aspose.pdf.ADocument.convertInternal(Unknown Source), com.aspose.pdf.Document.convertInternal(Unknown Source), com.aspose.pdf.ADocument.convert(Unknown Source), com.aspose.pdf.Document.convert(Unknown Source), com.ideosante.document.PDFDocument.convertByAspose(PDFDocument.java:65), com.ideosante.document.AsposeDocumentImpl.convert(AsposeDocumentImpl.java:65), com.ideosante.Converter.convert(Converter.java:35), com.ideosante.ConverterTest.allPdfToPDF(ConverterTest.java:592), sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method), sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57), sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43), java.lang.reflect.Method.invoke(Method.java:606), org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44), org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15), org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41), org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20), org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28), org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263), org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:69), org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:48), org.junit.runners.ParentRunner$3.run(ParentRunner.java:231), org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60), org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229), org.junit.runners.ParentRunner.access$000(ParentRunner.java:50), org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222), org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28), org.junit.runners.ParentRunner.run(ParentRunner.java:292), org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86), org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38), org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459), org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675), org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382), org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)]
[]


Thanks in advance for your help.


Hi Florian,


Thanks for contacting support.

Can you please share the input PDF file and code snippet which you are using, so that we can test the scenario in our environment. We are sorry for this inconvenience.

Dear support,

The input PDF has been attached in the first post.
For the code, please find it below :

TextFragmentAbsorber absorber = new TextFragmentAbsorber(new TextEditOptions(TextEditOptions.FontReplace.RemoveUnusedFonts));
this.document.getPages().accept(absorber);

TextFragmentCollection textFragmentCollection = absorber.getTextFragments();
for (Iterator iterator = textFragmentCollection.iterator(); iterator.hasNext();) {
    TextFragment textFragment = iterator.next();
    Font font = textFragment.getTextState().getFont();
    String fontName = font.getFontName();
    if (!DefaultFont.getAllFont().contains(fontName) && !font.isEmbedded()) {
        textFragment.getTextState().setFont(FontRepository.findFont(getParametres().getPoliceRemplacement()));
        font.setEmbedded(true);
    }
}

The code was based on your documentation (Extract fonts from PDF|Aspose.PDF for Java)

Hi

I didn’t create a new thread because the cause for my issue might be the same as the one discussed here.

I’ve noticed the same exception (com.aspose.pdf.internal.ms.System.z10: Value cannot be null.) when trying to extract text from some PDF’s (not all, it works for most PDF’s). On Windows it works. On a Unix system with no fonts installed, it fails with the same exception (different stacktrace) as the one posted above by Florian.

When installing fonts (ttf-mscorefonts-installer), it works on Unix too.
I assume text extraction should work without installed fonts or otherwise at least provide an option to do it without installed fonts. Or is there a reason why fonts are required?

Tested with Aspose.Pdf for Java Versions 11.0.0 and 11.4.0.

I wasn’t able to create an own PDF that will result in the same exception, thus providing an original one. See additional attachment for log file.

Code to reproduce:
try (FileInputStream fis = new FileInputStream(inputFile)) {
Document document = new Document(fis);

@SuppressWarnings(“cast”)
Iterable<com.aspose.pdf.Page> pages = (Iterable<com.aspose.pdf.Page>) document.getPages();
for (Page page : pages) {
TextExtractionOptions textExtOptions = new TextExtractionOptions(TextFormattingMode.Pure);
TextDevice txtDevice = new TextDevice(textExtOptions);
txtDevice.setEncoding(StandardCharsets.UTF_8);
txtDevice.setExtractionOptions(textExtOptions);
txtDevice.process(page, outputStream);
}
}


Kind regards
Stephan

florian.melot:
Dear support,

The input PDF has been attached in the first post.
For the code, please find it below :

TextFragmentAbsorber absorber = new TextFragmentAbsorber(new TextEditOptions(TextEditOptions.FontReplace.RemoveUnusedFonts));
this.document.getPages().accept(absorber);

TextFragmentCollection textFragmentCollection = absorber.getTextFragments();
for (Iterator iterator = textFragmentCollection.iterator(); iterator.hasNext();) {
TextFragment textFragment = iterator.next();
Font font = textFragment.getTextState().getFont();
String fontName = font.getFontName();
if (!DefaultFont.getAllFont().contains(fontName) && !font.isEmbedded()) {
textFragment.getTextState().setFont(FontRepository.findFont(getParametres().getPoliceRemplacement()));
font.setEmbedded(true);
}
}

The code was based on your documentation (http://www.aspose.com/docs/display/pdfjava/Replace+fonts+in+existing+PDF+file)
Hi Florian,


Thanks for sharing the details. The document shared in first post appears to be updated / modified file using Aspose.Pdf for Java. Can you please share the resource file, before its manipulated with our API. We are sorry for this inconvenience.

Hi Nayyer,

Indeed, the shared document has been modified by Aspose to remove embedded font.
And after that, I must change the missing font by another.
That’s the reason why I need to add font.

Regards,

Florian

bsiagch:
Hi

I didn’t create a new thread because the cause for my issue might be the same as the one discussed here.

I’ve noticed the same exception (com.aspose.pdf.internal.ms.System.z10: Value cannot be null.) when trying to extract text from some PDF’s (not all, it works for most PDF’s). On Windows it works. On a Unix system with no fonts installed, it fails with the same exception (different stacktrace) as the one posted above by Florian.

When installing fonts (ttf-mscorefonts-installer), it works on Unix too.
I assume text extraction should work without installed fonts or otherwise at least provide an option to do it without installed fonts. Or is there a reason why fonts are required?

Tested with Aspose.Pdf for Java Versions 11.0.0 and 11.4.0.

I wasn’t able to create an own PDF that will result in the same exception, thus providing an original one. See additional attachment for log file.

Code to reproduce:
try (FileInputStream fis = new FileInputStream(inputFile)) {
Document document = new Document(fis);

@SuppressWarnings(“cast”)
Iterable<com.aspose.pdf.Page> pages = (Iterable<com.aspose.pdf.Page>) document.getPages();
for (Page page : pages) {
TextExtractionOptions textExtOptions = new TextExtractionOptions(TextFormattingMode.Pure);
TextDevice txtDevice = new TextDevice(textExtOptions);
txtDevice.setEncoding(StandardCharsets.UTF_8);
txtDevice.setExtractionOptions(textExtOptions);
txtDevice.process(page, outputStream);
}
}

Hi Stephan,

Thanks for using our API’s.

The font is required so that API understands text inside PDF document. As each font has its own glyph for text representation, so its necessary to install the fonts on system where PDF file is being manipulated, so that accurate text is extracted from file. Now concerning to above stated problem, its been logged in our issue tracking system as PDFNEWJAVA-35806. We will further look into the details of this problem and will keep you posted on the status of correction. Please be patient and spare us little time. We are sorry for this inconvenience.

florian.melot:
Hi Nayyer,

Indeed, the shared document has been modified by Aspose to remove embedded font.
And after that, I must change the missing font by another.
That's the reason why I need to add font.

Hi Florian,

Thanks for sharing the details. Can you please share the resource/input file before font replace operation, so that we can test the scenario in our environment.

Hi Nayyer,

The original file has been attached to this message.

Regards,

Florian

florian.melot:
Hi Nayyer,

The original file has been attached to this message.
Hi Florian,

Thanks for sharing the resource file.

I have tested the scenario using latest release of Aspose.Pdf for Java 11.4.0 in Eclipse Mars project with JDK 1.8 running over Windows 10 and I am unable to notice any issue. For your reference, I have also attached the output generated over my end.

Can you please try using the latest release and in case you still face the same problem, please share some details regarding your working environment. We are sorry for your inconvenience.

[Java]

com.aspose.pdf.Document document = new com.aspose.pdf.Document("c:/pdftest/PDF_Purisa.pdf");

TextFragmentAbsorber absorber = new TextFragmentAbsorber(new TextEditOptions(TextEditOptions.FontReplace.RemoveUnusedFonts));

document.getPages().accept(absorber);

TextFragmentCollection textFragmentCollection = absorber.getTextFragments();

for (Iterator iterator = textFragmentCollection.iterator(); iterator.hasNext();) {

TextFragment textFragment = iterator.next();

Font font = textFragment.getTextState().getFont();

String fontName = font.getFontName();

System.out.print(fontName);

// if (!font.isEmbedded())

if (fontName.equals("Purisa"))

{

textFragment.getTextState().setFont(FontRepository.findFont("Arial"));

font.setEmbedded(true);

}

}

document.save("c:/pdftest/FontReplaced.pdf");

My issue is with PDF_Purisa_no_font.pdf and not PDF_Purisa.pdf

My original file is PDF_Purisa.pdf and I removed the font (Purisa) with Aspose.
After that, I tried to replace the missing font with Aspose which caused my issue.


Please find below ths code to create the PDF without embedded font :
Document doc = null;
try {
doc = new Document(new FileInputStream("/home/fmelot/Documents/Aspose/Purisa.pdf"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
TextFragmentAbsorber absorber = new TextFragmentAbsorber(new TextEditOptions(TextEditOptions.FontReplace.RemoveUnusedFonts));
doc.getPages().accept(absorber);

TextFragmentCollection textFragmentCollection = absorber.getTextFragments();
for (Iterator iterator = textFragmentCollection.iterator(); iterator.hasNext():wink: {
TextFragment textFragment = iterator.next();
Font font = textFragment.getTextState().getFont();
String fontName = font.getFontName();
font.setEmbedded(false);
}
doc.save("/home/fmelot/Documents/Aspose/Purisa-no-font.pdf");



Hi Florian,


Thanks for sharing the details.

I am testing the scenario based on latest information and will get back to you soon.

Hi Florian,


I have tested the scenario of font replace in Purisa-no-font.pdf file using Aspose.Pdf for Java 11.5.0 running in Eclipse Juno project in Windows 7 x64 and I am unable to notice any problem.

[Java]

Document doc = null;<o:p></o:p>

try {

doc = new Document(new FileInputStream("c:/pdftest/Purisa-no-font (1).pdf"));

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

com.aspose.pdf.TextFragmentAbsorber absorber = new com.aspose.pdf.TextFragmentAbsorber(new com.aspose.pdf.TextEditOptions(com.aspose.pdf.TextEditOptions.FontReplace.RemoveUnusedFonts));

doc.getPages().accept(absorber);

com.aspose.pdf.TextFragmentCollection textFragmentCollection = absorber.getTextFragments();

for (java.util.Iterator iterator = textFragmentCollection.iterator(); iterator.hasNext();) {

TextFragment textFragment = iterator.next();

com.aspose.pdf.Font font = textFragment.getTextState().getFont();

String fontName = font.getFontName();

font.setEmbedded(false);

}

doc.save(“c:/pdftest/Purisa-no-font_updated.pdf”);

Hi Nayyer,

First of all, thanks for your answer.

In another topic, I have some issues with font, and solution is to install Microsoft font on Unix.
It’s also solved this problem.


Hi Florian,


Thanks for sharing the feedback.

When manipulating the PDF files on Non-Windows platform where input files are using MS Fonts, we need to have these fonts installed. However we are glad to hear that your problem is resolved. Please continue using our API’s and in the event of any further query, please feel free to contact.

The issues you have found earlier (filed as PDFJAVA-35806) have been fixed in Aspose.PDF for Java 20.5.