none
How to set different fonts for a pdf files? RRS feed

  • Question

  • Hello everyone,

    :) I have a PDF files need to add some content and set different font for different paragraph(or other break of content). Then how to do that?

    Thank u.

    ps.I use VB.



    • Edited by Floraaa Wednesday, May 6, 2015 8:03 AM VB
    Wednesday, May 6, 2015 8:00 AM

Answers

  • When you create the "Arial" font at the first time, it will create the font cache. And when you create another "Arial" font in different size, it will take the font object from the cache and change the font cache size to the new size. That's the reason why you got the same font style in the first four paragraphs.


    To prevent it, you must use "Arial" font before another "Arial" font is created. Here is the code snippet you can refer to :
    Dim pdf As New PdfDocument()
    Dim page As PdfPageBase = pdf.Pages.Add()
    Dim font As String = "Arial"
    Dim x As Integer = 100
    Dim y As Integer = 100
    Dim BlackBrush As PdfBrush = PdfBrushes.Black
    Dim SmallestFont As New PdfTrueTypeFont(New Font(font, 5F, FontStyle.Regular))
    page.Canvas.DrawString("Spire 3.2.106 Test Application", SmallestFont, BlackBrush, x, y)
    y += CInt(SmallestFont.Height) + 5
    
    Dim SmallFont As New PdfTrueTypeFont(New Font(font, 7F, FontStyle.Regular))
    page.Canvas.DrawString("Spire 3.2.106 Test Application", SmallFont, BlackBrush, x, y)
    y += CInt(SmallFont.MeasureString("Spire 3.2.106 Test Application").Height) + 5
    
    Dim ContentFont As New PdfTrueTypeFont(New Font(font, 10F, FontStyle.Regular))
    page.Canvas.DrawString("Spire 3.2.106 Test Application", ContentFont, BlackBrush, x, y)
    y += CInt(SmallFont.MeasureString("Spire 3.2.106 Test Application").Height) + 5
    
    Dim HeadingFont As New PdfTrueTypeFont(New Font(font, 20F, FontStyle.Regular))
    page.Canvas.DrawString("Spire 3.2.106 Test Application", HeadingFont, BlackBrush, x, y)
    y += CInt(SmallFont.MeasureString("Spire 3.2.106 Test Application").Height) + 5
    
    Dim HeaderFont As New PdfTrueTypeFont(New Font(font, 30F, FontStyle.Regular))
    page.Canvas.DrawString("Spire 3.2.106 Test Application", HeaderFont, BlackBrush, x, y)
    y += CInt(SmallFont.MeasureString("Spire 3.2.106 Test Application").Height) + 5
    y = y + 16
    Dim trueTypeFont As New PdfTrueTypeFont(New Font(font, 50F, FontStyle.Regular))
    page.Canvas.DrawString("Spire 3.2.106 Test Application", trueTypeFont, BlackBrush, x, y)
    
    pdf.SaveToFile("Test.pdf")
    Process.Start("Test.pdf")

    Wednesday, May 6, 2015 8:14 AM

