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.

Mon Jun 28, 2021 7:22 am

Is there a way to automatically insert a line break at the end of the PDF document? If I have a 200 character text and I write it in a newly created PDF, the text is written beyond the margin.

Attached is my code:

The aim is simply to write the text freely written by the user into a PDF.


Code: Select all
Public Sub createMsgPDF(ByVal text As String)

        Dim tmpPath = "C:\temp\msgtest.pdf"

        Dim pdfdoc As New Spire.Pdf.PdfDocument
        Dim page As Spire.Pdf.PdfPageBase = pdfdoc.Pages.Add()

        Dim font As Spire.Pdf.Graphics.PdfFont = New Spire.Pdf.Graphics.PdfFont(Spire.Pdf.Graphics.PdfFontFamily.Helvetica, 10.0F)
        Dim brush As Spire.Pdf.Graphics.PdfSolidBrush = Spire.Pdf.Graphics.PdfBrushes.Black

        page.Canvas.DrawString(text, font, brush, 20, 20)

        pdfdoc.SaveToFile(tmpPath)

    End Sub


hammes_it
 
Posts: 13
Joined: Wed Nov 13, 2019 11:47 am

Mon Jun 28, 2021 10:25 am

Hello,

Thank you for your inquiry.
Please refer to the following code to achieve your requirement. If there is any questions, please feel free to contact us.
Code: Select all
    Public Shared Sub createMsgPDF(ByVal text As String)
        Dim tmpPath As String = "C:\temp\msgtest.pdf"
        Dim pdfdoc As Spire.Pdf.PdfDocument = New Spire.Pdf.PdfDocument
        Dim page As Spire.Pdf.PdfPageBase = pdfdoc.Pages.Add
        ' Get width and height of page
        Dim pageWidth As Single = page.GetClientSize.Width
        Dim pageHeight As Single = page.GetClientSize.Height
        Dim font As PdfFont = New PdfFont(PdfFontFamily.Helvetica, 10!)
        Dim brush As PdfSolidBrush = New PdfSolidBrush(Color.Black)
        Dim format As PdfStringFormat = New PdfStringFormat(PdfTextAlignment.Justify)
        page.Canvas.DrawString(text, font, brush, New RectangleF(0, 20, pageWidth, pageHeight), format)
        pdfdoc.SaveToFile(tmpPath)
    End Sub

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Fri Jul 02, 2021 7:17 am

Hello,

Hope you doing well.
How is your issue going? Can you give us some feedback at your convenience? Thanks in advance.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.PDF