Update chart properties in Open XML - c#

I am recently trying to create a bar chart in excel using Open XML and C#.
Below is portion of the script that helped me to create a bar chart in excel.
// Add a new drawing to the worksheet.
DrawingsPart drawingsPart = worksheetPart.AddNewPart<DrawingsPart>();
worksheetPart.Worksheet.Append(new DocumentFormat.OpenXml.Spreadsheet.Drawing() { Id = worksheetPart.GetIdOfPart(drawingsPart) });
worksheetPart.Worksheet.Save();
// Add a new chart and set the chart language to English-US.
ChartPart chartPart = drawingsPart.AddNewPart<ChartPart>();
chartPart.ChartSpace = new ChartSpace();
chartPart.ChartSpace.Append(new EditingLanguage() { Val = new StringValue("en-US") });
Drawing.Charts.Chart chart = chartPart.ChartSpace.AppendChild<Drawing.Charts.Chart>(new Drawing.Charts.Chart());
addChartTitle(chart, chartTitleText);
// Create a new clustered column chart.
PlotArea plotArea = chart.AppendChild<PlotArea>(new PlotArea());
Layout layout = plotArea.AppendChild<Layout>(new Layout());
addBarchart(plotArea);
addCategoryAxis(plotArea);
// Add the Value Axis.
ValueAxis valAx = plotArea.AppendChild<ValueAxis>(new ValueAxis(new AxisId() { Val = new UInt32Value(48672768u) },
new Scaling(new Orientation() { Val = new EnumValue<DocumentFormat.OpenXml.Drawing.Charts.OrientationValues>(DocumentFormat.OpenXml.Drawing.Charts.OrientationValues.MinMax) }),
new AxisPosition() { Val = new EnumValue<AxisPositionValues>(AxisPositionValues.Left) }, new MajorGridlines(), new DocumentFormat.OpenXml.Drawing.Charts.NumberingFormat()
{ FormatCode = new StringValue("General"), SourceLinked = new BooleanValue(true) }, new TickLabelPosition() { Val = new EnumValue<TickLabelPositionValues> (TickLabelPositionValues.NextTo) },
new CrossingAxis() { Val = new UInt32Value(48650112U) }, new Crosses() { Val = new EnumValue<CrossesValues>(CrossesValues.AutoZero) }, new CrossBetween() { Val = new EnumValue<CrossBetweenValues>(CrossBetweenValues.Between) }));
But unfortunately when I tried to edit the colors of bars, I couldn't make it successful. Please help me create a chart and to update chart properties using Open XML and C#.

Related

NoTextEdit ShapeLock is not working - OpenXML SDK

