Search code examples
vb.netitextdrawrectangle

How to Modify a iTextSharp.text.Rectangle Top left position in VB?


I am Using itextsharp dll in VB to generate a PDF. In the PDF, need to increase space between rectangle and text. I tried to setmargin and changed the rectangle coordinates, but the space between top rectangle and text is not creating. Please see the attachment. Below is the code i am using to generate PDF

                Dim reader As PdfReader = New PdfReader(tempFile)
                Dim size As Rectangle = reader.GetPageSize(1)
                Dim AcroAVDoc As Document = New Document(iTextSharp.text.PageSize.A4.Rotate())
                AcroAVDoc.SetMargins(0, 0, 0, 0)
                Dim FS As FileStream = New FileStream(newFile, FileMode.Create, FileAccess.Write)
                Dim writer As PdfWriter = PdfWriter.GetInstance(AcroAVDoc, FS)
                AcroAVDoc.Open()
                If (SaveDoc) Then
                   
                    Dim cb As PdfContentByte = writer.DirectContent

                    Dim bf As BaseFont = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED)
                    cb.SetColorFill(BaseColor.BLACK)
                    cb.SetFontAndSize(bf, 11)

                    cb.BeginText()
                    Dim text As String = "HeaderText  " + TableData(HeaderText)
                    ''put the alignment And coordinates here
                    cb.SetTextMatrix(240, 583)
                    cb.ShowText(text)
                    cb.EndText()

                    ''create the New page And add it to the pdf
                    Dim Page As PdfImportedPage = writer.GetImportedPage(reader, 1)
                    Dim psize As Rectangle = reader.GetPageSizeWithRotation(1)

                    cb.AddTemplate(Page, 0, -1.0F, 1.0F, 0, 0, psize.Height)

                    'AcroAVDoc.Save(1, newFile)
                End If
                AcroAVDoc.Close()
                FS.Close()
                writer.Close()
                reader.Close()

enter image description here

enter image description here

Need to decrease Right space


Solution

  • What your code does (if SaveDoc is true) is

    • creating a new PDF file in newFile with landscape A4 page size,
    • drawing the string text on its first page, and
    • drawing the rotated static contents of the first page of the PDF in tempFile on the same first page.

    Thus, to modify the position of the text relative to content copied from the other file, you have to adjust the position where you draw the text and/or the position where you draw the imported content.

    You set the position of the text you draw by setting the text matrix (translation coordinates only):

    cb.SetTextMatrix(240, 583)
    

    You set the position of the imported page content you draw by setting the transformation matrix (full matrix):

    cb.AddTemplate(Page, 0, -1.0F, 1.0F, 0, 0, psize.Height)
    

    The second through fifth parameter (0, -1.0F, 1.0F, 0) define a clockwise rotation by 90° around the bottom left corner and the sixth and seventh parameter (0, psize.Height) define an upwards translation. As the rotation around the bottom left corner rotates the content out of the visible page area, that upwards translation is needed to move it back into view.

    To tweak the position of the text in relation to the imported page, simply experiment with changing the 240, 583 for the text and/or the 0, psize.Height for the imported page a bit (e.g. plus or minus 10) until it matches your expectation.