如何自定义iTextsharp表格“cell”使用winform控件 [英] How to customize iTextsharp table "cells" using winform controls

查看:58
本文介绍了如何自定义iTextsharp表格“cell”使用winform控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点卡住,这是我第一次使用iTextsharp,我正在尝试创建一个生成随机数的C#应用​​程序,然后将其作为标签打印成PDF文件报告,用户将有一个面板和标签在女巫中,他可以移动面板中的任何位置,并可以更改面板大小,就像添加图像作为背景图像一样。



现在用户进入后指定大小并将其标签拖放到面板内的所需位置并选择图像作为背景图像,我的代码将根据用户输入开始制作具有列数和行数的iTextsharp表,并且每个单元格都是必须与表格中的面板一样大小,并且具有相同的背景图像,文字必须在确切的位置,但是由用户,



工作:

1-拖放工作正常。

2-我可以打印具有用户输入的确切大小的表格单元格。



不起作用的东西:

1-我可以添加图像但是当打印到PDF时我无法将其拉伸到单元格大小

2- I无法将文本对齐到正确的位置,仅限于(CENTER,TOP_LEFT,..等)

3-我无法在图像上添加文字



这是表格的屏幕截图: http://imgur.com/a/LAnSX



这是创建表格并添加图像和文字的代码



I am a bit stuck and it's my first time using iTextsharp, I am trying to create a C# application that generate random numbers then print it into PDF file as a label Report, the user is going to have a panel and a label in witch he can move any where with in the panel and can change the panel size as will as adding an image as a background image .

Now after the user enters the specified size and "drag and drop" the label it to the desired location inside the panel and choose an image as background image, My code will start making iTextsharp table with the number of columns and rows depending on the user input, and every cell is must be as the size as the panel from the Form and have the same Background image and the text must be in the exact place as but by the user,

The things that work:
1- Drag and drop is working fine.
2- I can print the table cells with the exact size as the user input.

Things that doesn't work:
1- I can add an image but i can't stretch it to the size of the cell when printing to PDF
2- I can't align the text at an exact position ,only (CENTER ,TOP_LEFT ,..etc)
3- I can't add a text on top of the image

This is a screen shot of the Form :http://imgur.com/a/LAnSX

This is the code for creating the table and adding the image and text

private iTextSharp.text.Image myImage; 
private ImagePath = "path";

private void ExportToPDF_Click(object sender, EventArgs e)
        {
            try
            {
                saveFileDialog1.Filter = "pdf File |*.pdf";
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    FilePath = saveFileDialog1.FileName.ToString();   
                    //create the pdf file and open it 
                    FileStream fs = new FileStream(FilePath, FileMode.Create,            FileAccess.Write, FileShare.None);
                    Document document = new Document(PageSize.A4, pageLeftMargen, pageRightMargen, pageTopMargen, pageBottomMargen);
                    PdfWriter writer = PdfWriter.GetInstance(document, fs);
                    document.Open();

                    //create the table and set width and height
                    PdfPTable table = new PdfPTable(Get_Columens_Count());
                    table.LockedWidth = true;
                    table.TotalWidth = Get_Tabel_Width();

                    //set the size for evey Cell 
                    float[] a = new float[Get_Columens_Count()];
                    for (int i = 0; i < Get_Columens_Count(); i++)
                    {
                        a[i] = Convert.ToInt32(Get_CellsWidth());
                    }
                    table.SetWidths(a);

                    //Add new cell
                    PdfPCell cell = new PdfPCell();
                    cell.BorderWidth = CellBorderWidth;
                    cell.Padding = CellsPadding;
                    cell.FixedHeight = Convert.ToInt32(Get_Cells_Height());

                    if (checkBox1.Checked == true && File.Exists(ImagePath) == true)
                    {
                        //set the image
                        myImage = iTextSharp.text.Image.GetInstance(ImagePath);
                        cell.AddElement(myImage);
                        
                    }
                       //start creating the cells 
                    for (int i = 0; i < Convert.ToInt32(Get_Records_Count()); i++)
                    {
                        table.AddCell(cell);
                    }

                    document.Add(table);

                    document.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                throw;
            }
        }





我的尝试:



我尝试了什么:



几乎所有我可以通过谷歌搜索和搜索表单找到的东西似乎没什么作为通缉工作。





What I have tried:

What have i tried :

Almost every thing that i could try and find by googling and searching the forms nothing seems to work as a wanted.

ScaleAbsolute
ScaleToFit



还有其他我现在记不住的东西。



有没有比我使用的更好的拉刀?



提前感谢:)


and punch of other stuff that i can't remember at the moment .

is there better a broach than the one that i am using ?

thanks in advance :)

推荐答案

stackOverFlow.com找到解决方案



我用
Found a solution on stackOverFlow.com

I have used
PdfContentByte 

代替桌子,这解决了我的问题:)



谢谢,

instead of tables and that solved my problem :)

Thanks,


这篇关于如何自定义iTextsharp表格“cell”使用winform控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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