/*************************************************************************************
DocX – DocX is the community edition of Xceed Words for .NET
Copyright (C) 2009-2016 Xceed Software Inc.
This program is provided to you under the terms of the Microsoft Public
License (Ms-PL) as published at http://wpftoolkit.codeplex.com/license
For more features and fast professional support,
pick up Xceed Words for .NET at https://xceed.com/xceed-words-for-net/
***********************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using System.IO.Packaging;
using System.IO;
using System.Drawing;
using System.Collections.ObjectModel;
using System.Diagnostics;
namespace Xceed.Words.NET
{
public abstract class Container : DocXElement
{
#region Public Properties
///
/// Returns a list of all Paragraphs inside this container.
///
///
///
/// Load a document.
/// using (DocX document = DocX.Load(@"Test.docx"))
/// {
/// // All Paragraphs in this document.
/// ]]> documentParagraphs = document.Paragraphs;
///
/// // Make sure this document contains at least one Table.
/// if (document.Tables.Count() > 0)
/// {
/// // Get the first Table in this document.
/// Table t = document.Tables[0];
///
/// // All Paragraphs in this Table.
/// ]]> tableParagraphs = t.Paragraphs;
///
/// // Make sure this Table contains at least one Row.
/// if (t.Rows.Count() > 0)
/// {
/// // Get the first Row in this document.
/// Row r = t.Rows[0];
///
/// // All Paragraphs in this Row.
/// ]]> rowParagraphs = r.Paragraphs;
///
/// // Make sure this Row contains at least one Cell.
/// if (r.Cells.Count() > 0)
/// {
/// // Get the first Cell in this document.
/// Cell c = r.Cells[0];
///
/// // All Paragraphs in this Cell.
/// ]]> cellParagraphs = c.Paragraphs;
/// }
/// }
/// }
///
/// // Save all changes to this document.
/// document.Save();
/// }// Release this document from memory.
///
///
public virtual ReadOnlyCollection Paragraphs
{
get
{
var paragraphs = this.GetParagraphs();
this.InitParagraphs( paragraphs );
return paragraphs.AsReadOnly();
}
}
public virtual ReadOnlyCollection ParagraphsDeepSearch
{
get
{
return this.Paragraphs;
//var paragraphs = this.GetParagraphs( true );
//this.InitParagraphs( paragraphs );
//return paragraphs.AsReadOnly();
}
}
public virtual List Sections
{
get
{
var paragraphs = Paragraphs;
var sections = new List();
var sectionParagraphs = new List();
foreach( Paragraph paragraph in paragraphs )
{
var sectionInPara = paragraph.Xml.Descendants().FirstOrDefault( s => s.Name.LocalName == "sectPr" );
if( sectionInPara != null )
{
sectionParagraphs.Add( paragraph );
var section = new Section( Document, sectionInPara );
section.SectionParagraphs = sectionParagraphs;
sections.Add( section );
sectionParagraphs = new List();
}
else
{
sectionParagraphs.Add( paragraph );
}
}
var body = Xml.DescendantsAndSelf( XName.Get( "body", DocX.w.NamespaceName ) ).FirstOrDefault();
var baseSectionXml = body?.Element( XName.Get( "sectPr", DocX.w.NamespaceName ) );
if (baseSectionXml != null)
{
var baseSection = new Section( Document, baseSectionXml );
baseSection.SectionParagraphs = sectionParagraphs;
sections.Add( baseSection );
}
return sections;
}
}
public virtual List
Tables
{
get
{
List
tables =
(
from t in Xml.Descendants( DocX.w + "tbl" )
select new Table( Document, t )
).ToList();
return tables;
}
}
public virtual List Hyperlinks
{
get
{
List hyperlinks = new List();
foreach( Paragraph p in Paragraphs )
hyperlinks.AddRange( p.Hyperlinks );
return hyperlinks;
}
}
public virtual List Pictures
{
get
{
List pictures = new List();
foreach( Paragraph p in Paragraphs )
pictures.AddRange( p.Pictures );
return pictures;
}
}
public virtual List Lists
{
get
{
var lists = new List();
var list = new List( Document, Xml );
foreach( var paragraph in Paragraphs )
{
if( paragraph.IsListItem )
{
if( list.CanAddListItem( paragraph ) )
{
list.AddItem( paragraph );
}
else
{
lists.Add( list );
list = new List( Document, Xml );
list.AddItem( paragraph );
}
}
}
lists.Add( list );
return lists;
}
}
#endregion
#region Public Methods
///
/// Sets the Direction of content.
///
/// Direction either LeftToRight or RightToLeft
///
/// Set the Direction of content in a Paragraph to RightToLeft.
///
/// // Load a document.
/// using (DocX document = DocX.Load(@"Test.docx"))
/// {
/// // Get the first Paragraph from this document.
/// Paragraph p = document.InsertParagraph();
///
/// // Set the Direction of this Paragraph.
/// p.Direction = Direction.RightToLeft;
///
/// // Make sure the document contains at lest one Table.
/// if (document.Tables.Count() > 0)
/// {
/// // Get the first Table from this document.
/// Table t = document.Tables[0];
///
/// /*
/// * Set the direction of the entire Table.
/// * Note: The same function is available at the Row and Cell level.
/// */
/// t.SetDirection(Direction.RightToLeft);
/// }
///
/// // Save all changes to this document.
/// document.Save();
/// }// Release this document from memory.
///
///
public virtual void SetDirection( Direction direction )
{
foreach( Paragraph p in Paragraphs )
p.Direction = direction;
}
public virtual List FindAll( string str )
{
return FindAll( str, RegexOptions.None );
}
public virtual List FindAll( string str, RegexOptions options )
{
List list = new List();
foreach( Paragraph p in Paragraphs )
{
List indexes = p.FindAll( str, options );
for( int i = 0; i < indexes.Count(); i++ )
indexes[ i ] += p._startIndex;
list.AddRange( indexes );
}
return list;
}
///
/// Find all unique instances of the given Regex Pattern,
/// returning the list of the unique strings found
///
///
///
///
public virtual List FindUniqueByPattern( string pattern, RegexOptions options )
{
var rawResults = new List();
foreach( Paragraph p in Paragraphs )
{ // accumulate the search results from all paragraphs
var partials = p.FindAllByPattern( pattern, options );
rawResults.AddRange( partials );
}
// this dictionary is used to collect results and test for uniqueness
var uniqueResults = new Dictionary();
foreach( string currValue in rawResults )
{
if( !uniqueResults.ContainsKey( currValue ) )
{ // if the dictionary doesn't have it, add it
uniqueResults.Add( currValue, 0 );
}
}
return uniqueResults.Keys.ToList(); // return the unique list of results
}
public virtual void ReplaceText( string searchValue,
string newValue,
bool trackChanges = false,
RegexOptions options = RegexOptions.None,
Formatting newFormatting = null,
Formatting matchFormatting = null,
MatchFormattingOptions fo = MatchFormattingOptions.SubsetMatch,
bool escapeRegEx = true,
bool useRegExSubstitutions = false,
bool removeEmptyParagraph = true )
{
if( string.IsNullOrEmpty( searchValue ) )
{
throw new ArgumentException( "searchValue cannot be null or empty.", "searchValue" );
}
if( newValue == null )
{
throw new ArgumentException( "newValue cannot be null.", "newValue" );
}
// ReplaceText in Headers of the document.
var headerList = new List() { this.Document.Headers.First, this.Document.Headers.Even, this.Document.Headers.Odd };
foreach( Header h in headerList )
{
if( h != null )
{
foreach( Paragraph p in h.Paragraphs )
{
p.ReplaceText( searchValue, newValue, trackChanges, options, newFormatting, matchFormatting, fo, escapeRegEx, useRegExSubstitutions, removeEmptyParagraph );
}
}
}
// ReplaceText int main body of document.
foreach( Paragraph p in this.Paragraphs )
{
p.ReplaceText( searchValue, newValue, trackChanges, options, newFormatting, matchFormatting, fo, escapeRegEx, useRegExSubstitutions, removeEmptyParagraph );
}
// ReplaceText in Footers of the document.
var footerList = new List