2012-08-30 3 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 그러나 이미지

를 추가하는 방법을 알고, 당신이 테이블에 무언가를 그리워 생각합니다.

당신은 테이블이 셀

PdfPTable table = new PdfPTable(3); 

PdfPCell cell = new PdfPCell(new Phrase("Header spanning 3 columns")); 
을 추가 table.addCell를 사용해야합니다

읽기 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); 

그리고 그것은 작동합니다 :)

관련 문제