浏览代码

DocX.cs: Added the Properties PageWidth and PageHeight.

Paragraph.cs: Added the Properties LineSpacing, LineSpacingBefore and LineSpacingAfter.
master
coffeycathal_cp 14 年前
父节点
当前提交
428d237114
共有 2 个文件被更改,包括 232 次插入1 次删除
  1. 91
    0
      DocX/DocX.cs
  2. 141
    1
      DocX/Paragraph.cs

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

@@ -32,6 +32,97 @@ namespace Novacode
static internal XNamespace c = "http://schemas.openxmlformats.org/drawingml/2006/chart";
#endregion
public float PageWidth
{
get
{
XElement body = mainDoc.Root.Element(XName.Get("body", DocX.w.NamespaceName));
XElement sectPr = body.Element(XName.Get("sectPr", DocX.w.NamespaceName));
if (sectPr != null)
{
XElement pgSz = sectPr.Element(XName.Get("pgSz", DocX.w.NamespaceName));
if(pgSz != null)
{
XAttribute w = pgSz.Attribute(XName.Get("w", DocX.w.NamespaceName));
if(w != null)
{
float f;
if(float.TryParse(w.Value,out f))
return (int)(f / 15.0f);
}
}
}
return (int)(11906.0f / 15.0f);
}
set
{
XElement body = mainDoc.Root.Element(XName.Get("body", DocX.w.NamespaceName));
if (body != null)
{
XElement sectPr = body.Element(XName.Get("sectPr", DocX.w.NamespaceName));
if (sectPr != null)
{
XElement pgSz = sectPr.Element(XName.Get("pgSz", DocX.w.NamespaceName));
if (pgSz != null)
{
pgSz.SetAttributeValue(XName.Get("w", DocX.w.NamespaceName), value);
}
}
}
}
}
public float PageHeight
{
get
{
XElement body = mainDoc.Root.Element(XName.Get("body", DocX.w.NamespaceName));
XElement sectPr = body.Element(XName.Get("sectPr", DocX.w.NamespaceName));
if (sectPr != null)
{
XElement pgSz = sectPr.Element(XName.Get("pgSz", DocX.w.NamespaceName));
if (pgSz != null)
{
XAttribute w = pgSz.Attribute(XName.Get("h", DocX.w.NamespaceName));
if (w != null)
{
float f;
if (float.TryParse(w.Value, out f))
return (int)(f / 15.0f);
}
}
}
return (int)(16838.0f / 15.0f);
}
set
{
XElement body = mainDoc.Root.Element(XName.Get("body", DocX.w.NamespaceName));
if (body != null)
{
XElement sectPr = body.Element(XName.Get("sectPr", DocX.w.NamespaceName));
if (sectPr != null)
{
XElement pgSz = sectPr.Element(XName.Get("pgSz", DocX.w.NamespaceName));
if (pgSz != null)
{
pgSz.SetAttributeValue(XName.Get("h", DocX.w.NamespaceName), value);
}
}
}
}
}
/// <summary>
/// Returns true if any editing restrictions are imposed on this document.
/// </summary>

+ 141
- 1
DocX/Paragraph.cs 查看文件

@@ -1133,7 +1133,26 @@ namespace Novacode
/// </summary>
public Alignment Alignment
{
get { return alignment; }
get
{
XElement pPr = GetOrCreate_pPr();
XElement jc = pPr.Element(XName.Get("jc", DocX.w.NamespaceName));
if(jc != null)
{
XAttribute a = jc.Attribute(XName.Get("val", DocX.w.NamespaceName));
switch (a.Value.ToLower())
{
case "left": return Novacode.Alignment.left;
case "right": return Novacode.Alignment.right;
case "center": return Novacode.Alignment.center;
case "both": return Novacode.Alignment.both;
}
}
return Novacode.Alignment.left;
}
set
{
@@ -2764,6 +2783,34 @@ namespace Novacode
return this;
}
public float LineSpacing
{
get
{
XElement pPr = GetOrCreate_pPr();
XElement spacing = pPr.Element(XName.Get("spacing", DocX.w.NamespaceName));
if(spacing != null)
{
XAttribute line = spacing.Attribute(XName.Get("line", DocX.w.NamespaceName));
if(line != null)
{
float f;
if (float.TryParse(line.Value, out f))
return f / 20.0f;
}
}
return 1.1f * 20.0f;
}
set
{
Spacing(value);
}
}
public Paragraph Spacing(double spacing)
{
spacing *= 20;
@@ -2782,6 +2829,42 @@ namespace Novacode
return this;
}
public Paragraph SpacingBefore(double spacingBefore)
{
spacingBefore *= 20;
if (spacingBefore - (int)spacingBefore == 0)
{
if (!(spacingBefore > -1585 && spacingBefore < 1585))
throw new ArgumentException("SpacingBefore", "Value must be in the range: -1584 - 1584");
}
else
throw new ArgumentException("SpacingBefore", "Value must be either a whole or acurate to one decimal, examples: 32, 32.1, 32.2, 32.9");
ApplyTextFormattingProperty(XName.Get("before", DocX.w.NamespaceName), string.Empty, new XAttribute(XName.Get("val", DocX.w.NamespaceName), spacingBefore));
return this;
}
public Paragraph SpacingAfter(double spacingAfter)
{
spacingAfter *= 20;
if (spacingAfter - (int)spacingAfter == 0)
{
if (!(spacingAfter > -1585 && spacingAfter < 1585))
throw new ArgumentException("SpacingAfter", "Value must be in the range: -1584 - 1584");
}
else
throw new ArgumentException("SpacingAfter", "Value must be either a whole or acurate to one decimal, examples: 32, 32.1, 32.2, 32.9");
ApplyTextFormattingProperty(XName.Get("after", DocX.w.NamespaceName), string.Empty, new XAttribute(XName.Get("val", DocX.w.NamespaceName), spacingAfter));
return this;
}
public Paragraph Kerning(int kerning)
{
if (!new int?[] { 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72 }.Contains(kerning))
@@ -3504,6 +3587,63 @@ namespace Novacode
fldSimple.Add(content);
Xml.Add(fldSimple);
}
public float LineSpacingBefore
{
get
{
XElement pPr = GetOrCreate_pPr();
XElement spacing = pPr.Element(XName.Get("spacing", DocX.w.NamespaceName));
if (spacing != null)
{
XAttribute line = spacing.Attribute(XName.Get("before", DocX.w.NamespaceName));
if (line != null)
{
float f;
if (float.TryParse(line.Value, out f))
return f / 20.0f;
}
}
return 0.0f;
}
set
{
SpacingBefore(value);
}
}
public float LineSpacingAfter
{
get
{
XElement pPr = GetOrCreate_pPr();
XElement spacing = pPr.Element(XName.Get("spacing", DocX.w.NamespaceName));
if (spacing != null)
{
XAttribute line = spacing.Attribute(XName.Get("after", DocX.w.NamespaceName));
if (line != null)
{
float f;
if (float.TryParse(line.Value, out f))
return f / 20.0f;
}
}
return 10.0f;
}
set
{
SpacingAfter(value);
}
}
}
public class Run : DocXElement

正在加载...
取消
保存