Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Wed Jul 29, 2020 7:53 am

Hello,
I need to amend an existing PDF document.
When I like to add new texts, I am unable to use a font already existing in current PDF.
Unfortunately your documentation is lacking any info here, as it only consists of some tutorials and sample code.

I can see a lot of PdfUsedFonts in UsedFonts Property of document (all the same btw). But I cannot find any way to use one of these to draw text with given font.
I've tried to do it the way your code sample suggests:
Code: Select all
//True type font - embedded
            System.Drawing.Font font = new System.Drawing.Font("CUSTOMFONTNAME", 15f, FontStyle.Bold);
            PdfTrueTypeFont trueTypeFont = new PdfTrueTypeFont(font);


I tried to figure the name by getting name from FindAllText
Code: Select all
doc.Pages[0].FindAllText().Finds[0].OriginalFontName
and use it as name in code above.

I tried to use name derived from UsedFonts:
Code: Select all
doc.UsedFonts[0].Name


Nothing works, could you kindly provide a working sample? Or is it impossible, which would render your product really useless for us.

regards
J.

jseibert
 
Posts: 11
Joined: Wed Jul 29, 2020 7:36 am

Wed Jul 29, 2020 9:59 am

Hello,

Thanks for your inquiry.
I tested the following code with the latest Spire.PDF Pack(Hot Fix) Version:6.7.12 but did not encounter the issue you mentioned. Attached are my test file and output file for your better reference.
Code: Select all
        Spire.Pdf.PdfDocument doc = new PdfDocument("font.pdf");
        string name = doc.UsedFonts[0].Name;
        PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font(name, 15f, FontStyle.Bold));
        doc.Pages[0].Canvas.DrawString("Hello", font1, new PdfSolidBrush(Color.Black), 60, 300);
        doc.SaveToFile("out.pdf");

If you are using an older version, please try again with the latest version. If there are still issues, please provide your input file as well as the output you expect to help us investigate further. You could upload them here or send them to us(support@e-iceblue.com) via email. Thanks in advance.

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Wed Jul 29, 2020 1:21 pm

Sorry, it still doe not work. I've used V6.7.8, but even after updating to 6.7.12, result is still not correct.

I am not quite sure if it will work that way, you are creating a System.Drawing.Font, will this be able to detect fonts in PDF? (just wondering)
SystemDrawingFont.png

We are using a custom, non system font, which only exists in PDF.
Output.png


When we load the font it is rendered correctly, but we'd like to not go that way.
Code: Select all
      var font = new Font(doc.UsedFonts[0].Name, 15f, FontStyle.Regular);
      PdfTrueTypeFont trueTypeFont = new PdfTrueTypeFont(Path.Combine(basePath, "CustomFontType-Medium.otf"), 12f);
      PdfTrueTypeFont trueTypeFont2 = new PdfTrueTypeFont(font, 12f);

jseibert
 
Posts: 11
Joined: Wed Jul 29, 2020 7:36 am

Thu Jul 30, 2020 9:19 am

Hello,

Thanks for your feedback.
You need to install the font used in the PDF document on your system first, otherwise, if the corresponding font cannot be found in the system when creating a System.Drawing.Font, it will use another installed font to draw the text, just as shown in your attached picture(SystemDrawingFont.png). Hope you can understand.
If there are still issues after installing the fonts, please provide your PDF file and your font file for further investigation.

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Thu Jul 30, 2020 8:25 pm

Well, yes, but I would suggest you better amend your tutorial code, where it shows how to use an embedded font:
github.com Spire.PDF-for-.NET blob/master/CS/02_Text/Font.cs line 47

Anyways, we've gone the route of using font files and load them with concerning PdfTrueTypeFont ctor.

Thanks four your explanation
J.

jseibert
 
Posts: 11
Joined: Wed Jul 29, 2020 7:36 am

Fri Jul 31, 2020 5:56 am

Hello,

Thanks for your reply.
Regarding "embedded" mentioned in our tutorial code, it actually refers to embedding fonts into the PDF document, as shown below. We are sorry for the misunderstanding. If you have any other questions, please feel free to let us know.
embed font.png


Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Return to Spire.PDF