Free Support Forum - aspose.com

Set Font for entire PDF using Aspose.PDF for Java

Hi I did try to find answer on your documentation but not luck can you please give me some help.

I am creating a PDF from scratch on JAVA code base.

I want to set font for enteire PDF for example to “Arial” or any other for that matter.

Now what I need to do is to
font = FontRepository.findFont(fontName);
and use font down the document.

My questions is if I do not have “Arial” on my server it will not work as font will be null in that case.

If I do not have Arial on my server I get crash when creating PDF.

However I want PDF still to be created and it should use Arial if available on client machine or use its subtitution

Can you help me how to go about this.

Hi Ritesh,


Thanks for your inquriy. Please note If Aspose.Pdf for Java does not find the referenced font it automatically falls back to system default font.

I have tested the scenario with Aspose.Pdf for .NET 11.8.0 and unable to notice any exception in case of missing font but it uses system default font, Times New Roman in my case. Please download and try latest version of Aspose.Pdf for Java. However if the issue persist then please share your sample code here, we will look into it and will guide you accordingly.

Font font=
FontRepository.findFont(“ZapfDingbats”);<o:p></o:p>

//Font font= FontRepository.findFont("Arial");

TextState tstate= new TextState();

tstate.setFont(font);

Document doc= new Document();

doc.getPageInfo().setDefaultTextState(tstate);

com.aspose.pdf.Page page= doc.getPages().add();

TextFragment text= new TextFragment("Test text");

page.getParagraphs().add(text);

doc.save("testtext.pdf");


Best Regards,

I am looking at your Java Version and here is what i get when do


<pre style=“font-family: “Courier New”; font-size: 9pt; background-color: rgb(255, 255, 255);”>FontRepository.findFont(“Book Antiqua”)<pre style=“font-family: “Courier New”; font-size: 9pt; background-color: rgb(255, 255, 255);”>com.aspose.pdf.exceptions.FontNotFoundException: Font Book Antiqua was not found
What I am looking for is simple 
If “Book Antiqua” is not present on my server I expect aspose API to replace it with substitute 
I expect when my PDF document is opened on a client computer it will show fonts “Book Antiqua” if available on client machine. If not available then it will show me content in “Arial” which was substitute.
<span style=“font-size: 12px; font-family: “Courier New”;”>I hope this helps , we are your premium customers and your help will enables us to use Aspose to the full capacity.
<span style=“font-size: 12px; font-family: “Courier New”;”> 



Also when I do


<pre style=“font-family: “Courier New”; font-size: 9pt; background-color: rgb(255, 255, 255);”>com.aspose.pdf.Font reportFont1 = FontRepository.findFont(“ZapfDingbats”);<pre style=“font-family: “Courier New”; font-size: 9pt; background-color: rgb(255, 255, 255);”>
<pre style=“font-family: “Courier New”; font-size: 9pt; background-color: rgb(255, 255, 255);”>result is “Times New Roman” which is fine but when I do <pre style=“font-family: “Courier New”; font-size: 9pt; background-color: rgb(255, 255, 255);”>
<span style=“font-family: “Courier New”; font-size: 9pt; background-color: rgb(228, 228, 255);”>com<span style=“font-family: “Courier New”; font-size: 9pt; background-color: rgb(247, 250, 255);”>.aspose.pdf.Font reportFont1 = FontRepository.<span style=“font-family: “Courier New”; font-size: 9pt; font-style: italic; background-color: rgb(247, 250, 255);”>findFont<span style=“font-family: “Courier New”; font-size: 9pt; background-color: rgb(247, 250, 255);”>(nonamefont<span style=“font-family: “Courier New”; font-size: 9pt; background-color: rgb(247, 250, 255);”>);
<pre style=“font-family: “Courier New”; font-size: 9pt;”>
<pre style=“font-family: “Courier New”; font-size: 9pt;”>result is <pre style=“font-family: “Courier New”; font-size: 9pt;”>
Trace:
class com.aspose.pdf.exceptions.FontNotFoundException: Font nonamefont was not found
com.aspose.pdf.FontRepository.findFont(Unknown Source)
class com.aspose.pdf.exceptions.FontNotFoundException: Font nonamefont was not found
com.aspose.pdf.FontRepository.findFont(Unknown Source)
java.lang.Thread.run(Thread.java:745)
at com.aspose.pdf.FontRepository.findFont(Unknown Source)
<pre style=“font-family: “Courier New”; font-size: 9pt;”>Hope you can help

Hi Ritesh,


Thanks for your feedback. I am looking into the issue and will update you soon.

Best Regards,

I am still waiting for the solution, please understand this is critical to us. We do have your license it’s just that don’t have details to access priority support.

Hi Ritesh,


We are sorry for the inconvenience. I have tested the scenario and noticed the FontNotFoundException in case of “NoNameFont”, so logged a ticket PDFJAVA-36146 with high priority in our issue tracking system for further investigation and rectification. We will keep you updated about the issue resolution progress.

Best Regards,

Hi,


How can I track this issue , the number you provided earlier PDFJAVA-36146 ?

We are waiting of the fix to come thru

Hi Ritesh,


Thanks for your patience. Our product team has investigated the issue and would like to update you that apfDingbats font exist inside jar as a resource. We includes FontMetrics for this font. We also includes metrics for Courier, Helvetica, Symbol, Times-Roman fonts. But these are used only for calculation font’s symbol metrics. Adding text with such fonts will replace it with other existing font Time New Roman or Arial.

