Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
3.63/5 (4 votes)
See more:
Hi,

I found a C# source code for converting TIFF to PDF and I convert it to VB.NET.
VB
Dim doc As New Document(PageSize.A4, 0, 0, 0, 0)
Dim writer As PdfWriter = PdfWriter.GetInstance(doc, New FileStream(pdfPath, FileMode.Create))
Dim bmp As New Bitmap(tiffPath)
Dim pages As Integer = bmp.GetFrameCount(Imaging.FrameDimension.Page)
doc.Open()
Dim cb As PdfContentByte = writer.DirectContent
For i As Integer = 0 To pages - 1
    bmp.SelectActiveFrame(Imaging.FrameDimension.Page, i)
    Dim img As Image = Image.GetInstance(bmp, Imaging.ImageFormat.Bmp)

    img.ScalePercent(72.0F / img.DpiX * 100)
    img.SetAbsolutePosition(0, 0)
    cb.AddImage(img)
    doc.NewPage()
Next i
doc.Close()

The above code is running fine, my problem is I don't know how to make the image fit to A4 page size. The image is always in portrait but bigger than A4 size.

Please Help... Thanks
Posted
Updated 20-Dec-13 0:52am
v5
Comments
Maciej Los 19-Dec-13 15:11pm    
Interesting question. A5!
Sergey Alexandrovich Kryukov 19-Dec-13 15:41pm    
What is interesting? OP has 72% scaling. OP does not tell us that it does not scale at all/ Don't you think that different percentage would make the image smaller? :-)
—SA
Maciej Los 19-Dec-13 15:53pm    
I think, there is no problem with changing the value of scaling. A problem is: how to detect image scaling to fit it into A4 page.
Sergey Alexandrovich Kryukov 19-Dec-13 23:05pm    
Oh, you are right.
—SA
Maciej Los 20-Dec-13 1:39am    
;)

1 solution

Have you tried:

var image = iTextSharp.text.Image.GetInstance(page, ImageFormat.Png);
image.SetAbsolutePosition(0, 0);
image.ScaleAbsoluteHeight(_doc.PageSize.Height);
image.ScaleAbsoluteWidth(_doc.PageSize.Width);
_directContent.AddImage(image);
            
_doc.NewPage();


It works well for me.
 
Share this answer
 
Comments
NBhalodiya 23-Dec-19 1:47am    
Thanks its also works for me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900