Results 1 to 4 of 4

Thread: [RESOLVED] Centre Paragraph Text using ItextSharp

  1. #1

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Resolved [RESOLVED] Centre Paragraph Text using ItextSharp

    I have been trying to centre text unsuccessfully for 2 days now so unfortunately i now need help from the pros.

    I am using below example found in a youtube video to create a pdf file with text and image for a report app i am working on.

    I can centre a image but no luck with cantering text horizontally in a pdf document.

    I am thinking i may have to place text in a rectangle then possibly centre but not sure so thought i would ask here first.

    ps, i am aware library is redundant but itext7 looks so confusing.

    Any help or example much appreciated, thankyou



    Code:
            Dim pdfDoc As New iTextSharp.text.Document
            Dim Sstring As String = DdirName
            Dim DirToSavePdfTo As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\weights"
            Dim DIR_String As String = DirToSavePdfTo & "\" & Sstring & "\" & Sstring & " Weight Report.pdf"
            Dim pdfWrite As PdfWriter = PdfWriter.GetInstance(pdfDoc, New FileStream(DIR_String, FileMode.Create))
            pdfDoc.Open()
            pdfDoc.Add(New iTextSharp.text.Paragraph("Some text"))
            pdfDoc.Add(New iTextSharp.text.Paragraph(" "))
            'date and time
            Dim t As Date = DateTime.Now.ToString()
            pdfDoc.Add(New iTextSharp.text.Paragraph(t))
            pdfDoc.Add(New iTextSharp.text.Paragraph(" "))
            Dim RreportName As String = "Vehicle rego " & TxtVehicleRego.Text & " Van rego " & TxtVanRego.Text & " Report.pdf"
            pdfDoc.Add(New iTextSharp.text.Paragraph(RreportName))
            pdfDoc.Add(New iTextSharp.text.Paragraph(" "))

  2. #2
    Fanatic Member
    Join Date
    Jul 2022
    Location
    Buford, Ga USA
    Posts
    631

    Re: Centre Paragraph Text using ItextSharp

    I used a Paragraph object
    Code:
    Dim pdfParagraph As iTextSharp.text.Paragraph
    and then set the alignment of that object , have you tried this approach?

    Code:
            pdfDoc.Open()
    
            pdfParagraph = New iTextSharp.text.Paragraph
            pdfParagraph.Alignment = 1
            pdfParagraph.Add("Some Text")
    
            pdfDoc.Add(pdfParagraph)
    
            pdfParagraph = Nothing
    
            pdfDoc.Close()
            pdfDoc.Dispose()

  3. #3

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Re: Centre Paragraph Text using ItextSharp

    excellent, thankyou

  4. #4
    Fanatic Member
    Join Date
    Jul 2022
    Location
    Buford, Ga USA
    Posts
    631

    Re: [RESOLVED] Centre Paragraph Text using ItextSharp

    You're welcome, happy to help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width