将图像插入使用iTextSharp的PDF文件 [英] Inserting an image into a PDF using iTextSharp

查看:258
本文介绍了将图像插入使用iTextSharp的PDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将图像插入使用iTextSharp的新创建的PDF文档 - 尽管我不知道我在正确的方式去了解它。我已经创建了一个图像对象,然后试图将其添加到网页 - 但没有图像显示出来 - 虽然我插入的文本不会出现在PDF文档中

有没有人有什么想法?

 公共BOOL createPDF(字符串batchNumber,用户名字符串,字符串路径)
{
    //第1步:创建一个文件对象的
    文档myDocument中=新的文件(PageSize.A4.Rotate());

    尝试
    {
        //步骤2:
        //现在创建一个作家的监听该doucment并写入文件所需的视频流。
        PdfWriter.GetInstance(myDocument中,新的FileStream(路径,FileMode.Create));

        //第3步:现在打开的文档使用
        myDocument.Open();

        //第4步:现在一些内容添加到文档
        //批头例如批量表
        myDocument.Add(新段(编号+ batchNumber));
        myDocument.Add(新段(创建者:+用户名));

        iTextSharp.text.Image标志= iTextSharp.text.Image.GetInstance(code39吧code.png);
        PdfPCell电池=新PdfPCell(标识);
        myDocument.Add(细胞);
    }
    赶上(DocumentException日)
    {
        Console.Error.WriteLine(de.Message);
    }
    赶上(IOException异常IOE)
    {
        Console.Error.WriteLine(ioe.Message);
    }

    //第5步:请记住,关闭文件
    myDocument.Close();

    返回true;
}
 

解决方案

读的来知道如何添加图片

不过,我想你会错过一些与表。

您应该有一个表,并使用table.addCell添加细胞

  PdfPTable表=新PdfPTable(3);

PdfPCell电池=新PdfPCell(新词(页眉跨越3列));
 

阅读知道如何使用表

I am attempting to insert an image into a newly created PDF document using iTextSharp - although I am not sure I am going about it in the correct manner. I have created an image object and then attempted to add it to the page - but no image shows up - although the text I inserted does appear in the PDF document.

Does anyone have any ideas?

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;
}

解决方案

Read this to know how to add image

However, I think you miss something with table.

You should have a table and use table.addCell to add the cell

PdfPTable table = new PdfPTable(3);

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

Read this to know how to use table

这篇关于将图像插入使用iTextSharp的PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