Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
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.

C#
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 :)
Posted
Updated 18-Mar-17 1:23am
v4

1 solution

Found a solution on stackOverFlow.com

I have used
PdfContentByte 
instead of tables and that solved my problem :)

Thanks,
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900