All replies

  • You can try this PDF library,and download the hotfix, and add to your project then try the following code.

    Dim pdf As New PdfDocument
            Dim page As PdfPageBase = pdf.Pages.Add()
    
            Dim font As String = "Arial"
    
            Dim BlackBrush As PdfBrush = PdfBrushes.Black
            Dim SmallestFont As New PdfTrueTypeFont(New Font(font, 5.0F, FontStyle.Regular))
            Dim x As Integer = 100
            Dim SmallFont As PdfTrueTypeFont = New PdfTrueTypeFont(New Font(font, 7, FontStyle.Regular))
            Dim y As Integer = 100
            Dim ContentFont As PdfTrueTypeFont = New PdfTrueTypeFont(New Font(font, 8, FontStyle.Regular))
            Dim HeadingFont As PdfTrueTypeFont = New PdfTrueTypeFont(New Font(font, 9, FontStyle.Regular))
            Dim HeaderFont As PdfTrueTypeFont = New PdfTrueTypeFont(New Font(font, 12, FontStyle.Regular))
    
    
            page.Canvas.DrawString("Spire 3.2.106 Test Application", SmallestFont, BlackBrush, x, y)
            y += SmallestFont.Height + 5
    
            page.Canvas.DrawString("Spire 3.2.106 Test Application", SmallFont, BlackBrush, x, y)
            y += SmallFont.Height + 5
    
            page.Canvas.DrawString("Spire 3.2.106 Test Application", ContentFont, BlackBrush, x, y)
            y += ContentFont.Height + 5
    
            page.Canvas.DrawString("Spire 3.2.106 Test Application", HeadingFont, BlackBrush, x, y)
            y += HeadingFont.Height + 5
    
            page.Canvas.DrawString("Spire 3.2.106 Test Application", HeaderFont, BlackBrush, x, y)
            y += HeaderFont.Height + 5
    
            y = y + 16.0F
            Dim trueTypeFont As New PdfTrueTypeFont(New Font(font, 5.0F, FontStyle.Regular))
            page.Canvas.DrawString("Spire 3.2.106 Test Application", trueTypeFont, BlackBrush, x, y)
    
    
            pdf.SaveToFile("Test.pdf")
    
            Process.Start("Test.pdf")

    Wednesday, May 6, 2015 8:11 AM
  • Thanks a lot . But I tried your offered way,but can't run as I need . The latest font replace all the fonts before. Any better way?

    Wednesday, May 6, 2015 8:13 AM
  • When you create the "Arial" font at the first time, it will create the font cache. And when you create another "Arial" font in different size, it will take the font object from the cache and change the font cache size to the new size. That's the reason why you got the same font style in the first four paragraphs.


    To prevent it, you must use "Arial" font before another "Arial" font is created. Here is the code snippet you can refer to :
    Dim pdf As New PdfDocument()
    Dim page As PdfPageBase = pdf.Pages.Add()
    Dim font As String = "Arial"
    Dim x As Integer = 100
    Dim y As Integer = 100
    Dim BlackBrush As PdfBrush = PdfBrushes.Black
    Dim SmallestFont As New PdfTrueTypeFont(New Font(font, 5F, FontStyle.Regular))
    page.Canvas.DrawString("Spire 3.2.106 Test Application", SmallestFont, BlackBrush, x, y)
    y += CInt(SmallestFont.Height) + 5
    
    Dim SmallFont As New PdfTrueTypeFont(New Font(font, 7F, FontStyle.Regular))
    page.Canvas.DrawString("Spire 3.2.106 Test Application", SmallFont, BlackBrush, x, y)
    y += CInt(SmallFont.MeasureString("Spire 3.2.106 Test Application").Height) + 5
    
    Dim ContentFont As New PdfTrueTypeFont(New Font(font, 10F, FontStyle.Regular))
    page.Canvas.DrawString("Spire 3.2.106 Test Application", ContentFont, BlackBrush, x, y)
    y += CInt(SmallFont.MeasureString("Spire 3.2.106 Test Application").Height) + 5
    
    Dim HeadingFont As New PdfTrueTypeFont(New Font(font, 20F, FontStyle.Regular))
    page.Canvas.DrawString("Spire 3.2.106 Test Application", HeadingFont, BlackBrush, x, y)
    y += CInt(SmallFont.MeasureString("Spire 3.2.106 Test Application").Height) + 5
    
    Dim HeaderFont As New PdfTrueTypeFont(New Font(font, 30F, FontStyle.Regular))
    page.Canvas.DrawString("Spire 3.2.106 Test Application", HeaderFont, BlackBrush, x, y)
    y += CInt(SmallFont.MeasureString("Spire 3.2.106 Test Application").Height) + 5
    y = y + 16
    Dim trueTypeFont As New PdfTrueTypeFont(New Font(font, 50F, FontStyle.Regular))
    page.Canvas.DrawString("Spire 3.2.106 Test Application", trueTypeFont, BlackBrush, x, y)
    
    pdf.SaveToFile("Test.pdf")
    Process.Start("Test.pdf")

    Wednesday, May 6, 2015 8:14 AM
  • Ok .I tried and it seems working fine now. Use the font of specific part of text then add then following thing, thank u.
    Wednesday, May 6, 2015 8:16 AM
  • Hi Floraaa,

    With word 2013, we can convert a PDF into a word document and edit the content and save to PDF file.

    # Edit PDF content in Word

    https://support.office.com/en-us/article/Edit-PDF-content-in-Word-b2d1d729-6b79-499a-bcdb-233379c2f63a

    So we could change some text’s font and insert text.

    # Range.Font Property

    https://msdn.microsoft.com/en-us/library/office/ff821026.aspx

    # How to: Programmatically Insert Text into Word Documents

    https://msdn.microsoft.com/en-us/library/6b9478cs.aspx?f=255&MSPPError=-2147217396

    We could use Documents.Open method to open PDF through word.

    Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, May 7, 2015 6:12 AM
    Moderator