2012-08-30 46 views
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; 
} 

回答

2

this知道如何添加图像

不过,我想你错过桌上的东西。

你应该有一个表,并使用table.addCell增加该小区

PdfPTable table = new PdfPTable(3); 

PdfPCell cell = new PdfPCell(new Phrase("Header spanning 3 columns")); 

this知道如何使用表

+0

大家好,由@maxisam提供的资源已经足够好了,但是,从我的理解以及我最好的程序员那里,正在从磁盘驱动器读取图片图像。如果图片是从数据库表格中读取的,它以byte []的形式存储,该怎么办?需要对资源中的代码进行哪些更改:https://www.mikesdotnetting.com/Article/87/iTextSharp-Working-with-images(如果需要任何源代码示例,请告诉我) –

0

删除此:

PdfPCell cell = new PdfPCell(logo); 
myDocument.Add(cell); 

,并使用此:

myDocument.Add(logo); 

它的工作原理:)

相关问题