BMP to PDF - Distorted output file

Hi, I am trying to convert a BMP image stored in database in byte array format to PDF, then the resultant PDF seems totally distorted and unreadable. Here’s my approach of doing the conversion with C#. I attached the source and the output file here for your reference. Please assist, thank you. Your prompt response is greatly appreciated.


Aspose.Pdf.Generator.Pdf pdf1 = new Aspose.Pdf.Generator.Pdf();
MemoryStream ms = new MemoryStream();

using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[“ConnStr”].ConnectionString))
{
SqlDataReader myReader = null;
conn.Open();
SqlCommand myCommand = new SqlCommand(Sql, conn);
myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
Aspose.Pdf.Generator.Section sec1 = pdf1.Sections.Add();
sec1.PageInfo.Margin.Top = 0;
sec1.PageInfo.Margin.Bottom = 0;
sec1.PageInfo.Margin.Left = 0;
sec1.PageInfo.Margin.Right = 0;
Aspose.Pdf.Generator.Image image1 = new Aspose.Pdf.Generator.Image(sec1);
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;
sec1.Paragraphs.Add(image1);
}
}
pdf1.Save(@“C:\Output.pdf”);
ms.Close();

Hi ShiJien,


Thanks for your inquiry. Please use new DOM approach for Image to PDF conversion, It will help you to accomplish the task. please check sample code snippet as following.

string outFile = myDir + “ImagetoPDFDOM.pdf”;<o:p></o:p>

string inFile = myDir + "testimage (2).bmp";

FileStream fs = new FileStream(inFile, FileMode.Open, FileAccess.Read);

byte[] tmpBytes = new byte[fs.Length];

fs.Read(tmpBytes, 0, Convert.ToInt32(fs.Length));

MemoryStream mystream = new MemoryStream(tmpBytes);

Document doc = new Document();

Page page = doc.Pages.Add();

// Set margins so image will fit, etc.

page.PageInfo.Margin.Bottom = 0;

page.PageInfo.Margin.Top = 0;

page.PageInfo.Margin.Left = 0;

page.PageInfo.Margin.Right = 0;

page.PageInfo.Height = Aspose.Pdf.PageSize.A4.Height;

page.PageInfo.Width = Aspose.Pdf.PageSize.A4.Width;

//Create an image object

Aspose.Pdf.Image image1 = new Aspose.Pdf.Image();

//Add the image into paragraphs collection of the section

page.Paragraphs.Add(image1);

//Set the ImageStream to a MemoryStream object

image1.ImageStream = mystream;

doc.Save(outFile);

Please feel free to contact us for any further assistance.


Best Regards,

Hi,


I’m glad that this DOM approach that you suggested works fine. Thank you for the help, and may I know if the same problem might occur again when I am trying to convert .bmp images to .pdf in the future? It’s because that previously, I don’t encounter any of these conversion problem until I am trying to convert this particular file that I attached on the previous post. Thanks.

Hi ShiJien,


Thanks for sharing the feedback.

We are glad to hear that your problem is resolved. Please note that we cannot guarantee and 100% support for all type of BMP (raster images) to PDF conversion without any issue, but sometimes due to structure of any particular file, the conversion encounters some issue.

We look forward to continuing serving you and hope that you will continue using our API’s. If you have any further questions or require further information, please do not hesitate to contact us.