We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Image File to PDF

Just installed Aspose.pdf.net today and my first task is to convert a selected image file to a PDF. Using the sample and SDK, I can't seem to get it working. It seems to fail (no error message) in getting a reference to the image file. However, I am using pretty much the exact code at this point just to get a demo working.

Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click

Dim sFileName As String

Dim sFileExt As String

Dim sFinalFile As String

sFileName = "aspose-logo.jpg"

sFileExt = ".jpg"

Dim license As Aspose.Pdf.License = New Aspose.Pdf.License

Dim myStream As System.IO.FileStream = New System.IO.FileStream(Server.MapPath("~/bin/Aspose.Total.lic"), System.IO.FileMode.Open)

license.SetLicense(myStream)

Dim pdf1 As Pdf = New Pdf()

Dim sec1 As Section = pdf1.Sections.Add()

Dim image1 As Aspose.Pdf.Image = New Aspose.Pdf.Image(sec1)

Dim img As New Image

img.ImageInfo.File = "http://www.aspose.com/Images/aspose-logo.jpg"

img.ImageInfo.ImageFileType = ImageFileType.Jpeg

img.ImageInfo.Title = "JPEG Image"

sec1.Paragraphs.Add(image1)

sFinalFile = Server.MapPath("~/FILES/" & Replace(sFileName, sFileExt, "") & ".pdf")

pdf1.Save(sFinalFile)

End Sub

Hello Matthew,

Thanks for using our products.

As far as I can see from the code snippet that you have shared, you have created 2 image objects i.e. image1 and img. However, you have only given the file path information for img object and adding image1 to paragraphs collection of Section object. Please try using the following correct code snippet to convert the image file into PDF format.

[VB.NET]

Dim pdf1 As Pdf = New Pdf()
Dim sec1 As Aspose.Pdf.Section = pdf1.Sections.Add()

Dim image1 As Aspose.Pdf.Image = New Aspose.Pdf.Image(sec1)
image1.ImageInfo.File = http://www.aspose.com/Images/aspose-logo.jpg
image1.ImageInfo.ImageFileType = ImageFileType.Jpeg
image1.ImageInfo.Title = "JPEG Image"

sec1.Paragraphs.Add(image1)
pdf1.Save("d:/pdftest/ImageFileIssue.pdf")

The resultant PDF that I have generated using Aspose.Pdf for .NET 4.5.0 is also in attachment, please take a look.

Perfect, not sure how I ended up with two images.

Thanks