I'm trying to insert an image into a PDF file but I am struggling.
The sample suggests I can just supply the path to the image file to PdfBitmap however, PdfBitmap expects a Stream.
I have tried supplying a File Stream and a Memory stream but I end up with the same result, no image is displayed on the PDF (although a box is drawn for it), and if the PDF is opened in Adobe Reader there is a pop up box that says:
An error exists on this page. Acrobat may not display the page correctly. Please contact the person who created the PDF document to correct the problem.
I have also tried to use PdfImage.FromStream(stream) however this has the same result. Upon debugging, the stream appears to be populated with the file information, however, PdfImage doesnot appear to be populated.
Code below.
public async Task CreateQuotationPDF(Quotation Quotation) {
// create document
PdfDocument doc = new PdfDocument();
PdfPage page = doc.Pages.Add();
// set font
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12);
// create Header
float pageWidth = doc.Pages[0].GetClientSize().Width;
doc.Template.Top = CreateHeader("Quotation", pageWidth);
PdfGraphics imageGraphics = page.Graphics;
string webrootPath = _hostingEnvironment.WebRootPath.ToString();
string imagePath = "images\\documents\\logo.jpg";
string fullPath = Path.Combine(webrootPath, imagePath);
// get logo
using (var fileStream = new FileStream(fullPath, FileMode.Open))
{
PdfImage image = PdfImage.FromStream(fileStream);
headerImageGraphics.DrawImage(image, new PointF(10, 0), new SizeF(100, 40));
}
// create footer
PdfPageTemplateElement footer = await CreateFooter("", pageWidth);
return doc;
}