C# Microsoft Interop Word put shape inside table cell - c#

I'm trying to put a Microsoft.Office.Interop.Word.Shape inside a table cell like in this short example:
Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
Word.Document oDocument = oWord.Documents.Add();
Word.Table oTable = oDocument.Tables.Add(oDocument.Range(), 4, 1);
Word.Cell oCell1 = oTable.Cell(1,1);
Word.Shape oShape1 = oDocument.Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle.GetHashCode(), 7, 7, 11, 11, oCell1.Range);
Word.Cell oCell2 = oTable.Cell(2, 1);
Word.Shape oShape2 = oDocument.Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle.GetHashCode(), 7, 7, 11, 11, oCell2.Range);
Word.Cell oCell3 = oTable.Cell(3, 1);
Word.Shape oShape3 = oDocument.Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle.GetHashCode(), 7, 7, 11, 11, oCell3.Range);
Word.Cell oCell4 = oTable.Cell(4, 1);
Word.Shape oShape4 = oDocument.Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle.GetHashCode(), 7, 7, 11, 11, oCell4.Range);
oWord.Visible = true;
The rectangle only appears on the top left corner of the document.
I'm not sure what I'm doing wrong since I set the shape anchor to the cells range.
12/07/2016:
Okay, check this out,
I have now 5 columns and 100 rows and try to place the shape into the third column. I'm using the "in line with text" property.
Now, on the first two pages, all the shapes are placed in the first row/column. Starting at the third page it's looking right...
I'm using Office 2013.
Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
Word.Document oDocument = oWord.Documents.Add();
int numRows = 100;
Word.Table oTable = oDocument.Tables.Add(oDocument.Range(), numRows, 5);
oTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
oTable.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
for (int r = 1; r <= numRows; ++r)
{
Word.Range anchorRange = oTable.Cell(r, 3).Range;
Word.Shape oShape = oDocument.Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle.GetHashCode(), 7, 7, 11, 11, anchorRange);
oShape.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapInline;
}
oWord.Visible = true;

If you click on the rectangle that's added, and enable paragraph/formatting symbols (it's the backwards P looking button in Home > Paragraph), you'll notice that the anchors are indeed on the table cells. Text-wrapped shapes also need other settings to have certain positions on the page. Mess with the Size and Position dialog to get a feel for it.

Related

Word Interop: can't group shapes

I create 2 shapes (textbox), then i want to grop it. Shapes are selected but not grouped. I have no idea how to do this...
Here is my code
Word.Application wordapp = new Word.Application();
Word.Document doc = wordapp.Documents.Add();
wordapp.Visible = true;
Word.Shape shNum1 = doc.Shapes.AddTextbox(MsoTextOrientation.msoTextOrientationHorizontal, 100, 100, 10, 10);
shNum1.TextFrame.TextRange.Text = "123";
Word.Shape shNum2 = doc.Shapes.AddTextbox(MsoTextOrientation.msoTextOrientationHorizontal, 150, 150, 10, 10);
shNum2.TextFrame.TextRange.Text = "321";
doc.Shapes.SelectAll();
wordapp.Selection.ShapeRange.Group();

Misalignment of two chart areas

