I try crossout row in table. Line must be over all cells in center vertical.
So I add new shape to cell, like this:
var cell = node as Cell;
var width = cell.CellFormat.Width;
var line = new Shape(args.Document, ShapeType.Line);
line.Width = width;
line.HorizontalAlignment = HorizontalAlignment.Center;
line.RelativeVerticalPosition = RelativeVerticalPosition.Paragraph;
line.Top = 5;
line.BehindText = true;
line.WrapType = WrapType.None;
line.StrokeColor = Color.Black;
line.Stroke.LineStyle = ShapeLineStyle.Single;
line.StrokeWeight = 1;
_builder.MoveTo(cell.LastParagraph);
_builder.InsertNode(line);
But this work only when I have single text line in cell, if is two or more line text, my crossout line is not center:
How fix it?
Maybe is other solution for crossout whole row?
You can build logic on the following code that inserts a full-width Line Shape at the middle of each Cell in Table.
Document doc = new Document("E:\\temp\\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
LayoutCollector collector = new LayoutCollector(doc);
LayoutEnumerator enumerator = new LayoutEnumerator(doc);
foreach (Table table in doc.FirstSection.Body.Tables)
{
foreach (Row row in table.Rows)
{
foreach (Cell cell in row.Cells)
{
enumerator.Current = collector.GetEntity(cell.FirstParagraph);
while (enumerator.Type != LayoutEntityType.Cell)
{
enumerator.MoveParent();
}
double top = enumerator.Rectangle.Top + (enumerator.Rectangle.Height / 2);
double left = enumerator.Rectangle.Left;
double width = enumerator.Rectangle.Width;
builder.MoveTo(table.NextSibling);
Shape line = builder.InsertShape(ShapeType.Line, width, 0);
line.Top = top;
line.Left = left;
line.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
line.RelativeVerticalPosition = RelativeVerticalPosition.Page;
line.BehindText = true;
line.WrapType = WrapType.None;
line.StrokeColor = Color.Blue;
line.Stroke.LineStyle = ShapeLineStyle.Single;
line.StrokeWeight = 1;
}
}
}
doc.Save("E:\\temp\\19.1.docx");
Hope, this help. I work with Aspose as Developer Evangelist.
Related
How to remove this large blank space between my chart area and the legends? See that I even tried different docking styles but the gap is still there.
Any ideas?
I import the data from a CSV file, generated by another application. Every column is a new data series but they are all added into the same chart and chartArea element.
// Load data from CSV into a DataTable
DataTable dataTable = ReadCsvFile(System.IO.Path.GetFileName("mqlWeekRace.txt"), false);
Chart chart1 = new Chart();
chart1.Height = 1000;
chart1.Width = 1500;
chart1.ChartAreas.Add("ChartArea1");
chart1.ChartAreas["ChartArea1"].AxisX.MajorGrid.LineWidth = 1;
chart1.ChartAreas["ChartArea1"].AxisX.MajorGrid.LineDashStyle = ChartDashStyle.Dot;
chart1.ChartAreas["ChartArea1"].AxisY.MajorGrid.LineWidth = 1;
chart1.ChartAreas["ChartArea1"].AxisY.MajorGrid.LineDashStyle = ChartDashStyle.Dot;
chart1.ChartAreas["ChartArea1"].AxisX.IsMarginVisible = false;
// chart1.ChartAreas["ChartArea1"].AxisY.IsStartedFromZero = false;
// Line colors
string[] myColors = new string[] { "Blue", "DarkOrange", "Firebrick", "DarkSeaGreen", "DarkViolet", "Sienna",
"DarkOrchid", "IndianRed", "ForestGreen", "Teal", "MidnightBlue", "DimGray" };
int colorIndex = 0;
foreach (DataColumn col in dataTable.Columns)
{
if (!col.ColumnName.StartsWith("Data") && col.ColumnName.Length > 1)
{
chart1.Series.Add(col.ColumnName.ToString());
chart1.Series[col.ColumnName].ChartArea = "ChartArea1";
chart1.Legends.Add(new Legend(col.ColumnName));
chart1.Legends[col.ColumnName].LegendStyle = LegendStyle.Column;
chart1.Legends[col.ColumnName].Docking = Docking.Top;
chart1.Legends[col.ColumnName].Font = new Font(FontFamily.GenericSansSerif, 15);
chart1.Series[col.ColumnName].ChartType = SeriesChartType.Line;
chart1.Series[col.ColumnName].XValueMember = "Data";
chart1.Series[col.ColumnName].YValueMembers = col.ColumnName.ToString();
chart1.Series[col.ColumnName].XValueType = ChartValueType.Date;
chart1.Series[col.ColumnName].BorderWidth = 3;
chart1.Series[col.ColumnName].Color = System.Drawing.Color.FromName(myColors[colorIndex]);
colorIndex++;
}
}
chart1.DataManipulator.IsStartFromFirst = true;
chart1.DataSource = dataTable;
chart1.DataBind();
I have the code for generating an Excel sheet and I want to set the Row height there.
The code is as follows:
Columns lstColumns = sheet1.Worksheet.GetFirstChild<Columns>();
bool needToInsertColumns = false;
if (lstColumns == null)
{
lstColumns = new Columns();
needToInsertColumns = true;
}
lstColumns.Append(
new Column() { Min = 1, Max = 1, Width = 120, CustomWidth = true }
);
if (needToInsertColumns)
{
sheet1.Worksheet.InsertAt(lstColumns, 0);
}
// For rows.
Row row;
row = new Row() { RowIndex = 1 };
row.Height = 100;
sheetData.Append(row);
Through the following code I am able to set the width but not the height.
Do the following:
Row row;
row = new Row() { RowIndex = 1 };
row.Height = 10.0;
row.CustomHeight = true;
or, better:
var row = new Row { Height = 10.0, CustomHeight = true, RowIndex = 1 };
The CustomHeight property is important here.
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;
I am trying to create a column with WrapText set to true for an XLSX file using NPOI.
The following does not seem to work:
var workbook = new XSSFWorkbook();
var headerRow = sheet.CreateRow(0);
var cellType = CellType.String;
var colStyle = sheet.GetColumnStyle(3);
colStyle.WrapText = true;
sheet.SetDefaultColumnStyle(3, colStyle);
headerRow.CreateCell(0, cellType).SetCellValue("Name");
headerRow.CreateCell(3, cellType).SetCellValue("Comments");
var font = workbook.CreateFont();
font.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.Bold;
XSSFCellStyle style = (XSSFCellStyle)workbook.CreateCellStyle();
style.SetFont(font);
style.FillPattern = NPOI.SS.UserModel.FillPattern.SolidForeground;
style.FillForegroundColor = IndexedColors.Grey25Percent.Index;
style.IsLocked = false;
style.WrapText = true;
for (int i = 0; i < 6; i++)
{
var cell = headerRow.Cells[i];
cell.CellStyle = style;
}
headerRow.RowStyle = style;
sheet.SetColumnWidth(0, 30 * 256);
sheet.SetColumnWidth(1, 20 * 256);
sheet.SetColumnWidth(2, 20 * 256);
sheet.SetColumnWidth(3, 50 * 256);
sheet.SetColumnWidth(4, 30 * 256);
sheet.SetColumnWidth(5, 50 * 256);
int rowNumber = 1;
style = (XSSFCellStyle)workbook.CreateCellStyle();
style.IsLocked = false;
style.WrapText = true;
foreach (MemberListViewModel member in members)
{
var row = sheet.CreateRow(rowNumber++);
row.CreateCell(0).SetCellValue(member.FullName);
row.CreateCell(3).SetCellValue(member.endowed_professorship);
}
workbook.Write(output);
Naturally, the documentation is a little light for this one.
Any suggestions on changing the code? Otherwise, it seems to work fine.
I just can't get the wraptext to work.
EDIT:
After posting the question, I revised my code to use Reflection to detach the code from the individual classes (there are several I work with). This uses a modified Kendo interface that has the Excel Export button on the grid.
The revised code includes modifications included in the answer:
rowNumber = 1;
var cellStyle = (XSSFCellStyle)workbook.CreateCellStyle();
cellStyle.WrapText = true;
foreach (var member in members)
{
String value = "";
var aType = member.GetType();
var real = aType.GetProperty(headings[i]);
if (real != null)
{
value = (real.GetValue(member) != null) ? String.Format(format, real.GetValue(member)) : "";
}
var row = sheet.GetRow(rowNumber++);
cell = row.CreateCell(i);
cell.SetCellValue(new XSSFRichTextString(value));
cell.CellStyle = cellStyle;
}
This code now produces the desired result. I also added the XSSRichTextString to the formatting just for good measure.
"CellStyle.WrapText" set After Binding CellStyle ,its working For Me.
Solution:
ICellStyle CellCentertTopAlignment = workbook.CreateCellStyle();
CellCentertTopAlignment = workbook.CreateCellStyle();
CellCentertTopAlignment.SetFont(fontArial16);
CellCentertTopAlignment.Alignment = HorizontalAlignment.Center;
CellCentertTopAlignment.VerticalAlignment = VerticalAlignment.Top;
ICell Row1 = Row1.CreateCell(1);
Row1.SetCellValue(new HSSFRichTextString("I find a solution for this Problem.."));
Row1.CellStyle = CellCentertTopAlignment;
Row1.CellStyle.WrapText = true;
I think you forgot to change the cell's style. You're setting it only on the header.
This:
row.CreateCell(0).SetCellValue(member.FullName);
row.CreateCell(3).SetCellValue(member.endowed_professorship);
Should be something like:
var cell0 = row.CreateCell(0);
cell0.SetCellValue(member.FullName);
cell0.Style = style;
var cell3 = row.CreateCell(3);
cell3.SetCellValue(member.endowed_professorship);
cell3.Style = style;
There's probably a better way of doing this; I'm no NPOI specialist. =)
I am using Open XML SDK 2.5 to insert Tables in a Word Document. The issue I currently have is with the row height of my table heading. It works fine with normal text direction of Left to Right Top to Bottom LTTB. However as soon as I set the Text Direction to Bottom to Top Left to Right BTLR my heading rows do not adjust to fit the cell contents.
Code Below
void InsertTable(string[,] tableData, int numberOfRows, int numberOfColumns, string locationInDocument, string textDirectionHeadings)
{
using (WordprocessingDocument myDoc = WordprocessingDocument.Open(_newDocument, true))
{
var docPart = myDoc.MainDocumentPart;
var doc = docPart.Document;
var table = new Table();
var tableBorderTop = new TopBorder();
var tableBorderBottom = new BottomBorder();
var tableBorderLeft = new LeftBorder();
var tableBorderRight = new RightBorder();
var tableBorderHorizontal = new InsideHorizontalBorder();
var tableBorderVertical = new InsideVerticalBorder();
var tableProperties = new TableProperties();
var borders = new TableBorders();
// Set Border Styles for Table
tableBorderTop.Val = BorderValues.Single;
tableBorderTop.Size = 6;
tableBorderBottom.Val = BorderValues.Single;
tableBorderBottom.Size = 6;
tableBorderLeft.Val = BorderValues.Single;
tableBorderLeft.Size = 6;
tableBorderRight.Val = BorderValues.Single;
tableBorderRight.Size = 6;
tableBorderHorizontal.Val = BorderValues.Single;
tableBorderHorizontal.Size = 6;
tableBorderVertical.Val = BorderValues.Single;
tableBorderVertical.Size = 6;
// Assign Border Styles to Table Borders
borders.TopBorder = tableBorderTop;
borders.BottomBorder = tableBorderBottom;
borders.LeftBorder = tableBorderLeft;
borders.RightBorder = tableBorderRight;
borders.InsideHorizontalBorder = tableBorderHorizontal;
borders.InsideVerticalBorder = tableBorderVertical;
// Append Border Styles to Table Properties
tableProperties.Append(borders);
// Assign Table Properties to Table
table.Append(tableProperties);
//Adds the Table Headings for each Column
var tableRowHeader = new TableRow();
tableRowHeader.Append(new TableRowHeight() { HeightType = HeightRuleValues.Auto });
for (int i = 0; i < numberOfColumns; i++)
{
var tableCellHeader = new TableCell();
//Assign Font Properties to Run
var runPropHeader = new RunProperties();
runPropHeader.Append(new Bold());
runPropHeader.Append(new Color() { Val = "000000" });
//Create New Run
var runHeader = new Run();
//Assign Font Properties to Run
runHeader.Append(runPropHeader);
var columnHeader = new Text();
//Assign the Pay Rule Name to the Run
columnHeader = new Text(tableData[0, i]);
runHeader.Append(columnHeader);
//Create Properties for Paragraph
var justificationHeader = new Justification();
justificationHeader.Val = JustificationValues.Left;
var paraPropsHeader = new ParagraphProperties(justificationHeader);
SpacingBetweenLines spacing = new SpacingBetweenLines() { Line = "240", LineRule = LineSpacingRuleValues.Auto, Before = "0", After = "0" };
paraPropsHeader.Append(spacing);
var paragraphHeader = new Paragraph();
paragraphHeader.Append(paraPropsHeader);
paragraphHeader.Append(runHeader);
tableCellHeader.Append(paragraphHeader);
var tableCellPropertiesHeader = new TableCellProperties();
var tableCellWidthHeader = new TableCellWidth();
tableCellPropertiesHeader.Append(new Shading() { Val = ShadingPatternValues.Clear, Color = "auto", Fill = "#C0C0C0" });
var textDirectionHeader = new TextDirection();
if (textDirectionHeadings == "BTLR")
{
textDirectionHeader.Val = TextDirectionValues.BottomToTopLeftToRight;
}
if (textDirectionHeadings == "LRTB")
{
textDirectionHeader.Val = TextDirectionValues.LefToRightTopToBottom;
}
tableCellPropertiesHeader.Append(textDirectionHeader);
tableCellWidthHeader.Type = TableWidthUnitValues.Auto;
tableCellPropertiesHeader.Append(tableCellWidthHeader);
tableCellHeader.Append(tableCellPropertiesHeader);
tableRowHeader.Append(tableCellHeader);
}
tableRowHeader.AppendChild(new TableHeader());
table.Append(tableRowHeader);
//Create New Row in Table for Each Record
int r = 1;
for (int a = 0; a < (numberOfRows - 1); a++)
{
var tableRow = new TableRow();
for (int i = 0; i < numberOfColumns; i++)
{
var propertyText = tableData[r, i];
var tableCell = new TableCell();
//Assign Font Properties to Run
var runProp = new RunProperties();
runProp.Append(new Bold());
runProp.Append(new Color() { Val = "000000" });
//Create New Run
var run = new Run();
//Assign Font Properties to Run
run.Append(runProp);
//Assign the text to the Run
var text = new Text(propertyText);
run.Append(text);
//Create Properties for Paragraph
var justification = new Justification();
justification.Val = JustificationValues.Left;
var paraProps = new ParagraphProperties(justification);
var paragraph = new Paragraph();
paragraph.Append(paraProps);
paragraph.Append(run);
tableCell.Append(paragraph);
var tableCellProperties = new TableCellProperties();
var tableCellWidth = new TableCellWidth();
tableCellWidth.Type = TableWidthUnitValues.Auto;
tableCellProperties.Append(tableCellWidth);
tableCell.Append(tableCellProperties);
tableRow.Append(tableCell);
}
r = r + 1;
table.Append(tableRow);
};
var res = from bm in docPart.Document.Body.Descendants<BookmarkStart>()
where bm.Name == locationInDocument
select bm;
var bookmark = res.SingleOrDefault();
var parent = bookmark.Parent; // bookmark's parent element
Paragraph newParagraph = new Paragraph();
parent.InsertAfterSelf(newParagraph);
if (bookmark != null)
{
newParagraph.InsertBeforeSelf(table);
}
}
}