Преглед изворни кода

Fixed a bug with insertDocument: Tables now insert correctly.

master
coffeycathal_cp пре 13 година
родитељ
комит
8b507159ae
2 измењених фајлова са 53 додато и 21 уклоњено
  1. 51
    19
      DocX/DocX.cs
  2. 2
    2
      DocX/HelperFunctions.cs

+ 51
- 19
DocX/DocX.cs Прегледај датотеку

} }
} }
int id = 0;
var local_docPrs = mainDoc.Root.Descendants(XName.Get("docPr", DocX.wp.NamespaceName));
foreach (var local_docPr in local_docPrs)
{
XAttribute a_id = local_docPr.Attribute(XName.Get("id"));
int a_id_value;
if (a_id != null && int.TryParse(a_id.Value, out a_id_value))
if (a_id_value > id)
id = a_id_value;
}
id++;
// docPr must be sequential
var docPrs = remote_body.Descendants(XName.Get("docPr", DocX.wp.NamespaceName));
foreach (var docPr in docPrs)
{
docPr.SetAttributeValue(XName.Get("id"), id);
id++;
}
// Add the remote documents contents to this document. // Add the remote documents contents to this document.
XElement local_body = mainDoc.Root.Element(XName.Get("body", DocX.w.NamespaceName)); XElement local_body = mainDoc.Root.Element(XName.Get("body", DocX.w.NamespaceName));
local_body.Add(remote_body.Elements()); local_body.Add(remote_body.Elements());
// Save the modified local custom styles.xml file. // Save the modified local custom styles.xml file.
using (TextWriter tw = new StreamWriter(local_pp.GetStream(FileMode.Create, FileAccess.Write))) using (TextWriter tw = new StreamWriter(local_pp.GetStream(FileMode.Create, FileAccess.Write)))
local_custom_document.Save(tw, SaveOptions.DisableFormatting);
local_custom_document.Save(tw, SaveOptions.None);
} }
private void merge_numbering(PackagePart remote_pp, PackagePart local_pp, XDocument remote_mainDoc, DocX remote) private void merge_numbering(PackagePart remote_pp, PackagePart local_pp, XDocument remote_mainDoc, DocX remote)
} }
} }
foreach(XElement e in remote_mainDoc.Root.Descendants(XName.Get("tblStyle", DocX.w.NamespaceName)))
{
XAttribute e_styleId = e.Attribute(XName.Get("val", DocX.w.NamespaceName));
if (e_styleId != null && e_styleId.Value.Equals(styleId.Value))
{
e_styleId.SetValue(styleId.Value + "_" + guuid);
}
}
if (remote_endnotes != null) if (remote_endnotes != null)
{ {
foreach (XElement e in remote_endnotes.Root.Descendants(XName.Get("rStyle", DocX.w.NamespaceName))) foreach (XElement e in remote_endnotes.Root.Descendants(XName.Get("rStyle", DocX.w.NamespaceName)))
// Save the main document // Save the main document
using (TextWriter tw = new StreamWriter(mainDocumentPart.GetStream(FileMode.Create, FileAccess.Write))) using (TextWriter tw = new StreamWriter(mainDocumentPart.GetStream(FileMode.Create, FileAccess.Write)))
mainDoc.Save(tw, SaveOptions.DisableFormatting);
mainDoc.Save(tw, SaveOptions.None);
#endregion #endregion
#region StylePart #region StylePart
// Save the main document // Save the main document
using (TextWriter tw = new StreamWriter(headerPart.GetStream(FileMode.Create, FileAccess.Write))) using (TextWriter tw = new StreamWriter(headerPart.GetStream(FileMode.Create, FileAccess.Write)))
header.Save(tw, SaveOptions.DisableFormatting);
header.Save(tw, SaveOptions.None);
string type; string type;
switch (i) switch (i)
// Save the main document // Save the main document
using (TextWriter tw = new StreamWriter(mainPart.GetStream(FileMode.Create, FileAccess.Write))) using (TextWriter tw = new StreamWriter(mainPart.GetStream(FileMode.Create, FileAccess.Write)))
mainDoc.Save(tw, SaveOptions.DisableFormatting);
mainDoc.Save(tw, SaveOptions.None);
XElement body = mainDoc.Root.Element(w + "body"); XElement body = mainDoc.Root.Element(w + "body");
XElement sectPr = body.Descendants(w + "sectPr").FirstOrDefault(); XElement sectPr = body.Descendants(w + "sectPr").FirstOrDefault();
( (
new XDeclaration("1.0", "UTF-8", "yes"), new XDeclaration("1.0", "UTF-8", "yes"),
even even
).Save(tw, SaveOptions.DisableFormatting);
).Save(tw, SaveOptions.None);
} }
} }
( (
new XDeclaration("1.0", "UTF-8", "yes"), new XDeclaration("1.0", "UTF-8", "yes"),
odd odd
).Save(tw, SaveOptions.DisableFormatting);
).Save(tw, SaveOptions.None);
} }
} }
( (
new XDeclaration("1.0", "UTF-8", "yes"), new XDeclaration("1.0", "UTF-8", "yes"),
first first
).Save(tw, SaveOptions.DisableFormatting);
).Save(tw, SaveOptions.None);
} }
} }
( (
new XDeclaration("1.0", "UTF-8", "yes"), new XDeclaration("1.0", "UTF-8", "yes"),
odd odd
).Save(tw, SaveOptions.DisableFormatting);
).Save(tw, SaveOptions.None);
} }
} }
( (
new XDeclaration("1.0", "UTF-8", "yes"), new XDeclaration("1.0", "UTF-8", "yes"),
even even
).Save(tw, SaveOptions.DisableFormatting);
).Save(tw, SaveOptions.None);
} }
} }
( (
new XDeclaration("1.0", "UTF-8", "yes"), new XDeclaration("1.0", "UTF-8", "yes"),
first first
).Save(tw, SaveOptions.DisableFormatting);
).Save(tw, SaveOptions.None);
} }
} }
// Save the settings document. // Save the settings document.
using (TextWriter tw = new StreamWriter(settingsPart.GetStream(FileMode.Create, FileAccess.Write))) using (TextWriter tw = new StreamWriter(settingsPart.GetStream(FileMode.Create, FileAccess.Write)))
settings.Save(tw, SaveOptions.DisableFormatting);
settings.Save(tw, SaveOptions.None);
if (endnotesPart != null) if (endnotesPart != null)
{ {
using (TextWriter tw = new StreamWriter(endnotesPart.GetStream(FileMode.Create, FileAccess.Write))) using (TextWriter tw = new StreamWriter(endnotesPart.GetStream(FileMode.Create, FileAccess.Write)))
endnotes.Save(tw, SaveOptions.DisableFormatting);
endnotes.Save(tw, SaveOptions.None);
} }
if (footnotesPart != null) if (footnotesPart != null)
{ {
using (TextWriter tw = new StreamWriter(footnotesPart.GetStream(FileMode.Create, FileAccess.Write))) using (TextWriter tw = new StreamWriter(footnotesPart.GetStream(FileMode.Create, FileAccess.Write)))
footnotes.Save(tw, SaveOptions.DisableFormatting);
footnotes.Save(tw, SaveOptions.None);
} }
if (stylesPart != null) if (stylesPart != null)
{ {
using (TextWriter tw = new StreamWriter(stylesPart.GetStream(FileMode.Create, FileAccess.Write))) using (TextWriter tw = new StreamWriter(stylesPart.GetStream(FileMode.Create, FileAccess.Write)))
styles.Save(tw, SaveOptions.DisableFormatting);
styles.Save(tw, SaveOptions.None);
} }
if (stylesWithEffectsPart != null) if (stylesWithEffectsPart != null)
{ {
using (TextWriter tw = new StreamWriter(stylesWithEffectsPart.GetStream(FileMode.Create, FileAccess.Write))) using (TextWriter tw = new StreamWriter(stylesWithEffectsPart.GetStream(FileMode.Create, FileAccess.Write)))
stylesWithEffects.Save(tw, SaveOptions.DisableFormatting);
stylesWithEffects.Save(tw, SaveOptions.None);
} }
if (numberingPart != null) if (numberingPart != null)
{ {
using (TextWriter tw = new StreamWriter(numberingPart.GetStream(FileMode.Create, FileAccess.Write))) using (TextWriter tw = new StreamWriter(numberingPart.GetStream(FileMode.Create, FileAccess.Write)))
numbering.Save(tw, SaveOptions.DisableFormatting);
numbering.Save(tw, SaveOptions.None);
} }
if (fontTablePart != null) if (fontTablePart != null)
{ {
using (TextWriter tw = new StreamWriter(fontTablePart.GetStream(FileMode.Create, FileAccess.Write))) using (TextWriter tw = new StreamWriter(fontTablePart.GetStream(FileMode.Create, FileAccess.Write)))
fontTable.Save(tw, SaveOptions.DisableFormatting);
fontTable.Save(tw, SaveOptions.None);
} }
} }
// Close the document so that it can be saved. // Close the document so that it can be saved.
package.Flush(); package.Flush();
#region Save this document back to a file or stream, that was specified by the user at save time. #region Save this document back to a file or stream, that was specified by the user at save time.
if (filename != null) if (filename != null)
{ {
using (FileStream fs = new FileStream(filename, FileMode.Create)) using (FileStream fs = new FileStream(filename, FileMode.Create))
{
fs.Write(memoryStream.ToArray(), 0, (int)memoryStream.Length); fs.Write(memoryStream.ToArray(), 0, (int)memoryStream.Length);
}
} }
else else
{ {
// Set the length of this stream to 0 // Set the length of this stream to 0
// Save the custom properties // Save the custom properties
using (TextWriter tw = new StreamWriter(customPropPart.GetStream(FileMode.Create, FileAccess.Write))) using (TextWriter tw = new StreamWriter(customPropPart.GetStream(FileMode.Create, FileAccess.Write)))
customPropDoc.Save(tw, SaveOptions.DisableFormatting);
customPropDoc.Save(tw, SaveOptions.None);
// Refresh all fields in this document which display this custom property. // Refresh all fields in this document which display this custom property.
UpdateCustomPropertyValue(this, cp.Name, cp.Value.ToString()); UpdateCustomPropertyValue(this, cp.Name, cp.Value.ToString());

+ 2
- 2
DocX/HelperFunctions.cs Прегледај датотеку

); );
using (TextWriter tw = new StreamWriter(customPropertiesPart.GetStream(FileMode.Create, FileAccess.Write))) using (TextWriter tw = new StreamWriter(customPropertiesPart.GetStream(FileMode.Create, FileAccess.Write)))
customPropDoc.Save(tw, SaveOptions.DisableFormatting);
customPropDoc.Save(tw, SaveOptions.None);
document.package.CreateRelationship(customPropertiesPart.Uri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties"); document.package.CreateRelationship(customPropertiesPart.Uri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties");
} }
// Save /word/styles.xml // Save /word/styles.xml
using (TextWriter tw = new StreamWriter(word_styles.GetStream(FileMode.Create, FileAccess.Write))) using (TextWriter tw = new StreamWriter(word_styles.GetStream(FileMode.Create, FileAccess.Write)))
stylesDoc.Save(tw, SaveOptions.DisableFormatting);
stylesDoc.Save(tw, SaveOptions.None);
PackagePart mainDocumentPart = package.GetParts().Where PackagePart mainDocumentPart = package.GetParts().Where
( (

Loading…
Откажи
Сачувај