浏览代码

Add method to insert chart after paragraph

master
mkalyakin 9 年前
父节点
当前提交
3af779e6b3
共有 1 个文件被更改,包括 54 次插入0 次删除
  1. 54
    0
      DocX/DocX.cs

+ 54
- 0
DocX/DocX.cs 查看文件

@@ -4378,6 +4378,60 @@ namespace Novacode
p.Xml.Add(chartElement);
}
/// <summary>
/// Insert a chart in document after paragraph
/// </summary>
public void InsertChartAfterParagraph(Chart chart, Paragraph paragraph) {
// Create a new chart part uri.
String chartPartUriPath = String.Empty;
Int32 chartIndex = 1;
do {
chartPartUriPath = String.Format
(
"/word/charts/chart{0}.xml",
chartIndex
);
chartIndex++;
} while (package.PartExists(new Uri(chartPartUriPath, UriKind.Relative)));
// Create chart part.
PackagePart chartPackagePart = package.CreatePart(new Uri(chartPartUriPath, UriKind.Relative), "application/vnd.openxmlformats-officedocument.drawingml.chart+xml", CompressionOption.Normal);
// Create a new chart relationship
String relID = GetNextFreeRelationshipID();
PackageRelationship rel = mainPart.CreateRelationship(chartPackagePart.Uri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart", relID);
// Save a chart info the chartPackagePart
using (TextWriter tw = new StreamWriter(chartPackagePart.GetStream(FileMode.Create, FileAccess.Write)))
chart.Xml.Save(tw);
// Insert a new chart into a paragraph.
Paragraph p = paragraph;
XElement chartElement = new XElement(
XName.Get("r", DocX.w.NamespaceName),
new XElement(
XName.Get("drawing", DocX.w.NamespaceName),
new XElement(
XName.Get("inline", DocX.wp.NamespaceName),
new XElement(XName.Get("extent", DocX.wp.NamespaceName), new XAttribute("cx", "5486400"), new XAttribute("cy", "3200400")),
new XElement(XName.Get("effectExtent", DocX.wp.NamespaceName), new XAttribute("l", "0"), new XAttribute("t", "0"), new XAttribute("r", "19050"), new XAttribute("b", "19050")),
new XElement(XName.Get("docPr", DocX.wp.NamespaceName), new XAttribute("id", "1"), new XAttribute("name", "chart")),
new XElement(
XName.Get("graphic", DocX.a.NamespaceName),
new XElement(
XName.Get("graphicData", DocX.a.NamespaceName),
new XAttribute("uri", DocX.c.NamespaceName),
new XElement(
XName.Get("chart", DocX.c.NamespaceName),
new XAttribute(XName.Get("id", DocX.r.NamespaceName), relID)
)
)
)
)
));
p.Xml.Add(chartElement);
}
/// <summary>
/// Inserts a default TOC into the current document.
/// Title: Table of contents

正在加载...
取消
保存