OpenXML / Xceed insert Table without empty lines - ms-word

I have an existing.docx document with some text. All I want is to insert programmatically a table to a specific place. So my idea was to add a Keyword where the table should be inserted. There are no empty lines, before and after the keyword.
After the insertion of the table I add \n, before and after the table, for an empty line but somehow the Xceed library adds three after the table and two before the table.
Here is how I'm doing it:
using (DocX document = DocX.Load(#"C:\Users\rk\Desktop\test.docx"))
{
var IntTableSoftwareLocation = document.FindAll("Table").FirstOrDefault();
document.ReplaceText("Table", "");
var tableSoftware = document.InsertTable(IntTableSoftwareLocation, 3, 5);
tableSoftware.InsertParagraphBeforeSelf("\n");
tableSoftware.InsertParagraphAfterSelf("\n");
tableSoftware.SetBorder(TableBorderType.InsideH, new Border());
tableSoftware.SetBorder(TableBorderType.InsideV, new Border());
tableSoftware.SetBorder(TableBorderType.Left, new Border());
tableSoftware.SetBorder(TableBorderType.Bottom, new Border());
tableSoftware.SetBorder(TableBorderType.Top, new Border());
tableSoftware.SetBorder(TableBorderType.Right, new Border());
//Header
tableSoftware.Rows[0].Cells[0].Paragraphs[0].Append("Col1").Bold().Font("Arial").FontSize(11d);
tableSoftware.Rows[0].Cells[1].Paragraphs[0].Append("Col2").Bold().Font("Arial").FontSize(11d);
tableSoftware.Rows[0].Cells[2].Paragraphs[0].Append("Col3").Bold().Font("Arial").FontSize(11d);
tableSoftware.Rows[0].Cells[3].Paragraphs[0].Append("Col4.").Bold().Font("Arial").FontSize(11d);
tableSoftware.Rows[0].Cells[4].Paragraphs[0].Append("Col5").Bold().Font("Arial").FontSize(11d);
//Content
string TextToInsert = "Some Text";
//Column width
for (int i = 0; i < tableSoftware.RowCount; i++)
{
for (int x = 0; x < tableSoftware.ColumnCount; x++)
{
#region set column width
if (x == 0)
{
tableSoftware.Rows[i].Cells[x].Width = 28.3; // 1cm
}
else if (x == 1)
{
tableSoftware.Rows[i].Cells[x].Width = 318;
}
else if (x == 2)
{
tableSoftware.Rows[i].Cells[x].Width = 50;
}
else if (x == 3)
{
tableSoftware.Rows[i].Cells[x].Width = 28.3;
}
else if (x == 4)
{
tableSoftware.Rows[i].Cells[x].Width = 64;
}
#endregion
}
}
tableSoftware.Rows[2].Cells[1].Paragraphs[0].Append(TextToInsert + "\n").FontSize(11d).Bold().Font("Arial");
tableSoftware.Rows[2].Cells[2].Paragraphs[0].Append("User").Font("Arial").Alignment = Alignment.center;
tableSoftware.Rows[2].Cells[2].VerticalAlignment = VerticalAlignment.Center;
tableSoftware.Rows[2].Cells[3].Paragraphs[0].Append("1").Font("Arial").Alignment = Alignment.center;
tableSoftware.Rows[2].Cells[3].VerticalAlignment = VerticalAlignment.Center;
tableSoftware.Rows[2].Cells[4].Paragraphs[0].Append("2.199,00 €").Font("Arial").Alignment = Alignment.right;
tableSoftware.Rows[2].Cells[4].VerticalAlignment = VerticalAlignment.Center;
document.Save();
}
And thats how my docx Document looks like:
laksjdf
Table
alskdfjs

Ok, this is how it should be done:
//Find the Paragraph by keyword
var paraTable = document.Paragraphs.FirstOrDefault(x => x.Text.Contains("Table"));
// Remove the Keyword
paraTable.RemoveText(0);
//Insert the table into Paragraph
var table = paraTable.InsertTableAfterSelf(3, 5);
No strange empty lines anymore

Related

Word Addin is getting crashed when trying to delete a row which contains a content control in a table

I'm developing a Word Add-in (Word API + Office.js) where i am working with content controls, I am trying to read the table content inside a content control where I need remove the empty rows
Sample: I have this table inside a content control I have to remove the blank rows
i am able to achieve this functionality with this code, but if the table contains a content control which is blank then when i try to delete that row the addin itself is getting crashed.
function checktable(element) {
Word.run(function (context) {
// Queue a command to get the current selection and then
// create a proxy range object with the results.
var contentControl = context.document.contentControls.getByTag('control1').getFirst();
var table = contentControl.tables.getFirst();
context.load(contentControl, 'tables');
table.load('values');
return context.sync()
.then(function () {
// Get the longest word from the selection.
if (contentControl.tables.items.length === 0) {
document.getElementById('lblstatus').innerText += "No Tables found";
}
else {
document.getElementById('lblstatus').innerText += " Tables found";
var Tablevaules = table.values;
for (var i = 0, len = Tablevaules.length; i < len; i++)
{
var nullcheck = "";
var inner=Tablevaules[i];
// inner loop applies to sub-arrays
for (var j = 0, len2 = inner.length; j < len2; j++) {
// accesses each element of each sub-array in turn
if (inner[j] == "") {
if (nullcheck != "False") {
nullcheck = "True";
}
}
else {
nullcheck = "False";
}
}
if (nullcheck == "True") {
table.deleteRows(i);
}
}
}
})
.then(context.sync)
.then(function () {
// Queue a command to highlight the search results.
document.getElementById('lblstatus').innerText += element + ":" + "Successs";
});
})
.catch(errorHandler);
}
Please let me know whether i am missing something or its a known bug!!

Arcobjects: set measures based on the polyline length

I am currently linear referencing a set of roads. Using IMSegmatation2.SetMsAsDistance2 works fine for single-part polylines but for multi-part polylines I want to set the M values as the length along the polyline to the point, not the shortest distance between the point and the start point of the polyline. The SetMsAsDistance2 function sets the Measures on parallel lines as equal. I want them to be different.
Apart from setting an M value for each polyline vertex does anyone know of a method that sets the M as the length along the polyline?
The solution is to use the IMSegmentation3.SetAndInterpolateMsBetween function.
Note that the solution assumes that the geometries that make up the feature are in the right order. It assigns measures geometry-by-geometry.
The code for this is:
public static void measuresAdd_NonDuplicating(ref IFeature pFeat, bool bHasZ, bool bIgnoreGaps, out string sError)
{
// Add non-duplicating measures to the feature
sError = "";
IMSegmentation3 pSeg;
IGeometryCollection pGeomColl;
//IGeometryCollection pNewGeomColl; // Use if geometries are to be re-ordered.
IGeometry pGeom;
IPolyline4 pPoly;
IQueryFilter pQ;
IMAware pMAware;
IZAware pZAware;
double dStartMeasure;
double dToMeasure;
double dMeasure;
double dLen;
try
{
if (pFeat.ShapeCopy.IsEmpty == false)
{
pGeomColl = (IGeometryCollection)new PolylineClass();
pGeomColl = (IGeometryCollection)pFeat.ShapeCopy;
if (pGeomColl.GeometryCount == 1)
{
// Single line geometry. Duplication not an issue.
pMAware = (IMAware)pFeat.ShapeCopy;
pMAware.MAware = true;
pSeg = (IMSegmentation3)pMAware;
pPoly = geometryToPolyline((IGeometry)pFeat.ShapeCopy, out sError);
if (sError.Length > 0)
{
sError = "measuresAdd_NonDuplicating\r\n" + sError;
return;
}
pSeg.SetMsAsDistance2(pPoly.FromPoint, 1, 0, bIgnoreGaps);
pFeat.Shape = (IGeometry)pSeg;
pFeat.Store();
}
else
{
// For re-ordering geometries. Not currently used.
//pNewGeomColl = (IGeometryCollection)new Polyline();
//IZAware pZawareNew = (IZAware)pNewGeomColl;
//pZawareNew.ZAware = bHasZ;
//IMAware pMAwareNew = (IMAware)pNewGeomColl;
//pMAwareNew.MAware = true;
dStartMeasure = 0;
dMeasure = 0;
// MultiGeometry. Place them in order and set the measures on each part increasing.
// Currently assumes the existing order is correct.
for (int i = 0; i < pGeomColl.GeometryCount; i++)
{
pGeom = pGeomColl.Geometry[i];
pPoly = geometryToPolyline(pGeom, out sError);
if (sError.Length > 0)
{
sError = "measuresAdd_NonDuplicating\r\n" + sError;
return;
}
// Measure Values
dStartMeasure = dMeasure;
if (i > 0) dStartMeasure += 0.01;
dLen = pPoly.Length;
dToMeasure = dMeasure + dLen;
// Set Measures
pMAware = (IMAware)pPoly;
pMAware.MAware = true;
pZAware = (IZAware)pPoly;
pZAware.ZAware = bHasZ;
pSeg = (IMSegmentation3)pPoly;
pSeg.SetAndInterpolateMsBetween(dStartMeasure, dToMeasure);
// If geometries are re-ordered into a connecting network
//IGeometryCollection pXGeomColl = new PolylineClass();
//pMAware = (IMAware)pXGeomColl;
//pMAware.MAware = true;
//pZAware = (IZAware)pXGeomColl;
//pZAware.ZAware = bHasZ;
//pXGeomColl = (IGeometryCollection)pPoly;
//for (int j = 0; j < pXGeomColl.GeometryCount; j++)
// pNewGeomColl.AddGeometry(pXGeomColl.Geometry[j]);
dMeasure += dLen;
}
pFeat.Shape = (IGeometry)pGeomColl;
}
}
}
catch (Exception ex)
{
System.Diagnostics.StackTrace pStack = new System.Diagnostics.StackTrace(ex, true);
System.Diagnostics.StackFrame pFrame = pStack.GetFrame(pStack.FrameCount - 1);
int iLineNo = pFrame.GetFileLineNumber();
sError = "ERROR: measuresAdd_NonDuplicating; Line: " + iLineNo + "\n" + ex.ToString();
}
}

Chart.js click on labels, using bar chart

i need help with my Chart.js interactivity. When I click on the label, I need to return the column(index) number at which I clicked.
I tried to use getElementsAtEvent but it only work if I click directly at chart.
This http://jsfiddle.net/yxz2sjam/ is pretty much what I am looking for but getPointsAtEvent is no longer available in the new versions.
canvas.onclick = function (evt) {
var points = chart.getPointsAtEvent(evt);
alert(chart.datasets[0].points.indexOf(points[0]));
};
I also found this http://jsfiddle.net/1Lngmtz7/ but it isn't working with bar chart.
var ctx = document.getElementById("myChart").getContext("2d");
var myRadarChart = new Chart(ctx, {
type: 'radar',
data: data
})
$('#myChart').click(function (e) {
var helpers = Chart.helpers;
var eventPosition = helpers.getRelativePosition(e, myRadarChart.chart);
var mouseX = eventPosition.x;
var mouseY = eventPosition.y;
var activePoints = [];
helpers.each(myRadarChart.scale.ticks, function (label, index) {
for (var i = this.getValueCount() - 1; i >= 0; i--) {
var pointLabelPosition = this.getPointPosition(i, this.getDistanceFromCenterForValue(this.options.reverse ? this.min : this.max) + 5);
var pointLabelFontSize = helpers.getValueOrDefault(this.options.pointLabels.fontSize, Chart.defaults.global.defaultFontSize);
var pointLabeFontStyle = helpers.getValueOrDefault(this.options.pointLabels.fontStyle, Chart.defaults.global.defaultFontStyle);
var pointLabeFontFamily = helpers.getValueOrDefault(this.options.pointLabels.fontFamily, Chart.defaults.global.defaultFontFamily);
var pointLabeFont = helpers.fontString(pointLabelFontSize, pointLabeFontStyle, pointLabeFontFamily);
ctx.font = pointLabeFont;
var labelsCount = this.pointLabels.length,
halfLabelsCount = this.pointLabels.length / 2,
quarterLabelsCount = halfLabelsCount / 2,
upperHalf = (i < quarterLabelsCount || i > labelsCount - quarterLabelsCount),
exactQuarter = (i === quarterLabelsCount || i === labelsCount - quarterLabelsCount);
var width = ctx.measureText(this.pointLabels[i]).width;
var height = pointLabelFontSize;
var x, y;
if (i === 0 || i === halfLabelsCount)
x = pointLabelPosition.x - width / 2;
else if (i < halfLabelsCount)
x = pointLabelPosition.x;
else
x = pointLabelPosition.x - width;
if (exactQuarter)
y = pointLabelPosition.y - height / 2;
else if (upperHalf)
y = pointLabelPosition.y - height;
else
y = pointLabelPosition.y
if ((mouseY >= y && mouseY <= y + height) && (mouseX >= x && mouseX <= x + width))
activePoints.push({ index: i, label: this.pointLabels[i] });
}
}, myRadarChart.scale);
var firstPoint = activePoints[0];
if (firstPoint !== undefined) {
alert(firstPoint.index + ': ' + firstPoint.label);
}
});
Thank for response.
I solve the problem with
document.getElementById("chart").onclick = function(e)
{
var activeElement = weatherMainChart.lastTooltipActive;
console.log(activeElement[0]._index);
};
this solution register clicks on chart and label, then I restricted it with e.layerY to register only clicks on label section.
document.getElementById("chart").onclick = function(e)
{
var activeElement = weatherMainChart.lastTooltipActive;
if(e.layerY > 843 && e.layerY < 866 && activeElement[0] !== undefined)
console.log(activeElement[0]._index);
};
If you add a click handler through the onClick option you can use the following code using the getElementsAtEventForMode() call:
function handleClick(evt) {
var col;
switch(chartType) {
case "horizontalBar":
this.getElementsAtEventForMode(evt, "y", 1).forEach(function(item) { col = item._index });
break;
case "bar":
this.getElementsAtEventForMode(evt, "x", 1).forEach(function(item) { col = item._index });
break;
}
if (!col) {
return;
}
alert("Column " + col + " was selected");
};
You'll probably need to add extra switch checks for other chart types but I'm sure you get the idea.
Using version 2.4.0, i created an onClick Event, and inside it
var activeIndex = localChart.tooltip._lastActive[0]._index;
var clickCoordinates = Chart.helpers.getRelativePosition(e, localChart.chart);
if (clickCoordinates.y >= 530) { //custom value, depends on chart style,size, etc
alert("clicked on " + localChart.data.labels[activeIndex]);
}
I Solved this problem with single or multiple label click you will be find using true/false
First you need to set your chartJs Id click
below code SessionChart = Your ChartJs ID e.g. ("myChart") I was replace it for my Id
document.getElementById("SessionChart").onclick = function (evt) {
var meta = SubscriberSessionChart.getDatasetMeta(0);
if (meta.$filler.chart.legend.legendItems[0].text.toLowerCase() "sessions")
{
if (meta.$filler.chart.legend.legendItems[0].hidden) {
sessionHidden = true;
}
}
}
here "sessions" = first label text
meta.$filler.chart.legend.legendItems[0].text.toLowerCase() = is your first label
from Array so you can get multiple label's click here true / false
if (meta.$filler.chart.legend.legendItems[0].hidden) = your label is not active then
you will get hidden true otherwise you will get false if not tick on label
by default label tick hidden is false in chart js

inserting into SQL via sqlbulk

hallo i have this sniped code like this:
public static void Put_CSVtoSQL_Adhesion()
{
bool IsFirst = true;
DataTable dt = new DataTable();
string line = null;
int i = 0;
try
{
string fileName = Path.Combine(HttpContext.Current.Server.MapPath(UploadDirectory), TheFileName);
using (StreamReader sr = File.OpenText(fileName))
{
while ((line = sr.ReadLine()) != null)
{
string[] data = line.Split(';');
if (data.Length > 0)
{
if (i == 0)
{
foreach (var item in data)
{
dt.Columns.Add(new DataColumn());
}
i++;
}
DataRow row = dt.NewRow();
row.ItemArray = data;
// Pour enlever la tete
if (!IsFirst) dt.Rows.Add(row);
IsFirst = false;
}
}
}
using (var connectionWrapper = new Connexion())
{
var connectedConnection = connectionWrapper.GetConnected();
using (SqlBulkCopy copy = new SqlBulkCopy(connectionWrapper.conn))
{
int CountColl = dt.Columns.Count;
copy.ColumnMappings.Add(0, 1);
copy.ColumnMappings.Add(1, 2);
copy.ColumnMappings.Add(2, 3);
copy.ColumnMappings.Add(3, 4);
copy.ColumnMappings.Add(4, 5);
copy.DestinationTableName = "cotisation";
copy.WriteToServer(dt);
}
}
}
catch (Exception excThrown)
{
throw new Exception(excThrown.Message);
}
}
this code work well, but now i have more than 60 column, should i type manualy from 1 to 60 column or there are another methode ?
copy.ColumnMappings.Add(0, 1);
copy.ColumnMappings.Add(1, 2);
copy.ColumnMappings.Add(2, 3);
copy.ColumnMappings.Add(3, 4);
copy.ColumnMappings.Add(4, 5);
...until 60 column ?
the column is all the same i just shifted 1 column, because the first one is autoincremented column as an ID
Write a loop?
for (int i = 0; i < dt.Columns.Count - 1; i++)
{
copy.ColumnMappings.Add(i, i + 1);
}

Fix the cell width in Word using Open XML SDK

I have a table with one row in a docx file and I want to add some rows. For the first existing row I set the GridSpan to 3. In the next one I want add a row only with two cell, then I have the result as on the picture. How can I fix the new row width to the table width? The same I want to do, when I add only a one cell in the new row.
My code:
private static TableRow AddRow(WordprocessingDocument docx, Table tbl, int cellsQuantity)
{
TableRow newRow = new TableRow();
var firstCell = tbl.Descendants<TableRow>().First()
.Descendants<TableCell>().First();
firstCell.Append(new TableCellProperties(new GridSpan() { Val = 3 }));
for (int i = 0, max = cellsQuantity; i < max; i++)
{
// TableCellProperties tcp = new TableCellProperties(new TableCellWidth() { Width = firstCell.TableCellProperties.TableCellWidth.Width, Type = firstCell.TableCellProperties.TableCellWidth.Type });
TableCell cell = new TableCell(new Paragraph(new Run(new Text("test"))));
newRow.AppendChild(cell);
}
tbl.Append(newRow);
return newRow;
}
Ok, I thing I got it :)
private static TableRow AddRow(WordprocessingDocument docx, Table tbl, int cellsQuantity)
{
TableRow newRow = new TableRow();
var grid = tbl.Descendants<TableGrid>().First();
var firstCell = tbl.Descendants<TableRow>().First()
.Descendants<TableCell>().First();
firstCell.Append(new TableCellProperties(new GridSpan() { Val = 4 }));
int ind = 0;
int[] widthPerColumn = new int[] { 3070, 1536, 1535, 3071 };
grid.Descendants<GridColumn>().ToList().ForEach(x =>
{
x.Width = widthPerColumn[ind++].ToString();
});
int[] gridSpan = null;
switch (cellsQuantity)
{
case 4:
gridSpan = new int[] { 1, 1, 1, 1 };
break;
case 3:
gridSpan = new int[] { 1, 2, 1 };
break;
case 2:
gridSpan = new int[] { 2, 2 };
break;
case 1:
gridSpan = new int[] { 4 };
break;
default:
throw new InvalidOperationException("The cellsQuantity variable must have a value from [1,4] range");
}
for (int i = 0, max = cellsQuantity; i < max; i++)
{
TableCellProperties tcp = new TableCellProperties(new GridSpan() { Val = gridSpan[i] });
TableCell cell = new TableCell(tcp, new Paragraph(new Run(new Text("test"))));
newRow.AppendChild(cell);
}
tbl.Append(newRow);
return newRow;
}
TableCell tCell = new TableCell();
tCell.Append(new TableCellProperties(
new GridSpan { Val = table.LastChild.ChildElements.Count },
new Justification { Val = JustificationValues.Right })
);