I was trying to overlap two chart areas. They would share the same x values, but Y would have different values and scales.
Here is outcome of my code:
As you can see red series is not in alignment with green series.I was searching this site for answers, but couldn't find one that worked. Could someone explain me why they don't align?
Code:
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
namespace TestGraph
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
#region Data
// Creating first series
Series s1 = new Series();
s1.Name = "Values";
s1.ChartType = SeriesChartType.Column;
s1.XValueType = ChartValueType.DateTime;
s1.Color = Color.Green;
s1.BorderWidth = 2;
// Hard Coding test values
DataPoint[] values =
{
new DataPoint(new DateTime(2017, 8, 1).ToOADate(), 10),
new DataPoint(new DateTime(2017, 8, 2).ToOADate(), 11),
new DataPoint(new DateTime(2017, 8, 3).ToOADate(), 12),
new DataPoint(new DateTime(2017, 8, 4).ToOADate(), 13),
};
// Adding vales to s1
foreach (DataPoint p in values)
{
s1.Points.Add(p);
}
// Creating second series
Series s2 = new Series();
s2.Name = "Values 2";
s2.ChartType = SeriesChartType.Column;
s2.XValueType = ChartValueType.DateTime;
s2.Color = Color.Red;
s2.BorderWidth = 2;
// Hard Coding test values
DataPoint[] values2 =
{
new DataPoint(new DateTime(2017, 8, 1).ToOADate(), 0.1),
new DataPoint(new DateTime(2017, 8, 2).ToOADate(), -0.2),
new DataPoint(new DateTime(2017, 8, 3).ToOADate(), -0.7),
new DataPoint(new DateTime(2017, 8, 4).ToOADate(), 13),
};
// Adding vales to s2
foreach (DataPoint p in values2)
{
s2.Points.Add(p);
}
#endregion
#region Charts
// Initializing chart
Chart mainChart = new Chart();
ChartArea area = new ChartArea();
ChartArea area2 = new ChartArea();
Controls.Add(mainChart);
mainChart.Dock = DockStyle.Fill;
// Adding areas to mainChart
mainChart.ChartAreas.Add(area);
mainChart.ChartAreas.Add(area2);
// Adding series to areas
s1.ChartArea = area.Name;
s2.ChartArea = area2.Name;
mainChart.Series.Add(s1);
mainChart.Series.Add(s2);
// Aligning areas
// Overlapping area2 with area
area2.AlignmentStyle = AreaAlignmentStyles.All;
area2.AlignmentOrientation = AreaAlignmentOrientations.All;
area2.AlignWithChartArea = area.Name;
// Scale actualization
area2.RecalculateAxesScale();
area.RecalculateAxesScale();
// Defining Y scale
area2.AxisY.Maximum = 2;
area2.AxisY.Minimum = -2;
area2.BackColor = Color.Transparent;
// Disabling unnecessary graphics
area2.BackGradientStyle = GradientStyle.None;
area2.AxisX.IsMarginVisible = false;
area2.AxisX.LabelStyle.Enabled = false;
area2.AxisY.LabelStyle.Enabled = false;
area2.AxisX.Enabled = AxisEnabled.False;
area2.AxisY.Enabled = AxisEnabled.False;
// Resizing chart back to 100%
area.Position = new ElementPosition(0, 0, 100, 100);
#endregion
}
}
}
One of your chart has AxisX.IsMarginVisible set to true, the other to false, hence the mismatch.
However, if you are trying to plot overlapping series, why are you not adding them to the same chart area instead of going through all this trouble?

Using secondary axis for chart cause x-axis and primary y-axis issue (Excel)

