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 Feb 14, 2018 1:04 am

hi,
I using Spire.PDF Professional.
When I looked up the documentation, I could only use a few system fonts when converting text to pdf (Helvetica, Times Roman...)

ex)
PdfFont font = new PdfFont(PdfFontFamily.Courier, 10);

Can I use custom fonts when I convert text to PDF?

ex) WingDings, Verdana, ....

cspi
 
Posts: 5
Joined: Tue Nov 21, 2017 2:18 am

Wed Feb 14, 2018 3:38 am

Hello,

Thanks for your inquiry.
Yes, you can use the custom font while converting text to Pdf. Below is sample code for your kind reference. If there is any confusion, welcome to write back.
Code: Select all
string text = File.ReadAllText("Sample.txt");
PdfDocument doc = new PdfDocument();
PdfSection section = doc.Sections.Add();
PdfPageBase page = section.Pages.Add();

//If your system has the fonts(WingDing,Verdana...) installed, please directly use below method to specify the font name.
System.Drawing.Font font = new System.Drawing.Font("Wingdings", 14f);
PdfTrueTypeFont trueTypeFont = new PdfTrueTypeFont(font,true);

//If your system doesn't install the fonts, please use below method to specify the font file path.
//String fontFileName = "C:\\Wingdings.ttf";
//PdfTrueTypeFont trueTypeFont = new PdfTrueTypeFont(fontFileName, 14f);

PdfStringFormat format = new PdfStringFormat();
format.LineSpacing = 20f;
PdfBrush brush = PdfBrushes.Black;
PdfTextWidget textWidget = new PdfTextWidget(text, trueTypeFont, brush);
float y = 20;
PdfTextLayout textLayout = new PdfTextLayout();
textLayout.Break = PdfLayoutBreakType.FitPage;
textLayout.Layout = PdfLayoutType.Paginate;
RectangleF bounds = new RectangleF(new PointF(0, y), page.Canvas.ClientSize);
textWidget.StringFormat = format;
textWidget.Draw(page, bounds, textLayout);
doc.SaveToFile("TxtToPDf.pdf", FileFormat.PDF);


Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1187
Joined: Tue Sep 27, 2016 1:06 am

Wed Feb 14, 2018 5:13 am

I did not realize that this was possible. thanks.

cspi
 
Posts: 5
Joined: Tue Nov 21, 2017 2:18 am

Wed Feb 14, 2018 5:47 am

Hello,

Thanks for your prompt feedback.
If you need further assistance, please feel free to contact us.

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1187
Joined: Tue Sep 27, 2016 1:06 am

Return to Spire.PDF