I am trying to make a TextBox locked by using OpenXML SDK. I've tried this method but the TextBox NoTextEdit is not working.
public DocumentFormat.OpenXml.Presentation.Shape GenerateShape(string StrText)
{
DocumentFormat.OpenXml.Presentation.Shape shape1 = new DocumentFormat.OpenXml.Presentation.Shape();
DocumentFormat.OpenXml.Presentation.NonVisualShapeProperties nonVisualShapeProperties1 = new DocumentFormat.OpenXml.Presentation.NonVisualShapeProperties();
DocumentFormat.OpenXml.Presentation.NonVisualDrawingProperties nonVisualDrawingProperties1 = new DocumentFormat.OpenXml.Presentation.NonVisualDrawingProperties() { Id = (UInt32Value)3U, Name = "ID",Hidden=true ,Description="Do not Remove" ,MCAttributes=null };
DocumentFormat.OpenXml.Presentation.NonVisualShapeDrawingProperties nonVisualShapeDrawingProperties1 = new DocumentFormat.OpenXml.Presentation.NonVisualShapeDrawingProperties();
Drawing.ShapeLocks shapeLocks1 = new Drawing.ShapeLocks() { NoTextEdit = true, NoGrouping = true,NoMove=true,NoSelection=true,NoEditPoints=true ,NoAdjustHandles=true ,NoRotation=true,NoChangeArrowheads=true,NoChangeAspect=true,NoChangeShapeType=true,NoResize=true};
nonVisualShapeDrawingProperties1.Append(shapeLocks1);
ApplicationNonVisualDrawingProperties applicationNonVisualDrawingProperties1 = new ApplicationNonVisualDrawingProperties();
PlaceholderShape placeholderShape1 = new PlaceholderShape() { Type = PlaceholderValues.SubTitle, Index = (UInt32Value)1U };
applicationNonVisualDrawingProperties1.Append(placeholderShape1);
nonVisualShapeProperties1.Append(nonVisualDrawingProperties1);
nonVisualShapeProperties1.Append(nonVisualShapeDrawingProperties1);
nonVisualShapeProperties1.Append(applicationNonVisualDrawingProperties1);
DocumentFormat.OpenXml.Presentation.ShapeProperties shapeProperties1 = new DocumentFormat.OpenXml.Presentation.ShapeProperties();
DocumentFormat.OpenXml.Presentation.TextBody textBody1 = new DocumentFormat.OpenXml.Presentation.TextBody();
Drawing.BodyProperties bodyProperties1 = new Drawing.BodyProperties();
Drawing.ListStyle listStyle1 = new Drawing.ListStyle();
Drawing.TextShape shp = new Drawing.TextShape();
Drawing.Paragraph paragraph1 = new Drawing.Paragraph();
Drawing.EndParagraphRunProperties endParagraphRunProperties1 = new Drawing.EndParagraphRunProperties() { Language = "en-US" ,Dirty=false };
paragraph1.Append(GenerateRun(StrText));
paragraph1.Append(endParagraphRunProperties1);
textBody1.Append(bodyProperties1);
textBody1.Append(listStyle1);
textBody1.Append(paragraph1);
shape1.Append(nonVisualShapeProperties1);
shape1.Append(shapeProperties1);
shape1.Append(textBody1);
return shape1;
}
public Drawing.Run GenerateRun(string StrText)
{
Drawing.Run run1 = new Drawing.Run();
Drawing.RunProperties runProperties1 = new Drawing.RunProperties() { Language = "en-US", Dirty = false };
runProperties1.SetAttribute(new OpenXmlAttribute("", "smtClean", "", "0"));
Drawing.Text text1 = new Drawing.Text();
text1.Text = StrText;
Drawing.SolidFill solidFill2 = new Drawing.SolidFill();
Drawing.SchemeColor schemeColor = new Drawing.SchemeColor();
string y = System.Drawing.Color.Transparent.ToArgb().ToString("X");
Drawing.RgbColorModelHex rgbColorModelHex2 = new Drawing.RgbColorModelHex() { Val = "FFFFFF" };//Set Font-Color to Blue (Hex "0070C0").
solidFill2.Append(rgbColorModelHex2);
runProperties1.Append(solidFill2);
Color color = new Color() { Val = "365F91", ThemeColor = ThemeColorValues.Accent1, ThemeShade = "BF" };
run1.Append(runProperties1);
run1.Append(text1);
return run1;
}
Everything works fine except editing. Still the user can edit the TextBox values by double clicking it. How can I avoid this ?
Is there any permanent solution to prevent editing ? Please help me to find a better solution.
Thanks in advance
By researching and communications with the MVP team I've pointed out that there is no way to Protect the TextBox from editing.
As Cindy Meister mentioned in the comments,
Are you able to do it in the PowerPoint application user interface? If not, then Open XML cannot do it... If yes, you can.
If you do not want to text to be changed , Just change it as image then lock that by using NoSelection=true/1 and NoMove=true/1 properties. If you enable these properties the user can't either delete nor change it's position.
For your ref: https://answers.microsoft.com/en-us/msoffice/forum/msoffice_powerpoint-mso_windows8-mso_2016/shape-lock-is-not-working/c1705b55-d2aa-4adb-b538-574ed2fc8eca?tm=1579265435636&page=1&rtAction=1579495439869

Exporting OxyPlot chart to png in asp.net phoject

