i'd like to create a table and fill it with my datas (it's supposed to look like cards than a datagrid)
I've copy paste microsoft doc but my table is not printed on the Main window. : https://learn.microsoft.com/en-us/dotnet/desktop/wpf/advanced/how-to-build-a-table-programmatically?view=netframeworkdesktop-4.8
(i usually use winform, thats why i'm lost)
here is my code :
public MainWindow()
{
InitializeComponent();
// instantiation de Data (qui contient les ExoBatt)
DataExo Data = new DataExo();
// Create the parent FlowDocument...
FlowDocument flowDoc = new FlowDocument();
// Create the Table...
Table table1 = new Table();
// ...and add it to the FlowDocument Blocks collection.
flowDoc.Blocks.Add(table1);
// Set some global formatting properties for the table.
table1.CellSpacing = 10;
table1.Background = Brushes.White;
// Create 6 columns and add them to the table's Columns collection.
int numberOfColumns = 6;
for (int x = 0; x < numberOfColumns; x++)
{
table1.Columns.Add(new TableColumn());
// Set alternating background colors for the middle colums.
if (x % 2 == 0)
table1.Columns[x].Background = Brushes.Beige;
else
table1.Columns[x].Background = Brushes.LightSteelBlue;
}
// Create and add an empty TableRowGroup to hold the table's Rows.
table1.RowGroups.Add(new TableRowGroup());
// Add the first (title) row.
table1.RowGroups[0].Rows.Add(new TableRow());
// Alias the current working row for easy reference.
TableRow currentRow = table1.RowGroups[0].Rows[0];
// Global formatting for the title row.
currentRow.Background = Brushes.Silver;
currentRow.FontSize = 40;
currentRow.FontWeight = System.Windows.FontWeights.Bold;
// Add the header row with content,
currentRow.Cells.Add(new TableCell(new Paragraph(new Run("2004 Sales Project"))));
// and set the row to span all 6 columns.
currentRow.Cells[0].ColumnSpan = 6;
// Add the second (header) row.
table1.RowGroups[0].Rows.Add(new TableRow());
currentRow = table1.RowGroups[0].Rows[1];
// Global formatting for the header row.
currentRow.FontSize = 18;
currentRow.FontWeight = FontWeights.Bold;
// Add cells with content to the second row.
currentRow.Cells.Add(new TableCell(new Paragraph(new Run("Product"))));
currentRow.Cells.Add(new TableCell(new Paragraph(new Run("Quarter 1"))));
currentRow.Cells.Add(new TableCell(new Paragraph(new Run("Quarter 2"))));
currentRow.Cells.Add(new TableCell(new Paragraph(new Run("Quarter 3"))));
currentRow.Cells.Add(new TableCell(new Paragraph(new Run("Quarter 4"))));
currentRow.Cells.Add(new TableCell(new Paragraph(new Run("TOTAL"))));
// Add the third row.
table1.RowGroups[0].Rows.Add(new TableRow());
currentRow = table1.RowGroups[0].Rows[2];
// Global formatting for the row.
currentRow.FontSize = 12;
currentRow.FontWeight = FontWeights.Normal;
// Add cells with content to the third row.
currentRow.Cells.Add(new TableCell(new Paragraph(new Run("Widgets"))));
currentRow.Cells.Add(new TableCell(new Paragraph(new Run("$50,000"))));
currentRow.Cells.Add(new TableCell(new Paragraph(new Run("$55,000"))));
currentRow.Cells.Add(new TableCell(new Paragraph(new Run("$60,000"))));
currentRow.Cells.Add(new TableCell(new Paragraph(new Run("$65,000"))));
currentRow.Cells.Add(new TableCell(new Paragraph(new Run("$230,000"))));
// Bold the first cell.
currentRow.Cells[0].FontWeight = FontWeights.Bold;
table1.RowGroups[0].Rows.Add(new TableRow());
currentRow = table1.RowGroups[0].Rows[3];
// Global formatting for the footer row.
currentRow.Background = Brushes.LightGray;
currentRow.FontSize = 18;
currentRow.FontWeight = System.Windows.FontWeights.Normal;
// Add the header row with content,
currentRow.Cells.Add(new TableCell(new Paragraph(new Run("Projected 2004 Revenue: $810,000"))));
// and set the row to span all 6 columns.
currentRow.Cells[0].ColumnSpan = 6;
}
Thank you !
Try to set the Content of the window to a FlowDocumentScrollViewer that displays your FlowDocument:
Content = new FlowDocumentScrollViewer() { Document = flowDoc };
Then you should see the document.
The other option is to create the FlowDocumentScrollViewer in the XAML markup of MainWindow.xaml and set the Document property of this instance:
<FlowDocumentScrollViewer x:Name="fdsv" />
...
fdsv.Document = flowDoc;
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 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 was adding a table to pdf. I have 3 rows and 3 columns. I want the first column to appear only once as a single cell for all the rows. How can I do that?My code is as follows. My output should come like Deloitte in the column of company as shown in the image:
PdfPTable table = new PdfPTable(dt.Columns.Count);
PdfPRow row = null;
float[] widths = new float[] { 4f, 4f, 4f };
table.SetWidths(widths);
table.WidthPercentage = 100;
int iCols = 0;
string colname = "";
PdfPCell cells = new PdfPCell(new Phrase("Products"));
cells.Colspan = dt.Columns.Count;
foreach (DataColumn c in dt.Columns)
{
table.AddCell(new Phrase(c.ColumnName, fontbold));
}
foreach (DataRow r in dt.Rows)
{
if (dt.Rows.Count > 0)
{
table.AddCell(new Phrase(r[0].ToString(), font5));
table.AddCell(new Phrase(r[1].ToString(), font5));
table.AddCell(new Phrase(r[2].ToString(), font5));
}
} document.Add(table);
The MyFirstTable example from my book does exactly what you need. Ported to C#, it looks like this:
PdfPTable table = new PdfPTable(3);
// the cell object
PdfPCell cell;
// we add a cell with colspan 3
cell = new PdfPCell(new Phrase("Cell with colspan 3"));
cell.Colspan = 3;
table.AddCell(cell);
// now we add a cell with rowspan 2
cell = new PdfPCell(new Phrase("Cell with rowspan 2"));
cell.Rowspan = 2;
table.AddCell(cell);
// we add the four remaining cells with addCell()
table.AddCell("row 1; cell 1");
table.AddCell("row 1; cell 2");
table.AddCell("row 2; cell 1");
table.AddCell("row 2; cell 2");
You can look at the resulting PDF here. In your case you'd need
cell.Rowspan = 6;
For the cell with value Deloitte.
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 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);