For your scenario you can catch FontNotFoundException and substitute directly one font to another as following. Hopefully it will help you to accomplish the task.

try<o:p></o:p>

{<o:p></o:p>


originalFont = FontRepository.findFont(
“ZapfDingbats”);<o:p></o:p>

}<o:p></o:p>

catch
(com.aspose.pdf.exceptions.FontNotFoundException e)
<o:p></o:p>

{<o:p></o:p>


FontRepository.getSubstitutions().add(
new
SimpleFontSubstitution(
“ZapfDingbats”, “Arial”)); <o:p></o:p>

}


Please feel free to contact us for any further assistance.


Best Regards,


in this case your originalFont will still be null right. So won’t I get exception down the line in code ?


I want to set fonts to “Book Antiqua” and I do not have these fonts on my server. But I know that my audience (clients) do have “Book Antiqua” on their computers.


So I want PDF which when opened on client computers render “Book Antiqua” as expected , if “Book Antiuqa” is not available then use replacement.

Will this work ? can you give me sample code demonstrating this.

Hi Ritesh,


Thanks for your inquiry. Yes, above shared code substitution will work in your described scenario. Please check following code snippet. If "Book Antiqua" font is available on your machine then for testing you can amend font name to fallback to Arial.


Font font= null;

try

{

font = FontRepository.findFont("Book Antiqua");

}

catch (com.aspose.pdf.exceptions.FontNotFoundException e)

{

FontRepository.getSubstitutions().add(new SimpleFontSubstitution("Book Antiqua", "Arial"));

}

TextState tstate= new TextState();

tstate.setFont(font);

Document doc= new Document();

//configure font substitution

//CustomSubstitutor subst1 = new CustomSubstitutor();

//FontRepository.getSubstitutions().add(subst1);

doc.getPageInfo().setDefaultTextState(tstate);

com.aspose.pdf.Page page= doc.getPages().add();

TextFragment text= new TextFragment("Test text");

page.getParagraphs().add(text);

doc.save(myDir+"testtext.pdf");


Please feel free to contact us for any further assistance.


Best Regards,

Hi Tahil,


It’s kind of hard to explain you what I am looking for and we are going round in circles.

I have latest version of your PDF downloaded just now. 11.9.0 just so that you don’t have to worry if I am using old version.

Your code does not work and fails with error below, simply because I do not have any font such as “Book Antiqua”

But my client to whom I am sending this PDF does have the font “Book Antiqua”

Now I want to create PDF with font set as “Book Antiqua” , so when I open the PDF document on my local computer obviously because I do not have “Book Antiqua” it must open and display this in a substitute font.

Same file now goes to my client and when it is opened on his computer the same file displays font in “Book Antiqua”

I hope I am able to explain you what I need , please look at the stack trace below

org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:745)
at com.aspose.pdf.internal.p630.z28.(Unknown Source)
at com.aspose.pdf.internal.p630.z28.(Unknown Source)
at com.aspose.pdf.internal.p630.z28.(Unknown Source)
at com.aspose.pdf.internal.p630.z14.m6(Unknown Source)
at com.aspose.pdf.internal.p250.z141.m2(Unknown Source)
at com.aspose.pdf.internal.p250.z333.(Unknown Source)
at com.aspose.pdf.internal.p250.z385.m3(Unknown Source)
at com.aspose.pdf.internal.p250.z385.m2(Unknown Source)
at com.aspose.pdf.internal.p250.z426.m2(Unknown Source)
at com.aspose.pdf.internal.p250.z335.m1(Unknown Source)
at com.aspose.pdf.internal.p250.z335.m1(Unknown Source)
at com.aspose.pdf.internal.p250.z343.m1(Unknown Source)
at com.aspose.pdf.internal.p66.z38.m1(Unknown Source)
at com.aspose.pdf.internal.p68.z1.m1(Unknown Source)
at com.aspose.pdf.internal.p69.z6.m1(Unknown Source)
at com.aspose.pdf.internal.p69.z9.m1(Unknown Source)
at com.aspose.pdf.TextSegment.setText(Unknown Source)
at com.aspose.pdf.TextSegment.m1(Unknown Source)
at com.aspose.pdf.TextBuilder.m1(Unknown Source)
at com.aspose.pdf.TextBuilder.appendParagraph(Unknown Source)
at com.aspose.pdf.TextBuilder.appendParagraph(Unknown Source)
at com.aspose.pdf.z148.m1(Unknown Source)
at com.aspose.pdf.z148.m8(Unknown Source)
at com.aspose.pdf.Page.m2(Unknown Source)
at com.aspose.pdf.Page.m8(Unknown Source)
at com.aspose.pdf.ADocument.processParagraphs(Unknown Source)
at com.aspose.pdf.Document.processParagraphs(Unknown Source)
at com.aspose.pdf.ADocument.saveInternal(Unknown Source)
at com.aspose.pdf.Document.saveInternal(Unknown Source)
at com.aspose.pdf.ADocument.save(Unknown Source)
at com.aspose.pdf.Document.save(Unknown Source)

Hi Ritesh,


We are sorry for any misunderstanding. As per my understanding according to your above shared requirements, you want to create a PDF document using Aspose.Pdf for Java with a font that is not available on your system. I am afraid it is not possible, the referenced font must available on host machine.

However if there is any difference in your requirements and my understanding then please share some more details and your sample code, so I will look into it and will guide you accordingly.

Best Regards,