Migradoc Header with table - c#

I can make a header in Migradoc like this:
//Create Header
Paragraph paragraph = section.Headers.Primary.AddParagraph();
paragraph.AddText("Roto");
paragraph.Format.Font.Size = 9;
paragraph.Format.Alignment = ParagraphAlignment.Center;
And I can make make a simple table like this:
// Create the HEADER table for the top of every page
this.table = section.AddTable();
this.table.Style = "Table";
this.table.Borders.Color = TableBorder;
this.table.Borders.Width = 0.25;
this.table.Borders.Left.Width = 0.5;
this.table.Borders.Right.Width = 0.5;
this.table.Rows.LeftIndent = 0;
Column column = this.table.AddColumn("8cm");
column.Format.Alignment = ParagraphAlignment.Center;
column = this.table.AddColumn("8cm");
column.Format.Alignment = ParagraphAlignment.Center;
// Create the header of the table
Row row = table.AddRow();
//row = table.AddRow();
row.HeadingFormat = true;
row.Format.Alignment = ParagraphAlignment.Center;
row.Format.Font.Bold = true;
row.Shading.Color = TableBlue;
row.Cells[0].AddParagraph("Rotary");
row.Cells[0].MergeRight = 1;
row = table.AddRow();
row.HeadingFormat = true;
row.Format.Alignment = ParagraphAlignment.Center;
row.Format.Font.Bold = true;
row.Shading.Color = TableBlue;
row.Cells[0].AddParagraph("Part No.:");
row.Cells[0].Format.Alignment = ParagraphAlignment.Left;
row.Cells[1].AddParagraph("Tested by:");
row.Cells[1].Format.Alignment = ParagraphAlignment.Left;
row = table.AddRow();
row.Cells[0].MergeRight = 1;
How do I get the table into the header so it appears at the top of every page?
EDIT:
So to make it work I changed:
this.table = section.AddTable();
to:
this.table = section.Headers.Primary.AddTable();

If you want to have the same header on every page:
Use section.Headers.Primary.AddTable() instead of section.Headers.Primary.AddParagraph().
By setting row.HeadingFormat = true; for the first n rows of your table, you mark this rows as header rows. When the table grows and breaks over several pages, the header rows will be repeated on every page (but in the "normal" page body, not the header area). This is the typical usage of heading rows. If you don't add other rows to your header table, HeadingFormat = true will not have any effect.

Related

Minimum height of Row [Migradoc]

