I have created a flow document code-behind. I use a table with four columns.
But the fourth column didn't appear when printing
It seems that the pagewidth is just over half page
When I reduce columnwidth of columns - they appear but the text is wrapping
Here is the code I have
PrintDialog printDlg = new PrintDialog();
FlowDocument FlowTabledoc = new FlowDocument();
// FlowTabledoc.PageWidth = Double.NaN; dit not work
FlowTabledoc.PageWidth = printDlg.PrintableAreaWidth;
Table logflowtable = new Table();
FlowTabledoc.Blocks.Add(logflowtable);
logflowtable.CellSpacing = 5;
int numberOfColumns = 4;
for (int x = 0; x < numberOfColumns; x++)
{
logflowtable.Columns.Add(new TableColumn());
}
logflowtable.Columns[0].Width = new GridLength(60.0, GridUnitType.Pixel);
logflowtable.Columns[1].Width = new GridLength(120.0, GridUnitType.Pixel);
logflowtable.Columns[2].Width = new GridLength(190.0, GridUnitType.Pixel);
logflowtable.Columns[3].Width = new GridLength(90.0, GridUnitType.Pixel);
logflowtable.RowGroups.Add(new TableRowGroup());
int RowAnzahl = LogTB.Rows.Count;
int ColAnzahl = LogTB.Columns.Count;
Paragraph Abschnitt = new Paragraph();
Abschnitt.FontSize = 14;
for (int r = 0; r <= RowAnzahl - 1; r++)
{
logflowtable.RowGroups[0].Rows.Add(new TableRow());
currentRow = logflowtable.RowGroups[0].Rows[r+1];
for (int c = 0; c <= ColAnzahl - 1; c++)
{
currentRow.FontSize = 14;
currentRow.FontWeight = FontWeights.Normal;
currentRow.Cells.Add(new TableCell(new Paragraph(new Run(LogTB.Rows[r][c].ToString()))));
}
FlowTabledoc.Blocks.Add(Abschnitt);
}
FlowTabledoc.Name = "FlowDoc";
IDocumentPaginatorSource idpSource = FlowTabledoc;
printDlg.PrintDocument(idpSource.DocumentPaginator, "Title-Header");
}
}
Solution found:
FlowTabledoc.ColumnWidth = 793;
Related
I am using the Itextsharp PDF tool to generate PDF using asp.net and C# , In that in one PDFPtable the last row data is repeating on next page means part of a table forwarded to next page. so want to show header for that Table on next page.
This is my code.
Document doc = new Document(iTextSharp.text.PageSize.A4.Rotate(), 10, 10, 20, 10);
string pdfFilePath = Server.MapPath(".") + "/pdf/myPdf" + Guid.NewGuid().ToString("N") + ".pdf";
try
{
//Create Document class object and set its size to letter and give space left, right, Top, Bottom Margin
FileStream fs = new FileStream(pdfFilePath, FileMode.Create);
//
PdfWriter wri = PdfWriter.GetInstance(doc, fs);
//Header Section
string strImagePath = Server.MapPath("Images");
string strReportName = oReportDTO.strReportName + " " + oReportDTO.oDateRange.FromDate.ToString(DateFormat);
Chunk headerchunk = new Chunk(strReportName, new Font(1, 8.0f));
HeaderFooter oHeader = new HeaderFooter(new Phrase(headerchunk), false);
oHeader.Border = Rectangle.NO_BORDER;
oHeader.Alignment = 1;
doc.Header = oHeader;
//Footer Section
string name ="Logged in as : " +currentLoggedInUser.UserName + new string(' ',70)+ "Page Number : " + doc.PageNumber;
Chunk Footerchunk1 = new Chunk(name, new Font(1, 5.0f));
HeaderFooter oFooter1 = new HeaderFooter(new Phrase(Footerchunk1), true);
oFooter1.Border = Rectangle.NO_BORDER;
oFooter1.Alignment = 1;
doc.Footer = oFooter1;
iTextSharp.text.Image imgFooter = iTextSharp.text.Image.GetInstance(System.Drawing.Image.FromFile(strImagePath + "/TransRisk Logo168x97.png"), System.Drawing.Imaging.ImageFormat.Jpeg);
imgFooter.ScaleAbsolute(80, 50);
Chunk footerchunk = new Chunk(imgFooter, 260.0f, 0.0f);
HeaderFooter oFooter = new HeaderFooter(new Phrase(name),new Phrase(footerchunk));
oFooter.Border =Rectangle.NO_BORDER;
doc.Footer = oFooter;
doc.Open();//Open Document to write
Font font8 = FontFactory.GetFont("ARIAL", 7);
HtmlTable dt = customMatrixReport;
if (dt.Rows.Count > 0)
{
//Craete instance of the pdf table and set the number of column in that table
int startColumnPosition = 1;
int endColumnPosition = 13;//End Column number in Pdf Page
int NoOfReports = Convert.ToInt32(Math.Ceiling((decimal)(dt.Rows[0].Cells.Count - 1) / endColumnPosition));//Count How many Pages to show
int pageRowCount = 0;
List<PdfPCell> lstHeaderCells = new List<PdfPCell>();
PdfPTable oPdfTable = null;
PdfPCell oPdfPCell = null;
for (int report = 1; report <= NoOfReports; ++report)
{
doc.Add(oHeader);
//ColumnText.ShowTextAligned(
int noOfColumns = -1;
if (endColumnPosition > dt.Rows[0].Cells.Count - 1) { endColumnPosition = dt.Rows[0].Cells.Count - 1; noOfColumns = (endColumnPosition - startColumnPosition) + 2; oPdfTable = new PdfPTable(noOfColumns); }
else
{
oPdfTable = new PdfPTable(14);
//Widths Count
noOfColumns = 14;
}
oPdfTable.TotalWidth = 650f;
List<float> lstwidths = new List<float>();
lstwidths.Add(100f);
for (int i = 2; i <= noOfColumns; ++i)
{
lstwidths.Add(80f);
}
oPdfTable.SetTotalWidth(lstwidths.ToArray());
pageRowCount = 0;
for (int rows = 0; rows < dt.Rows.Count; rows++)
{
//PageRowCount
pageRowCount = pageRowCount + 1;
//Description celll
if (rows == 0 )
{
//Background color for table header
oPdfPCell = new PdfPCell(new Phrase(new Chunk(dt.Rows[rows].Cells[0].InnerText, new Font(1, 8.0f, 1, Color.WHITE))));
oPdfPCell.BackgroundColor = new Color(118, 147, 199);
oPdfTable.AddCell(oPdfPCell);
}
else
{
//background color for Table cells
oPdfPCell = new PdfPCell(new Phrase(new Chunk(dt.Rows[rows].Cells[0].InnerText,new Font(1, 8.0f))));
oPdfPCell.BackgroundColor = new Color(232, 237, 255);
oPdfTable.AddCell(oPdfPCell);
}
//for header cel
if (rows == 0)
{
lstHeaderCells.Add(oPdfPCell);
}
for (int column = startColumnPosition; column <= endColumnPosition; column++)
{
if (rows == 0)
{
oPdfPCell = new PdfPCell(new Phrase(new Chunk(dt.Rows[rows].Cells[column].InnerText, new Font(1, 8.0f, 1, Color.WHITE))));
oPdfPCell.BackgroundColor = new Color(118, 147, 199);
oPdfTable.AddCell(oPdfPCell);
}
else
{
oPdfPCell = new PdfPCell(new Phrase(new Chunk(dt.Rows[rows].Cells[column].InnerText, new Font(1, 8.0f))));
oPdfPCell.BackgroundColor = new Color(232, 237, 255);
oPdfPCell.Column.Alignment = 2;
oPdfTable.AddCell(oPdfPCell);
}
if (rows == 0)
{
lstHeaderCells.Add(oPdfPCell);
}
}
if (pageRowCount >= 40 && rows != (dt.Rows.Count - 1))
{
pageRowCount = 0; doc.Add(oPdfTable); doc.NewPage(); doc.Add(oHeader); oPdfTable = new PdfPTable(noOfColumns); oPdfTable.SetTotalWidth(lstwidths.ToArray()); foreach (PdfPCell oHeaderCell in lstHeaderCells) { oPdfTable.AddCell(oHeaderCell); }
}
}
startColumnPosition = endColumnPosition + 1;
endColumnPosition = endColumnPosition + 13;
oPdfTable.SpacingBefore = 10;
oPdfTable.SpacingAfter = 10;
doc.Add(oPdfTable);
//doc.NewPage();
}
}
else
doc.Add(oHeader);
}
finally
{
doc.Close();
}
help me..
Change this line-
if (endColumnPosition > dt.Rows[0].Cells.Count - 1) { endColumnPosition = dt.Rows[0].Cells.Count - 1; noOfColumns = (endColumnPosition - startColumnPosition) + 2; oPdfTable = new PdfPTable(noOfColumns); }
To this-
if (endColumnPosition > dt.Rows[0].Cells.Count - 1) { endColumnPosition = dt.Rows[0].Cells.Count - 1; noOfColumns = (endColumnPosition - startColumnPosition) + 2; oPdfTable = new PdfPTable(noOfColumns); oPdfTable.HeaderRows = 1;}
After this line-
oPdfTable = new PdfPTable(14);
Add this-
oPdfTable.HeaderRows = 1;
Change this line-
pageRowCount = 0; doc.Add(oPdfTable); doc.NewPage(); doc.Add(oHeader); oPdfTable = new PdfPTable(noOfColumns); oPdfTable.SetTotalWidth(lstwidths.ToArray()); foreach (PdfPCell oHeaderCell in lstHeaderCells) { oPdfTable.AddCell(oHeaderCell); }
To this-
pageRowCount = 0; doc.Add(oPdfTable); doc.NewPage(); doc.Add(oHeader); oPdfTable = new PdfPTable(noOfColumns); oPdfTable.HeaderRows = 1; oPdfTable.SetTotalWidth(lstwidths.ToArray()); foreach (PdfPCell oHeaderCell in lstHeaderCells) { oPdfTable.AddCell(oHeaderCell); }
I am trying to create a word table from xml file but the rows are not in their proper order,ie the first element occurs as last row, 2nd as 1st row and so on. Where am i going wrong?
Word.Table oTable;
Word.Range wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
oTable = oDoc.Tables.Add(wrdRng, ds.Tables[0].Rows.Count, ds.Tables[0].Columns.Count, ref oMissing, ref oMissing);
oTable.Range.ParagraphFormat.SpaceAfter = 6;
oTable.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
oTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
for (int r = 1; r <= ds.Tables[0].Rows.Count; r++)
{
for (int c = 1; c <= ds.Tables[0].Columns.Count; c++)
{
oTable.Cell(r, c).Range.Text = ds.Tables[0].Rows[r-1].ItemArray[c-1].ToString();
}
}
Here is the sample code to create simple word file with Table. It will give you a loop hint how to proceed with it.
class Program
{
static void Main(string[] args)
{
Document doc = new Document();
Section s = doc.AddSection();
Table table = s.AddTable(true);
//Create Header and Data
String[] header = { "Item", "Description", "Qty", "Unit Price", "Price" };
String[][] data = {
new String[]{ "Spire.Doc for .NET",".NET Word Component","1","$799.00","$799.00"},
new String[]{"Spire.XLS for .NET",".NET Excel Component","2","$799.00","$1,598.00"},
new String[]{"Spire.Office for .NET",".NET Office Component","1","$1,899.00","$1,899.00"},
new String[]{"Spire.PDF for .NET",".NET PDFComponent","2","$599.00","$1,198.00"},
};
//Add Cells
table.ResetCells(data.Length + 1, header.Length);
//Header Row
TableRow fRow = table.Rows[0];
fRow.IsHeader = true;
//Row Height
fRow.Height = 23;
//Header Format
fRow.RowFormat.BackColor = Color.AliceBlue;
for (int i = 0; i < header.Length; i++)
{
//Cell Alignment
Paragraph p = fRow.Cells[i].AddParagraph();
fRow.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
p.Format.HorizontalAlignment = HorizontalAlignment.Center;
//Data Format
TextRange TR = p.AppendText(header[i]);
TR.CharacterFormat.FontName = "Calibri";
TR.CharacterFormat.FontSize = 14;
TR.CharacterFormat.TextColor = Color.Teal;
TR.CharacterFormat.Bold = true;
}
//Data Row
for (int r = 0; r < data.Length; r++)
{
TableRow dataRow = table.Rows[r + 1];
//Row Height
dataRow.Height = 20;
//C Represents Column.
for (int c = 0; c < data[r].Length; c++)
{
//Cell Alignment
dataRow.Cells[c].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
//Fill Data in Rows
Paragraph p2 = dataRow.Cells[c].AddParagraph();
TextRange TR2 = p2.AppendText(data[r][c]);
//Format Cells
p2.Format.HorizontalAlignment = HorizontalAlignment.Center;
TR2.CharacterFormat.FontName = "Calibri";
TR2.CharacterFormat.FontSize = 12;
TR2.CharacterFormat.TextColor = Color.Brown;
}
}
//Save and Launch
doc.SaveToFile("WordTable.docx", FileFormat.Docx2013);
System.Diagnostics.Process.Start("WordTable.docx");
Console.ReadLine();
}
for (var i = 0; i <= data.GetUpperBound(0); i++)
{
var tr = new DocumentFormat.OpenXml.Wordprocessing.TableRow();
for (var j = 0; j <= data.GetUpperBound(1); j++)
{
var tc = new DocumentFormat.OpenXml.Wordprocessing.TableCell();
tc.Append(new DocumentFormat.OpenXml.Wordprocessing.Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(new DocumentFormat.OpenXml.Wordprocessing.Text(data[i, j]))));
tr.Append(tc);
}
table.Append(tr);
}
I want to change fontsize in table cell. Can you help me with that? I don't know why they didn't add a property for cell fontsize.
To change the fontsize of a table cell, you need to add a RunProperties to the Run. The fontsize is specified inside a FontSize element inside that RunProperties.
For example to change all of your entries to fontsize 18, your code would look like:
for (var i = 0; i <= data.GetUpperBound(0); i++)
{
var tr = new DocumentFormat.OpenXml.Wordprocessing.TableRow();
for (var j = 0; j <= data.GetUpperBound(1); j++)
{
var tc = new DocumentFormat.OpenXml.Wordprocessing.TableCell();
var paragraph = new DocumentFormat.OpenXml.Wordprocessing.Paragraph();
var run = new DocumentFormat.OpenXml.Wordprocessing.Run();
var text = new DocumentFormat.OpenXml.Wordprocessing.Text(data[i, j]);
// your old code for reference: tc.Append(new DocumentFormat.OpenXml.Wordprocessing.Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(new DocumentFormat.OpenXml.Wordprocessing.Text(data[i, j]))));
RunProperties runProperties1 = new RunProperties();
FontSize fontSize1 = new FontSize(){ Val = "36" };
runProperties1.Append(fontSize1);
run.Append(runProperties1);
run.Append(text);
paragraph.Append(run);
tc.Append(paragraph);
tr.Append(tc);
}
table.Append(tr);
}
int counter;
CheckBox[] _cbs;
ScrollLabel._lines contain 151l ines.
counter is int
I want that on line 0 and then each 3 lines to add a checkbox in the beginning of the line with one space to the right so there will be one space place between the text in the line and the checkBox.
Now im getting exception in the loop on the line:
private void button1_Click(object sender, EventArgs e)
{
if (this.button1.Text == "stop")
{
this.button1.Text = "start";
this.scrollLabel1.StopTimer();
for (int i = 0; i < ScrollLabel._lines.Length; i++)
{
counter += 3;
_cbs[i] = new CheckBox();
_cbs[i].Location = new Point(0, counter);
this.scrollLabel1.Controls.Add(_cbs[i]);
}
}
else
{
this.button1.Text = "stop";
this.scrollLabel1.MilliSecsSpeed = (int)this.numericUpDown1.Value;
this.scrollLabel1.StartTimer();
}
}
_cbs[i] = new CheckBox();
_cbs is null.
This is the constructor:
counter = 0;
this.scrollLabel1.MouseWheel += new MouseEventHandler(ScrollLabel1_MouseWheel);
this.scrollLabel1.MouseEnter += ScrollLabel1_MouseEnter;
readableRss = RssReader.covertRss("http://rotter.net/rss/rotternews.xml");
RssReader.CnnRss();
this.DoubleBuffered = true;
this.scrollLabel1.Text = readableRss;//File.ReadAllText(#"Demo.txt");
this.scrollLabel1.MilliSecsSpeed = (int)this.numericUpDown1.Value;
this.scrollLabel1.YStep = (float)this.numericUpDown2.Value;
this.scrollLabel1.Words = new List<WordColor>();
this.scrollLabel1.Words.Add(new WordColor() { WordOrText = "scrollLabel1", ForeColor = Color.Red, ColorOnlyThisWord = true, BackColor = Color.LightBlue });
this.scrollLabel1.Words.Add(new WordColor() { WordOrText = "using", ForeColor = Color.Blue, DrawRect = true, RectColor = Color.Black, BackColor = Color.Wheat });
this.scrollLabel1.PopLinesOnNonBmpMode = this.checkBox6.Checked;
this.scrollLabel1.BitmapModus = this.checkBox4.Checked;
this.scrollLabel1.TextLayoutCentered = this.checkBox5.Checked;
this.scrollLabel1.AdditionalLinesAtEnd = (int)this.numericUpDown3.Value;
for (int i = 0; i < ScrollLabel._lines.Length; i++)
{
_cbs = new CheckBox[i];
}
This is how the loop in the button click look like now:
for (int i = 0; i < ScrollLabel._lines.Length; i++)
{
counter += 3;
_cbs[i].Location = new Point(0, counter);
this.scrollLabel1.Controls.Add(_cbs[i]);
}
But all the _cbs are null inside.
EDIT**
for (int i = 0; i < ScrollLabel._lines.Length; i++)
{
_cbs[i] = new CheckBox();
counter += 3;
_cbs[i].Location = new Point(0, counter);
this.scrollLabel1.Controls.Add(_cbs[i]);
}
Not getting null but i dont see checkBox near/in the beginning of each 3 lines why ?
_cbs is an array which you have declared and you are reallocating in your constructor:
for (int i = 0; i < ScrollLabel._lines.Length; i++)
{
_cbs = new CheckBox[i]; // This line reallocates the array as a larger array each time through the loop!
}
You need to define the array only once:
CheckBox[] _cbs = new CheckBox[ScrollLabel._lines.Length];
Then you allocate individual checkboxes to the elements of the array:
for (int i = 0; i < ScrollLabel._lines.Length; i++) {
_cbs[i] = new CheckBox(); // This allocates a new checkbox for the i-th element
}
To place a checkbox on every third line, try something like this:
int lineHeight = 20; // pixels, set this to whatever your line height is
int checkboxInterval = lineHeight * 3; // every third line
int numCheckboxes = ScrollLabel._lines.Length / 3;
for (int i = 0; i < numCheckboxes; i++) {
_cbs[i] = new CheckBox();
_cbs[i].Location = new Point(0, i * checkboxInterval);
this.scrollLabel1.Controls.Add(_cbs[i]);
}
Note that you only need one-third as many checkboxes as the number of lines.
I want to dynamically create a excel-like table with datagridview, but I want to have the option to set colspan to some columns.
The idea is just to display data, the user will not type anything, but it should be in table/spreadsheet look. If I cannot have datagridview with colspan is there any other type of table-like tool which has a colspan?
I other hand the columns will be created dynamically from database query result.
I'm using windows forms.
Any ideas?
You might take a look at the TableLayoutPanel Class, which has a TableLayoutPanel.SetColumnSpan Method.
Here's a code sample, which spans the one of the text boxes on 2 columns:
var dt = new DataTable();
dt.Columns.Add("Id");
dt.Columns.Add("Value1");
dt.Columns.Add("Value2");
dt.Rows.Add(1, "aa", "xx");
dt.Rows.Add(2, "bb","yy");
dt.Rows.Add(3, "cc", "zz");
tableLayoutPanel1.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
tableLayoutPanel1.ColumnCount = 4;
tableLayoutPanel1.AutoSize = true;
for (int i = 0; i < dt.Columns.Count; i++)
{
var l = new Label();
l.Dock = DockStyle.Fill;
l.Text = dt.Columns[i].ColumnName;
tableLayoutPanel1.Controls.Add(l, i, 0);
}
var emptyLabel = new Label();
emptyLabel.Text = "Empty label";
tableLayoutPanel1.Controls.Add(emptyLabel, 4, 0);
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Rows[i].ItemArray.Length; j++)
{
var tb = new TextBox();
tb.Multiline = true;
tb.Dock = DockStyle.Fill;
tb.Text = dt.Rows[i][j].ToString();
tableLayoutPanel1.Controls.Add(tb, j, i+1);
if (i == 1 && j == 2)
tableLayoutPanel1.SetColumnSpan(tb, 2);
}
}