Reputation: 705
I am generating a byte array image and in order to convert it to pdf I am adding that image into a PDF document. the image size is exactly 812, 1015 DPI and even though I have document with the same size the image is being offset by about an inch (the red bar represents this offset) because of this I am missing about the same amount on the other side. Why is the PDF adding the image in this way. Here is the code:
var resizedImage = new Bitmap(812, 1015);
var drawResizedImage = Graphics.FromImage(resizedImage);
drawResizedImage.DrawImage(img, 0, 0, 812, 1015);
resizedImage.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
return getPDFDocument(memoryStream);
private byte[] getPDFDocument(MemoryStream inputImageStream)
{
MemoryStream workStream = new MemoryStream();
iTextSharp.text.Document document = new iTextSharp.text.Document(new iTextSharp.text.Rectangle(812, 1015));
PdfWriter.GetInstance(document, workStream).CloseStream = false;
document.Open();
iTextSharp.text.Image pdfImage = iTextSharp.text.Image.GetInstance(inputImageStream.ToArray());
document.Add(pdfImage);
document.Close();
byte[] byteInfo = workStream.ToArray();
workStream.Write(byteInfo, 0, byteInfo.Length);
workStream.Position = 0;
return workStream.ToArray();
}
Upvotes: 1
Views: 634
Reputation: 705
Add this line to your code block
pdfImage.SetAbsolutePosition(0, 0);
You should see this:
document.Open();
iTextSharp.text.Image pdfImage = iTextSharp.text.Image.GetInstance(inputImageStream.ToArray());
pdfImage.SetAbsolutePosition(0, 0);
document.Add(pdfImage);
document.Close();
That should give you the desired positioning.
Upvotes: 1