Asp.net itextsharp issue with row's text close to bottomline - asp.net

I am working on generating pdf using itextsharp ,In my table i have multiple columns such as sr.ni. name .],quantity ,mrp ,price tax etc.
Name column has round about 40% of total width all values in other column comes right in middle of row but name's value is somehow close to bottom line ,Everything is same for all columns ie. style,font etc.
code
var cell=new PdfPcell();
cell = new PdfPCell(new Phrase(price, font));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.VerticleAlignMent= Element.ALIGN_CENTER;
producttable.AddCell(cell);
same code for all values
any solutions.
also tried
cell.AddElement(new Chunk(name, font));
cell.HorizontalAlignment = Element.ALIGN_LEFT;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
productsTable.AddCell(cell);
didnt work

You can assign vertical alignment to your particular cell as shown below:
pdfCell2.VerticalAlignment = Element.ALIGN_BOTTOM;
pdfCell3.VerticalAlignment = Element.ALIGN_MIDDLE;
pdfCell4.VerticalAlignment = Element.ALIGN_TOP;

Related

How to put table in proper way with itextpdf in android?

I want to export my Sqlite to a PDF. I am able to do it but instead of putting the result in just one row, it puts the the result in the other column i have. What I want is to fill the row in the same column(incremente the rows)with the database results. This is what I have, Can you help me with this?
db = new Database(FirstActivity.this);
Cursor c = bd.getGroupsNamesMachines();
PdfPTable table = new PdfPTable(2);
cell = new PdfPCell(new Phrase("groups"));
cell2 = new PdfPCell(new Phrase("machines"));
if (c.moveToFirst()) {
do {
String groups = c.getString(c.getColumnIndex("groupname"));
cell3 = new PdfPCell(new Phrase(groups));
table.addCell(cell3);
} while (c.moveToNext());
}
cell.setColspan(1);
cell2.setColspan(2);
cell3.setColspan(1);
table.addCell(cell);
table.addCell(cell2);
table.addCell(cell3);
Please read the documentation, or watch this video (before the first 10 minutes are over, you'll know how to create a table that shows all the states of the US). The code for that example can be found here: set format of header row using iText
In your case, you are doing something really strange. You create a table with 2 columns. That's OK if each record has two fields. However, you add the body cells before you add the header cells. I think you're trying to create a header row with 3 columns (which is illogical), and you probably don't complete the last row (and iText only adds complete rows by default).
You need something like this:
// Table
PdfPTable table = new PdfPTable(2);
// Header
PdfPCell cell1 = new PdfPCell(new Phrase("groups"));
PdfPCell cell2 = new PdfPCell(new Phrase("machines"));
table.addCell(cell1);
table.addCell(cell2);
// Data
db = new Database(FirstActivity.this);
Cursor c = db.getGroupsNamesMachines();
if (c.moveToFirst()) {
do {
String group = c.getString(c.getColumnIndex("groupname"));
cell1 = new PdfPCell(new Phrase(group));
table.addCell(cell1);
String machine = c.getString(c.getColumnIndex("machinename"));
cell2 = new PdfPCell(new Phrase(machine));
table.addCell(cell2);
} while (c.moveToNext());
}
document.add(table);
I don't know if the database code is OK; it's your responsibility to know how to get the correct data from the database, but please compare your very asymmetric code with the logical code I changed it into.
First define the header, and add it to the table,
Then loop over the data, and add the data row by row,
Never add more cells to a row than there are columns!

Giving zero indent for inner cell Indent in Aspose Word

I am trying to create inner cells in pdf with using aspose words. But I have issues about its style. I need to center and fit my text in cell. But when I set center property for cell it gives some padding automatically.
I made a sample with MS word as below image:
This one is that I have created programmatically with using aspose words.
As you can see it doesn't fit when I set center property true for columns.
My inner cell import code is below:
Cell cell = table.Rows[j].Cells[i];
cell.CellFormat.RightPadding = 0;
foreach (Paragraph pf in cell.Paragraphs)
{
pf.ParagraphFormat.LeftIndent = 0;
}
cell.CellFormat.WrapText = false;
builder.MoveTo(cell.FirstParagraph);
builder.InsertCell();
builder.Writeln(tr.Rows[j][i].Value);
builder.EndTable();
Which property should I set to fit it in cell, thanks.
Please use following code example to get the required output. You may modify the table/cell width according to your requirement. I suggest you please read following link to work with tables. Hope this helps you.
Working with Tables
If you still face problem, share your input and expected output documents. I will share the code according to your expected document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Font.Name = "Calibri";
builder.CellFormat.LeftPadding = 0;
builder.CellFormat.RightPadding = 0;
builder.CellFormat.TopPadding = 0;
builder.CellFormat.BottomPadding = 0;
Table table = builder.StartTable();
// Insert a cell
builder.InsertCell();
builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(33);
//Insert inner table
Table table1 = builder.StartTable();
builder.InsertCell();
table1.Alignment = TableAlignment.Left;
table1.PreferredWidth = PreferredWidth.FromPercent(50);
builder.Write("This is cell 1");
builder.EndRow();
builder.EndTable();
// Insert a cell
builder.InsertCell();
//Insert inner table
Table table2 = builder.StartTable();
builder.InsertCell();
table2.Alignment = TableAlignment.Center;
table2.PreferredWidth = PreferredWidth.FromPercent(50);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.Write("This is cell 2");
builder.EndRow();
builder.EndTable();
// Insert a cell
builder.InsertCell();
builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(33);
//Insert inner table
Table table3 = builder.StartTable();
builder.InsertCell();
table3.PreferredWidth = PreferredWidth.FromPercent(50);
table3.Alignment = TableAlignment.Right;
builder.ParagraphFormat.Alignment = ParagraphAlignment.Right;
builder.Write("This is cell 3");
builder.EndRow();
builder.EndTable();
builder.EndRow();
builder.EndTable();
doc.Save(MyDir + "Out.docx");
I work with Aspose as Developer evangelist.

How to add a row in gridview of Devexpress

I have a form where i have some text boxes and a Gridview(with Columns in it).the values entered in textboxes should be added as a row in gridview when pressing a ADD button..below is the code in the click event of ADD button..It add as a single column not as a row..Please help me with this.
BindingList<string> Grid_data = new BindingList<string>();
Grid_data.Add(txtMaterial.Text.ToString());
Grid_data.Add(txtPlastic.Text.ToString());
Grid_data.Add(txtCoatingtype.Text.ToString());
Grid_data.Add(txtManufacturer.Text.ToString());
Grid_data.Add(txtManuDate.Text.ToString());
Grid_data.Add(txtLineSize.Text.ToString());
Grid_data.Add(txtFootage.Text.ToString());
DGMaterial.DataSource = Grid_data;
GV_Material.PopulateColumns();
To begin with, when you write : Grid_data.Add(someString) it adds a new element to the binding list not to the row. You have to understand that each element of the BindingList represents a row, not a value for a cell.
What you should do is the following :
BindingList<List<string>> gridData = new BindingList<List<string>>();
gridData.add(new List<string>() {"someString", "anotherString"}); // Your inputs here
DGMaterial.DataSource = gridData ;
GV_Material.PopulateColumns();
Hope that helps !

How to add an image to a table cell in iTextSharp using webmatrix

I have made a table with cells and interested in having an image in one of the cell.
Below is my code:
doc.Open();
PdfPTable table = new PdfPTable(2);
table.TotalWidth = 570f;
table.LockedWidth = true;
table.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
PdfPCell points = new PdfPCell(new Phrase("and is therefore entitled to 2 points", arialCertify));
points.Colspan = 2;
points.Border = 0;
points.PaddingTop = 40f;
points.HorizontalAlignment = 1;//0=Left, 1=Centre, 2=Right
table.AddCell(points);
// add a image
doc.Add(table);
Image jpg = Image.GetInstance(imagepath + "/logo.jpg");
doc.Add(jpg);
With the above code, the image shows in my pdf but I want it to be inside a cell so that I can add more cells to the right of the image.
On the very basic level you can simply add the image to a PdfPCell and add this cell to your table.
So using your code...
PdfPCell points = new PdfPCell(new Phrase("and is therefore entitled to 2 points", arialCertify));
points.Colspan = 2;
points.Border = 0;
points.PaddingTop = 40f;
points.HorizontalAlignment = 1;//0=Left, 1=Centre, 2=Right
// add a image
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imagepath + "/logo.jpg");
PdfPCell imageCell = new PdfPCell(jpg);
imageCell.Colspan = 2; // either 1 if you need to insert one cell
imageCell.Border = 0;
imageCell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.AddCell(points);
// add a image
table.AddCell(imageCell);
doc.Add(table);
Update
Check your imagepath. This should be an absolute path to the image, and not relative like in a website page. Also, change your `/logo.jpg' to '\logo.jpg'
this is assuming imagepath is actually to the directory, and not the actual image...
I.E
Server.MapPath(imagepath) + "\\logo.jpg"

how to force a justified alignment on a pdfpcell

to justify a text on a pdfpcell i know these commands
PdfPCell Example= new PdfPCell(new paragraph("Some text here",MyFont));//previous created font
Example.HorizontalAlignment = Element.ALIGN_JUSTIFIED;
but i have a hmtl string , and i need to decode, and im doing like this
string txt = "long html string text"
PdfPCell Example2= new PdfPCell();
List<IElement> sr = HTMLWorker.ParseToList(new StringReader(txt ), style);//style was previous created
foreach (IElement element in sr)
{
Example2.AddElement(element);
}
Example2.SetLeading(0f, 1.5f);
Example2.Border = 0;
Example2.PaddingBottom = 20;
Table1.AddCell(Example2);//table declared previous
ok , until this point all is working and my pdfp document is fine but i need to ignore the alignment of the html string and force all text to be justified.
im tried this
Example2.HorizontalAlignment = Element.ALIGN_JUSTIFIED;
but doesnt work .
In the first example, you're working in "text mode" and the properties of the cell are respected.
As soon as you use AddElement (), you're working in "composite mode" and the properties of the cell are ignored (as documented in my book). Instead, the properties of the separate elements are used.
In answer to your question: you shouldn't define the alignment at the level of the cell, but at the level of the element.

Resources