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.

Thu Apr 23, 2020 12:43 am

Hello .. Good evening!
I am using the following code to convert images to pdf:

Dim doc As New PdfDocument()
Dim section As PdfSection = doc.Sections.Add
Dim page As PdfPageBase = doc.Pages.Add
Dim image As PdfImage = PdfImage.FromFile(inputDoc)
Dim widthFitRate As Single = image.PhysicalDimension.Width / page.Canvas.ClientSize.Width
Dim heightFitRate As Single = image.PhysicalDimension.Height / page.Canvas.ClientSize.Height
Dim fitRate As Single = Math.Max(widthFitRate, heightFitRate)
Dim fitWidth As Single = image.PhysicalDimension.Width / fitRate
Dim fitHeight As Single = image.PhysicalDimension.Height / fitRate
page.Canvas.DrawImage(image, 30, 30, fitWidth, fitHeight)
doc.SaveToFile(TempDir & "\" & IO.Path.GetFileNameWithoutExtension(inputDoc) & ".pdf")
doc.Close()

However, it is generating a pdf with the original image in the center of the page.
What modification is necessary to generate a pdf page with the same measurements (width and height) as the original image?
Thank you in advance!

gafforelli
 
Posts: 37
Joined: Sat Sep 15, 2018 6:38 pm

Thu Apr 23, 2020 3:27 am

Hello,

Thanks for your inquiry.
Please refer to the following code to achieve your requirements. If there is any question, please get back to us.
Code: Select all
        Dim doc As PdfDocument = New PdfDocument
        Dim section As PdfSection = doc.Sections.Add
        Dim image As PdfImage = PdfImage.FromFile("Logo.png")
        Dim width As Single = image.Width
        Dim height As Single = image.Height
        Dim margins As PdfMargins = New PdfMargins(0, 0)
        Dim page As PdfPageBase = doc.Pages.Add(New SizeF(width, height), margins)
        page.Canvas.DrawImage(image, 0, 0, width, height)
        doc.SaveToFile("out.pdf")
        doc.Close()


Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Tue Apr 28, 2020 8:12 am

Hello,

Greetings from E-iceblue.
Does my code help you? Thanks in advance for your feedback.

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Sat May 02, 2020 12:55 pm

Hello Rachel, good morning!
I apologize, but forgot to give feedback.
Worked perfectly.
Thank you very much again for the quick return.

gafforelli
 
Posts: 37
Joined: Sat Sep 15, 2018 6:38 pm

Mon May 04, 2020 1:02 am

Hello,

Thanks for your feedback.
Feel free to contact us if you have any other questions.

Sincerely,
Rachel
E-icbelue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Return to Spire.PDF