I'm using asp.net for generating pdf file. In pdf i must show plot. I decided using oxyplot with exporting it in png and inserting in pdf. The documentation says using PlotModel, filling it with series and axes and simply export by using PngExporting object. But when I'm trying generate it, nothing is presented except the axes.
The code I'm using:
var _plotModel = new OxyPlot.PlotModel()
{
PlotAreaBorderColor = OxyPlot.OxyColors.Transparent,
LegendBorder = OxyPlot.OxyColors.Transparent,
TitleToolTip = "Temperature",
Culture = new System.Globalization.CultureInfo("ru-Ru"),
Series =
{
new OxyPlot.Series.LineSeries()
{
ItemsSource = new List<SimplePointModel>
{
new SimplePointModel { Date = DateTime.Now, Temperature = 36.6f },
new SimplePointModel { Date = DateTime.Now.AddDays(1), Temperature = 42.6f },
new SimplePointModel { Date = DateTime.Now.AddDays(2), Temperature = 48.6f },
},
MarkerFill = OxyPlot.OxyColor.Parse("#A9CF9C"),
MarkerType = OxyPlot.MarkerType.Circle,
MarkerSize = item.ListSegments[0].Count() == 1 ? 2 : 1,
DataFieldX = "Date",
DataFieldY = "Temperature",
XAxisKey = "Date",
YAxisKey = "Temperature",
Color = OxyPlot.OxyColor.Parse("#A9CF9C"),
}
},
Axes =
{
new OxyPlot.Axes.DateTimeAxis()
{
Position = OxyPlot.Axes.AxisPosition.Bottom,
Key = "Date",
ToolTip = "Temperature",
AbsoluteMinimum = OxyPlot.Axes.Axis.ToDouble(DateTime.Now.Date),
AbsoluteMaximum = OxyPlot.Axes.Axis.ToDouble(DateTime.Now.AddDays(10000))
},
new OxyPlot.Axes.LinearAxis()
{
Position = OxyPlot.Axes.AxisPosition.Left,
Key = "Temperature",
ToolTip = "Temperature",
IsZoomEnabled = false,
IsPanEnabled = false,
}
}
};
using (var stream = System.IO.File.Create(Path.Combine(folder, "temp.png")))
{
PngExporter.Export(_plotModel, stream, 800, 400, OxyPlot.OxyColors.Transparent);
}
What can I do?
I found solution: we must load data to Points list parameter in Series object instead of ItemSource parameter. ItemSource parameter used in mobile version of library.

Export Datatable to Word Document With Page Numbers using Open XML

My requirement is : Exporting a dynamic DataTable to Word document with Page Numbers
We need to use Open XML to achieve this.
I have code to export Datatable to Word. And also to insert page numbers.
I got Below code code to export datatable
public void CreateWordtable(string filename,DataTable data)
{
WordprocessingDocument doc = WordprocessingDocument.Create(filename, WordprocessingDocumentType.Document);
MainDocumentPart mainDocPart = doc.AddMainDocumentPart();
mainDocPart.Document = new Document();
Body body = new Body();
mainDocPart.Document.Append(body);
//rinks#::creating new table
DocumentFormat.OpenXml.Wordprocessing.Table table = new DocumentFormat.OpenXml.Wordprocessing.Table();
for (int i = 0; i < data.Rows.Count; ++i)
{
TableRow row = new TableRow();
for (int j = 0; j < data.Columns.Count; j++)
{
TableCell cell = new TableCell();
cell.Append(new Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(new DocumentFormat.OpenXml.Wordprocessing.Text(data.Rows[i][j].ToString()))));
cell.Append(new TableCellProperties(new TableCellWidth { Type = TableWidthUnitValues.Dxa, Width = "1200" }));
row.Append(cell);
}
table.Append(row);
}
body.Append(table);
doc.MainDocumentPart.Document.Save();
doc.Dispose();
}
And below code is to insert page numbers in a word document
private static void AddPageNumberFooters(WordprocessingDocument parent)
{
string documentPath = #"D:\EmptyDoc.docx";
using (WordprocessingDocument package =
WordprocessingDocument.Open(documentPath, true))
{
var mainDocumentPart = parent.AddMainDocumentPart();
GenerateMainDocumentPart().Save(mainDocumentPart);
var documentSettingsPart =
mainDocumentPart.AddNewPart
<DocumentSettingsPart>("rId1");
GenerateDocumentSettingsPart().Save(documentSettingsPart);
var firstPageFooterPart = mainDocumentPart.AddNewPart<FooterPart>("rId1");
GeneratePageFooterPart("Page 1 of 2").Save(firstPageFooterPart);
var secondPageFooterPart = mainDocumentPart.AddNewPart<FooterPart>("rId2");
GeneratePageFooterPart("Page 2 of 2").Save(secondPageFooterPart);
}
}
private static Document GenerateMainDocumentPart()
{
var element =
new Document(
new Body(
new Paragraph(
new Run(
new Text("Page 1 content"))
),
new Paragraph(
new Run(
new Break() { Type = BreakValues.Page })
),
new Paragraph(
new Run(
new LastRenderedPageBreak(),
new Text("Page 2 content"))
),
new Paragraph(
new Run(
new Break() { Type = BreakValues.Page })
),
new SectionProperties(
new FooterReference()
{
Type = HeaderFooterValues.First,
Id = "rId1"
},
new FooterReference()
{
Type = HeaderFooterValues.Even,
Id = "rId2"
},
new PageMargin()
{
Top = 1440,
Right = (UInt32Value)1440UL,
Bottom = 1440,
Left = (UInt32Value)1440UL,
Header = (UInt32Value)720UL,
Footer = (UInt32Value)720UL,
Gutter = (UInt32Value)0UL
},
new TitlePage()
)));
return element;
}
private static Header GeneratePageHeaderPart(string HeaderText)
{
var element =
new Header(
new Paragraph(
new ParagraphProperties(
new ParagraphStyleId() { Val = "Header" }),
new Run(
new Text(HeaderText))
));
return element;
}
My problem is, I have combine above both methods to export data along with page numbers.
if we know there are 2 pages in the word document, i can insert 2 FooterParts.
But i don't know how many pages will be created after exporting the data.
try the following code to automatically add page numbers:
private static string GenerateFooterPartContent(WordprocessingDocument package, string text = null)
{
FooterPart footerPart1 = package.MainDocumentPart.FooterParts.FirstOrDefault();
if (footerPart1 == null)
{
footerPart1 = package.MainDocumentPart.AddNewPart<FooterPart>();
}
var relationshipId = package.MainDocumentPart.GetIdOfPart(footerPart1);
// Get SectionProperties and set HeaderReference and FooterRefernce with new Id
SectionProperties sectionProperties1 = new SectionProperties();
FooterReference footerReference2 = new FooterReference() { Type = HeaderFooterValues.Default, Id = relationshipId };
sectionProperties1.Append(footerReference2);
package.MainDocumentPart.Document.Body.Append(sectionProperties1);
Footer footer1 = new Footer();
var r = new Run();
PositionalTab positionalTab2 = new PositionalTab() { Alignment = AbsolutePositionTabAlignmentValues.Right,
RelativeTo = AbsolutePositionTabPositioningBaseValues.Margin,
Leader = AbsolutePositionTabLeaderCharValues.None };
r.Append(positionalTab2);
paragraph2.Append(r);
r = new Run(new Text("Page: ") { Space = SpaceProcessingModeValues.Preserve },
// *** Adaptation: This will output the page number dynamically ***
new SimpleField() { Instruction = "PAGE" },
new Text(" of ") { Space = SpaceProcessingModeValues.Preserve },
// *** Adaptation: This will output the number of pages dynamically ***
new SimpleField() { Instruction = "NUMPAGES" });
paragraph2.Append(r);
footer1.Append(paragraph2);
footerPart1.Footer = footer1;
return relationshipId;
}

