c# - 使用 iTextSharp 将图像插入 PDF

标签 c# .net windows pdf itextsharp

我正在尝试使用 iTextSharp 将图像插入到新创建的 PDF 文档中 - 虽然我不确定我是否以正确的方式进行操作。我创建了一个图像对象,然后尝试将其添加到页面 - 但没有图像显示 - 尽管我插入的文本确实出现在 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;
}

最佳答案

阅读this了解如何添加图像

但是,我认为您错过了一些关于表格的内容。

您应该有一个表格并使用 table.addCell 添加单元格

PdfPTable table = new PdfPTable(3);

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

阅读this了解如何使用表格

关于c# - 使用 iTextSharp 将图像插入 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12195922/

相关文章:

windows - 将 Perl 脚本生成为具有特定名称的进程

c# - 使用 ASP.NET MVC 获取用户的本地路径

c# - 如何创建具有可变数量 StackPanel 的 StackPanel 的 DataTemplate

c# - string.Empty vs null。你用哪个?

windows - 打开cmd,切换目录,执行命令

c++ - 外部 dll 中缺少 Qt 信号

c# - 将带有非托管导出的 C# DLL 中的字符串返回到 Inno Setup 脚本

c# - 在 C# 中只允许输入数字而不是 NumericUpDown 的文本框

.net - NuGetPackageImportStamp 的用途是什么?

c# - 用户控件的可见属性何时会影响其子项?