Making a chart using secondary axis,
makes my chart primary y-axis is shown with some values which I don't want to have.
Only x-axis and secondary y-axis.
and also the x-axis is drawn without the date values what I've passed.
Code:
chartType2 = GetChartType(worksheet, chartToDraw, endcolcnt, i, chartType2, chartType);
chartType2.UseSecondaryAxis = true;
Scale(headerString, endcolcnt, worksheet, chartType2, stcol, isFieldSame, endcol, stcolumn1, endrow, startRow);
and Scale Function only assigns the header names and all.
Details about the series taken
Output:
Input
Hard to say without more code. What are those functions doing exactly?
Are you trying to just get the axis on the right side? Based on the black chart you posted that would seem like what you are after. You could just do chartType.YAxis.Crosses = eCrosses.Max.
Or do you actually want TWO axes which would require two charts/series? If you want that then you would need to create a second chart based on the first (looks like your function might be doing that) and then add a unique series to each but with a common x-value dataset. Just make sure you add them in the right order.
This shows both scenarios:
[TestMethod]
public void Chart_Secondary_Axis_Test()
{
//http://stackoverflow.com/questions/28540458/using-secondary-axis-for-chart-cause-x-axis-and-primary-y-axis-issue-excel
var existingFile = new FileInfo(#"c:\temp\temp.xlsx");
if (existingFile.Exists)
existingFile.Delete();
using (var pck = new ExcelPackage(existingFile))
{
var wsContent = pck.Workbook.Worksheets.Add("Content");
//Some data
wsContent.Cells["A1"].Value = "A"; wsContent.Cells["B1"].Value = "B"; wsContent.Cells["C1"].Value = "C"; wsContent.Cells["D1"].Value = "D";
wsContent.Cells["A2"].Value = 100; wsContent.Cells["A3"].Value = 400; wsContent.Cells["A4"].Value = 200; wsContent.Cells["A5"].Value = 300; wsContent.Cells["A6"].Value = 600; wsContent.Cells["A7"].Value = 500;
wsContent.Cells["B2"].Value = 300; wsContent.Cells["B3"].Value = 200; wsContent.Cells["B4"].Value = 1000; wsContent.Cells["B5"].Value = 600; wsContent.Cells["B6"].Value = 500; wsContent.Cells["B7"].Value = 200;
wsContent.Cells["D2"].Value = new DateTime(2015, 1, 1); wsContent.Cells["D3"].Value = new DateTime(2015, 1, 2); wsContent.Cells["D4"].Value = new DateTime(2015, 1, 3); wsContent.Cells["D5"].Value = new DateTime(2015, 1, 4); wsContent.Cells["D6"].Value = new DateTime(2015, 1, 5); wsContent.Cells["D7"].Value = new DateTime(2015, 1, 6);
const int dataRow = 7;
const string FORMATDATE = "m/d/yy";
wsContent.Cells[2, 4, dataRow, 4].Style.Numberformat.Format = FORMATDATE;
//Single Axis with intersection on the right
var chart1 = wsContent.Drawings.AddChart("Chart1", eChartType.XYScatterLines);
chart1.SetSize(600, 400);
var serie1 = (ExcelScatterChartSerie)chart1.Series.Add(wsContent.Cells[2, 1, dataRow, 1], wsContent.Cells[2, 4, dataRow, 4]);
serie1.Header = wsContent.Cells[1, 1].Value.ToString();
chart1.YAxis.Crosses = eCrosses.Max;
//Dual Axis
var chart2a = wsContent.Drawings.AddChart("Chart2", eChartType.ColumnStacked);
chart2a.SetSize(600, 400);
chart2a.SetPosition(400, 0);
var serie2a = chart2a.Series.Add(wsContent.Cells[2, 2, dataRow, 2], wsContent.Cells[2, 4, dataRow, 4]);
serie2a.Header = wsContent.Cells[1, 2].Value.ToString();
var chart2b = chart2a.PlotArea.ChartTypes.Add(eChartType.XYScatterLines);
var serie2b = chart2b.Series.Add(wsContent.Cells[2, 1, dataRow, 1], wsContent.Cells[2, 4, dataRow, 4]);
serie2b.Header = wsContent.Cells[1, 1].Value.ToString();
chart2b.UseSecondaryAxis = true; //Flip the axes
pck.Save();
}
}

iTextSharp - Is it possible to set a different font color for the same cell and row?

I am using the iTextSharp.dll with the following code:
var Title = "This is title";
var Description = "This is description";
Innertable.AddCell(new PdfPCell(new Phrase(string.Format("{0} {1}", Title, Description.Trim()), listTextFont)) { BackgroundColor = new BaseColor(233, 244, 249), BorderWidth = 0, PaddingTop = 4, PaddingLeft = -240, PaddingBottom = 5, HorizontalAlignment = Element.ALIGN_LEFT });
Can we set different font colors for title and description, but only using single cell (ie without creating a new table)?
Any help in this matter would be greatly appreciated.
What you want to do is create 2 Chunk objects, and then combine these into 1 Phrase which you will add to the cell.
var blackListTextFont = FontFactory.GetFont("Arial", 28, Color.BLACK);
var redListTextFont = FontFactory.GetFont("Arial", 28, Color.RED);
var titleChunk = new Chunk("Title", blackListTextFont);
var descriptionChunk = new Chunk("Description", redListTextFont);
var phrase = new Phrase(titleChunk);
phrase.Add(descriptionChunk);
table.AddCell(new PdfPCell(phrase));
Have a look at http://www.mikesdotnetting.com/Article/82/iTextSharp-Adding-Text-with-Chunks-Phrases-and-Paragraphs
Try like this to set a different foreground color in a pdf cell:
var FontColour = new BaseColor(35, 31, 32);
var Calibri8 = FontFactory.GetFont("Calibri", 8, FontColour);
PdfPCell R3C2 = new PdfPCell(new Paragraph("Hello", Calibri8));
R3C2.BorderWidth = 0f;
R3C2.HorizontalAlignment = PdfPCell.ALIGN_RIGHT;
table.AddCell(R3C2);
For VB.net Use the following function
Public Function CreateFont(size As Integer, Optional style As Integer = iTextSharp.text.Font.BOLD) As iTextSharp.text.Font
Dim FontColour = New BaseColor(193, 36, 67) 'Color code
Return New iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, size, style, FontColour)
End Function
Dim p As New Paragraph
p.Add(New Paragraph("Your Sentence Here", CreateFont(12, iTextSharp.text.Font.BOLDITALIC)))
pdfDoc.Add(p)
The trick is to create phrases (NOT chunks) with different fonts and add these to a parent phrase. As far as I can tell if you create chunks with different fonts and add these to a phrase, all of the text in the final phrase displays with the same font.
Here's an example which works for me:
// create the font we'll use
var fNormal = FontFactory.GetFont("Helvetica", 10f);
fNormal.SetColor(0, 0, 0);
// add phrase containing link
var pFooter = new Phrase();
// add phrase to this containing text only
var footerStart = new Phrase("Visit ");
footerStart.Font = fNormal;
pFooter.Add(footerStart);
// now create anchor and add with different font
string wolSiteUrl = "http://www.whateveryoulike.com";
Anchor wolWebSiteLink = new Anchor(wolSiteUrl, fNormal);
wolWebSiteLink.Reference = wolSiteUrl;
wolWebSiteLink.Font = fNormal;
wolWebSiteLink.Font.SetColor(242, 132, 0);
pFooter.Add(wolWebSiteLink);
// add text to go after this link
var footerEnd = new Phrase(" to view these results online.");
footerEnd.Font = fNormal;
pFooter.Add(footerEnd);
var paraFooter = new Paragraph(pFooter);
// add the phrase we've built up containing lots of little phrases to document
// (assume you have one of these ...)
doc.Add(paraFooter);