Create table using OpenXML in powerpoint

I have a scenario where I have Datagrid in my Silverlight application and I want the data to be exported to Power Point.
I researched and found only links, to export image(screen captured) to powerpoint. In my scenario , a screen capture also wont work as I have scroll bar with 20 columns and it is not getting displayed with the above solution.
Any work around for this scenario.
P.S : I do not intend to use any 3rd party controls.
Edit :
I have now tried to use OpenXML , but i am getting an error as below :
My code is as below :
using A = DocumentFormat.OpenXml.Drawing;
using P14 = DocumentFormat.OpenXml.Office2010.PowerPoint;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Presentation;
using P = DocumentFormat.OpenXml.Presentation;
using D = DocumentFormat.OpenXml.Drawing;
using DocumentFormat.OpenXml.Drawing;
public static void CreateTableInLastSlide(PresentationDocument presentationDocument)
{
// Get the presentation Part of the presentation document
PresentationPart presentationPart = presentationDocument.PresentationPart;
// Get the Slide Id collection of the presentation document
var slideIdList = presentationPart.Presentation.SlideIdList;
if (slideIdList == null)
{
throw new NullReferenceException("The number of slide is empty, please select a ppt with a slide at least again");
}
// Get all Slide Part of the presentation document
var list = slideIdList.ChildElements
.Cast<SlideId>()
.Select(x => presentationPart.GetPartById(x.RelationshipId))
.Cast<SlidePart>();
// Get the last Slide Part of the presentation document
var tableSlidePart = (SlidePart)list.Last();
// Declare and instantiate the graphic Frame of the new slide
P.GraphicFrame graphicFrame = tableSlidePart.Slide.CommonSlideData.ShapeTree.AppendChild(new P.GraphicFrame());
ApplicationNonVisualDrawingPropertiesExtension applicationNonVisualDrawingPropertiesExtension = new ApplicationNonVisualDrawingPropertiesExtension();
P14.ModificationId modificationId1 = new P14.ModificationId() { Val = 3229994563U };
modificationId1.AddNamespaceDeclaration("p14", "http://schemas.microsoft.com/office/powerpoint/2010/main");
applicationNonVisualDrawingPropertiesExtension.Append(modificationId1);
graphicFrame.NonVisualGraphicFrameProperties = new DocumentFormat.OpenXml.Presentation.NonVisualGraphicFrameProperties
(new A.NonVisualDrawingProperties() { Id = 5, Name = "table 1" },
new A.NonVisualGraphicFrameDrawingProperties(new A.GraphicFrameLocks() { NoGrouping = true }),
new ApplicationNonVisualDrawingProperties(new ApplicationNonVisualDrawingPropertiesExtensionList(applicationNonVisualDrawingPropertiesExtension)));
graphicFrame.Transform = new Transform(new Offset() { X = 10, Y = 10 });
graphicFrame.Graphic = new A.Graphic(new A.GraphicData(GenerateTable()) { Uri = "http://schemas.openxmlformats.org/drawingml/2006/table" });
presentationPart.Presentation.Save();
}
private static A.Table GenerateTable()
{
string[,] tableSources = new string[,] { { "name", "age" }, { "Tom", "25" } };
// Declare and instantiate table
A.Table table = new A.Table();
// Specify the required table properties for the table
A.TableProperties tableProperties = new A.TableProperties() { FirstRow = true, BandRow = true };
A.TableStyleId tableStyleId = new A.TableStyleId();
tableStyleId.Text = "{5C22544A-7EE6-4342-B048-85BDC9FD1C3A}";
tableProperties.Append(tableStyleId);
// Declare and instantiate tablegrid and colums
A.TableGrid tableGrid1 = new A.TableGrid();
A.GridColumn gridColumn1 = new A.GridColumn() { Width = 3048000L };
A.GridColumn gridColumn2 = new A.GridColumn() { Width = 3048000L };
tableGrid1.Append(gridColumn1);
tableGrid1.Append(gridColumn2);
table.Append(tableProperties);
table.Append(tableGrid1);
for (int row = 0; row < tableSources.GetLength(0); row++)
{
// Instantiate the table row
A.TableRow tableRow = new A.TableRow() { Height = 370840L };
for (int column = 0; column < tableSources.GetLength(1); column++)
{
tableRow.Append(CreateTextCell(tableSources.GetValue(row, column).ToString()));
}
table.Append(tableRow);
}
return table;
}
Am i using the correct graphics and transform ??
The line which is causing the problem according to me is :
P.GraphicFrame graphicFrame = tableSlidePart.Slide.CommonSlideData.ShapeTree.AppendChild(new P.GraphicFrame());
As if i comment his line i dont get the error, but neither do i get the table :o/
Any help ?
Finally i was able to solve the problem using the Open XML productivity tool (here) . The line i highlighted was the error. I needed to add the below code :
List<OpenXmlElement> elements = new List<OpenXmlElement>();
elements.Add(new P.NonVisualGraphicFrameProperties
(new P.NonVisualDrawingProperties() { Id = 1, Name = "xyz" }, new P.NonVisualGraphicFrameDrawingProperties(),new ApplicationNonVisualDrawingProperties()));
P.GraphicFrame graphicFrame = tableSlidePart.Slide.CommonSlideData.ShapeTree.AppendChild(new P.GraphicFrame(elements));
Thus i was able to get output without any errors :)

