我一直在尝试在 word 中的简单图表中添加一条线。此代码不会创建工作文档。如果有人能告诉我为什么会很棒。
在实际应用程序中,我正在处理“包”变量是指 Xceed.Words.NET.DocX 文档的 PackagePart.Package 属性。在这种情况下,CreateRelationship 方法不会引发异常,但会在 \word\charts_rels 文件夹中创建损坏的 chart1.xml.rels 文件。
我知道这里有一些图书馆可能会这样做,但我不允许:(
我不确定这两个问题是否相互关联。
任何人?
using System;
using System.IO;
using System.Text;
using System.IO.Packaging;
using System.Xml.Linq;
namespace ConsoleApp1
{
class Program
{
private static void Main()
{
using (var package = Package.Open("fix.docx", FileMode.Open, FileAccess.ReadWrite, FileShare.None))
{
//Add drawing
var drawingPartUri = PackUriHelper.CreatePartUri(new Uri("/word/drawings/drawing1.xml", UriKind.Relative));
var drawingPart = package.CreatePart(drawingPartUri, System.Net.Mime.MediaTypeNames.Text.Xml);
using (var mStream = new MemoryStream(Encoding.ASCII.GetBytes("<c:userShapes xmlns:c=\"http://schemas.openxmlformats.org/drawingml/2006/chart\"><ns8:relSizeAnchor xmlns:ns8=\"http://schemas.openxmlformats.org/drawingml/2006/chartDrawing\"><ns8:from><ns8:x>0.52502</ns8:x><ns8:y>0.25595</ns8:y></ns8:from><ns8:to><ns8:x>0.52502</ns8:x><ns8:y>0.49702</ns8:y></ns8:to><ns8:cxnSp macro=\"\"><ns8:nvCxnSpPr><ns8:cNvPr id=\"4\" name=\"Straight Arrow Connector 3\"/><ns8:cNvCxnSpPr/></ns8:nvCxnSpPr><ns8:spPr><a:xfrm xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\"><a:off x=\"3590925\" y=\"819150\"/><a:ext cx=\"0\" cy=\"771525\"/></a:xfrm><a:prstGeom xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" prst=\"straightConnector1\"><a:avLst/></a:prstGeom><a:ln xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\"><a:headEnd type=\"triangle\"/><a:tailEnd type=\"triangle\"/></a:ln></ns8:spPr><ns8:style><a:lnRef xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" idx=\"1\"><a:schemeClr val=\"accent1\"/></a:lnRef><a:fillRef xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" idx=\"0\"><a:schemeClr val=\"accent1\"/></a:fillRef><a:effectRef xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" idx=\"0\"><a:schemeClr val=\"accent1\"/></a:effectRef><a:fontRef xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" idx=\"minor\"><a:schemeClr val=\"tx1\"/></a:fontRef></ns8:style></ns8:cxnSp></ns8:relSizeAnchor></c:userShapes>")))
{
if (drawingPart != null)
using (var pStream = drawingPart.GetStream())
{
mStream.CopyTo(pStream);
}
}
//Create relationship
var chartUri = PackUriHelper.CreatePartUri(new Uri("/word/charts/chart1.xml", UriKind.Relative));
var chartPart = package.GetPart(chartUri);
var rel = chartPart.CreateRelationship(drawingPartUri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/chartUserShapes");
//Modify chart
var doc = XDocument.Load(chartPart.GetStream());
XNamespace c = "http://schemas.openxmlformats.org/drawingml/2006/chart";
XNamespace r = "http://schemas.openxmlformats.org/officeDocument/2006/relationships";
var userShapes = new XElement(c + "userShapes", new XAttribute(r + "id", rel.Id));
doc.Root?.LastNode.AddAfterSelf(userShapes);
doc.Save(chartPart.GetStream());
}
}
}
}