I have tried this.....
_doc = new FlowDocument();
Table t = new Table();
for (int i = 0; i < 7; i++)
{
t.Columns.Add(new TableColumn());
}
TableRow row = new TableRow();
row.Background = Brushes.Silver;
row.FontSize = 40;
row.FontWeight = FontWeights.Bold;
row.Cells.Add(new TableCell(new Paragraph(new Run("I span 7 columns"))));
row.Cells[0].ColumnSpan = 6;
_doc2.Blocks.Add(t);
When I go to view this document the table never shows.....although the border image and document title that I add to this document before adding this table outputs fine.
You add the Columns to the Table, but where is the code that adds the row? It just isn't connected.
Add something like:
...
var rg = new TableRowGroup();
rg.Rows.Add(row);
t.RowGroups.Add(rg);
_doc2.Blocks.Add(t);
Related
I am producing a flow document that has a document.table instance added to it as a block.
I produced my table using this tutorial here
I'm trying to set the second group row of the table so that the text aligns at the right side of the cells whilst keeping the other group rows at centred.
I can set the whole table alignment using table1.TextAlignment = TextAlignment.Right; but i want to be able to set just a group rows, rows, or individual cells to have the right text align.
This is my code
FlowDocument doc = new FlowDocument();
doc.TextAlignment = TextAlignment.Center;
doc.FontFamily = new FontFamily("Century Gothic");
Table table1 = new Table();
table1.TextAlignment = TextAlignment.Center;
doc.Blocks.Add(table1);
int numberOfColumns = 12;
for (int i = 0; i < numberOfColumns; i++)
{
table1.Columns.Add(new TableColumn());
}
table1.Columns[0].Width = new GridLength(125);
//Header row group
table1.RowGroups.Add(new TableRowGroup());
table1.RowGroups[0].Rows.Add(new TableRow());
TableRow currentRow = table1.RowGroups[0].Rows[0];
currentRow.Cells.Add(new TableCell(new Paragraph(new
Run("Header 1"))));
currentRow.Cells[0].ColumnSpan = numberOfColumns;
table1.RowGroups[0].Rows.Add(new TableRow());
currentRow = table1.RowGroups[0].Rows[1];
currentRow.Cells.Add(new TableCell(new Paragraph(new
Run("Header 2"))));
currentRow.Cells[0].ColumnSpan = numberOfColumns;
//Main body
table1.RowGroups.Add(new TableRowGroup());
for (int i = 0; i < list.Count; i++)
{
table1.RowGroups[1].Rows.Add(new TableRow());
currentRow = table1.RowGroups[1].Rows[i];
currentRow.Cells.Add(new TableCell(new Paragraph(new Run(Time))));
//Rest of the columns here (These columns need right text alignment)
}
I have found this but when i try and do this currentRow.Cells.Add(new TableCell(new Paragraph(new Run(Time)).TextAlignment = TextAlignment.Right));
I get this error
cannot convert from 'System.Windows.TextAlignment' to 'System.Windows.Documents.Block'
Does anybody have a solution or alternative?
I have an Array of Tables. For example 6 x 6. and a PlaceHolder.
I need to place the tables 6 next to each other in the PlaceHolder and then a new line of 6 next to each other, etc. What css properties do I add for each grid to achieve this.
I have
LiteralControl ltr = new LiteralControl();
ltr.Text = "<style type=\"text/css\" rel=\"stylesheet\">" + #".fl { float: left}</style>";
this.Page.Header.Controls.Add(ltr);
Table[,] tableArray = new Table[6,6];
for (int j = 0; j < tableArray.GetLength(0); j++)
{
bool first = true;
for (int i = 0; i < tableArray.GetLength(1); i++)
{
if (first)
{
tableArray[j, i].CssClass = "mGrid";
first = false;
}
else
{
tableArray[j, i].CssClass = "fl mGrid";
}
tableArray[j, i].Width = Unit.Percentage(100 / 6);
PlaceHolderTables.Controls.Add(tableArray[j, i]);
}
}
But I do not know how to start a new row and then have 5 next to it etc. I am inexperienced with CSS. The tables has been initialised else where. mGrid is defined elsewhere as well.
You can place those 36 tables inside a table with 6x6.
protected void Page_Load(object sender, EventArgs e)
{
LiteralControl ltr = new LiteralControl();
ltr.Text = "<style type=\"text/css\" rel=\"stylesheet\">" + #".fl { float: left}</style>";
Page.Header.Controls.Add(ltr);
Table main = new Table();
for (int i = 0; i < 6; i++)
{
TableRow row = new TableRow();
for (int j = 0; j < 6; j++)
{
Table table = CreateTable($"{i}x{j}");
TableCell cell = new TableCell();
cell.Controls.Add(table);
row.Controls.Add(cell);
}
main.Controls.Add(row);
}
PlaceHolderTables.Controls.Add(main);
}
private Table CreateTable(string text)
{
TableCell cell = new TableCell();
cell.Controls.Add(new Literal {Text = text });
TableRow row = new TableRow();
row.Cells.Add(cell);
Table table = new Table();
table.Rows.Add(row);
return table;
}
I am reading data from database in grid view and try to bind the same gridview in a previously created pdf templete, but i am not sure how to do that.
Is there a possible way to do that in c#.
iTextSharp is a good library for create pdf file in c# and asp.net. it's fully optional and have a lot of document
Using itextsharp you can create a pdf document
You can loop through the gridview and populate a table in the pdf file.
int[] clmwidths111 = { 30, 20 };
PdfPTable tbl14 = new PdfPTable(2);
tbl14.SetWidths(clmwidths111);
tbl14.WidthPercentage = 70;
tbl14.HorizontalAlignment = Element.ALIGN_CENTER;
tbl14.SpacingBefore = 25;
tbl14.SpacingAfter = 10;
tbl14.DefaultCell.Border = 1;
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
cell = new PdfPCell(new Phrase(((Literal)row.FindControl("Totalprem1")).Text, bodyFont2));
cell.HorizontalAlignment = 0;
cell.Colspan = 1;
cell.Border = 0;
tbl14.AddCell(cell);
cell = new PdfPCell(new Phrase(((Literal)row.FindControl("Totalcom1")).Text, bodyFont2));
cell.HorizontalAlignment = 2;
cell.Colspan = 1;
cell.Border = 0;
tbl14.AddCell(cell);
}
}
i am trying to build a pdf, in which i have to add a table without border and i am doing like this, but is there any better way to do this?
my code is like this:
PdfPTable row1 = new PdfPTable(4);
row1.TotalWidth = 350f;
row1.LockedWidth = true;
int[] intTblWidth1 = { 20,50,20,40 };
row1.SetWidths(intTblWidth1);
row1.SpacingBefore = 20f;
row1.HorizontalAlignment = Element.ALIGN_LEFT;
PdfPCell cel = new PdfPCell(new Phrase("Ordered By: ", bodyFont));
cel.Colspan = 1;
cel.Border = 0;
cel.HorizontalAlignment = 0;
row1.AddCell(cel);
PdfPCell cel1 = new PdfPCell(new Phrase(_requester, titleFont));
cel1.Border = 0;
cel1.HorizontalAlignment = 0;
cel1.VerticalAlignment = 0;
row1.AddCell(cel1);
PdfPCell cel2 = new PdfPCell(new Phrase("Order #: ", bodyFont));
cel2.Colspan = 1;
cel2.Border = 0;
cel2.HorizontalAlignment = 0;
row1.AddCell(cel2);
PdfPCell cel3 = new PdfPCell(new Phrase(_orderNumber, titleFont));
cel3.Colspan = 1;
cel3.Border = 0;
cel3.HorizontalAlignment = 0;
row1.AddCell(cel3);
doc.Add(row1);
i am using new table to create new row.
if i do like this:-
PdfPTable table = new PdfPTable(3);
PdfPCell cell = new PdfPCell(new Phrase("Header spanning 3 columns"));
cell.Colspan = 3;
cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
table.AddCell(cell);
table.AddCell("Col 1 Row 1");
table.AddCell("Col 2 Row 1");
table.AddCell("Col 3 Row 1");
table.AddCell("Col 1 Row 2");
table.AddCell("Col 2 Row 2");
table.AddCell("Col 3 Row 2");
doc.Add(table);
i am not able to hide the border of table, and i don't wanna any border line in the table.
i have to generate a dynamic pdf. any suggestion will be appreciated, i will mark your answer if it work for me. thank you in advance ;) happy coding.
Try doing this:
table.DefaultCell.Border = Rectangle.NO_BORDER;
or you should try this for each cell
cellxxx.Border = Rectangle.NO_BORDER;
The Border Elements of the PdfPTable are defined by the PdfPCell which are added to the table. Each Cell will have its own style/formatting. Here is the API: http://api.itextpdf.com/
I'm working with ASP.NET an C# I have a dynamic table and I want a cell to have an initial value then I want to use this value as a parameter in a method and fill the same cell with another value....
here is snippet of my code
for (int index = 0; index < size; index++) {
List<Hotel> h = listHotelList[index];
Hotel myHotel = new Hotel();
mytable = new Table();
mytable.ID = "HotelTable"+index;
Page.Form.Controls.Add(mytable);
mytable.CellSpacing = 20;
mytable.CellPadding = 10;
for (int g = 0; g < h.Count; g++)
{
myHotel = h[g];
TableRow row = new TableRow();
for (int i = 0; i < 2; i++)
{
TableCell cell = new TableCell();
if (i == 0)
{
// I want to leave this with only the value of the hotelId
// and I want my method to fill this cell with an image by retrieving
// the value of the hotelId
}
if (i == 1)
{
Label tb = new Label();
tb.ID = "label1_" + g + "Col" + i;
tb.Text = "<h4>" + myHotel.hotelName + "</h4><br />";
cell.Controls.Add(tb);
}
row.Cells.Add(cell);
}
mytable.Rows.Add(row);
}
}
for my case I'm taking a hotel Id from the cell when the user click a button then it trigger a method that would take the value in the cell "hotel Id" and retrieve the image for this particular hotel and fill the cell with the image
Is the problem that you don't see the tabel?
If so,
add the table to your page
Page.Controls.Add(mytable);
If the positioning is not good, then put a placeholder on your page, and add the control there.
Placeholder.Controls.Add(mytable);