John
John

Reputation: 33

How to absolute position the image in existing pdf using itextsharp

Here is the code I have so far:

Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports System.IO

Module Module1

    Sub Main()
        AddjImage("C:\test.png", "c:\pdfTemplate.pdf", "C:\output.pdf")
    End Sub
    Private Function AddjImage(ByVal strImageFileName As String, ByVal pdfTemplateFile As String, ByVal outputPdf As String) As Boolean
        Try
            Dim iPdfReader As PdfReader = New PdfReader(pdfTemplateFile)
            Dim iPdfStamper As PdfStamper = New PdfStamper(iPdfReader, New FileStream(outputPdf, FileMode.Create))
            Dim imgjImage As iTextSharp.text.Image
            Dim bytContent As PdfContentByte
            'Insert Image
            imgjImage = iTextSharp.text.Image.GetInstance(strImageFileName)
            imgjImage.Alignment = iTextSharp.text.Image.ALIGN_TOP
            imgjImage.ScalePercent(78)
            imgjImage.SetAbsolutePosition(445, 0)
            bytContent = iPdfStamper.GetOverContent(1)
            bytContent.AddImage(imgjImage)
            iPdfStamper.FormFlattening = True
            iPdfStamper.Close()
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function    
End Module

The pdf is in landscape layout. The page size is A4. I am trying to insert the image on right side of the pdf page. I want to align the image on x=445 and y=0 position.

I have couple of images with two sizes. They are: image 1 with width=500px; height=910px; image 2 with width=500px; height=400px;

The problem is, both the images are aligned to bottom instead of top. because of that the top portion of image 1 is cut off.

Upvotes: 1

Views: 5245

Answers (1)

user1744515
user1744515

Reputation: 1

I tried your code(with modifications) to suit my button click event in a wpf app. The line below has to be altered to make the image go up. I feel the 0 you are using starts from bottom.

imgjImage.SetAbsolutePosition(445, 0)

to be altered to

imgjImage.SetAbsolutePosition(445, 200)

the 200 is not absolute, it has to be readjusted for your image actual size.

Upvotes: 1

Related Questions