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

Free Support Forum - aspose.com

Getting JPEG image to fit in whole PDF page during conversion


I am having problem when converting images specifivally JPEG (in binary format) to PDF. The approach I am using here is by converting an image binary string to Memory Stream then convert them to PDF.

Just wanted to clarify here that this JPEG has previously been converted from PDF which I suppose has the same page ratio as a PDF one. (I have my reason to convert it to JPEG first then to PDF but not straight from Words to PDF).

When I attempt to convert this JPEG image. Then the resultant PDF does not take the image as a whole page, in fact, it is treated like an image embeded in PDF instead, which the image just concentrate at the middle of the PDF page. What should I do in order to get the whole image to fit a full PDF page? Here I share my code snippet for the conversion and the resultant PDF in attachment. Please advice. Thank you.

Aspose.Pdf.Generator.Pdf pdf1 = new Aspose.Pdf.Generator.Pdf();
MemoryStream ms = new MemoryStream();
String attmimetype = “”;
//query to retrieve binary content and mimetype
String Sql = String.Format(“select Content, mimetype from SourceTable where SourceId = ‘{0}’”, Guid);

using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[“ConnStr”].ConnectionString))
SqlDataReader myReader = null;
SqlCommand myCommand = new SqlCommand(Sql, conn);
myReader = myCommand.ExecuteReader();
while (myReader.Read())
attmimetype = myReader[“mimetype”].ToString();
Aspose.Pdf.Generator.Section sec1 = pdf1.Sections.Add();
Aspose.Pdf.Generator.Image image1 = new Aspose.Pdf.Generator.Image(sec1);

switch (attmimetype)
case “image/jpeg”:
image1.ImageInfo.ImageFileType = Aspose.Pdf.Generator.ImageFileType.Jpeg;

case “image/gif”:
image1.ImageInfo.ImageFileType = Aspose.Pdf.Generator.ImageFileType.Gif;

case “image/png”:
image1.ImageInfo.ImageFileType = Aspose.Pdf.Generator.ImageFileType.Png;

case “image/bmp”:
image1.ImageInfo.ImageFileType = Aspose.Pdf.Generator.ImageFileType.Bmp;
image1.ImageInfo.FixWidth = sec1.PageInfo.PageWidth - sec1.PageInfo.Margin.Left - sec1.PageInfo.Margin.Right;
ms = new MemoryStream((byte[])myReader[“Content”]);
image1.ImageInfo.ImageStream = ms;
String OutputPath = “C:\output.pdf”;

Hi ShiJien,

Thanks for your inquiry. Please set the section margin to zero, it will help you to accomplish the task.

// Set margins so
image will fit, etc.

sec1.PageInfo.Margin.Top = 5;

sec1.PageInfo.Margin.Bottom = 5;

sec1.PageInfo.Margin.Left = 5;

sec1.PageInfo.Margin.Right = 5;

Please feel free to contact us for any further assistance.

Best Regards,