3
我试图用iTextSharp将图像插入到新创建的PDF文档中 - 虽然我不确定我是否以正确的方式进行了操作。我创建了一个图像对象,然后尝试将其添加到页面 - 但没有图像显示 - 尽管我插入的文本确实出现在PDF文档中。使用iTextSharp将图像插入到PDF中
有没有人有任何想法?
public bool createPDF(string batchNumber, string userName, string path)
{
// step 1: creation of a document-object
Document myDocument = new Document(PageSize.A4.Rotate());
try
{
// step 2:
// Now create a writer that listens to this doucment and writes the document to desired Stream.
PdfWriter.GetInstance(myDocument, new FileStream(path, FileMode.Create));
// step 3: Open the document now using
myDocument.Open();
// step 4: Now add some contents to the document
// batch Header e.g. Batch Sheet
myDocument.Add(new Paragraph("Number: " + batchNumber));
myDocument.Add(new Paragraph("Created By: " + userName));
iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance("code39-barcode.png");
PdfPCell cell = new PdfPCell(logo);
myDocument.Add(cell);
}
catch (DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch (IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: Remember to close the document
myDocument.Close();
return true;
}
大家好,由@maxisam提供的资源已经足够好了,但是,从我的理解以及我最好的程序员那里,正在从磁盘驱动器读取图片图像。如果图片是从数据库表格中读取的,它以byte []的形式存储,该怎么办?需要对资源中的代码进行哪些更改:https://www.mikesdotnetting.com/Article/87/iTextSharp-Working-with-images(如果需要任何源代码示例,请告诉我) –