开发者

how to get a gif in a pdfpcell to line up with trailing text

开发者 https://www.devze.com 2023-04-08 20:57 出处:网络
I am using iTextSharp to create a .pdf.I\'ve got a table and in the table, I need to populate two checkboxes base开发者_Python百科d on data.

I am using iTextSharp to create a .pdf. I've got a table and in the table, I need to populate two checkboxes base开发者_Python百科d on data.

My code looks like this. dv is the dataview containing the data, and chkchecked and chkunchecked are gifs of a checked box and unchecked box respectively.

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

And this almost works. But my checkboxes are on the line above the respective text, and I want the check boxe to line up beside the text that follows it. How do I get the gif to share the line with the following text in the cell?


The trick is to wrap the individual images in a Chunk and then wrap that and the text inside of a Paragraph. When you create a Chunk from an iTextSharp.text.Image you need to specify at least two floats representing x and y offsets. The sample below uses 0 for both but if you see your image riding too high try using a negative, like -3 for the y parameter. Also, my sample images were a little too big so I needed to scale them down.

Below is a full working WinForms example targeting iTextSharp 5.1.1.0. See the code for additional comments.

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();
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消