如何在pdfpcell中获取gif以与尾随文本对齐

时间:2011-09-28 17:14:33

标签: c# itextsharp

我正在使用iTextSharp来创建.pdf。我有一张桌子,在表格中,我需要根据数据填充两个复选框。

我的代码看起来像这样。 dv是包含数据的数据视图,chkchecked和chkunchecked分别是复选框和未选中框的GIF。

Pdfpcell cell = new Pdfpcell(new Phrase(""));
cell.AddElement ((int)dv[i]["Return_Reason"] == 6 ? chkchecked : chkunchecked);
Phrase p = new Phrase ("item was not authorized for the payee")
cell.AddElement (p);
cell.AddElement ((int)dv[i]["Return_Reason"] == 7 ? chkchecked : chkunchecked);
p = new Phrase ("item was not authorized for the amount")
cell.AddElement (p);
table.AddCell (cell);

这几乎可行。但是我的复选框位于相应文本上方的行上,我希望复选框在其后面的文本旁边排成一行。如何让gif与单元格中的以下文本共享该行?

1 个答案:

答案 0 :(得分:2)

诀窍是将单个图像包装在Chunk中,然后将其包裹在Paragraph内。当您从Chunk创建iTextSharp.text.Image时,您需要指定至少两个代表x和y偏移的浮点数。下面的示例使用0作为两者,但如果您看到图像骑得太高,请尝试使用负数,例如-3作为y参数。此外,我的样本图像有点太大,所以我需要缩小它们。

以下是针对iTextSharp 5.1.1.0的全功能WinForms示例。请参阅代码以获取其他评论。

using System;
using System.Text;
using System.Windows.Forms;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace Full_Profile1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string workingFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            string outputFile = Path.Combine(workingFolder, "Test.pdf");
            string checkedImagePath = Path.Combine(workingFolder, "checked.png");
            string uncheckedImagePath = Path.Combine(workingFolder, "unchecked.png");

            using (FileStream fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                using (Document doc = new Document(PageSize.LETTER))
                {
                    using (PdfWriter w = PdfWriter.GetInstance(doc, fs))
                    {
                        //Open our document for writing
                        doc.Open();

                        //Create images from our file paths
                        var chkchecked = iTextSharp.text.Image.GetInstance(checkedImagePath);
                        var chkunchecked = iTextSharp.text.Image.GetInstance(uncheckedImagePath);

                        //Scale the images to an appropriate size (if needed)
                        chkchecked.ScaleAbsolute(12, 12);
                        chkunchecked.ScaleAbsolute(12, 12);

                        //Create a Paragraph object to contain our images and text
                        Paragraph p = new Paragraph();

                        //Add an image
                        p.Add(new Chunk(chkchecked, 0, 0));

                        //Add some text
                        p.Add("checked");

                        //Add another image
                        p.Add(new Chunk(chkunchecked, 0, 0));

                        //Add some more text
                        p.Add("checked");

                        //Create a one column table
                        PdfPTable table = new PdfPTable(1);

                        //Create a cell for the table
                        PdfPCell cell = new PdfPCell();

                        //Add the paragraph to the cell
                        cell.AddElement(p);

                        //Add the cell to the table
                        table.AddCell(cell);

                        //Add the table to the document
                        doc.Add(table);

                        //Close the document
                        doc.Close();
                    }
                }
            }
            this.Close();
        }
    }
}