Переглянути джерело

Change to add support for paragraph:

- "Keep With Next"
- "Keep Lines Together"

Also fixes an issue with:
- SpacingBefore
- SpacingAfter

Also 2 examples for Keep With Next and Keep Lines Together added into Exampels. 

Patch provided by someotherdevguy. Thanks!
master
MadBoy_cp 10 роки тому
джерело
коміт
204ad62d12
2 змінених файлів з 146 додано та 17 видалено
  1. 109
    17
      DocX/Paragraph.cs
  2. 37
    0
      Examples/Program.cs

+ 109
- 17
DocX/Paragraph.cs Переглянути файл

@@ -436,7 +436,75 @@ namespace Novacode
}
}
}
/// <summary>
/// This paragraph will be kept on the same page as the next paragraph
/// </summary>
/// <example>
/// Create a Paragraph that will stay on the same page as the paragraph that comes next
/// <code>
/// // Create a new document.
/// using (DocX document = DocX.Create("Test.docx"))
///
/// {
/// // Create a new Paragraph with the text "Hello World".
/// Paragraph p = document.InsertParagraph("Hello World.");
/// p.KeepWithNext();
/// document.InsertParagraph("Previous paragraph will appear on the same page as this paragraph");
/// // Save all changes made to this document.
/// document.Save();
/// }
/// </code>
/// </example>
/// <param name="keepWithNext"></param>
/// <returns></returns>
public Paragraph KeepWithNext(bool keepWithNext = true)
{
var pPr = GetOrCreate_pPr();
var keepWithNextE = pPr.Element(XName.Get("keepNext", DocX.w.NamespaceName));
if (keepWithNextE == null && keepWithNext)
{
pPr.Add(new XElement(XName.Get("keepNext", DocX.w.NamespaceName)));
}
if (!keepWithNext && keepWithNextE != null)
{
keepWithNextE.Remove();
}
return this;
}
/// <summary>
/// Keep all lines in this paragraph together on a page
/// </summary>
/// <example>
/// Create a Paragraph whose lines will stay together on a single page
/// <code>
/// // Create a new document.
/// using (DocX document = DocX.Create("Test.docx"))
/// {
/// // Create a new Paragraph with the text "Hello World".
/// Paragraph p = document.InsertParagraph("All lines of this paragraph will appear on the same page...\nLine 2\nLine 3\nLine 4\nLine 5\nLine 6...");
/// p.KeepLinesTogether();
/// // Save all changes made to this document.
/// document.Save();
/// }
/// </code>
/// </example>
/// <param name="keepTogether"></param>
/// <returns></returns>
public Paragraph KeepLinesTogether(bool keepTogether = true)
{
var pPr = GetOrCreate_pPr();
var keepLinesE = pPr.Element(XName.Get("keepLines", DocX.w.NamespaceName));
if (keepLinesE == null && keepTogether)
{
pPr.Add(new XElement(XName.Get("keepLines", DocX.w.NamespaceName)));
}
if (!keepTogether && keepLinesE != null)
{
keepLinesE.Remove();
}
return this;
}
/// <summary>
/// If the pPr element doesent exist it is created, either way it is returned by this function.
/// </summary>
@@ -3286,16 +3354,28 @@ namespace Novacode
{
spacingBefore *= 20;
if (spacingBefore - (int)spacingBefore == 0)
var pPr = GetOrCreate_pPr();
var spacing = pPr.Element(XName.Get("spacing", DocX.w.NamespaceName));
if (spacingBefore > 0)
{
if (!(spacingBefore > -1585 && spacingBefore < 1585))
throw new ArgumentException("SpacingBefore", "Value must be in the range: -1584 - 1584");
if (spacing == null)
{
spacing = new XElement(XName.Get("spacing", DocX.w.NamespaceName));
pPr.Add(spacing);
}
var attr = spacing.Attribute(XName.Get("before", DocX.w.NamespaceName));
if (attr == null)
spacing.SetAttributeValue(XName.Get("before", DocX.w.NamespaceName), spacingBefore);
else
attr.SetValue(spacingBefore);
}
if (Math.Abs(spacingBefore) < 0.1f && spacing != null)
{
var attr = spacing.Attribute(XName.Get("before", DocX.w.NamespaceName));
attr.Remove();
if (!spacing.HasAttributes)
spacing.Remove();
}
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;
}
@@ -3304,16 +3384,28 @@ namespace Novacode
{
spacingAfter *= 20;
if (spacingAfter - (int)spacingAfter == 0)
var pPr = GetOrCreate_pPr();
var spacing = pPr.Element(XName.Get("spacing", DocX.w.NamespaceName));
if (spacingAfter > 0)
{
if (!(spacingAfter > -1585 && spacingAfter < 1585))
throw new ArgumentException("SpacingAfter", "Value must be in the range: -1584 - 1584");
if (spacing == null)
{
spacing = new XElement(XName.Get("spacing", DocX.w.NamespaceName));
pPr.Add(spacing);
}
var attr = spacing.Attribute(XName.Get("after", DocX.w.NamespaceName));
if (attr == null)
spacing.SetAttributeValue(XName.Get("after", DocX.w.NamespaceName), spacingAfter);
else
attr.SetValue(spacingAfter);
}
if (Math.Abs(spacingAfter) < 0.1f && spacing != null)
{
var attr = spacing.Attribute(XName.Get("after", DocX.w.NamespaceName));
attr.Remove();
if (!spacing.HasAttributes)
spacing.Remove();
}
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;
}

+ 37
- 0
Examples/Program.cs Переглянути файл

@@ -19,6 +19,8 @@ namespace Examples
// Easy
Console.WriteLine("\nRunning Easy Examples");
HelloWorld();
HelloWorldKeepLinesTogether();
HelloWorldKeepWithNext();
RightToLeft();
Indentation();
HeadersAndFooters();
@@ -1567,5 +1569,40 @@ namespace Examples
document.Save();
}
}
static void HelloWorldKeepWithNext()
{
// Create a Paragraph that will stay on the same page as the paragraph that comes next
Console.WriteLine("\tHelloWorldKeepWithNext()");
// Create a new document.
using (DocX document = DocX.Create("docs\\HelloWorldKeepWithNext.docx"))
{
// Create a new Paragraph with the text "Hello World".
Paragraph p = document.InsertParagraph("Hello World.");
p.KeepWithNext();
document.InsertParagraph("Previous paragraph will appear on the same page as this paragraph");
// Save all changes made to this document.
document.Save();
Console.WriteLine("\tCreated: docs\\HelloWorldKeepWithNext.docx\n");
}
}
static void HelloWorldKeepLinesTogether()
{
// Create a Paragraph that will stay on the same page as the paragraph that comes next
Console.WriteLine("\tHelloWorldKeepLinesTogether()");
// Create a new document.
using (DocX document = DocX.Create("docs\\HelloWorldKeepLinesTogether.docx"))
{
// Create a new Paragraph with the text "Hello World".
Paragraph p = document.InsertParagraph("All lines of this paragraph will appear on the same page...\nLine 2\nLine 3\nLine 4\nLine 5\nLine 6...");
p.KeepLinesTogether();
// Save all changes made to this document.
document.Save();
Console.WriteLine("\tCreated: docs\\HelloWorldKeepLinesTogether.docx\n");
}
}
}
}

Завантаження…
Відмінити
Зберегти