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 Nov 13, 2019 2:10 am

I am trying to create a PDF that has multiple types of fonts. One type of font will be "Garamond". How would I be able to use this and other types of fonts?

metrofuse
 
Posts: 4
Joined: Mon Nov 11, 2019 4:58 pm

Wed Nov 13, 2019 2:44 am

Hi,

Thanks for your inquiry.
Please refer to the code below to create a PDF file with multiple types of fonts. And I have attached my result PDF file here, please check it.

Code: Select all
            //Create a pdf document.
            PdfDocument doc = new PdfDocument();
            PdfPageBase page = doc.Pages.Add(PdfPageSize.A4);

            //Draw the first string with the first type of font “Garamond”.
            string s1 = "Spire.PDF owns the ability of drawing text in PDF document. It supports text formatting, multilingual and text extraction. Here we will introduce how to draw text in PDF with different styles. ";
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Garamond", 18f, FontStyle.Regular));
            PdfSolidBrush brush1 = new PdfSolidBrush(Color.DeepSkyBlue);
            RectangleF re = new RectangleF(0, 20, page.GetClientSize().Width, page.GetClientSize().Height);
            page.Canvas.DrawString(s1, font, brush1, re);

            //Draw the second string with the second type of font.
            string s2 = "Spire.PDF enables users to draw rotate text in PDF, draw transform text in PDF, draw alignment text in PDF, draw alignment text in rectangle and other style settings such as font, size, color, pen, etc.";
            PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("TimesRoman", 12f, FontStyle.Italic));
            PdfSolidBrush brush2 = new PdfSolidBrush(Color.Red);
            RectangleF re1 = new RectangleF(0, 100, page.GetClientSize().Width, page.GetClientSize().Height);
            page.Canvas.DrawString(s2, font1, brush2, re1);

            //Save to PDF file.
            doc.SaveToFile("result.pdf", FileFormat.PDF);


If the code couldn't solve your issue, please offer us your desired result file for further investigation. You could upload it here or send us([email protected]) via email.

Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Thu Nov 14, 2019 2:49 pm

I am so sorry but I forgot to mention that I am programming in VB.NET. Do you have a VB.NET example?

Sorry again for the mistake.

metrofuse
 
Posts: 4
Joined: Mon Nov 11, 2019 4:58 pm

Fri Nov 15, 2019 2:12 am

Hi,

Thanks for your reply.
Below is the sample code in VB.Net for you.
Code: Select all
        Dim doc As PdfDocument = New PdfDocument
        Dim page As PdfPageBase = doc.Pages.Add(PdfPageSize.A4)

        'Draw the first string with the first type of font “Garamond”.
        Dim s1 As String = "Spire.PDF owns the ability of drawing text in PDF document. It supports text formatting, multilingual and text extraction. Here we will introduce how to draw text in PDF with different styles. "
        Dim font As PdfTrueTypeFont = New PdfTrueTypeFont(New Font("Garamond", 18.0F, FontStyle.Regular))
        Dim brush1 As PdfSolidBrush = New PdfSolidBrush(Color.DeepSkyBlue)
        Dim re As RectangleF = New RectangleF(0, 20, page.GetClientSize.Width, page.GetClientSize.Height)
        page.Canvas.DrawString(s1, font, brush1, re)

        'Draw the second string with the second type of font.
        Dim s2 As String = "Spire.PDF enables users to draw rotate text in PDF, draw transform text in PDF, draw alignment text in PDF, draw alignment text in rectangle and other style settings such as font, size, color, pen, etc."
        Dim font1 As PdfTrueTypeFont = New PdfTrueTypeFont(New Font("TimesRoman", 12.0F, FontStyle.Italic))
        Dim brush2 As PdfSolidBrush = New PdfSolidBrush(Color.Red)
        Dim re1 As RectangleF = New RectangleF(0, 100, page.GetClientSize.Width, page.GetClientSize.Height)
        page.Canvas.DrawString(s2, font1, brush2, re1)

        'Save to Pdf file.
        doc.SaveToFile("result.pdf", FileFormat.PDF)


Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Return to Spire.PDF