Set RTL direction for "Tablecell" or "Table" in OpenXml

I want to set a RTL direction for some cell of a table that I create with OpenXml.
row.Append(
new TableCell(
new Paragraph(
new Run(
new Text("FullName")))){
TableCellProperties = new TableCellProperties()
{
TableCellWidth = new TableCellWidth(){
Type = TableWidthUnitValues.Dxa,
Width = "3400" },
TextDirection = new TextDirection(){
Val = new EnumValue<TextDirectionValues>(TextDirectionValues.TopToBottomRightToLeft)}
}
});
I wrote this code, but TextDirectionValues Enum dosen't have a RTL value.
If your tables are like this:
TableRow > TableCell > Paragraph > Run > Text.
This code may help you:
//Justification
aRow.Descendants<TableCell>().ElementAt(indx).Descendants<Paragraph>()
.ElementAt(0).ParagraphProperties = new ParagraphProperties()
{
Justification = new Justification()
{
Val = new EnumValue<JustificationValues>(JustificationValues.Right)
}
};
//RightToLeftText
foreach (var r in aRow.Descendants<TableCell>().ElementAt(indx).Descendants<Run>())
{
r.RunProperties = new RunProperties()
{
RightToLeftText = new RightToLeftText()
{
Val = new OnOffValue(true)
}
};
}

Categories