I want to set the minimum height of row. however it seems there is limit
i am using below code [http://forum.pdfsharp.de/viewtopic.php?f=2&t=2812 ]
var spacerRow = t1.AddRow();
spacerRow.Height = "0.1mm";
var para2 = new Paragraph();
para2.Format.LineSpacingRule = LineSpacingRule.Exactly;
para2.Format.LineSpacing = "0.1mm";
para2.Format.SpaceBefore = 0;
para2.Format.SpaceAfter = 0;
spacerRow.Cells[0].Add(para2);
but the height is not reducing any further.
the spacer row is between the borderd rows as show in attached picture.
If you want to do it for all rows:
Table table = new Table();
table.Format.Alignment = ParagraphAlignment.Center;
table.Rows.HeightRule = RowHeightRule.Exactly;
table.Rows.Height = 5;
For a single row:
row = table.AddRow();
row.HeightRule = RowHeightRule.Exactly;
row.Height = 5;

How to Set Value in DataGridViewTextBoxCell in WindowForm c#

I can't set Value in DataGridViewTextBox
Here is my code
DataGridViewTextBoxColumn tbCol = new DataGridViewTextBoxColumn();
DataGridViewTextBoxCell tbCell = new DataGridViewTextBoxCell();
tbCell.Value = "1";
tbCol.CellTemplate = tbCell;
tbCol.Name = "qtySelect";
tbCol.HeaderText = "เลือกจำนวน";
gridProduct.Columns.Add(tbCol);
When I Run Textbox are added but it's a blank textbox
any one can help me.
Thank.
You should do this:
yourdatagridview["columnName", rowindex].Value = "Your value";
DataGridViewTextBoxColumn tbCol = new DataGridViewTextBoxColumn();
DataGridViewTextBoxCell tbCell = new DataGridViewTextBoxCell();
// Used for the appearence of the cell, it doesn't mean that there is a correlation between the cell and the column (see below ***)
tbCell.Style.ForeColor = Color.Blue;
tbCol.CellTemplate = tbCell;
tbCol.Name = "qtySelect";
tbCol.HeaderText = "Header";
int colIndex = gridProduct.Columns.Add(tbCol);
const int rowNumber = 0; // define which row you want
//Pay attention that you may need to add rows before writting to the cell
//const int rowNumber = 1;
//gridProduct.Rows.Add();
gridProduct.Rows[rowNumber].Cells[colIndex].Value = "Value for cell";
//*** If you want to work with the cell itself, you first need to get a reference to it
//tbCell = gridProduct.Rows[0].Cells[0] as DataGridViewTextBoxCell;
//tbCell.Value = "1"; // and then you can overwrite the value

NPOI apply font to entire row of cells

I'm using NPOI to export my data to excel. The problem is I found it really hard for any kind of graphical changes.
This is the method I'm using now to apply bold font to my cells.
//Create new Excel workbook
var workbook = new HSSFWorkbook();
//Create new Excel sheet
var sheet = workbook.CreateSheet();
//Create a header row
var headerRow = sheet.CreateRow(0);
var boldFont = workbook.CreateFont();
boldFont.FontHeightInPoints = 11;
boldFont.FontName = "Calibri";
boldFont.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.Bold;
int cellCounter = 0;
//day
var cell = headerRow.CreateCell(cellCounter++);
cell.SetCellValue("Day");
cell.CellStyle = workbook.CreateCellStyle();
cell.CellStyle.SetFont(boldFont);
//month
cell = headerRow.CreateCell(cellCounter++);
cell.SetCellValue("Month");
cell.CellStyle = workbook.CreateCellStyle();
cell.CellStyle.SetFont(boldFont);
//year
cell = headerRow.CreateCell(cellCounter++);
cell.SetCellValue("Year");
cell.CellStyle = workbook.CreateCellStyle();
cell.CellStyle.SetFont(boldFont);
//machine name
cell = headerRow.CreateCell(cellCounter++);
cell.SetCellValue("Machine unique name");
cell.CellStyle = workbook.CreateCellStyle();
cell.CellStyle.SetFont(boldFont); //and so on
Is there a ,,cleaner" way to do this ? Now i have to manually add font for individual cells. I've tried many ways to do this on the internet and nothing seems to be working. Do you have a tested way to apply style to specific column or row ?
OffTopic: If not can you provide me with some good open source libraries with decent documentation and support that allow excel export (learning new dll is a pain but... :) what can you do)?
I'm doing something similar and modified my take on it closer for your use:
private string[] columnHeaders =
{
"Day",
"Month",
"Year",
"Machine Unique Name"
}
private void buildSheet(HSSFWorkbook wb, DataTable data, string sheetName)
{
var cHelp = wb.GetCreationHelper();
var sheet = wb.CreateSheet(sheetName);
HSSFFont hFont = (HSSFFont)wb.CreateFont();
hFont.FontHeightInPoints = 11;
hFont.FontName = "Calibri";
hFont.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.Bold;
HSSFCellStyle hStyle = (HSSFCellStyle)wb.CreateCellStyle();
hStyle.SetFont(hFont);
IRow headerRow = sheet.CreateRow(1);
int cellCount = 1;
foreach (string str in columnHeaders)
{
HSSFCell cell = (HSSFCell)headerRow.CreateCell(cellCount);
cell.SetCellValue(cHelp.CreateRichTextString((str)));
cell.CellStyle = hStyle;
cellCount += 1;
}
This iterates over however many headers you want starting at the second cell (cellCount = 1) second row (sheet.CreateRow(1)).

Multiselect property of Datagridview now selecting full row

I have a window form that has a datagridview. In this datagridview i am adding two columns dynamically as shown in below code.My problem is that when i click on these added columns ,Multiselect property is not selecting full row but when i click on first column of grid it selected full row.
if (gvlayoutload.Columns.Count == 0)
{
DataGridViewTextBoxColumn comboBoxColumnRInfo =
new DataGridViewTextBoxColumn();
comboBoxColumnRInfo.Name = "RowInfo";
comboBoxColumnRInfo.HeaderText = "";
comboBoxColumnRInfo.DataPropertyName = "RowInfo";
comboBoxColumnRInfo.ReadOnly = true;
comboBoxColumnRInfo.Width = 25;
comboBoxColumnRInfo.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
//comboBoxColumnRInfo.Frozen = true;
this.gvlayoutload.Columns.Add(comboBoxColumnRInfo);
DataGridViewTextBoxColumn comboBoxColumn =
new DataGridViewTextBoxColumn();
comboBoxColumn.HeaderText = "Row #";
comboBoxColumn.DataPropertyName = "RowNo";
comboBoxColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet;
comboBoxColumn.Width = 45;
comboBoxColumn.FillWeight = 45;
//comboBoxColumn.Frozen = true;
FRColumn col = new FRColumn();
col.Name = comboBoxColumn.HeaderText;
col.Type = 1;
col.Variable1 = variable1;
col.Variable2 = variable2;
col.Percent = Percent;
col.Rowno = Rowno;
col.Headersize = 8;
col.Bodysize = 8;
col.HeaderAlign = 0;
col.BodyAlign = 0;
lstcolumn.Add(col);
this.gvlayoutload.Columns.Add(comboBoxColumn);
When i click on RowInfo column or Row # column Multiselect property not selected full row.I have set multiselect property to true and multiselection mode is fullrowmode.
The DataGridView.SelectionMode property indicates how the cells of the DataGridView can be selected. The default value is RowHeaderSelect. The behavior of each mode is described in this MSDN link.
The first column in the DataGridView contains the row headers. If you select any cell in this column it will select the full row when the SelectionMode is RowHeaderSelect or FullRowSelect.
If you want to select multiple rows then set the DataGridView.MultiSelect = true.
You can also hide that column by setting DataGridView.RowHeadersVisible = false.

MigraDoc: Forcing embed tables to break at logical points

I have a large form I'm transforming into a PDF. The first 1/6th of it looks like this:
http://i.imgur.com/y4pO8Th.png
The number of entered fields however, varies from 1 to 20 per section, and I need to be able to make this document break pages intelligently. My plan was to originally draw the tables piece by piece and just manage the Y-coordinate by grabbing the number of rows in all previous tables. This worked, but falls apart when I get to a page break, and I start needing some semi-complicated logic to make it work, and it's the kind of logic that gets messier and messier with each additional table added.
My second plan was to reproduce the table structure of the HTML document in the PDF, which I manage to do successfully...
private void DrawPDF()
{
Document tDoc = new Document();
MigraDoc.DocumentObjectModel.Style style = tDoc.Styles["Normal"];
style.Font.Name = tPdfFont;
style.Font.Size = 10;
Section tSec = tDoc.AddSection();
MigraDoc.DocumentObjectModel.Tables.Table masterTable = new MigraDoc.DocumentObjectModel.Tables.Table();
masterTable = tSec.AddTable();
masterTable.Borders.Visible = false;
Column leftColumn = masterTable.AddColumn("365pt");
Column spacer = masterTable.AddColumn("10pt");
Column rightColumn = masterTable.AddColumn("365pt");
Row tFS = masterTable.AddRow();
Cell tCell = tFS.Cells[0];
//
// Farm Assets Column
//
{
MigraDoc.DocumentObjectModel.Tables.Table tAssetsTable = new MigraDoc.DocumentObjectModel.Tables.Table();
tAssetsTable.Borders.Visible = false;
Column tColumn = tAssetsTable.AddColumn("365pt");
tCell.Elements.Add(tAssetsTable);
//
// Current Farm Assets
//
for (int i = 0; i < 10; i++) // Drawn 10 times to force it to draw over the 1st page.
{
Section thisSection = tDoc.AddSection();
Row tAssetsRow = tAssetsTable.AddRow();
Cell tAssetsCell = tAssetsRow.Cells[0];
MigraDoc.DocumentObjectModel.Tables.Table table = new MigraDoc.DocumentObjectModel.Tables.Table();
table = thisSection.AddTable();
table.Borders.Width = 0.2;
table.Rows.LeftIndent = 0;
Column columnData = table.AddColumn("295pt");
columnData.Borders.Left.Visible = false;
Column columnValue = table.AddColumn("70pt");
Row rowA = table.AddRow();
rowA.Shading.Color = Color.FromRgbColor((byte)255, Color.Parse("0xa2a2d2"));
rowA.Cells[0].AddParagraph("CURRENT FARM ASSETS");
rowA.Cells[1].AddParagraph("$ Value");
rowA.Cells[1].Format.Alignment = ParagraphAlignment.Right;
Row row1 = table.AddRow();
row1.Borders.Bottom.Visible = false;
row1.Cells[0].AddParagraph("Cash: Savings: ($" + MP.FormFinancialStatement.CurrentStaticAssets.Savings + ") Checking: ($" + MP.FormFinancialStatement.CurrentStaticAssets.Checking + ")");
row1.Cells[1].AddParagraph(MP.FormFinancialStatement.CurrentStaticAssets.CashTotal);
row1.Cells[1].Format.Alignment = ParagraphAlignment.Right;
Row row2 = table.AddRow();
row2.Borders.Bottom.Visible = false;
row2.Cells[0].AddParagraph("Invest: Time Cret $" + MP.FormFinancialStatement.CurrentStaticAssets.TimeCret + " Other: $" + MP.FormFinancialStatement.CurrentStaticAssets.OtherInvestments + "");
row2.Cells[1].AddParagraph(MP.FormFinancialStatement.CurrentStaticAssets.InvestTotal);
row2.Cells[1].Format.Alignment = ParagraphAlignment.Right;
Row row3 = table.AddRow();
row3.Borders.Bottom.Visible = false;
row3.Cells[0].AddParagraph("Replacement Account");
row3.Cells[1].AddParagraph(MP.FormFinancialStatement.CurrentStaticAssets.ReplacementAccount);
row3.Cells[1].Format.Alignment = ParagraphAlignment.Right;
Row row4 = table.AddRow();
row4.Cells[0].AddParagraph("Accouts and Notes Recievable");
row4.Cells[1].AddParagraph(MP.FormFinancialStatement.CurrentStaticAssets.AccountsNotesReceivable);
row4.Cells[1].Format.Alignment = ParagraphAlignment.Right;
MigraDoc.DocumentObjectModel.Tables.Table clone = (MigraDoc.DocumentObjectModel.Tables.Table)table.Clone();
tAssetsCell.Elements.Add(clone);
}
}
MigraDoc.Rendering.DocumentRenderer docRenderer = new DocumentRenderer(tDoc);
docRenderer.PrepareDocument();
docRenderer.RenderObject(gfx, 30, 85, "740pt", masterTable);
}
But alas, this does not actually break pages correctly. I tried sectioning off each individual table, hoping that'd do page break magic, but it does not.
How can I structure this to allow for good page breaks?
You can use the KeepWith property of the table rows to keep blocks together on one page. Only use this for chunks that will surely fit on one page.
See also:
https://stackoverflow.com/a/6831048/1015447
https://stackoverflow.com/a/1327228/1015447

Categories