MS Chart C# - Why the Chart don't show the first Monthname?

I have a Ms Chart on a simple Form and the follow Testcode:
ChartArea myAreachart2 = new ChartArea();
myAreachart2.AxisX.IntervalType = DateTimeIntervalType.Months;
myAreachart2.AxisX.Minimum = new DateTime(2011, 1, 1).ToOADate();
myAreachart2.AxisX.Maximum = new DateTime(2011, 12, 31).ToOADate();
myAreachart2.AxisX.IsLabelAutoFit = false;
myAreachart2.AxisX.LabelStyle.IsEndLabelVisible = false;
myAreachart2.AxisX.LabelStyle.Format = "MMMM";
chart2.ChartAreas.Add(myAreachart2);
chart2.Series.Add("Default");
chart2.Series[0].XValueType = ChartValueType.DateTime
chart2.Series[0].BorderWidth = 4;
chart2.Series[0].Color = Color.Black;
chart2.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
chart2.Series[0].Points.AddXY(new DateTime(2011, 1, 1), 100);
chart2.Series[0].Points.AddXY(new DateTime(2011, 2, 1), 200);
chart2.Series[0].Points.AddXY(new DateTime(2011, 3, 1), 300);
chart2.Series[0].Points.AddXY(new DateTime(2011, 4, 1), 400);
chart2.Series[0].Points.AddXY(new DateTime(2011, 5, 1), 500);
The result is a Chart with the Values and on the x-Axis are the monthnames as Labels. But not on january. The monthname will not be displayed. (There ist nothing.)
Please help me ? :-) I seach a lot of websites an examples, but i can't find any solution.
Thank you very much.
Might be because you set:
myAreachart2.AxisX.LabelStyle.IsEndLabelVisible = false;
From MSDN:
LabelStyle.IsEndLabelVisible Property Gets or sets a flag that
determines whether the labels are shown at axis ends.

Categories

Resources