Upload to server
uploading
This commit is contained in:
88
omegapro/PowerTools/Cmdlets/AddOpenXmlContentCmdlet.cs
Normal file
88
omegapro/PowerTools/Cmdlets/AddOpenXmlContentCmdlet.cs
Normal file
@@ -0,0 +1,88 @@
|
||||
/***************************************************************************
|
||||
|
||||
Copyright (c) Microsoft Corporation 2011.
|
||||
|
||||
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
|
||||
|
||||
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace OpenXmlPowerTools.Commands
|
||||
{
|
||||
[Cmdlet(VerbsCommon.Add, "OpenXmlContent", SupportsShouldProcess = true)]
|
||||
[OutputType("OpenXmlPowerToolsDocument")]
|
||||
public class AddOpenXmlContentCmdlet : PowerToolsModifierCmdlet
|
||||
{
|
||||
#region Parameters
|
||||
|
||||
string[] xmlPartPaths;
|
||||
string xpathInsertionPoint;
|
||||
string xmlContent;
|
||||
|
||||
[Parameter(Position = 2, Mandatory = true, HelpMessage = "Custom Xml part path")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string[] PartPath
|
||||
{
|
||||
get
|
||||
{
|
||||
return xmlPartPaths;
|
||||
}
|
||||
set
|
||||
{
|
||||
xmlPartPaths = value;
|
||||
}
|
||||
}
|
||||
|
||||
[Parameter(Position = 3, Mandatory = true, HelpMessage = "Insertion point location")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string InsertionPoint
|
||||
{
|
||||
get
|
||||
{
|
||||
return xpathInsertionPoint;
|
||||
}
|
||||
set
|
||||
{
|
||||
xpathInsertionPoint = value;
|
||||
}
|
||||
}
|
||||
|
||||
[Parameter(Position = 4, Mandatory = true, HelpMessage = "Xml to insert")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string Content
|
||||
{
|
||||
get
|
||||
{
|
||||
return xmlContent;
|
||||
}
|
||||
set
|
||||
{
|
||||
xmlContent = value;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cmdlet Overrides
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
foreach (var document in AllDocuments("Add-OpenXmlContent"))
|
||||
{
|
||||
try
|
||||
{
|
||||
OutputDocument(PowerToolsExtensions.InsertXml(document, xmlPartPaths, xpathInsertionPoint, xmlContent));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, document));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
@@ -0,0 +1,66 @@
|
||||
/***************************************************************************
|
||||
|
||||
Copyright (c) Microsoft Corporation 2011.
|
||||
|
||||
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
|
||||
|
||||
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Management.Automation;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace OpenXmlPowerTools.Commands
|
||||
{
|
||||
[Cmdlet(VerbsCommon.Add, "OpenXmlDigitalSignature", SupportsShouldProcess = true)]
|
||||
[OutputType("OpenXmlPowerToolsDocument")]
|
||||
public class AddOpenXmlDigitalSignatureCmdlet : PowerToolsModifierCmdlet
|
||||
{
|
||||
#region Parameters
|
||||
|
||||
private string certificate;
|
||||
|
||||
[Parameter(
|
||||
Position = 2,
|
||||
Mandatory = true,
|
||||
ValueFromPipelineByPropertyName = true,
|
||||
HelpMessage = "Digital certificate path")
|
||||
]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string Certificate
|
||||
{
|
||||
get
|
||||
{
|
||||
return certificate;
|
||||
}
|
||||
set
|
||||
{
|
||||
certificate = value;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cmdlet Overrides
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
IEnumerable<string> certList = SessionState.Path.GetResolvedPSPathFromPSPath(certificate).Select(e => e.Path);
|
||||
foreach (var document in AllDocuments("Add-OpenXmlDigitalSignature"))
|
||||
{
|
||||
try
|
||||
{
|
||||
OutputDocument(DigitalSignatureAccessor.Insert(document, certList));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, document));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
93
omegapro/PowerTools/Cmdlets/AddOpenXmlDocumentIndexCmdlet.cs
Normal file
93
omegapro/PowerTools/Cmdlets/AddOpenXmlDocumentIndexCmdlet.cs
Normal file
@@ -0,0 +1,93 @@
|
||||
/***************************************************************************
|
||||
|
||||
Copyright (c) Microsoft Corporation 2008.
|
||||
|
||||
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
|
||||
|
||||
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Management.Automation;
|
||||
using DocumentFormat.OpenXml.Packaging;
|
||||
|
||||
namespace OpenXmlPowerTools.Commands
|
||||
{
|
||||
/// <summary>
|
||||
/// Add-OpenXmlIndex cmdlet
|
||||
/// </summary>
|
||||
[Cmdlet(VerbsCommon.Add, "OpenXmlDocumentIndex", SupportsShouldProcess = true)]
|
||||
[OutputType("OpenXmlPowerToolsDocument")]
|
||||
public class AddOpenXmlDocumentIndexCmdlet : PowerToolsModifierCmdlet
|
||||
{
|
||||
#region Parameters
|
||||
|
||||
string stylesSourcePath = "";
|
||||
bool addDefaultStyles = false;
|
||||
|
||||
[Parameter(
|
||||
Position = 2,
|
||||
Mandatory = false,
|
||||
HelpMessage = "Path of the styles file used to get the styles of the index")
|
||||
]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string StylesSourcePath
|
||||
{
|
||||
get
|
||||
{
|
||||
return stylesSourcePath;
|
||||
}
|
||||
set
|
||||
{
|
||||
stylesSourcePath = value;
|
||||
}
|
||||
}
|
||||
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
HelpMessage = "Specifies if the styles used in the index must be added to the document")
|
||||
]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public SwitchParameter AddDefaultStyles
|
||||
{
|
||||
get
|
||||
{
|
||||
return addDefaultStyles;
|
||||
}
|
||||
set
|
||||
{
|
||||
addDefaultStyles = value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cmdlet Overrides
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
foreach (var document in AllDocuments("Add-OpenXmlDocumentIndex"))
|
||||
{
|
||||
try
|
||||
{
|
||||
using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(document))
|
||||
{
|
||||
using (WordprocessingDocument doc = streamDoc.GetWordprocessingDocument())
|
||||
{
|
||||
IndexAccessor.Generate(doc);
|
||||
StyleAccessor.CreateIndexStyles(doc, stylesSourcePath, addDefaultStyles);
|
||||
}
|
||||
OutputDocument(streamDoc.GetModifiedDocument());
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, document));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
64
omegapro/PowerTools/Cmdlets/AddOpenXmlDocumentTOACmdlet.cs
Normal file
64
omegapro/PowerTools/Cmdlets/AddOpenXmlDocumentTOACmdlet.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
/***************************************************************************
|
||||
|
||||
Copyright (c) Microsoft Corporation 2011.
|
||||
|
||||
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
|
||||
|
||||
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Management.Automation;
|
||||
using DocumentFormat.OpenXml.Packaging;
|
||||
|
||||
namespace OpenXmlPowerTools.Commands
|
||||
{
|
||||
/// <summary>
|
||||
/// Add-OpenXmlDocumentTOA cmdlet
|
||||
/// </summary>
|
||||
[Cmdlet(VerbsCommon.Add, "OpenXmlDocumentTOA", SupportsShouldProcess = true)]
|
||||
[OutputType("OpenXmlPowerToolsDocument")]
|
||||
public class AddOpenXmlDocumentTOACmdlet : PowerToolsModifierCmdlet
|
||||
{
|
||||
#region Parameters
|
||||
|
||||
[Parameter(
|
||||
Position = 2,
|
||||
Mandatory = true,
|
||||
HelpMessage = "XPath where the table of contents will be created.")
|
||||
]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string InsertionPoint;
|
||||
|
||||
[Parameter(
|
||||
Position = 3,
|
||||
Mandatory = true,
|
||||
HelpMessage = "Choices of how the table of contents will be created.")
|
||||
]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string Switches;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cmdlet Overrides
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
foreach (var document in AllDocuments("Add-OpenXmlDocumentTOA"))
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!(document is WmlDocument))
|
||||
throw new PowerToolsDocumentException("Not a wordprocessing document.");
|
||||
OutputDocument(ReferenceAdder.AddToa((WmlDocument)document, InsertionPoint, Switches, null));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, document));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
63
omegapro/PowerTools/Cmdlets/AddOpenXmlDocumentTOCCmdlet.cs
Normal file
63
omegapro/PowerTools/Cmdlets/AddOpenXmlDocumentTOCCmdlet.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
/***************************************************************************
|
||||
|
||||
Copyright (c) Microsoft Corporation 2011.
|
||||
|
||||
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
|
||||
|
||||
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace OpenXmlPowerTools.Commands
|
||||
{
|
||||
/// <summary>
|
||||
/// Add-OpenXmlDocumentTOC cmdlet
|
||||
/// </summary>
|
||||
[Cmdlet(VerbsCommon.Add, "OpenXmlDocumentTOC", SupportsShouldProcess = true)]
|
||||
[OutputType("OpenXmlPowerToolsDocument")]
|
||||
public class AddOpenXmlDocumentTOCCmdlet : PowerToolsModifierCmdlet
|
||||
{
|
||||
#region Parameters
|
||||
|
||||
[Parameter(
|
||||
Position = 2,
|
||||
Mandatory = true,
|
||||
HelpMessage = "XPath where the table of contents will be created.")
|
||||
]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string InsertionPoint;
|
||||
|
||||
[Parameter(
|
||||
Position = 3,
|
||||
Mandatory = true,
|
||||
HelpMessage = "Choices of how the table of contents will be created.")
|
||||
]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string Switches;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cmdlet Overrides
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
foreach (var document in AllDocuments("Add-OpenXmlDocumentTOC"))
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!(document is WmlDocument))
|
||||
throw new PowerToolsDocumentException("Not a wordprocessing document.");
|
||||
OutputDocument(ReferenceAdder.AddToc((WmlDocument)document , InsertionPoint, Switches, null, null));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, document));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
63
omegapro/PowerTools/Cmdlets/AddOpenXmlDocumentTOFCmdlet.cs
Normal file
63
omegapro/PowerTools/Cmdlets/AddOpenXmlDocumentTOFCmdlet.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
/***************************************************************************
|
||||
|
||||
Copyright (c) Microsoft Corporation 2011.
|
||||
|
||||
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
|
||||
|
||||
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace OpenXmlPowerTools.Commands
|
||||
{
|
||||
/// <summary>
|
||||
/// Add-OpenXmlDocumentTOF cmdlet
|
||||
/// </summary>
|
||||
[Cmdlet(VerbsCommon.Add, "OpenXmlDocumentTOF", SupportsShouldProcess = true)]
|
||||
[OutputType("OpenXmlPowerToolsDocument")]
|
||||
public class AddOpenXmlDocumentTOFCmdlet : PowerToolsModifierCmdlet
|
||||
{
|
||||
#region Parameters
|
||||
|
||||
[Parameter(
|
||||
Position = 2,
|
||||
Mandatory = true,
|
||||
HelpMessage = "XPath where the table of figures will be created.")
|
||||
]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string InsertionPoint;
|
||||
|
||||
[Parameter(
|
||||
Position = 3,
|
||||
Mandatory = true,
|
||||
HelpMessage = "Choices of how the table of contents will be created.")
|
||||
]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string Switches;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cmdlet Overrides
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
foreach (var document in AllDocuments("Add-OpenXmlDocumentTOF"))
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!(document is WmlDocument))
|
||||
throw new PowerToolsDocumentException("Not a wordprocessing document.");
|
||||
OutputDocument(ReferenceAdder.AddTof((WmlDocument)document, InsertionPoint, Switches, null));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, document));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
107
omegapro/PowerTools/Cmdlets/AddOpenXmlPictureCmdlet.cs
Normal file
107
omegapro/PowerTools/Cmdlets/AddOpenXmlPictureCmdlet.cs
Normal file
@@ -0,0 +1,107 @@
|
||||
/***************************************************************************
|
||||
|
||||
Copyright (c) Microsoft Corporation 2011.
|
||||
|
||||
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
|
||||
|
||||
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace OpenXmlPowerTools.Commands
|
||||
{
|
||||
[Cmdlet(VerbsCommon.Add, "OpenXmlPicture", SupportsShouldProcess = true)]
|
||||
[OutputType("OpenXmlPowerToolsDocument")]
|
||||
public class AddOpenXmlPictureCmdlet : PowerToolsModifierCmdlet
|
||||
{
|
||||
#region Parameters
|
||||
|
||||
private string xpathInsertionPoint;
|
||||
private string picturePath;
|
||||
private System.Drawing.Image image;
|
||||
|
||||
[Parameter(Position = 2, Mandatory = true, HelpMessage = "Insertion point location")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string InsertionPoint
|
||||
{
|
||||
get
|
||||
{
|
||||
return xpathInsertionPoint;
|
||||
}
|
||||
set
|
||||
{
|
||||
xpathInsertionPoint = value;
|
||||
}
|
||||
}
|
||||
|
||||
[Parameter(Position = 3, Mandatory = false, HelpMessage = "Picture path")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string PicturePath
|
||||
{
|
||||
get
|
||||
{
|
||||
return picturePath;
|
||||
}
|
||||
set
|
||||
{
|
||||
picturePath = SessionState.Path.GetResolvedPSPathFromPSPath(value).First().Path;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ImageFile parameter
|
||||
/// </summary>
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
ValueFromPipeline = true,
|
||||
HelpMessage = "Image object to be inserted")
|
||||
]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public System.IO.FileInfo ImageFile
|
||||
{
|
||||
get
|
||||
{
|
||||
return new System.IO.FileInfo(picturePath == null ? "." : picturePath);
|
||||
}
|
||||
set
|
||||
{
|
||||
picturePath = value.FullName;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cmdlet Overrides
|
||||
|
||||
/// <summary>
|
||||
/// Entry point for PowerShell cmdlets
|
||||
/// </summary>
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
string picName = "PowerToolsPicture";
|
||||
if (image == null && picturePath != null)
|
||||
{
|
||||
image = System.Drawing.Image.FromFile(picturePath, true);
|
||||
picName = picturePath.Substring(picturePath.LastIndexOf('\\') + 1);
|
||||
}
|
||||
foreach (var document in AllDocuments("Add-OpenXmlPicture"))
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!(document is WmlDocument))
|
||||
throw new PowerToolsDocumentException("Not a wordprocessing document.");
|
||||
OutputDocument(PictureAccessor.Insert((WmlDocument)document, xpathInsertionPoint, image, picName));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, document));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
211
omegapro/PowerTools/Cmdlets/AddOpenXmlSpreadSheetTableCmdlet.cs
Normal file
211
omegapro/PowerTools/Cmdlets/AddOpenXmlSpreadSheetTableCmdlet.cs
Normal file
@@ -0,0 +1,211 @@
|
||||
/***************************************************************************
|
||||
|
||||
Copyright (c) Microsoft Corporation 2011.
|
||||
|
||||
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
|
||||
|
||||
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
|
||||
|
||||
***************************************************************************/
|
||||
using System;
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace OpenXmlPowerTools.Commands
|
||||
{
|
||||
/// <summary>
|
||||
/// Cmdlet for adding a new table to a worksheet in a SpreadsheetML document
|
||||
/// </summary>
|
||||
[Cmdlet(VerbsCommon.Add, "OpenXmlSpreadSheetTable", SupportsShouldProcess = true)]
|
||||
[OutputType("OpenXmlPowerToolsDocument")]
|
||||
public class AddOpenXmlSpreadSheetTableCmdlet : PowerToolsModifierCmdlet
|
||||
{
|
||||
#region Parameters
|
||||
|
||||
private string tableStyle = string.Empty;
|
||||
private SwitchParameter hasHeaders =false;
|
||||
private short fromColumn;
|
||||
private short toColumn;
|
||||
private int fromRow;
|
||||
private int toRow;
|
||||
private string worksheetName;
|
||||
|
||||
[Parameter(Position = 2,
|
||||
Mandatory = true,
|
||||
HelpMessage = "Table Style Name")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string TableStyle
|
||||
{
|
||||
get
|
||||
{
|
||||
return tableStyle;
|
||||
}
|
||||
set
|
||||
{
|
||||
tableStyle = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Use Headers parameter
|
||||
/// </summary>
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
HelpMessage = "Has Headers?")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public SwitchParameter HasHeaders
|
||||
{
|
||||
get
|
||||
{
|
||||
return hasHeaders;
|
||||
}
|
||||
set
|
||||
{
|
||||
hasHeaders = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initial table column
|
||||
/// </summary>
|
||||
[Parameter(Position = 3,
|
||||
Mandatory = true,
|
||||
HelpMessage = "Initial table column")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public short FromColumn
|
||||
{
|
||||
get
|
||||
{
|
||||
return fromColumn;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (value > 0)
|
||||
{
|
||||
fromColumn = value;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new System.Management.Automation.ParameterBindingException("Initial Table Column must be greater than 0");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Final table column
|
||||
/// </summary>
|
||||
[Parameter(Position = 4,
|
||||
Mandatory = true,
|
||||
HelpMessage = "Final table column")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public short ToColumn
|
||||
{
|
||||
get
|
||||
{
|
||||
return toColumn;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (value > 0)
|
||||
{
|
||||
toColumn = value;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new System.Management.Automation.ParameterBindingException("Final table column must be greater than 0");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initial table row
|
||||
/// </summary>
|
||||
[Parameter(Position = 5,
|
||||
Mandatory = true,
|
||||
HelpMessage = "Initial table row")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public int FromRow
|
||||
{
|
||||
get
|
||||
{
|
||||
return fromRow;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (value > 0)
|
||||
{
|
||||
fromRow = value;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new System.Management.Automation.ParameterBindingException("Initial Table Row must be greater than 0");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Final table row
|
||||
/// </summary>
|
||||
[Parameter(Position = 6,
|
||||
Mandatory = true,
|
||||
HelpMessage = "Final table row")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public int ToRow
|
||||
{
|
||||
get
|
||||
{
|
||||
return toRow;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (value > 0)
|
||||
{
|
||||
toRow = value;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new System.Management.Automation.ParameterBindingException("Final table row must be greater than 0");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Index for worksheet to add the table to
|
||||
/// </summary>
|
||||
[Parameter(Position = 7,
|
||||
Mandatory = true,
|
||||
HelpMessage = "Worksheet name to add the table to")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string WorksheetName
|
||||
{
|
||||
get
|
||||
{
|
||||
return worksheetName;
|
||||
}
|
||||
set
|
||||
{
|
||||
worksheetName = value;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cmdlet Overrides
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
foreach (var document in AllDocuments("Add-OpenXmlSpreadSheetTable"))
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!(document is SmlDocument))
|
||||
throw new PowerToolsDocumentException("Not a spreadsheet document.");
|
||||
OutputDocument(SpreadSheetTableAccessor.Add((SmlDocument)document, worksheetName, tableStyle, hasHeaders, fromColumn, toColumn, fromRow, toRow));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, document));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
70
omegapro/PowerTools/Cmdlets/ConfirmOpenXmlValidCmdlet.cs
Normal file
70
omegapro/PowerTools/Cmdlets/ConfirmOpenXmlValidCmdlet.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
/***************************************************************************
|
||||
|
||||
Copyright (c) Microsoft Corporation 2011.
|
||||
|
||||
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
|
||||
|
||||
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Management.Automation;
|
||||
using DocumentFormat.OpenXml;
|
||||
using DocumentFormat.OpenXml.Packaging;
|
||||
using DocumentFormat.OpenXml.Validation;
|
||||
|
||||
namespace OpenXmlPowerTools.Commands
|
||||
{
|
||||
public class ValidationInfo
|
||||
{
|
||||
public OpenXmlPowerToolsDocument Document;
|
||||
public string FileName;
|
||||
public string Description;
|
||||
public ValidationErrorType ErrorType;
|
||||
public string Id;
|
||||
public OpenXmlElement Node;
|
||||
public OpenXmlPart Part;
|
||||
public string XPath;
|
||||
public OpenXmlElement RelatedNode;
|
||||
public OpenXmlPart RelatedPart;
|
||||
|
||||
public ValidationInfo(OpenXmlPowerToolsDocument doc, ValidationErrorInfo err)
|
||||
{
|
||||
Document = doc;
|
||||
FileName = doc.FileName;
|
||||
Description = err.Description;
|
||||
ErrorType = err.ErrorType;
|
||||
Id = err.Id;
|
||||
Node = err.Node;
|
||||
Part = err.Part;
|
||||
XPath = err.Path.XPath;
|
||||
RelatedNode = err.RelatedNode;
|
||||
RelatedPart = err.RelatedPart;
|
||||
}
|
||||
}
|
||||
|
||||
[Cmdlet(VerbsLifecycle.Confirm, "OpenXmlValid")]
|
||||
[OutputType("ValidationInfo")]
|
||||
public class ConfirmOpenXmlValidCmdlet : PowerToolsReadOnlyCmdlet
|
||||
{
|
||||
#region Cmdlet Overrides
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
foreach (var document in AllDocuments("Confirm-OpenXmlValid"))
|
||||
{
|
||||
try
|
||||
{
|
||||
foreach (ValidationErrorInfo item in PowerToolsExtensions.ValidateXml(document))
|
||||
WriteObject(new ValidationInfo(document, item), true);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, document));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
48
omegapro/PowerTools/Cmdlets/EditOpenXmlChangeCmdlet.cs
Normal file
48
omegapro/PowerTools/Cmdlets/EditOpenXmlChangeCmdlet.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
/***************************************************************************
|
||||
|
||||
Copyright (c) Microsoft Corporation 2011.
|
||||
|
||||
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
|
||||
|
||||
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace OpenXmlPowerTools.Commands
|
||||
{
|
||||
[Cmdlet(VerbsData.Edit, "OpenXmlChange", SupportsShouldProcess = true)]
|
||||
[OutputType("OpenXmlPowerToolsDocument")]
|
||||
public class EditOpenXmlChangeCmdlet : PowerToolsModifierCmdlet
|
||||
{
|
||||
[Parameter(Mandatory = false)]
|
||||
public SwitchParameter Accept;
|
||||
|
||||
// Future option
|
||||
//[Parameter(Mandatory = false)]
|
||||
//public SwitchParameter Reject;
|
||||
|
||||
#region Cmdlet Overrides
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
foreach (var document in AllDocuments("Edit-OpenXmlChange"))
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!(document is WmlDocument))
|
||||
throw new PowerToolsDocumentException("Not a wordprocessing document.");
|
||||
if (Accept)
|
||||
OutputDocument(RevisionAccepter.AcceptRevisions((WmlDocument)document));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, document));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
247
omegapro/PowerTools/Cmdlets/ExportOpenXmlSpreadsheetCmdlet.cs
Normal file
247
omegapro/PowerTools/Cmdlets/ExportOpenXmlSpreadsheetCmdlet.cs
Normal file
@@ -0,0 +1,247 @@
|
||||
/***************************************************************************
|
||||
|
||||
Copyright (c) Microsoft Corporation 2011.
|
||||
|
||||
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
|
||||
|
||||
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Management.Automation;
|
||||
using DocumentFormat.OpenXml.Packaging;
|
||||
|
||||
namespace OpenXmlPowerTools.Commands
|
||||
{
|
||||
[Cmdlet(VerbsData.Export, "OpenXmlSpreadsheet", SupportsShouldProcess = true)]
|
||||
[OutputType("OpenXmlPowerToolsDocument")]
|
||||
public class ExportOpenXmlSpreadsheetCmdlet : PowerToolsCreateCmdlet
|
||||
{
|
||||
#region Parameters
|
||||
|
||||
private PSObject[] pipeObjects;
|
||||
private Collection<PSObject> processedObjects = new Collection<PSObject>();
|
||||
private List<string> columnsToChart;
|
||||
string headerColumn;
|
||||
string outputPath;
|
||||
bool displayChart;
|
||||
private int initialRow = 1;
|
||||
ChartType chartType;
|
||||
|
||||
[Parameter(
|
||||
Position = 0,
|
||||
Mandatory = true,
|
||||
ValueFromPipeline = false,
|
||||
HelpMessage = "Path of file in which to store results")
|
||||
]
|
||||
public string OutputPath
|
||||
{
|
||||
get
|
||||
{
|
||||
return outputPath;
|
||||
}
|
||||
set
|
||||
{
|
||||
outputPath = Path.Combine(SessionState.Path.CurrentLocation.Path, value);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// InputObject parameter
|
||||
/// </summary>
|
||||
[Parameter(
|
||||
ValueFromPipeline = true,
|
||||
HelpMessage = "Objects passed by pipe to be included in spreadsheet")
|
||||
]
|
||||
public PSObject[] InputObject
|
||||
{
|
||||
get
|
||||
{
|
||||
return pipeObjects;
|
||||
}
|
||||
set
|
||||
{
|
||||
pipeObjects = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Chart parameter
|
||||
/// </summary>
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
ValueFromPipeline = false,
|
||||
ParameterSetName = "charting",
|
||||
HelpMessage = "Whether generate a chart from loaded data or not")
|
||||
]
|
||||
public SwitchParameter Chart
|
||||
{
|
||||
get
|
||||
{
|
||||
return displayChart;
|
||||
}
|
||||
set
|
||||
{
|
||||
displayChart = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ChartType parameter
|
||||
/// </summary>
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
ValueFromPipeline = false,
|
||||
ParameterSetName = "charting",
|
||||
HelpMessage = "Type of chart to be generated")
|
||||
]
|
||||
public ChartType ChartType
|
||||
{
|
||||
get
|
||||
{
|
||||
return chartType;
|
||||
}
|
||||
set
|
||||
{
|
||||
chartType = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ColumnsToChart parameter
|
||||
/// </summary>
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
ValueFromPipeline = false,
|
||||
ParameterSetName = "charting",
|
||||
HelpMessage = "Columns from data to be used as series in chart")
|
||||
]
|
||||
public List<string> ColumnsToChart
|
||||
{
|
||||
get
|
||||
{
|
||||
return columnsToChart;
|
||||
}
|
||||
set
|
||||
{
|
||||
columnsToChart = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// HeaderColumn parameter
|
||||
/// </summary>
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
ValueFromPipeline = false,
|
||||
ParameterSetName = "charting",
|
||||
HelpMessage = "Column from data to be used as category in chart")
|
||||
]
|
||||
public string HeaderColumn
|
||||
{
|
||||
get
|
||||
{
|
||||
return headerColumn;
|
||||
}
|
||||
set
|
||||
{
|
||||
headerColumn = value;
|
||||
}
|
||||
}
|
||||
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
ValueFromPipeline = false,
|
||||
HelpMessage = "Header Row")
|
||||
]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public int InitialRow
|
||||
{
|
||||
get
|
||||
{
|
||||
return initialRow;
|
||||
}
|
||||
set
|
||||
{
|
||||
initialRow = value;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cmdlet Overrides
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
if (pipeObjects != null)
|
||||
{
|
||||
foreach (PSObject pipeObject in pipeObjects)
|
||||
processedObjects.Add(pipeObject);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void EndProcessing()
|
||||
{
|
||||
if (!File.Exists(outputPath) || ShouldProcess(outputPath, "Export-OpenXmlSpreadsheet"))
|
||||
{
|
||||
using (OpenXmlMemoryStreamDocument streamDoc = OpenXmlMemoryStreamDocument.CreateSpreadsheetDocument())
|
||||
{
|
||||
using (SpreadsheetDocument document = streamDoc.GetSpreadsheetDocument())
|
||||
{
|
||||
if (processedObjects.Count > 0)
|
||||
{
|
||||
List<string> headerList = new List<string>();
|
||||
foreach (PSPropertyInfo propertyInfo in processedObjects[0].Properties)
|
||||
{
|
||||
headerList.Add(propertyInfo.Name.ToUpper());
|
||||
}
|
||||
|
||||
// Stores into a matrix all properties of objects passed as parameter
|
||||
int rowLength = headerList.Count;
|
||||
int rowCount = processedObjects.Count;
|
||||
string[][] valueMatrix = new string[rowCount][];
|
||||
|
||||
int currentRow = 0, currentColumn = 0;
|
||||
foreach (PSObject obj in processedObjects)
|
||||
{
|
||||
currentColumn = 0;
|
||||
valueMatrix[currentRow] = new string[rowLength];
|
||||
foreach (PSPropertyInfo propertyInfo in obj.Properties)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (propertyInfo.Value != null)
|
||||
{
|
||||
valueMatrix[currentRow][currentColumn] = propertyInfo.Value.ToString();
|
||||
}
|
||||
}
|
||||
// Suppress errors on properties that cannot be read, but write the information to debug output.
|
||||
catch (GetValueInvocationException e)
|
||||
{
|
||||
WriteDebug(string.Format(CultureInfo.InvariantCulture, "Exception ({0}) at Object {1}, property {2}", e.Message, currentRow, currentColumn));
|
||||
}
|
||||
currentColumn++;
|
||||
}
|
||||
currentRow++;
|
||||
}
|
||||
if (displayChart)
|
||||
SpreadsheetDocumentManager.Create(document, headerList, valueMatrix, chartType, headerColumn, columnsToChart, initialRow);
|
||||
else
|
||||
SpreadsheetDocumentManager.Create(document, headerList, valueMatrix, initialRow);
|
||||
}
|
||||
}
|
||||
OpenXmlPowerToolsDocument output = streamDoc.GetModifiedDocument();
|
||||
output.FileName = outputPath;
|
||||
OutputDocument(output);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
163
omegapro/PowerTools/Cmdlets/ExportOpenXmlToHtmlCmdlet.cs
Normal file
163
omegapro/PowerTools/Cmdlets/ExportOpenXmlToHtmlCmdlet.cs
Normal file
@@ -0,0 +1,163 @@
|
||||
/***************************************************************************
|
||||
|
||||
Copyright (c) Microsoft Corporation 2011.
|
||||
|
||||
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
|
||||
|
||||
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Drawing.Imaging;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Management.Automation;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace OpenXmlPowerTools.Commands
|
||||
{
|
||||
/// <summary>
|
||||
/// Transform-OpenXmlToHtml cmdlet
|
||||
/// </summary>
|
||||
[Cmdlet(VerbsData.Export, "OpenXmlToHtml", SupportsShouldProcess = true)]
|
||||
public class ExportOpenXmlToHtmlCmdlet : PowerToolsReadOnlyCmdlet
|
||||
{
|
||||
#region Parameters
|
||||
|
||||
[Parameter(
|
||||
Position = 1,
|
||||
Mandatory = true,
|
||||
HelpMessage = "Title for the HTML page")
|
||||
]
|
||||
public string PageTitle;
|
||||
|
||||
[Parameter(
|
||||
Position = 2,
|
||||
Mandatory = false,
|
||||
HelpMessage = "Name of the html output file")
|
||||
]
|
||||
public string OutputPath;
|
||||
|
||||
[Parameter(
|
||||
Position = 3,
|
||||
Mandatory = false,
|
||||
HelpMessage = "Folder where image files are going to be placed")
|
||||
]
|
||||
public string ImageFolder;
|
||||
|
||||
[Parameter(
|
||||
Position = 4,
|
||||
Mandatory = false,
|
||||
HelpMessage = "Prefix for image file names")
|
||||
]
|
||||
public string ImagePrefix;
|
||||
|
||||
[Parameter(
|
||||
Position = 5,
|
||||
Mandatory = false,
|
||||
HelpMessage = "CSS file name")
|
||||
]
|
||||
public string CssPath;
|
||||
|
||||
[Parameter(
|
||||
Position = 6,
|
||||
Mandatory = false,
|
||||
HelpMessage = "CSS class name prefix")
|
||||
]
|
||||
public string CssClassPrefix;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cmdlet Overrides
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
foreach (var document in AllDocuments("Export-OpenXmlToHtml"))
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!(document is WmlDocument))
|
||||
throw new PowerToolsDocumentException("Not a wordprocessing document.");
|
||||
FileInfo info = null;
|
||||
if (document.FileName != null)
|
||||
info = new FileInfo(document.FileName);
|
||||
string htmlFileName;
|
||||
if (OutputPath != null)
|
||||
htmlFileName = System.IO.Path.Combine(SessionState.Path.CurrentLocation.Path, OutputPath);
|
||||
else
|
||||
{
|
||||
if (info == null)
|
||||
throw new ArgumentException("No output file name available.");
|
||||
htmlFileName = System.IO.Path.Combine(info.DirectoryName, info.Name.Substring(0, info.Name.Length - info.Extension.Length) + ".html");
|
||||
}
|
||||
string imageFolder;
|
||||
FileInfo outputInfo = new FileInfo(htmlFileName);
|
||||
if (ImageFolder != null)
|
||||
imageFolder = SessionState.Path.GetResolvedPSPathFromPSPath(ImageFolder).First().Path;
|
||||
else
|
||||
imageFolder = System.IO.Path.Combine(outputInfo.DirectoryName, "images");
|
||||
DirectoryInfo imageFolderInfo = new DirectoryInfo(imageFolder);
|
||||
if (!imageFolderInfo.Exists)
|
||||
imageFolderInfo.Create();
|
||||
HtmlConverterSettings settings = new HtmlConverterSettings();
|
||||
settings.PageTitle = PageTitle;
|
||||
if (CssPath != null)
|
||||
{
|
||||
settings.CssClassPrefix = CssClassPrefix;
|
||||
// Read CSS file into a string
|
||||
string cssFileName = SessionState.Path.GetResolvedPSPathFromPSPath(CssPath).First().Path;
|
||||
settings.Css = File.ReadAllText(cssFileName);
|
||||
}
|
||||
int imageCounter = 0;
|
||||
XElement html = HtmlConverter.ConvertToHtml((WmlDocument)document, settings,
|
||||
imageInfo =>
|
||||
{
|
||||
++imageCounter;
|
||||
string extension = imageInfo.ContentType.Split('/')[1].ToLower();
|
||||
ImageFormat imageFormat = null;
|
||||
if (extension == "png")
|
||||
{
|
||||
// Convert png to jpeg.
|
||||
extension = "jpeg";
|
||||
imageFormat = ImageFormat.Jpeg;
|
||||
}
|
||||
else if (extension == "bmp")
|
||||
imageFormat = ImageFormat.Bmp;
|
||||
else if (extension == "jpeg")
|
||||
imageFormat = ImageFormat.Jpeg;
|
||||
else if (extension == "tiff")
|
||||
imageFormat = ImageFormat.Tiff;
|
||||
|
||||
// If the image format isn't one that we expect, ignore it,
|
||||
// and don't return markup for the link.
|
||||
if (imageFormat == null)
|
||||
return null;
|
||||
|
||||
string imageFileName = System.IO.Path.Combine(imageFolder, "image" + imageCounter.ToString() + "." + extension);
|
||||
try
|
||||
{
|
||||
imageInfo.Bitmap.Save(imageFileName, imageFormat);
|
||||
}
|
||||
catch (System.Runtime.InteropServices.ExternalException)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
XElement img = new XElement(Xhtml.img,
|
||||
new XAttribute(NoNamespace.src, imageFileName),
|
||||
imageInfo.ImgStyleAttribute,
|
||||
imageInfo.AltText != null ?
|
||||
new XAttribute(NoNamespace.alt, imageInfo.AltText) : null);
|
||||
return img;
|
||||
});
|
||||
File.WriteAllText(htmlFileName, html.ToStringNewLineOnAttributes());
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, document));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
107
omegapro/PowerTools/Cmdlets/ExportOpenXmlWordprocessingCmdlet.cs
Normal file
107
omegapro/PowerTools/Cmdlets/ExportOpenXmlWordprocessingCmdlet.cs
Normal file
@@ -0,0 +1,107 @@
|
||||
/***************************************************************************
|
||||
|
||||
Copyright (c) Microsoft Corporation 2011.
|
||||
|
||||
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
|
||||
|
||||
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
using System.IO;
|
||||
using System.Collections.Generic;
|
||||
using System.Management.Automation;
|
||||
using System.Linq;
|
||||
using System.Xml.Linq;
|
||||
using DocumentFormat.OpenXml.Packaging;
|
||||
|
||||
namespace OpenXmlPowerTools.Commands
|
||||
{
|
||||
/// <summary>
|
||||
/// Export-OpenXmlWordprocessing cmdlet
|
||||
/// </summary>
|
||||
[Cmdlet("Export", "OpenXmlWordprocessing", SupportsShouldProcess = true)]
|
||||
[OutputType("OpenXmlPowerToolsDocument")]
|
||||
public class ExportOpenXmlWordprocessingCmdlet : PowerToolsCreateCmdlet
|
||||
{
|
||||
#region Parameters
|
||||
|
||||
private string outputPath;
|
||||
private string[] text;
|
||||
private List<string> processedObjects = new List<string>();
|
||||
|
||||
/// <summary>
|
||||
/// OutputPath parameter
|
||||
/// </summary>
|
||||
[Parameter(
|
||||
Position = 0,
|
||||
Mandatory = true,
|
||||
HelpMessage = "Path of file to store export results")
|
||||
]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string OutputPath
|
||||
{
|
||||
get
|
||||
{
|
||||
return outputPath;
|
||||
}
|
||||
set
|
||||
{
|
||||
outputPath = Path.Combine(SessionState.Path.CurrentLocation.Path, value);
|
||||
}
|
||||
}
|
||||
|
||||
[Parameter(
|
||||
Position = 1,
|
||||
ValueFromPipeline = true,
|
||||
Mandatory = true,
|
||||
HelpMessage = "Text to insert in the new wordprocessing document")
|
||||
]
|
||||
[AllowEmptyString]
|
||||
public string[] Text
|
||||
{
|
||||
get
|
||||
{
|
||||
return text;
|
||||
}
|
||||
set
|
||||
{
|
||||
text = value;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cmdlet Overrides
|
||||
|
||||
/// <summary>
|
||||
/// Entry point for PowerShell cmdlets
|
||||
/// </summary>
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
if (text != null)
|
||||
{
|
||||
foreach (string item in text)
|
||||
processedObjects.Add(item);
|
||||
}
|
||||
}
|
||||
protected override void EndProcessing()
|
||||
{
|
||||
if (PassThru || !File.Exists(outputPath) || ShouldProcess(outputPath, "Export-OpenXmlWordprocessing"))
|
||||
{
|
||||
using (OpenXmlMemoryStreamDocument streamDoc = OpenXmlMemoryStreamDocument.CreateWordprocessingDocument())
|
||||
{
|
||||
using (WordprocessingDocument document = streamDoc.GetWordprocessingDocument())
|
||||
{
|
||||
if (processedObjects.Count > 0)
|
||||
PowerToolsExtensions.SetContent(document, processedObjects.Select(e => new XElement(W.p, new XElement(W.r, new XElement(W.t, new XAttribute(XNamespace.Xml + "space", "preserve"), e)))));
|
||||
}
|
||||
OpenXmlPowerToolsDocument output = streamDoc.GetModifiedDocument();
|
||||
output.FileName = outputPath;
|
||||
OutputDocument(output);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
134
omegapro/PowerTools/Cmdlets/GetOpenXmlBackgroundCmdlet.cs
Normal file
134
omegapro/PowerTools/Cmdlets/GetOpenXmlBackgroundCmdlet.cs
Normal file
@@ -0,0 +1,134 @@
|
||||
/***************************************************************************
|
||||
|
||||
Copyright (c) Microsoft Corporation 2011.
|
||||
|
||||
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
|
||||
|
||||
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace OpenXmlPowerTools.Commands
|
||||
{
|
||||
/// <summary>
|
||||
/// Get-OpenXmlBackground cmdlet
|
||||
/// </summary>
|
||||
[Cmdlet(VerbsCommon.Get, "OpenXmlBackground", SupportsShouldProcess = true)]
|
||||
public class GetOpenXmlBackgroundCmdlet : PowerToolsReadOnlyCmdlet
|
||||
{
|
||||
private bool backgroundColor;
|
||||
private bool backgroundImage;
|
||||
private string outputFolder;
|
||||
|
||||
#region Parameters
|
||||
|
||||
[Parameter(Position = 1,
|
||||
Mandatory = false,
|
||||
ValueFromPipeline = false,
|
||||
HelpMessage = "Path of folder to store result documents")
|
||||
]
|
||||
public string OutputFolder
|
||||
{
|
||||
get
|
||||
{
|
||||
return outputFolder;
|
||||
}
|
||||
set
|
||||
{
|
||||
outputFolder = SessionState.Path.Combine(SessionState.Path.CurrentLocation.Path, value);
|
||||
}
|
||||
}
|
||||
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
HelpMessage = "Gets the image from the background.")
|
||||
]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public SwitchParameter Image
|
||||
{
|
||||
get
|
||||
{
|
||||
return backgroundImage;
|
||||
}
|
||||
set
|
||||
{
|
||||
backgroundImage = value;
|
||||
}
|
||||
}
|
||||
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
HelpMessage = "Gets the color of the background (hexadecimal format)")
|
||||
]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public SwitchParameter Color
|
||||
{
|
||||
get
|
||||
{
|
||||
return backgroundColor;
|
||||
}
|
||||
set
|
||||
{
|
||||
backgroundColor = value;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cmdlet Overrides
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
if (backgroundColor || backgroundImage)
|
||||
{
|
||||
foreach (var document in AllDocuments("Get-OpenXmlBackground"))
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!(document is WmlDocument))
|
||||
throw new PowerToolsDocumentException("Not a wordprocessing document.");
|
||||
string bgColor = string.Empty;
|
||||
string bgImagePath = string.Empty;
|
||||
if (backgroundColor)
|
||||
{
|
||||
bgColor = BackgroundAccessor.GetBackgroundColor((WmlDocument)document);
|
||||
if (bgColor != "")
|
||||
WriteObject(System.Drawing.Color.FromArgb(Convert.ToInt32(bgColor, 16)), true);
|
||||
}
|
||||
else if (backgroundImage)
|
||||
{
|
||||
string filename = BackgroundAccessor.GetImageFileName((WmlDocument)document);
|
||||
if (filename != "")
|
||||
{
|
||||
string target = filename;
|
||||
if (OutputFolder != null)
|
||||
{
|
||||
FileInfo temp = new FileInfo(filename);
|
||||
target = OutputFolder + "\\" + temp.Name;
|
||||
}
|
||||
if (!File.Exists(target) || ShouldProcess(target, "Get-OpenXmlBackground"))
|
||||
{
|
||||
BackgroundAccessor.SaveImageToFile((WmlDocument)document, target);
|
||||
System.IO.FileInfo fileInfo = new System.IO.FileInfo(filename);
|
||||
WriteObject(fileInfo, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, document));
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
WriteError(new ErrorRecord(new ArgumentException("Requires one of the two switches: Color or Image."), "OpenXmlPowerToolsError", ErrorCategory.InvalidArgument, null));
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
40
omegapro/PowerTools/Cmdlets/GetOpenXmlCommentCmdlet.cs
Normal file
40
omegapro/PowerTools/Cmdlets/GetOpenXmlCommentCmdlet.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
/***************************************************************************
|
||||
|
||||
Copyright (c) Microsoft Corporation 2011.
|
||||
|
||||
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
|
||||
|
||||
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace OpenXmlPowerTools.Commands
|
||||
{
|
||||
[Cmdlet(VerbsCommon.Get, "OpenXmlComment")]
|
||||
public class GetOpenXmlCommentCmdlet : PowerToolsReadOnlyCmdlet
|
||||
{
|
||||
#region Cmdlet Overrides
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
foreach (var document in AllDocuments("Get-OpenXmlComment"))
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!(document is WmlDocument))
|
||||
throw new PowerToolsDocumentException("Not a wordprocessing document.");
|
||||
object comments = CommentAccessor.GetAllComments((WmlDocument)document, CommentFormat.Xml);
|
||||
WriteObject(comments, true);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, document));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
70
omegapro/PowerTools/Cmdlets/GetOpenXmlCustomXmlDataCmdlet.cs
Normal file
70
omegapro/PowerTools/Cmdlets/GetOpenXmlCustomXmlDataCmdlet.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
/***************************************************************************
|
||||
|
||||
Copyright (c) Microsoft Corporation 2011.
|
||||
|
||||
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
|
||||
|
||||
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace OpenXmlPowerTools.Commands
|
||||
{
|
||||
/// <summary>
|
||||
/// Get-OpenXmlCustomXmlData cmdlet
|
||||
/// </summary>
|
||||
[Cmdlet(VerbsCommon.Get, "OpenXmlCustomXmlData")]
|
||||
[OutputType("XDocument")]
|
||||
public class GetOpenXmlCustomXmlData : PowerToolsReadOnlyCmdlet
|
||||
{
|
||||
#region Parameters
|
||||
|
||||
string xmlPartName;
|
||||
|
||||
/// <summary>
|
||||
/// Part parameter
|
||||
/// </summary>
|
||||
[Parameter(
|
||||
Position = 1,
|
||||
Mandatory = true,
|
||||
HelpMessage = "Custom Xml part name")
|
||||
]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string Part
|
||||
{
|
||||
get
|
||||
{
|
||||
return xmlPartName;
|
||||
}
|
||||
set
|
||||
{
|
||||
xmlPartName = value;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cmdlet Overrides
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
foreach (var document in AllDocuments("Get-OpenXmlCustomXmlData"))
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!(document is WmlDocument))
|
||||
throw new PowerToolsDocumentException("Not a wordprocessing document.");
|
||||
WriteObject(CustomXmlAccessor.Find((WmlDocument)document, xmlPartName), true);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, document));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
@@ -0,0 +1,43 @@
|
||||
/***************************************************************************
|
||||
|
||||
Copyright (c) Microsoft Corporation 2011.
|
||||
|
||||
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
|
||||
|
||||
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace OpenXmlPowerTools.Commands
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets a summary of digital signatures present inside a document
|
||||
/// </summary>
|
||||
[Cmdlet(VerbsCommon.Get, "OpenXmlDigitalSignature")]
|
||||
[OutputType("string")]
|
||||
public class GetOpenXmlDigitalSignatureCmdlet : PowerToolsReadOnlyCmdlet
|
||||
{
|
||||
#region Cmdlet Overrides
|
||||
/// <summary>
|
||||
/// Entry point for Power Shell Cmdlets
|
||||
/// </summary>
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
foreach (var document in AllDocuments("Get-OpenXmlDigitalSignature"))
|
||||
{
|
||||
try
|
||||
{
|
||||
WriteObject(DigitalSignatureAccessor.GetList(document), true);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, document));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
41
omegapro/PowerTools/Cmdlets/GetOpenXmlDocumentCmdlet.cs
Normal file
41
omegapro/PowerTools/Cmdlets/GetOpenXmlDocumentCmdlet.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
/***************************************************************************
|
||||
|
||||
Copyright (c) Microsoft Corporation 2011.
|
||||
|
||||
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
|
||||
|
||||
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace OpenXmlPowerTools.Commands
|
||||
{
|
||||
/// <summary>
|
||||
/// Get-OpenXmlDocument Cmdlet
|
||||
/// </summary>
|
||||
[Cmdlet(VerbsCommon.Get, "OpenXmlDocument")]
|
||||
[OutputType("OpenXmlPowerToolsDocument")]
|
||||
public class GetOpenXmlDocumentCmdlet : PowerToolsReadOnlyCmdlet
|
||||
{
|
||||
#region Cmdlet Overrides
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
foreach (var document in AllDocuments("Get-OpenXmlCustomXmlData"))
|
||||
{
|
||||
try
|
||||
{
|
||||
WriteObject(new OpenXmlPowerToolsDocument(document), true);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, document));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
77
omegapro/PowerTools/Cmdlets/GetOpenXmlFooterCmdlet.cs
Normal file
77
omegapro/PowerTools/Cmdlets/GetOpenXmlFooterCmdlet.cs
Normal file
@@ -0,0 +1,77 @@
|
||||
/***************************************************************************
|
||||
|
||||
Copyright (c) Microsoft Corporation 2011.
|
||||
|
||||
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
|
||||
|
||||
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace OpenXmlPowerTools.Commands
|
||||
{
|
||||
/// <summary>
|
||||
/// Get the footer files of a word document
|
||||
/// </summary>
|
||||
[Cmdlet(VerbsCommon.Get, "OpenXmlFooter")]
|
||||
[OutputType("XDocument")]
|
||||
public class GetOpenXmlFooter : PowerToolsReadOnlyCmdlet
|
||||
{
|
||||
#region Parameters
|
||||
|
||||
private FooterType footerType;
|
||||
|
||||
/// <summary>
|
||||
/// FooterType parameter
|
||||
/// </summary>
|
||||
[Parameter(Position = 1,
|
||||
Mandatory = false,
|
||||
HelpMessage = "Specifies the type of the footer to extract.")
|
||||
]
|
||||
public FooterType FooterType
|
||||
{
|
||||
get
|
||||
{
|
||||
return footerType;
|
||||
}
|
||||
set
|
||||
{
|
||||
footerType = value;
|
||||
}
|
||||
}
|
||||
|
||||
[Parameter(Position = 2,
|
||||
Mandatory = false,
|
||||
HelpMessage = "Number of section")
|
||||
]
|
||||
public int Section = 1;
|
||||
|
||||
# endregion
|
||||
|
||||
#region Cmdlet Overrides
|
||||
|
||||
/// <summary>
|
||||
/// Entry point for PowerShell commandlets
|
||||
/// </summary>
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
foreach (var document in AllDocuments("Get-OpenXmlFooter"))
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!(document is WmlDocument))
|
||||
throw new PowerToolsDocumentException("Not a wordprocessing document.");
|
||||
WriteObject(FooterAccessor.GetFooter((WmlDocument)document, footerType, Section - 1), true);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, document));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
74
omegapro/PowerTools/Cmdlets/GetOpenXmlHeaderCmdlet.cs
Normal file
74
omegapro/PowerTools/Cmdlets/GetOpenXmlHeaderCmdlet.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
/***************************************************************************
|
||||
|
||||
Copyright (c) Microsoft Corporation 2011.
|
||||
|
||||
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
|
||||
|
||||
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace OpenXmlPowerTools.Commands
|
||||
{
|
||||
/// <summary>
|
||||
/// Get the header files of a word document
|
||||
/// </summary>
|
||||
[Cmdlet(VerbsCommon.Get, "OpenXmlHeader")]
|
||||
[OutputType("XDocument")]
|
||||
public class GetOpenXmlHeader : PowerToolsReadOnlyCmdlet
|
||||
{
|
||||
#region Parameters
|
||||
|
||||
private HeaderType headerType;
|
||||
|
||||
[Parameter(Position = 1,
|
||||
Mandatory = false,
|
||||
HelpMessage = "Specifies the type of the header to extract.")
|
||||
]
|
||||
public HeaderType HeaderType
|
||||
{
|
||||
get
|
||||
{
|
||||
return headerType;
|
||||
}
|
||||
set
|
||||
{
|
||||
headerType = value;
|
||||
}
|
||||
}
|
||||
|
||||
[Parameter(Position = 2,
|
||||
Mandatory = false,
|
||||
HelpMessage = "Number of section")
|
||||
]
|
||||
public int Section = 1;
|
||||
|
||||
# endregion
|
||||
|
||||
#region Cmdlet Overrides
|
||||
|
||||
/// <summary>
|
||||
/// Entry point for PowerShell cmdlets
|
||||
/// </summary>
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
foreach (var document in AllDocuments("Get-OpenXmlHeader"))
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!(document is WmlDocument))
|
||||
throw new PowerToolsDocumentException("Not a wordprocessing document.");
|
||||
WriteObject(HeaderAccessor.GetHeader((WmlDocument)document, headerType, Section - 1), true);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, document));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
43
omegapro/PowerTools/Cmdlets/GetOpenXmlStyleCmdlet.cs
Normal file
43
omegapro/PowerTools/Cmdlets/GetOpenXmlStyleCmdlet.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
/***************************************************************************
|
||||
|
||||
Copyright (c) Microsoft Corporation 2011.
|
||||
|
||||
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
|
||||
|
||||
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace OpenXmlPowerTools.Commands
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the style.xml of a word document
|
||||
/// </summary>
|
||||
[Cmdlet(VerbsCommon.Get, "OpenXmlStyle")]
|
||||
[OutputType("XDocument")]
|
||||
public class GetOpenXmlStyleCmdlet : PowerToolsReadOnlyCmdlet
|
||||
{
|
||||
#region Cmdlet Overrides
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
foreach (var document in AllDocuments("Get-OpenXmlStyle"))
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!(document is WmlDocument))
|
||||
throw new PowerToolsDocumentException("Not a wordprocessing document.");
|
||||
WriteObject(StyleAccessor.GetStylesDocument((WmlDocument)document), true);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, document));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
65
omegapro/PowerTools/Cmdlets/GetOpenXmlThemeCmdlet.cs
Normal file
65
omegapro/PowerTools/Cmdlets/GetOpenXmlThemeCmdlet.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
/***************************************************************************
|
||||
|
||||
Copyright (c) Microsoft Corporation 2011.
|
||||
|
||||
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
|
||||
|
||||
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace OpenXmlPowerTools.Commands
|
||||
{
|
||||
/// <summary>
|
||||
/// Get-OpenXmlTheme cmdlet
|
||||
/// </summary>
|
||||
[Cmdlet(VerbsCommon.Get, "OpenXmlTheme")]
|
||||
[OutputType("OpenXmlPowerToolsDocument")]
|
||||
public class GetOpenXmlThemeCmdlet : PowerToolsReadOnlyCmdlet
|
||||
{
|
||||
#region Parameters
|
||||
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
HelpMessage = "Use this switch to pipe out the extracted theme.")
|
||||
]
|
||||
public SwitchParameter PassThru;
|
||||
|
||||
[Parameter(Position = 1,
|
||||
Mandatory = false,
|
||||
ValueFromPipeline = false,
|
||||
HelpMessage = "Path of folder to store result documents")
|
||||
]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string OutputPath;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cmdlet Overrides
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
foreach (var document in AllDocuments("Get-OpenXmlTheme"))
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!(document is WmlDocument))
|
||||
throw new PowerToolsDocumentException("Not a wordprocessing document.");
|
||||
OpenXmlPowerToolsDocument theme = ThemeAccessor.GetTheme((WmlDocument)document);
|
||||
if (OutputPath != null)
|
||||
theme.SaveAs(System.IO.Path.Combine(SessionState.Path.CurrentLocation.Path, OutputPath));
|
||||
if (PassThru || OutputPath == null)
|
||||
WriteObject(theme, true);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, document));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
46
omegapro/PowerTools/Cmdlets/GetOpenXmlWatermarkCmdlet.cs
Normal file
46
omegapro/PowerTools/Cmdlets/GetOpenXmlWatermarkCmdlet.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
/***************************************************************************
|
||||
|
||||
Copyright (c) Microsoft Corporation 2011.
|
||||
|
||||
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
|
||||
|
||||
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace OpenXmlPowerTools.Commands
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the text of a watermark in a document.
|
||||
/// </summary>
|
||||
[Cmdlet(VerbsCommon.Get, "OpenXmlWatermark")]
|
||||
[OutputType("string")]
|
||||
public class GetOpenXmlWatermarkCmdlet : PowerToolsReadOnlyCmdlet
|
||||
{
|
||||
#region Cmdlet Overrides
|
||||
|
||||
/// <summary>
|
||||
/// Entry point for PowerShell cmdlets
|
||||
/// </summary>
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
foreach (var document in AllDocuments("Get-OpenXmlWatermark"))
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!(document is WmlDocument))
|
||||
throw new PowerToolsDocumentException("Not a wordprocessing document.");
|
||||
WriteObject(WatermarkAccessor.GetWatermarkText((WmlDocument)document), true);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, document));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
41
omegapro/PowerTools/Cmdlets/LockOpenXmlDocumentCmdlet.cs
Normal file
41
omegapro/PowerTools/Cmdlets/LockOpenXmlDocumentCmdlet.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
/***************************************************************************
|
||||
|
||||
Copyright (c) Microsoft Corporation 2011.
|
||||
|
||||
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
|
||||
|
||||
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Management.Automation;
|
||||
using DocumentFormat.OpenXml.Packaging;
|
||||
|
||||
namespace OpenXmlPowerTools.Commands
|
||||
{
|
||||
[Cmdlet(VerbsCommon.Lock, "OpenXmlDocument", SupportsShouldProcess = true)]
|
||||
[OutputType("OpenXmlPowerToolsDocument")]
|
||||
public class LockOpenXmlDocumentCmdlet : PowerToolsModifierCmdlet
|
||||
{
|
||||
#region Cmdlet Overrides
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
foreach (var document in AllDocuments("Lock-OpenXmlDocument"))
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!(document is WmlDocument))
|
||||
throw new PowerToolsDocumentException("Not a wordprocessing document.");
|
||||
OutputDocument(PowerToolsExtensions.Lock((WmlDocument)document));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, document));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
264
omegapro/PowerTools/Cmdlets/MergeOpenXmlDocumentCmdlet.cs
Normal file
264
omegapro/PowerTools/Cmdlets/MergeOpenXmlDocumentCmdlet.cs
Normal file
@@ -0,0 +1,264 @@
|
||||
/***************************************************************************
|
||||
|
||||
Copyright (c) Microsoft Corporation 2011.
|
||||
|
||||
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
|
||||
|
||||
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Management.Automation;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace OpenXmlPowerTools
|
||||
{
|
||||
/// <summary>
|
||||
/// Specify the entire source document
|
||||
/// </summary>
|
||||
public class DocumentSource
|
||||
{
|
||||
/// <summary>
|
||||
/// Full or relative path name for the file
|
||||
/// </summary>
|
||||
public string SourceFile;
|
||||
/// <summary>
|
||||
/// Starting paragraph number (1 is the first paragraph)
|
||||
/// </summary>
|
||||
public int Start;
|
||||
/// <summary>
|
||||
/// Number of paragraphs
|
||||
/// </summary>
|
||||
public int Count;
|
||||
/// <summary>
|
||||
/// True, if you want to keep the section at the end of the document
|
||||
/// </summary>
|
||||
public bool KeepSection;
|
||||
|
||||
/// <summary>
|
||||
/// Specify the entire source document
|
||||
/// </summary>
|
||||
public DocumentSource(string source)
|
||||
{
|
||||
SourceFile = source;
|
||||
Start = -1;
|
||||
Count = -1;
|
||||
}
|
||||
/// <summary>
|
||||
/// Specify from "start" to the end of the document
|
||||
/// </summary>
|
||||
public DocumentSource(string source, int start)
|
||||
{
|
||||
SourceFile = source;
|
||||
Start = start;
|
||||
Count = -1;
|
||||
}
|
||||
/// <summary>
|
||||
/// Specify from "start" and include "count" number of paragraphs
|
||||
/// </summary>
|
||||
public DocumentSource(string source, int start, int count)
|
||||
{
|
||||
SourceFile = source;
|
||||
Start = start;
|
||||
Count = count;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace OpenXmlPowerTools.Commands
|
||||
{
|
||||
/// <summary>
|
||||
/// Transform-OpenXmlToHtml cmdlet
|
||||
/// </summary>
|
||||
[Cmdlet(VerbsData.Merge, "OpenXmlDocument", SupportsShouldProcess = true)]
|
||||
public class MergeOpenXmlDocumentCmdlet : PowerToolsReadOnlyCmdlet
|
||||
{
|
||||
private DocumentSource[] m_Sources;
|
||||
private string m_OutputPath = "";
|
||||
private int m_Start = 1;
|
||||
private int m_Count = -1;
|
||||
private bool m_KeepSections = false;
|
||||
|
||||
private List<Source> buildSources = new List<Source>();
|
||||
|
||||
#region Parameters
|
||||
|
||||
/// <summary>
|
||||
/// OutputPath parameter
|
||||
/// </summary>
|
||||
[Parameter(
|
||||
Position = 1,
|
||||
Mandatory = true,
|
||||
HelpMessage = "Path of file for output document")
|
||||
]
|
||||
public string OutputPath
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_OutputPath;
|
||||
}
|
||||
set
|
||||
{
|
||||
m_OutputPath = System.IO.Path.Combine(SessionState.Path.CurrentLocation.Path, value);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Start parameter
|
||||
/// </summary>
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
HelpMessage = "Starting paragraph number to extract")
|
||||
]
|
||||
public int Start
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_Start;
|
||||
}
|
||||
set
|
||||
{
|
||||
m_Start = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Count parameter
|
||||
/// </summary>
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
HelpMessage = "Number of paragraphs to extract")
|
||||
]
|
||||
public int Count
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_Count;
|
||||
}
|
||||
set
|
||||
{
|
||||
m_Count = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sources parameter
|
||||
/// </summary>
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
HelpMessage = "Array of sources to extract")
|
||||
]
|
||||
public DocumentSource[] Sources
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_Sources;
|
||||
}
|
||||
set
|
||||
{
|
||||
m_Sources = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// KeepSections parameter
|
||||
/// </summary>
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
HelpMessage = "Keep a section break between each merged document.")
|
||||
]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public SwitchParameter KeepSections
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_KeepSections;
|
||||
}
|
||||
set
|
||||
{
|
||||
m_KeepSections = value;
|
||||
}
|
||||
}
|
||||
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
HelpMessage = "Use this switch to pipe out the processed documents.")
|
||||
]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public SwitchParameter PassThru;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cmdlet Overrides
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
foreach (var document in AllDocuments("Merge-OpenXmlDocumentCmdlet"))
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!(document is WmlDocument))
|
||||
throw new PowerToolsDocumentException("Not a wordprocessing document.");
|
||||
if (m_Count != -1)
|
||||
buildSources.Add(new Source((WmlDocument)document, m_Start - 1, m_Count, m_KeepSections));
|
||||
else
|
||||
buildSources.Add(new Source((WmlDocument)document, m_Start - 1, m_KeepSections));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, document));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected override void EndProcessing()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (m_Sources != null)
|
||||
{
|
||||
foreach (DocumentSource source in m_Sources)
|
||||
{
|
||||
Collection<PathInfo> fileList = SessionState.Path.GetResolvedPSPathFromPSPath(source.SourceFile);
|
||||
foreach (var file in fileList)
|
||||
{
|
||||
OpenXmlPowerToolsDocument document = OpenXmlPowerToolsDocument.FromFileName(file.Path);
|
||||
try
|
||||
{
|
||||
if (!(document is WmlDocument))
|
||||
throw new PowerToolsDocumentException("Not a wordprocessing document.");
|
||||
if (source.Count != -1)
|
||||
buildSources.Add(new Source((WmlDocument)document, source.Start - 1, source.Count, source.KeepSection));
|
||||
else if (source.Start != -1)
|
||||
buildSources.Add(new Source((WmlDocument)document, source.Start - 1, source.KeepSection));
|
||||
else
|
||||
buildSources.Add(new Source((WmlDocument)document, source.KeepSection));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, document));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
WmlDocument result = DocumentBuilder.BuildDocument(buildSources);
|
||||
if (m_OutputPath != null)
|
||||
{
|
||||
if (!File.Exists(m_OutputPath) || ShouldProcess(m_OutputPath, "Merge-OpenXmlDocumentCmdlet"))
|
||||
result.SaveAs(m_OutputPath);
|
||||
}
|
||||
if (PassThru)
|
||||
WriteObject(result, true);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, null));
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
@@ -0,0 +1,95 @@
|
||||
/***************************************************************************
|
||||
|
||||
Copyright (c) Microsoft Corporation 2011.
|
||||
|
||||
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
|
||||
|
||||
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Management.Automation;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace OpenXmlPowerTools.Commands
|
||||
{
|
||||
[Cmdlet(VerbsData.Merge, "OpenXmlDocumentComment", SupportsShouldProcess = true)]
|
||||
public class MergeOpenXmlDocumentCommentCmdlet : PowerToolsReadOnlyCmdlet
|
||||
{
|
||||
private string m_OutputPath = "";
|
||||
|
||||
private WmlDocument current;
|
||||
|
||||
#region Parameters
|
||||
|
||||
[Parameter(
|
||||
Position = 1,
|
||||
Mandatory = true,
|
||||
HelpMessage = "Path of file for output document")
|
||||
]
|
||||
public string OutputPath
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_OutputPath;
|
||||
}
|
||||
set
|
||||
{
|
||||
m_OutputPath = System.IO.Path.Combine(SessionState.Path.CurrentLocation.Path, value);
|
||||
}
|
||||
}
|
||||
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
HelpMessage = "Use this switch to pipe out the processed document.")
|
||||
]
|
||||
public SwitchParameter PassThru;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cmdlet Overrides
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
foreach (var document in AllDocuments("Merge-OpenXmlDocumentCommentCmdlet"))
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!(document is WmlDocument))
|
||||
throw new PowerToolsDocumentException("Not a wordprocessing document.");
|
||||
if (current == null)
|
||||
current = (WmlDocument)document;
|
||||
else
|
||||
current = CommentMerger.MergeComments(current, (WmlDocument)document);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, document));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected override void EndProcessing()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (m_OutputPath != null)
|
||||
{
|
||||
if (!File.Exists(m_OutputPath) || ShouldProcess(m_OutputPath, "Merge-OpenXmlDocumentCommentCmdlet"))
|
||||
current.SaveAs(m_OutputPath);
|
||||
}
|
||||
if (PassThru)
|
||||
WriteObject(current, true);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, null));
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
331
omegapro/PowerTools/Cmdlets/OpenXmlCmdlet.cs
Normal file
331
omegapro/PowerTools/Cmdlets/OpenXmlCmdlet.cs
Normal file
@@ -0,0 +1,331 @@
|
||||
/***************************************************************************
|
||||
|
||||
Copyright (c) Microsoft Corporation 2011.
|
||||
|
||||
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
|
||||
|
||||
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Management.Automation;
|
||||
using System.IO;
|
||||
using System.Xml;
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
namespace OpenXmlPowerTools.Commands
|
||||
{
|
||||
public class PowerToolsReadOnlyCmdlet : PSCmdlet
|
||||
{
|
||||
private OpenXmlPowerToolsDocument[] documents;
|
||||
internal string[] fileNameReferences;
|
||||
|
||||
#region Parameters
|
||||
|
||||
/// <summary>
|
||||
/// Specify the Document parameter
|
||||
/// </summary>
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
ValueFromPipeline = true,
|
||||
ValueFromPipelineByPropertyName = true,
|
||||
HelpMessage = "Specifies the documents to be processed.")]
|
||||
public OpenXmlPowerToolsDocument[] Document
|
||||
{
|
||||
get
|
||||
{
|
||||
return documents;
|
||||
}
|
||||
set
|
||||
{
|
||||
documents = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Specify the Path parameter
|
||||
/// </summary>
|
||||
[Parameter(Position = 0,
|
||||
Mandatory = false,
|
||||
HelpMessage = "Specifies the path to the documents to be processed")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string[] Path
|
||||
{
|
||||
set
|
||||
{
|
||||
fileNameReferences = value;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
internal IEnumerable<OpenXmlPowerToolsDocument> AllDocuments(string action)
|
||||
{
|
||||
if (fileNameReferences != null)
|
||||
{
|
||||
foreach (var path in fileNameReferences)
|
||||
{
|
||||
Collection<PathInfo> fileList;
|
||||
try
|
||||
{
|
||||
fileList = SessionState.Path.GetResolvedPSPathFromPSPath(path);
|
||||
}
|
||||
catch (ItemNotFoundException e)
|
||||
{
|
||||
WriteError(new ErrorRecord(e, "OpenXmlPowerToolsError", ErrorCategory.OpenError, path));
|
||||
continue;
|
||||
}
|
||||
foreach (var file in fileList)
|
||||
{
|
||||
OpenXmlPowerToolsDocument document;
|
||||
try
|
||||
{
|
||||
document = OpenXmlPowerToolsDocument.FromFileName(file.Path);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(new ErrorRecord(e, "OpenXmlPowerToolsError", ErrorCategory.OpenError, file));
|
||||
continue;
|
||||
}
|
||||
yield return document;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (Document != null)
|
||||
{
|
||||
foreach (OpenXmlPowerToolsDocument document in Document)
|
||||
{
|
||||
OpenXmlPowerToolsDocument specificDoc;
|
||||
try
|
||||
{
|
||||
specificDoc = OpenXmlPowerToolsDocument.FromDocument(document);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(new ErrorRecord(e, "OpenXmlPowerToolsError", ErrorCategory.InvalidType, document));
|
||||
continue;
|
||||
}
|
||||
yield return specificDoc;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class PowerToolsModifierCmdlet : PSCmdlet
|
||||
{
|
||||
private OpenXmlPowerToolsDocument[] documents;
|
||||
internal string[] fileNameReferences;
|
||||
protected bool passThru = false;
|
||||
private string outputFolder;
|
||||
|
||||
#region Parameters
|
||||
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
ValueFromPipeline = true,
|
||||
ValueFromPipelineByPropertyName = true,
|
||||
HelpMessage = "Specifies the documents to be processed.")]
|
||||
public OpenXmlPowerToolsDocument[] Document
|
||||
{
|
||||
get
|
||||
{
|
||||
return documents;
|
||||
}
|
||||
set
|
||||
{
|
||||
documents = value;
|
||||
}
|
||||
}
|
||||
|
||||
[Parameter(Position = 0,
|
||||
Mandatory = false,
|
||||
HelpMessage = "Specifies the path to the documents to be processed")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string[] Path
|
||||
{
|
||||
set
|
||||
{
|
||||
fileNameReferences = value;
|
||||
}
|
||||
}
|
||||
|
||||
[Parameter(Position = 1,
|
||||
Mandatory = false,
|
||||
ValueFromPipeline = false,
|
||||
HelpMessage = "Path of folder to store result documents")
|
||||
]
|
||||
public string OutputFolder
|
||||
{
|
||||
get
|
||||
{
|
||||
return outputFolder;
|
||||
}
|
||||
set
|
||||
{
|
||||
outputFolder = SessionState.Path.Combine(SessionState.Path.CurrentLocation.Path, value);
|
||||
}
|
||||
}
|
||||
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
HelpMessage = "Use this switch to pipe out the processed documents.")
|
||||
]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public SwitchParameter PassThru
|
||||
{
|
||||
get
|
||||
{
|
||||
return passThru;
|
||||
}
|
||||
set
|
||||
{
|
||||
passThru = value;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
internal IEnumerable<OpenXmlPowerToolsDocument> AllDocuments(string action)
|
||||
{
|
||||
if (fileNameReferences != null)
|
||||
{
|
||||
foreach (var path in fileNameReferences)
|
||||
{
|
||||
Collection<PathInfo> fileList;
|
||||
try
|
||||
{
|
||||
fileList = SessionState.Path.GetResolvedPSPathFromPSPath(path);
|
||||
}
|
||||
catch (ItemNotFoundException e)
|
||||
{
|
||||
WriteError(new ErrorRecord(e, "OpenXmlPowerToolsError", ErrorCategory.OpenError, path));
|
||||
continue;
|
||||
}
|
||||
foreach (var file in fileList)
|
||||
{
|
||||
string target = file.Path;
|
||||
if (OutputFolder != null)
|
||||
{
|
||||
FileInfo temp = new FileInfo(file.Path);
|
||||
target = OutputFolder + "\\" + temp.Name;
|
||||
}
|
||||
if (!File.Exists(target) || ShouldProcess(target, action))
|
||||
{
|
||||
OpenXmlPowerToolsDocument document;
|
||||
try
|
||||
{
|
||||
document = OpenXmlPowerToolsDocument.FromFileName(file.Path);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(new ErrorRecord(e, "OpenXmlPowerToolsError", ErrorCategory.OpenError, file));
|
||||
continue;
|
||||
}
|
||||
yield return document;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (Document != null)
|
||||
{
|
||||
foreach (OpenXmlPowerToolsDocument document in Document)
|
||||
{
|
||||
string target = document.FileName;
|
||||
if (OutputFolder != null)
|
||||
{
|
||||
FileInfo temp = new FileInfo(document.FileName);
|
||||
target = OutputFolder + "\\" + temp.Name;
|
||||
}
|
||||
if (!File.Exists(target) || ShouldProcess(target, action))
|
||||
{
|
||||
OpenXmlPowerToolsDocument specificDoc;
|
||||
try
|
||||
{
|
||||
specificDoc = OpenXmlPowerToolsDocument.FromDocument(document);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(new ErrorRecord(e, "OpenXmlPowerToolsError", ErrorCategory.InvalidType, document));
|
||||
continue;
|
||||
}
|
||||
yield return specificDoc;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Determines if and where to write the modified document
|
||||
internal void OutputDocument(OpenXmlPowerToolsDocument doc)
|
||||
{
|
||||
if (OutputFolder != null)
|
||||
{
|
||||
FileInfo file = new FileInfo(doc.FileName);
|
||||
string newName = OutputFolder + "\\" + file.Name;
|
||||
doc.SaveAs(newName);
|
||||
}
|
||||
else if (!PassThru)
|
||||
doc.Save();
|
||||
|
||||
if (PassThru)
|
||||
WriteObject(doc, true);
|
||||
}
|
||||
}
|
||||
|
||||
public class PowerToolsCreateCmdlet : PSCmdlet
|
||||
{
|
||||
protected bool passThru = false;
|
||||
|
||||
#region Parameters
|
||||
|
||||
/// <summary>
|
||||
/// PassThru parameter
|
||||
/// </summary>
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
HelpMessage = "Use this switch to pipe out the processed documents.")
|
||||
]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public SwitchParameter PassThru
|
||||
{
|
||||
get
|
||||
{
|
||||
return passThru;
|
||||
}
|
||||
set
|
||||
{
|
||||
passThru = value;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
// Determines if and where to write the modified document
|
||||
internal void OutputDocument(OpenXmlPowerToolsDocument doc)
|
||||
{
|
||||
if (PassThru)
|
||||
WriteObject(doc, true);
|
||||
else
|
||||
doc.Save();
|
||||
}
|
||||
}
|
||||
|
||||
public static class PowerToolsExceptionHandling
|
||||
{
|
||||
public static ErrorRecord GetExceptionErrorRecord(Exception e, OpenXmlPowerToolsDocument doc)
|
||||
{
|
||||
ErrorCategory cat = ErrorCategory.NotSpecified;
|
||||
if (e is ArgumentException)
|
||||
cat = ErrorCategory.InvalidArgument;
|
||||
else if (e is InvalidOperationException)
|
||||
cat = ErrorCategory.InvalidOperation;
|
||||
else if (e is PowerToolsDocumentException)
|
||||
cat = ErrorCategory.OpenError;
|
||||
else if (e is PowerToolsInvalidDataException || e is XmlException)
|
||||
cat = ErrorCategory.InvalidData;
|
||||
return new ErrorRecord(e, (cat == ErrorCategory.NotSpecified) ? "General" : "OpenXmlPowerToolsError", cat, doc);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
43
omegapro/PowerTools/Cmdlets/RemoveOpenXmlCommentCmdlet.cs
Normal file
43
omegapro/PowerTools/Cmdlets/RemoveOpenXmlCommentCmdlet.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
/***************************************************************************
|
||||
|
||||
Copyright (c) Microsoft Corporation 2011.
|
||||
|
||||
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
|
||||
|
||||
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace OpenXmlPowerTools.Commands
|
||||
{
|
||||
/// <summary>
|
||||
/// Remove-OpenXmlComment cmdlet
|
||||
/// </summary>
|
||||
[Cmdlet(VerbsCommon.Remove, "OpenXmlComment", SupportsShouldProcess = true)]
|
||||
[OutputType("OpenXmlPowerToolsDocument")]
|
||||
public class RemoveOpenXmlCommentCmdlet : PowerToolsModifierCmdlet
|
||||
{
|
||||
#region Cmdlet Overrides
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
foreach (var document in AllDocuments("Remove-OpenXmlComment"))
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!(document is WmlDocument))
|
||||
throw new PowerToolsDocumentException("Not a wordprocessing document.");
|
||||
OutputDocument(CommentAccessor.RemoveAll((WmlDocument)document));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, document));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
@@ -0,0 +1,44 @@
|
||||
/***************************************************************************
|
||||
|
||||
Copyright (c) Microsoft Corporation 2011.
|
||||
|
||||
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
|
||||
|
||||
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace OpenXmlPowerTools.Commands
|
||||
{
|
||||
/// <summary>
|
||||
/// Remove-OpenXmlDigitalSignature cmdlet
|
||||
/// </summary>
|
||||
[Cmdlet(VerbsCommon.Remove, "OpenXmlDigitalSignature", SupportsShouldProcess = true)]
|
||||
[OutputType("OpenXmlPowerToolsDocument")]
|
||||
public class RemoveOpenXmlDigitalSignaturesCmdlet : PowerToolsModifierCmdlet
|
||||
{
|
||||
#region Cmdlet Overrides
|
||||
|
||||
/// <summary>
|
||||
/// Entry point for PowerShell cmdlets
|
||||
/// </summary>
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
foreach (var document in AllDocuments("Remove-OpenXmlDigitalSignature"))
|
||||
{
|
||||
try
|
||||
{
|
||||
OutputDocument(DigitalSignatureAccessor.RemoveAll(document));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, document));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
156
omegapro/PowerTools/Cmdlets/RemoveOpenXmlMarkupCmdlet.cs
Normal file
156
omegapro/PowerTools/Cmdlets/RemoveOpenXmlMarkupCmdlet.cs
Normal file
@@ -0,0 +1,156 @@
|
||||
/***************************************************************************
|
||||
|
||||
Copyright (c) Microsoft Corporation 2011.
|
||||
|
||||
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
|
||||
|
||||
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace OpenXmlPowerTools.Commands
|
||||
{
|
||||
[Cmdlet(VerbsCommon.Remove, "OpenXmlMarkup", SupportsShouldProcess = true)]
|
||||
[OutputType("OpenXmlPowerToolsDocument")]
|
||||
public class RemoveOpenXmlMarkupCmdlet : PowerToolsModifierCmdlet
|
||||
{
|
||||
private SimplifyMarkupSettings settings = new SimplifyMarkupSettings();
|
||||
|
||||
#region Parameters
|
||||
[Parameter(Mandatory = false)]
|
||||
public SwitchParameter AcceptRevisions
|
||||
{
|
||||
get { return settings.AcceptRevisions; }
|
||||
set { settings.AcceptRevisions = value; }
|
||||
}
|
||||
|
||||
[Parameter(Mandatory = false)]
|
||||
public SwitchParameter RemoveContentControls
|
||||
{
|
||||
get { return settings.RemoveContentControls; }
|
||||
set { settings.RemoveContentControls = value; }
|
||||
}
|
||||
|
||||
[Parameter(Mandatory = false)]
|
||||
public SwitchParameter RemoveSmartTags
|
||||
{
|
||||
get { return settings.RemoveSmartTags; }
|
||||
set { settings.RemoveSmartTags = value; }
|
||||
}
|
||||
|
||||
[Parameter(Mandatory = false)]
|
||||
public SwitchParameter RemoveRsidInfo
|
||||
{
|
||||
get { return settings.RemoveRsidInfo; }
|
||||
set { settings.RemoveRsidInfo = value; }
|
||||
}
|
||||
|
||||
[Parameter(Mandatory = false)]
|
||||
public SwitchParameter RemoveComments
|
||||
{
|
||||
get { return settings.RemoveComments; }
|
||||
set { settings.RemoveComments = value; }
|
||||
}
|
||||
|
||||
[Parameter(Mandatory = false)]
|
||||
public SwitchParameter RemoveEndAndFootNotes
|
||||
{
|
||||
get { return settings.RemoveEndAndFootNotes; }
|
||||
set { settings.RemoveEndAndFootNotes = value; }
|
||||
}
|
||||
|
||||
[Parameter(Mandatory = false)]
|
||||
public SwitchParameter ReplaceTabsWithSpaces
|
||||
{
|
||||
get { return settings.ReplaceTabsWithSpaces; }
|
||||
set { settings.ReplaceTabsWithSpaces = value; }
|
||||
}
|
||||
|
||||
[Parameter(Mandatory = false)]
|
||||
public SwitchParameter RemoveFieldCodes
|
||||
{
|
||||
get { return settings.RemoveFieldCodes; }
|
||||
set { settings.RemoveFieldCodes = value; }
|
||||
}
|
||||
|
||||
[Parameter(Mandatory = false)]
|
||||
public SwitchParameter RemovePermissions
|
||||
{
|
||||
get { return settings.RemovePermissions; }
|
||||
set { settings.RemovePermissions = value; }
|
||||
}
|
||||
|
||||
[Parameter(Mandatory = false)]
|
||||
public SwitchParameter RemoveProof
|
||||
{
|
||||
get { return settings.RemoveProof; }
|
||||
set { settings.RemoveProof = value; }
|
||||
}
|
||||
|
||||
[Parameter(Mandatory = false)]
|
||||
public SwitchParameter RemoveSoftHyphens
|
||||
{
|
||||
get { return settings.RemoveSoftHyphens; }
|
||||
set { settings.RemoveSoftHyphens = value; }
|
||||
}
|
||||
|
||||
[Parameter(Mandatory = false)]
|
||||
public SwitchParameter RemoveLastRenderedPageBreak
|
||||
{
|
||||
get { return settings.RemoveLastRenderedPageBreak; }
|
||||
set { settings.RemoveLastRenderedPageBreak = value; }
|
||||
}
|
||||
|
||||
[Parameter(Mandatory = false)]
|
||||
public SwitchParameter RemoveBookmarks
|
||||
{
|
||||
get { return settings.RemoveBookmarks; }
|
||||
set { settings.RemoveBookmarks = value; }
|
||||
}
|
||||
|
||||
[Parameter(Mandatory = false)]
|
||||
public SwitchParameter RemoveWebHidden
|
||||
{
|
||||
get { return settings.RemoveWebHidden; }
|
||||
set { settings.RemoveWebHidden = value; }
|
||||
}
|
||||
|
||||
[Parameter(Mandatory = false)]
|
||||
public SwitchParameter RemoveGoBackBookmark
|
||||
{
|
||||
get { return settings.RemoveGoBackBookmark; }
|
||||
set { settings.RemoveGoBackBookmark = value; }
|
||||
}
|
||||
|
||||
[Parameter(Mandatory = false)]
|
||||
public SwitchParameter NormalizeXml
|
||||
{
|
||||
get { return settings.NormalizeXml; }
|
||||
set { settings.NormalizeXml = value; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cmdlet Overrides
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
foreach (var document in AllDocuments("Remove-OpenXmlMarkup"))
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!(document is WmlDocument))
|
||||
throw new PowerToolsDocumentException("Not a wordprocessing document.");
|
||||
OutputDocument(MarkupSimplifier.SimplifyMarkup((WmlDocument)document, settings));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, document));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
@@ -0,0 +1,46 @@
|
||||
/***************************************************************************
|
||||
|
||||
Copyright (c) Microsoft Corporation 2011.
|
||||
|
||||
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
|
||||
|
||||
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace OpenXmlPowerTools.Commands
|
||||
{
|
||||
/// <summary>
|
||||
/// Remove-OpenXmlPersonalInformation cmdlet
|
||||
/// </summary>
|
||||
[Cmdlet(VerbsCommon.Remove, "OpenXmlPersonalInformation", SupportsShouldProcess = true)]
|
||||
[OutputType("OpenXmlPowerToolsDocument")]
|
||||
public class RemoveOpenXmlPersonalInformationCmdlet : PowerToolsModifierCmdlet
|
||||
{
|
||||
#region Cmdlet Overrides
|
||||
|
||||
/// <summary>
|
||||
/// Entry point for PowerShell cmdlets
|
||||
/// </summary>
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
foreach (var document in AllDocuments("Remove-OpenXmlPersonalInformationCmdlet"))
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!(document is WmlDocument))
|
||||
throw new PowerToolsDocumentException("Not a wordprocessing document.");
|
||||
OutputDocument(PowerToolsExtensions.RemovePersonalInformation((WmlDocument)document));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, document));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
213
omegapro/PowerTools/Cmdlets/SelectOpenXmlStringCmdlet.cs
Normal file
213
omegapro/PowerTools/Cmdlets/SelectOpenXmlStringCmdlet.cs
Normal file
@@ -0,0 +1,213 @@
|
||||
/***************************************************************************
|
||||
|
||||
Copyright (c) Microsoft Corporation 2011.
|
||||
|
||||
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
|
||||
|
||||
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Collections.Generic;
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace OpenXmlPowerTools.Commands
|
||||
{
|
||||
/// <summary>
|
||||
/// Match information for Select-OpenXmlString
|
||||
/// </summary>
|
||||
public class MatchInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// Full path of file that matched
|
||||
/// </summary>
|
||||
public string Path { get; set; }
|
||||
/// <summary>
|
||||
/// Filename of file that matched
|
||||
/// </summary>
|
||||
public string Filename
|
||||
{
|
||||
get
|
||||
{
|
||||
if (Path == null)
|
||||
return null;
|
||||
FileInfo info = new FileInfo(Path);
|
||||
return info.Name;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Element number of element whose style or content matched
|
||||
/// </summary>
|
||||
public int ElementNumber { get; set; }
|
||||
/// <summary>
|
||||
/// Full contents, without formatting, of matched element
|
||||
/// </summary>
|
||||
public string Content { get; set; }
|
||||
/// <summary>
|
||||
/// The first style that matched the element
|
||||
/// </summary>
|
||||
public string Style { get; set; }
|
||||
/// <summary>
|
||||
/// The first pattern that matched the content of the element
|
||||
/// </summary>
|
||||
public string Pattern { get; set; }
|
||||
/// <summary>
|
||||
/// Indicates if case was ignored on the pattern match
|
||||
/// </summary>
|
||||
public bool IgnoreCase { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Simple constructor
|
||||
/// </summary>
|
||||
public MatchInfo()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the footer files of a word document
|
||||
/// </summary>
|
||||
[Cmdlet(VerbsCommon.Select, "OpenXmlString")]
|
||||
[OutputType("MatchInfo")]
|
||||
public class SelectOpenXmlString : PowerToolsReadOnlyCmdlet
|
||||
{
|
||||
private string[] m_StyleSearch;
|
||||
private string[] m_TextSearch;
|
||||
private bool m_SimpleMatch = false;
|
||||
private bool m_CaseSensitive = false;
|
||||
private bool m_List = false;
|
||||
|
||||
/// <summary>
|
||||
/// A list of text strings to match.
|
||||
/// </summary>
|
||||
[Parameter(Position = 1,
|
||||
Mandatory = false,
|
||||
HelpMessage = "Specifies the a list of text strings to match.")
|
||||
]
|
||||
public string[] Pattern
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_TextSearch;
|
||||
}
|
||||
set
|
||||
{
|
||||
m_TextSearch = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A list of style names to match.
|
||||
/// </summary>
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
HelpMessage = "Specifies the a list of style names to match.")
|
||||
]
|
||||
public string[] Style
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_StyleSearch;
|
||||
}
|
||||
set
|
||||
{
|
||||
m_StyleSearch = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// simpleMatch parameter
|
||||
/// </summary>
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
HelpMessage = "Specifies that a simple match, rather than a regular expression match, should be used.")
|
||||
]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public SwitchParameter simpleMatch
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_SimpleMatch;
|
||||
}
|
||||
set
|
||||
{
|
||||
m_SimpleMatch = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// caseSensitive parameter
|
||||
/// </summary>
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
HelpMessage = "Makes matches case sensitive. By default, matching is not case sensitive.")
|
||||
]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public SwitchParameter caseSensitive
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_CaseSensitive;
|
||||
}
|
||||
set
|
||||
{
|
||||
m_CaseSensitive = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// list parameter
|
||||
/// </summary>
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
HelpMessage = "Specifies that only one match should result for each input file. The returned MatchInfo objects only include information about that first match.")
|
||||
]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public SwitchParameter list
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_List;
|
||||
}
|
||||
set
|
||||
{
|
||||
m_List = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Entry point for PowerShell commandlets
|
||||
/// </summary>
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
List<MatchInfo> results = new List<MatchInfo>();
|
||||
foreach (var document in AllDocuments("Select-OpenXmlString"))
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!(document is WmlDocument))
|
||||
throw new PowerToolsDocumentException("Not a wordprocessing document.");
|
||||
MatchInfo[] result = PowerToolsExtensions.SearchInDocument((WmlDocument)document, m_StyleSearch, m_TextSearch, !m_SimpleMatch, !m_CaseSensitive);
|
||||
if (!m_List)
|
||||
foreach (MatchInfo item in result)
|
||||
{
|
||||
item.Path = document.FileName;
|
||||
results.Add(item);
|
||||
}
|
||||
else if (result.GetUpperBound(0) >= 0)
|
||||
{
|
||||
result[0].Path = document.FileName;
|
||||
results.Add(result[0]);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, document));
|
||||
}
|
||||
WriteObject(results, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
159
omegapro/PowerTools/Cmdlets/SetOpenXmlBackgroundCmdlet.cs
Normal file
159
omegapro/PowerTools/Cmdlets/SetOpenXmlBackgroundCmdlet.cs
Normal file
@@ -0,0 +1,159 @@
|
||||
/***************************************************************************
|
||||
|
||||
Copyright (c) Microsoft Corporation 2011.
|
||||
|
||||
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
|
||||
|
||||
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace OpenXmlPowerTools.Commands
|
||||
{
|
||||
/// <summary>
|
||||
/// Set-OpenXmlBackground cmdlet
|
||||
/// </summary>
|
||||
[Cmdlet(VerbsCommon.Set, "OpenXmlBackground", SupportsShouldProcess = true)]
|
||||
[OutputType("OpenXmlPowerToolsDocument")]
|
||||
public class SetOpenXmlBackgroundCmdlet : PowerToolsModifierCmdlet
|
||||
{
|
||||
#region Parameters
|
||||
|
||||
private string backgroundColor;
|
||||
private string backgroundImagePath;
|
||||
|
||||
/// <summary>
|
||||
/// ImagePath parameter
|
||||
/// </summary>
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
HelpMessage = "Path of image to set as document background")
|
||||
]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string ImagePath
|
||||
{
|
||||
get
|
||||
{
|
||||
return backgroundImagePath;
|
||||
}
|
||||
set
|
||||
{
|
||||
backgroundImagePath = SessionState.Path.GetResolvedPSPathFromPSPath(value).First().Path;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ColorName parameter
|
||||
/// </summary>
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
HelpMessage = "Color code (hexadecimal) to set as the document background")
|
||||
]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string ColorName
|
||||
{
|
||||
get
|
||||
{
|
||||
return backgroundColor;
|
||||
}
|
||||
set
|
||||
{
|
||||
backgroundColor = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Color parameter
|
||||
/// </summary>
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
ValueFromPipeline = true,
|
||||
ValueFromPipelineByPropertyName = true,
|
||||
HelpMessage = "Color object to set as the document background")
|
||||
]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public System.Drawing.Color Color
|
||||
{
|
||||
get
|
||||
{
|
||||
return System.Drawing.Color.FromArgb(backgroundColor == null ? 0 : Convert.ToInt32(backgroundColor,16));
|
||||
}
|
||||
set
|
||||
{
|
||||
backgroundColor = String.Format("{0:X2}{1:X2}{2:X2}", value.R, value.G, value.B);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ImageFile parameter
|
||||
/// </summary>
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
ValueFromPipeline = true,
|
||||
HelpMessage = "Image object to use as document background")
|
||||
]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public System.IO.FileInfo ImageFile
|
||||
{
|
||||
get
|
||||
{
|
||||
return new System.IO.FileInfo(backgroundImagePath == null ? "." : backgroundImagePath);
|
||||
}
|
||||
set
|
||||
{
|
||||
backgroundImagePath = value.FullName;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cmdlet Overrides
|
||||
|
||||
/// <summary>
|
||||
/// Entry point for PowerShell cmdlets
|
||||
/// </summary>
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
// If an image was piped in, the background color string will not be valid.
|
||||
if (backgroundImagePath != null)
|
||||
backgroundColor = null;
|
||||
// At least one of the backgroundColor or backgroundImage parameters must be set it
|
||||
if (backgroundColor == null && backgroundImagePath == null)
|
||||
{
|
||||
WriteError(new ErrorRecord(new ArgumentException("Requires at least one of the three parameters: Color, Image or ImagePath."), "OpenXmlPowerTools", ErrorCategory.InvalidArgument, null));
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var document in AllDocuments("Set-OpenXmlBackground"))
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!(document is WmlDocument))
|
||||
throw new PowerToolsDocumentException("Not a wordprocessing document.");
|
||||
if (backgroundImagePath != null)
|
||||
{
|
||||
// Open as image to verify that it is valid
|
||||
System.Drawing.Image.FromFile(backgroundImagePath);
|
||||
OutputDocument(BackgroundAccessor.SetImage((WmlDocument)document, backgroundImagePath));
|
||||
}
|
||||
else
|
||||
{
|
||||
// Validate color value
|
||||
System.Drawing.Color.FromArgb(Convert.ToInt32(backgroundColor, 16));
|
||||
OutputDocument(BackgroundAccessor.SetColor((WmlDocument)document, backgroundColor));
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, document));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
88
omegapro/PowerTools/Cmdlets/SetOpenXmlContentFormatCmdlet.cs
Normal file
88
omegapro/PowerTools/Cmdlets/SetOpenXmlContentFormatCmdlet.cs
Normal file
@@ -0,0 +1,88 @@
|
||||
/***************************************************************************
|
||||
|
||||
Copyright (c) Microsoft Corporation 2011.
|
||||
|
||||
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
|
||||
|
||||
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace OpenXmlPowerTools.Commands
|
||||
{
|
||||
/// <summary>
|
||||
/// Set-OpenXmlContentFormat cmdlet
|
||||
/// </summary>
|
||||
[Cmdlet(VerbsCommon.Set, "OpenXmlContentFormat", SupportsShouldProcess = true)]
|
||||
[OutputType("OpenXmlPowerToolsDocument")]
|
||||
public class SetOpenXmlContentFormat : PowerToolsModifierCmdlet
|
||||
{
|
||||
#region Parameters
|
||||
|
||||
private string xpathInsertionPoint;
|
||||
private string xmlContent;
|
||||
|
||||
/// <summary>
|
||||
/// InsertionPoint parameter
|
||||
/// </summary>
|
||||
[Parameter(Position = 2,
|
||||
Mandatory = true,
|
||||
HelpMessage = "Insertion point location")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string InsertionPoint
|
||||
{
|
||||
get
|
||||
{
|
||||
return xpathInsertionPoint;
|
||||
}
|
||||
set
|
||||
{
|
||||
xpathInsertionPoint = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Content parameter
|
||||
/// </summary>
|
||||
[Parameter(Position = 3,
|
||||
Mandatory = true,
|
||||
HelpMessage = "Xml to insert")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string Content
|
||||
{
|
||||
get
|
||||
{
|
||||
return xmlContent;
|
||||
}
|
||||
set
|
||||
{
|
||||
xmlContent = value;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cmdlet Overrides
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
foreach (var document in AllDocuments("Set-OpenXmlContentFormat"))
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!(document is WmlDocument))
|
||||
throw new PowerToolsDocumentException("Not a wordprocessing document.");
|
||||
OutputDocument(ContentFormatAccessor.Insert((WmlDocument)document, xpathInsertionPoint, xmlContent));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, document));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
141
omegapro/PowerTools/Cmdlets/SetOpenXmlContentStyleCmdlet.cs
Normal file
141
omegapro/PowerTools/Cmdlets/SetOpenXmlContentStyleCmdlet.cs
Normal file
@@ -0,0 +1,141 @@
|
||||
/***************************************************************************
|
||||
|
||||
Copyright (c) Microsoft Corporation 2011.
|
||||
|
||||
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
|
||||
|
||||
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Xml.Linq;
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace OpenXmlPowerTools.Commands
|
||||
{
|
||||
/// <summary>
|
||||
/// Set-OpenXmlContentStyle cmdlet
|
||||
/// </summary>
|
||||
[Cmdlet(VerbsCommon.Set, "OpenXmlContentStyle", SupportsShouldProcess = true)]
|
||||
[OutputType("OpenXmlPowerToolsDocument")]
|
||||
public class SetOpenXmlContentStyle : PowerToolsModifierCmdlet
|
||||
{
|
||||
#region Parameters
|
||||
|
||||
private string xpathInsertionPoint;
|
||||
private string stylesSourcePath;
|
||||
private XDocument stylesSource;
|
||||
private string styleName;
|
||||
|
||||
/// <summary>
|
||||
/// InsertionPoint parameter
|
||||
/// </summary>
|
||||
[Parameter(Position = 2,
|
||||
Mandatory = true,
|
||||
HelpMessage = "Insertion point location")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string InsertionPoint
|
||||
{
|
||||
get
|
||||
{
|
||||
return xpathInsertionPoint;
|
||||
}
|
||||
set
|
||||
{
|
||||
xpathInsertionPoint = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// StyleName parameter
|
||||
/// </summary>
|
||||
[Parameter(Position = 3,
|
||||
Mandatory = true,
|
||||
HelpMessage = "Style name")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string StyleName
|
||||
{
|
||||
get
|
||||
{
|
||||
return styleName;
|
||||
}
|
||||
set
|
||||
{
|
||||
styleName = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// StylesSourcePath parameter
|
||||
/// </summary>
|
||||
[Parameter(Position = 4,
|
||||
Mandatory = false,
|
||||
HelpMessage = "Style file path")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string StylesSourcePath
|
||||
{
|
||||
get
|
||||
{
|
||||
return stylesSourcePath;
|
||||
}
|
||||
set
|
||||
{
|
||||
stylesSourcePath = SessionState.Path.GetResolvedPSPathFromPSPath(value).First().Path;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// StylesSource parameter
|
||||
/// </summary>
|
||||
[Parameter(Position = 5,
|
||||
Mandatory = false,
|
||||
ValueFromPipeline = true,
|
||||
HelpMessage = "Styles from XDocument")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public XDocument StylesSource
|
||||
{
|
||||
get
|
||||
{
|
||||
return stylesSource;
|
||||
}
|
||||
set
|
||||
{
|
||||
stylesSource = value;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cmdlet Overrides
|
||||
|
||||
/// <summary>
|
||||
/// Entry point for PowerShell cmdlets
|
||||
/// </summary>
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
if (stylesSourcePath != null)
|
||||
stylesSource = XDocument.Load(stylesSourcePath);
|
||||
if (stylesSource == null)
|
||||
{
|
||||
WriteError(new ErrorRecord(new Exception("No styles source was specified."), "BadParameters", ErrorCategory.InvalidArgument, null));
|
||||
return;
|
||||
}
|
||||
foreach (var document in AllDocuments("Set-OpenXmlContentStyle"))
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!(document is WmlDocument))
|
||||
throw new PowerToolsDocumentException("Not a wordprocessing document.");
|
||||
OutputDocument(StyleAccessor.Insert((WmlDocument)document, xpathInsertionPoint, styleName, stylesSource));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, document));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
126
omegapro/PowerTools/Cmdlets/SetOpenXmlCustomXmlDataCmdlet.cs
Normal file
126
omegapro/PowerTools/Cmdlets/SetOpenXmlCustomXmlDataCmdlet.cs
Normal file
@@ -0,0 +1,126 @@
|
||||
/***************************************************************************
|
||||
|
||||
Copyright (c) Microsoft Corporation 2011.
|
||||
|
||||
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
|
||||
|
||||
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Xml.Linq;
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace OpenXmlPowerTools.Commands
|
||||
{
|
||||
/// <summary>
|
||||
/// Set-OpenXmlCustomXmlData cmdlet
|
||||
/// </summary>
|
||||
[Cmdlet(VerbsCommon.Set, "OpenXmlCustomXmlData", SupportsShouldProcess = true)]
|
||||
[OutputType("OpenXmlPowerToolsDocument")]
|
||||
public class SetOpenXmlCustomXmlDataCmdlet : PowerToolsModifierCmdlet
|
||||
{
|
||||
#region Parameters
|
||||
|
||||
private string xmlPath;
|
||||
private string partName;
|
||||
private XDocument customData;
|
||||
|
||||
/// <summary>
|
||||
/// Specify the CustomXmlPart parameter
|
||||
/// </summary>
|
||||
[Parameter(Position = 2,
|
||||
Mandatory = false,
|
||||
HelpMessage = "Custom Xml part path")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string PartPath
|
||||
{
|
||||
get
|
||||
{
|
||||
return xmlPath;
|
||||
}
|
||||
set
|
||||
{
|
||||
var xmlPartFileNames = SessionState.Path.GetResolvedPSPathFromPSPath(value);
|
||||
if (xmlPartFileNames.Count() == 1)
|
||||
{
|
||||
xmlPath = xmlPartFileNames.First().Path;
|
||||
}
|
||||
else if (xmlPartFileNames.Count() > 1)
|
||||
{
|
||||
throw new Exception("Too many xmlParts specified.");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Specify the CustomXmlPart parameter
|
||||
/// </summary>
|
||||
[Parameter(Position = 3,
|
||||
Mandatory = false,
|
||||
HelpMessage = "Name for the new custom part")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string PartName
|
||||
{
|
||||
get
|
||||
{
|
||||
return partName;
|
||||
}
|
||||
set
|
||||
{
|
||||
partName = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Specify the CustomXmlPart parameter
|
||||
/// </summary>
|
||||
[Parameter(Position = 4,
|
||||
Mandatory = false,
|
||||
ValueFromPipeline = true,
|
||||
HelpMessage = "Custom Xml document")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public XDocument Part
|
||||
{
|
||||
get
|
||||
{
|
||||
return customData;
|
||||
}
|
||||
set
|
||||
{
|
||||
customData = value;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cmdlet Overrides
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
if (xmlPath != null)
|
||||
{
|
||||
customData = XDocument.Load(xmlPath);
|
||||
if (partName == null)
|
||||
partName = System.IO.Path.GetFileName(xmlPath);
|
||||
}
|
||||
foreach (var document in AllDocuments("Set-OpenXmlCustomXmlData"))
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!(document is WmlDocument))
|
||||
throw new PowerToolsDocumentException("Not a wordprocessing document.");
|
||||
OutputDocument(CustomXmlAccessor.SetDocument((WmlDocument)document, customData, partName));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, document));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
124
omegapro/PowerTools/Cmdlets/SetOpenXmlFooterCmdlet.cs
Normal file
124
omegapro/PowerTools/Cmdlets/SetOpenXmlFooterCmdlet.cs
Normal file
@@ -0,0 +1,124 @@
|
||||
/***************************************************************************
|
||||
|
||||
Copyright (c) Microsoft Corporation 2011.
|
||||
|
||||
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
|
||||
|
||||
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Xml.Linq;
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace OpenXmlPowerTools.Commands
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Set the footer files of a word document
|
||||
/// </summary>
|
||||
[Cmdlet(VerbsCommon.Set, "OpenXmlFooter", SupportsShouldProcess = true)]
|
||||
[OutputType("OpenXmlPowerToolsDocument")]
|
||||
public class SetOpenXmlFooterCmdlet : PowerToolsModifierCmdlet
|
||||
{
|
||||
#region Parameters
|
||||
|
||||
private string footerPath;
|
||||
private XDocument footer;
|
||||
private FooterType kind;
|
||||
|
||||
[Parameter(Position = 2,
|
||||
Mandatory = false,
|
||||
HelpMessage = "The path of the footer to add in the document")
|
||||
]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string FooterPath
|
||||
{
|
||||
get
|
||||
{
|
||||
|
||||
return footerPath;
|
||||
}
|
||||
set
|
||||
{
|
||||
footerPath = SessionState.Path.GetResolvedPSPathFromPSPath(value).First().Path;
|
||||
}
|
||||
}
|
||||
|
||||
[Parameter(Position = 3,
|
||||
Mandatory = false,
|
||||
HelpMessage = "Specify the kind of the footer to extract")
|
||||
]
|
||||
public FooterType FooterType
|
||||
{
|
||||
get
|
||||
{
|
||||
return kind;
|
||||
}
|
||||
set
|
||||
{
|
||||
kind = value;
|
||||
}
|
||||
}
|
||||
|
||||
[Parameter(Position = 4,
|
||||
Mandatory = false,
|
||||
HelpMessage = "Number of section to modify")
|
||||
]
|
||||
public int Section = 1;
|
||||
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
ValueFromPipeline = true,
|
||||
ValueFromPipelineByPropertyName = true,
|
||||
HelpMessage = "XDocument of the footer to add")
|
||||
]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public XDocument Footer
|
||||
{
|
||||
get
|
||||
{
|
||||
return footer;
|
||||
}
|
||||
set
|
||||
{
|
||||
footer = value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cmdlet Overrides
|
||||
|
||||
/// <summary>
|
||||
/// Entry point for PowerShell cmdlets
|
||||
/// </summary>
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
if (footerPath != null)
|
||||
footer = XDocument.Load(footerPath);
|
||||
if (footer == null)
|
||||
{
|
||||
WriteError(new ErrorRecord(new ArgumentException("No footer was specified."), "OpenXmlPowerTools", ErrorCategory.InvalidArgument, null));
|
||||
return;
|
||||
}
|
||||
foreach (var document in AllDocuments("Set-OpenXmlFooter"))
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!(document is WmlDocument))
|
||||
throw new PowerToolsDocumentException("Not a wordprocessing document.");
|
||||
OutputDocument(FooterAccessor.SetFooter((WmlDocument)document, footer, kind, Section - 1));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, document));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
122
omegapro/PowerTools/Cmdlets/SetOpenXmlHeaderCmdlet.cs
Normal file
122
omegapro/PowerTools/Cmdlets/SetOpenXmlHeaderCmdlet.cs
Normal file
@@ -0,0 +1,122 @@
|
||||
/***************************************************************************
|
||||
|
||||
Copyright (c) Microsoft Corporation 2011.
|
||||
|
||||
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
|
||||
|
||||
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Xml.Linq;
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace OpenXmlPowerTools.Commands
|
||||
{
|
||||
/// <summary>
|
||||
/// Set the header files of a word document
|
||||
/// </summary>
|
||||
[Cmdlet(VerbsCommon.Set, "OpenXmlHeader", SupportsShouldProcess = true)]
|
||||
[OutputType("OpenXmlPowerToolsDocument")]
|
||||
public class SetOpenXmlHeaderCmdlet : PowerToolsModifierCmdlet
|
||||
{
|
||||
|
||||
#region Parameters
|
||||
|
||||
private string headerPath;
|
||||
private XDocument header;
|
||||
private HeaderType kind;
|
||||
|
||||
[Parameter(Position = 2,
|
||||
Mandatory = false,
|
||||
HelpMessage = "The path of the header to add in the document")
|
||||
]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string HeaderPath
|
||||
{
|
||||
get
|
||||
{
|
||||
return headerPath;
|
||||
}
|
||||
set
|
||||
{
|
||||
headerPath = SessionState.Path.GetResolvedPSPathFromPSPath(value).First().Path;
|
||||
}
|
||||
}
|
||||
|
||||
[Parameter(Position = 3,
|
||||
Mandatory = false,
|
||||
HelpMessage = "Specify the kind of the header to extract")
|
||||
]
|
||||
public HeaderType HeaderType
|
||||
{
|
||||
get
|
||||
{
|
||||
return kind;
|
||||
}
|
||||
set
|
||||
{
|
||||
kind = value;
|
||||
}
|
||||
}
|
||||
|
||||
[Parameter(Position = 4,
|
||||
Mandatory = false,
|
||||
HelpMessage = "Number of section to modify")
|
||||
]
|
||||
public int Section = 1;
|
||||
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
ValueFromPipeline = true,
|
||||
ValueFromPipelineByPropertyName = true,
|
||||
HelpMessage = "XDocument of the header to add")
|
||||
]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public XDocument Header
|
||||
{
|
||||
get
|
||||
{
|
||||
return header;
|
||||
}
|
||||
set
|
||||
{
|
||||
header = value;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cmdlet Overrides
|
||||
|
||||
/// <summary>
|
||||
/// Entry point for PowerShell cmdlets
|
||||
/// </summary>
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
if (headerPath != null)
|
||||
header = XDocument.Load(headerPath);
|
||||
if (header == null)
|
||||
{
|
||||
WriteError(new ErrorRecord(new ArgumentException("No footer was specified."), "OpenXmlPowerTools", ErrorCategory.InvalidArgument, null));
|
||||
return;
|
||||
}
|
||||
foreach (var document in AllDocuments("Set-OpenXmlHeader"))
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!(document is WmlDocument))
|
||||
throw new PowerToolsDocumentException("Not a wordprocessing document.");
|
||||
OutputDocument(HeaderAccessor.SetHeader((WmlDocument)document, header, kind, Section - 1));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, document));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
@@ -0,0 +1,203 @@
|
||||
/***************************************************************************
|
||||
|
||||
Copyright (c) Microsoft Corporation 2011.
|
||||
|
||||
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
|
||||
|
||||
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace OpenXmlPowerTools.Commands
|
||||
{
|
||||
/// <summary>
|
||||
/// Cmdlet class for setting a cell style in a SpreadsheetML document
|
||||
/// </summary>
|
||||
[Cmdlet(VerbsCommon.Set, "OpenXmlSpreadSheetCellStyle", SupportsShouldProcess = true)]
|
||||
[OutputType("OpenXmlPowerToolsDocument")]
|
||||
public class SetOpenXmlSpreadSheetCellStyleCmdlet : PowerToolsModifierCmdlet
|
||||
{
|
||||
#region Parameters
|
||||
private int fromRow;
|
||||
private int toRow;
|
||||
private short fromColumn;
|
||||
private short toColumn;
|
||||
private string cellStyle;
|
||||
private string worksheetName;
|
||||
|
||||
/// <summary>
|
||||
/// Initial row for start setting the value
|
||||
/// </summary>
|
||||
[Parameter(Position = 2,
|
||||
Mandatory = true,
|
||||
HelpMessage = "Initial row for start setting the cell style")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public int FromRow
|
||||
{
|
||||
get
|
||||
{
|
||||
return fromRow;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (value > 0)
|
||||
{
|
||||
fromRow = value;
|
||||
}
|
||||
else
|
||||
{
|
||||
new System.Management.Automation.ParameterBindingException("Initial row must be greater than zero");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initial row for start setting the value
|
||||
/// </summary>
|
||||
[Parameter(Position = 3,
|
||||
Mandatory = true,
|
||||
HelpMessage = "Final row for start setting the cell style")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public int ToRow
|
||||
{
|
||||
get
|
||||
{
|
||||
return toRow;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (value > 0)
|
||||
{
|
||||
toRow = value;
|
||||
}
|
||||
else
|
||||
{
|
||||
new System.Management.Automation.ParameterBindingException("Final row must be greater than zero");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initial column for start setting the value
|
||||
/// </summary>
|
||||
[Parameter(Position = 4,
|
||||
Mandatory = true,
|
||||
HelpMessage = "Initial column for start setting the cell style")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public short FromColumn
|
||||
{
|
||||
get
|
||||
{
|
||||
return fromColumn;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (value > 0)
|
||||
{
|
||||
fromColumn = value;
|
||||
}
|
||||
else
|
||||
{
|
||||
new System.Management.Automation.ParameterBindingException("Initial column must be greater than zero");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Final column for start setting the value
|
||||
/// </summary>
|
||||
[Parameter(Position = 5,
|
||||
Mandatory = true,
|
||||
HelpMessage = "Final column for start setting the cell style")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public short ToColumn
|
||||
{
|
||||
get
|
||||
{
|
||||
return toColumn;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (value > 0)
|
||||
{
|
||||
toColumn = value;
|
||||
}
|
||||
else
|
||||
{
|
||||
new System.Management.Automation.ParameterBindingException("Final column must be greater than zero");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Cell Style Name
|
||||
/// </summary>
|
||||
[Parameter(Position = 6,
|
||||
Mandatory = true,
|
||||
HelpMessage = "Cell Style Name")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string CellStyle
|
||||
{
|
||||
get
|
||||
{
|
||||
return cellStyle;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (value.Trim().Length > 0)
|
||||
{
|
||||
cellStyle = value;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ParameterBindingException("CellStyle cannot be empty");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Worksheet name to set the cell value
|
||||
/// </summary>
|
||||
[Parameter(Position = 7,
|
||||
Mandatory = true,
|
||||
HelpMessage = "Worksheet name to set the cell value")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string WorksheetName
|
||||
{
|
||||
get
|
||||
{
|
||||
return worksheetName;
|
||||
}
|
||||
set
|
||||
{
|
||||
worksheetName = value;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cmdlet Overrides
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
foreach (var document in AllDocuments("Set-OpenXmlSpreadSheetCellStyle"))
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!(document is SmlDocument))
|
||||
throw new PowerToolsDocumentException("Not a spreadsheet document.");
|
||||
OutputDocument(WorksheetAccessor.SetCellStyle((SmlDocument)document, worksheetName, fromColumn, toColumn, fromRow, toRow, cellStyle));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, document));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
@@ -0,0 +1,195 @@
|
||||
/***************************************************************************
|
||||
|
||||
Copyright (c) Microsoft Corporation 2011.
|
||||
|
||||
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
|
||||
|
||||
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace OpenXmlPowerTools.Commands
|
||||
{
|
||||
/// <summary>
|
||||
/// Cmdlet for setting a value for a specific cell in a worksheet in a SpreadsheetML document
|
||||
/// </summary>
|
||||
[Cmdlet(VerbsCommon.Set, "OpenXmlSpreadSheetCellValue", SupportsShouldProcess = true)]
|
||||
[OutputType("OpenXmlPowerToolsDocument")]
|
||||
public class SetOpenXmlSpreadSheetCellValueCmdlet : PowerToolsModifierCmdlet
|
||||
{
|
||||
#region Parameters
|
||||
private int fromRow;
|
||||
private int toRow;
|
||||
private short fromColumn;
|
||||
private short toColumn;
|
||||
private string _value;
|
||||
private string worksheetName;
|
||||
|
||||
/// <summary>
|
||||
/// Initial row for start setting the value
|
||||
/// </summary>
|
||||
[Parameter(Position = 2,
|
||||
Mandatory = true,
|
||||
HelpMessage = "Initial row for start setting the value")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public int FromRow
|
||||
{
|
||||
get
|
||||
{
|
||||
return fromRow;
|
||||
}
|
||||
set
|
||||
{
|
||||
if(value >0)
|
||||
{
|
||||
fromRow = value;
|
||||
}
|
||||
else
|
||||
{
|
||||
new System.Management.Automation.ParameterBindingException("Initial row must be greater than zero");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initial row for start setting the value
|
||||
/// </summary>
|
||||
[Parameter(Position = 3,
|
||||
Mandatory = true,
|
||||
HelpMessage = "Final row for start setting the value")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public int ToRow
|
||||
{
|
||||
get
|
||||
{
|
||||
return toRow;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (value > 0)
|
||||
{
|
||||
toRow = value;
|
||||
}
|
||||
else
|
||||
{
|
||||
new System.Management.Automation.ParameterBindingException("Final row must be greater than zero");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initial column for start setting the value
|
||||
/// </summary>
|
||||
[Parameter(Position = 4,
|
||||
Mandatory = true,
|
||||
HelpMessage = "Initial column for start setting the value")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public short FromColumn
|
||||
{
|
||||
get
|
||||
{
|
||||
return fromColumn;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (value > 0)
|
||||
{
|
||||
fromColumn = value;
|
||||
}
|
||||
else
|
||||
{
|
||||
new System.Management.Automation.ParameterBindingException("Initial column must be greater than zero");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Final column for start setting the value
|
||||
/// </summary>
|
||||
[Parameter(Position = 5,
|
||||
Mandatory = true,
|
||||
HelpMessage = "Final column for start setting the value")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public short ToColumn
|
||||
{
|
||||
get
|
||||
{
|
||||
return toColumn;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (value > 0)
|
||||
{
|
||||
toColumn = value;
|
||||
}
|
||||
else
|
||||
{
|
||||
new System.Management.Automation.ParameterBindingException("Final column must be greater than zero");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Cell value
|
||||
/// </summary>
|
||||
[Parameter(Position = 6,
|
||||
Mandatory = true,
|
||||
HelpMessage = "Cell value")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string Value
|
||||
{
|
||||
get
|
||||
{
|
||||
return _value;
|
||||
}
|
||||
set
|
||||
{
|
||||
_value = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Worksheet name to set the cell value
|
||||
/// </summary>
|
||||
[Parameter(Position = 7,
|
||||
Mandatory = true,
|
||||
HelpMessage = "Worksheet name to set the cell value")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string WorksheetName
|
||||
{
|
||||
get
|
||||
{
|
||||
return worksheetName;
|
||||
}
|
||||
set
|
||||
{
|
||||
worksheetName = value;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cmdlet Overrides
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
foreach (var document in AllDocuments("Set-OpenXmlSpreadSheetCellValue"))
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!(document is SmlDocument))
|
||||
throw new PowerToolsDocumentException("Not a spreadsheet document.");
|
||||
OutputDocument(WorksheetAccessor.SetCellValue((SmlDocument)document, worksheetName, fromRow, toRow, fromColumn, toColumn, _value));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, document));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
@@ -0,0 +1,142 @@
|
||||
/***************************************************************************
|
||||
|
||||
Copyright (c) Microsoft Corporation 2011.
|
||||
|
||||
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
|
||||
|
||||
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace OpenXmlPowerTools.Commands
|
||||
{
|
||||
/// <summary>
|
||||
/// Class for setting the width for a range of columns in a worksheet in a SpreadsheetML document
|
||||
/// </summary>
|
||||
[Cmdlet(VerbsCommon.Set, "OpenXmlSpreadSheetColumnWidth", SupportsShouldProcess = true)]
|
||||
[OutputType("OpenXmlPowerToolsDocument")]
|
||||
public class SetOpenXmlSpreadSheetColumnWidthCmdlet : PowerToolsModifierCmdlet
|
||||
{
|
||||
#region Parameters
|
||||
|
||||
private short fromColumn;
|
||||
private short toColumn;
|
||||
private int width;
|
||||
private string worksheetName;
|
||||
|
||||
/// <summary>
|
||||
/// Row index for setting value
|
||||
/// </summary>
|
||||
[Parameter(Position = 2,
|
||||
Mandatory = true,
|
||||
HelpMessage = "Initial Column for Setting Width")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public short FromColumn
|
||||
{
|
||||
get
|
||||
{
|
||||
return fromColumn;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (value > 0)
|
||||
{
|
||||
fromColumn = value;
|
||||
}
|
||||
else
|
||||
{
|
||||
new System.Management.Automation.ParameterBindingException("Initial column must be greater than zero");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Column index for setting value
|
||||
/// </summary>
|
||||
[Parameter(Position = 3,
|
||||
Mandatory = true,
|
||||
HelpMessage = "Final Column for setting width")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public short ToColumn
|
||||
{
|
||||
get
|
||||
{
|
||||
return toColumn;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (value > 0)
|
||||
{
|
||||
toColumn = value;
|
||||
}
|
||||
else
|
||||
{
|
||||
new System.Management.Automation.ParameterBindingException("Final Column must be greater than zero");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Cell Style Name
|
||||
/// </summary>
|
||||
[Parameter(Position = 4,
|
||||
Mandatory = true,
|
||||
HelpMessage = "Column Width")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public int Width
|
||||
{
|
||||
get
|
||||
{
|
||||
return width;
|
||||
}
|
||||
set
|
||||
{
|
||||
width = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Worksheet name to set the cell value
|
||||
/// </summary>
|
||||
[Parameter(Position = 5,
|
||||
Mandatory = true,
|
||||
HelpMessage = "Worksheet name to set the cell value")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string WorksheetName
|
||||
{
|
||||
get
|
||||
{
|
||||
return worksheetName;
|
||||
}
|
||||
set
|
||||
{
|
||||
worksheetName = value;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cmdlet Overrides
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
foreach (var document in AllDocuments("Set-OpenXmlSpreadSheetColumnWidth"))
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!(document is SmlDocument))
|
||||
throw new PowerToolsDocumentException("Not a spreadsheet document.");
|
||||
OutputDocument(WorksheetAccessor.SetColumnWidth((SmlDocument)document, worksheetName, fromColumn, toColumn, width));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, document));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
62
omegapro/PowerTools/Cmdlets/SetOpenXmlStringCmdlet.cs
Normal file
62
omegapro/PowerTools/Cmdlets/SetOpenXmlStringCmdlet.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
/***************************************************************************
|
||||
|
||||
Copyright (c) Microsoft Corporation 2011.
|
||||
|
||||
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
|
||||
|
||||
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace OpenXmlPowerTools.Commands
|
||||
{
|
||||
[Cmdlet(VerbsCommon.Set, "OpenXmlString", SupportsShouldProcess = true)]
|
||||
[OutputType("OpenXmlPowerToolsDocument")]
|
||||
public class SetOpenXmlStringCmdlet : PowerToolsModifierCmdlet
|
||||
{
|
||||
#region Parameters
|
||||
|
||||
[Parameter(Position = 2,
|
||||
Mandatory = true,
|
||||
HelpMessage = "Text to match in document")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string Pattern;
|
||||
|
||||
[Parameter(Position = 3,
|
||||
Mandatory = true,
|
||||
HelpMessage = "Text to replace in document")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string Replace;
|
||||
|
||||
[Parameter()]
|
||||
public SwitchParameter CaseSensitive;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cmdlet Overrides
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
foreach (var document in AllDocuments("Set-OpenXmlString"))
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!(document is WmlDocument) && !(document is PmlDocument))
|
||||
throw new PowerToolsDocumentException("Not a supported document.");
|
||||
if (document is WmlDocument)
|
||||
OutputDocument(TextReplacer.SearchAndReplace((WmlDocument)document, Pattern, Replace, CaseSensitive));
|
||||
if (document is PmlDocument)
|
||||
OutputDocument(TextReplacer.SearchAndReplace((PmlDocument)document, Pattern, Replace, CaseSensitive));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, document));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
122
omegapro/PowerTools/Cmdlets/SetOpenXmlStyleCmdlet.cs
Normal file
122
omegapro/PowerTools/Cmdlets/SetOpenXmlStyleCmdlet.cs
Normal file
@@ -0,0 +1,122 @@
|
||||
/***************************************************************************
|
||||
|
||||
Copyright (c) Microsoft Corporation 2011.
|
||||
|
||||
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
|
||||
|
||||
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Xml.Linq;
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace OpenXmlPowerTools.Commands
|
||||
{
|
||||
/// <summary>
|
||||
/// Set the style.xml of a word document
|
||||
/// </summary>
|
||||
[Cmdlet(VerbsCommon.Set, "OpenXmlStyle", SupportsShouldProcess = true)]
|
||||
[OutputType("OpenXmlPowerToolsDocument")]
|
||||
public class SetOpenXmlStyleCmdlet : PowerToolsModifierCmdlet
|
||||
{
|
||||
#region Parameters
|
||||
|
||||
private string styleFile;
|
||||
private XDocument stylesDocument;
|
||||
|
||||
/// <summary>
|
||||
/// StylePath parameter
|
||||
/// </summary>
|
||||
[Parameter(Position = 2,
|
||||
Mandatory = false,
|
||||
HelpMessage = "New Style.xml path")
|
||||
]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string StylePath
|
||||
{
|
||||
get
|
||||
{
|
||||
return styleFile;
|
||||
}
|
||||
set
|
||||
{
|
||||
styleFile = SessionState.Path.GetResolvedPSPathFromPSPath(value).First().Path;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Style parameter
|
||||
/// </summary>
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
ValueFromPipeline = true,
|
||||
HelpMessage = "New styles document")
|
||||
]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public XDocument Style
|
||||
{
|
||||
get
|
||||
{
|
||||
return stylesDocument;
|
||||
}
|
||||
set
|
||||
{
|
||||
stylesDocument = value;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cmdlet Overrides
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (styleFile != null)
|
||||
stylesDocument = XDocument.Load(styleFile);
|
||||
if (stylesDocument == null)
|
||||
{
|
||||
WriteError(new ErrorRecord(new ArgumentException("No styles document was specified."), "OpenXmlPowerTools", ErrorCategory.InvalidArgument, null));
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var document in AllDocuments("Set-OpenXmlStyle"))
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!(document is WmlDocument))
|
||||
throw new PowerToolsDocumentException("Not a wordprocessing document.");
|
||||
OutputDocument(StyleAccessor.SetStylePart((WmlDocument)document, stylesDocument));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, document));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (ItemNotFoundException e)
|
||||
{
|
||||
WriteError(new ErrorRecord(e, "FileNotFound", ErrorCategory.OpenError, null));
|
||||
}
|
||||
catch (InvalidOperationException e)
|
||||
{
|
||||
WriteError(new ErrorRecord(e, "InvalidOperation", ErrorCategory.InvalidOperation, null));
|
||||
}
|
||||
catch (ArgumentException e)
|
||||
{
|
||||
WriteError(new ErrorRecord(e, "InvalidArgument", ErrorCategory.InvalidArgument, null));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(new ErrorRecord(e, "General", ErrorCategory.NotSpecified, null));
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
99
omegapro/PowerTools/Cmdlets/SetOpenXmlThemeCmdlet.cs
Normal file
99
omegapro/PowerTools/Cmdlets/SetOpenXmlThemeCmdlet.cs
Normal file
@@ -0,0 +1,99 @@
|
||||
/***************************************************************************
|
||||
|
||||
Copyright (c) Microsoft Corporation 2011.
|
||||
|
||||
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
|
||||
|
||||
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.IO.Packaging;
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace OpenXmlPowerTools.Commands
|
||||
{
|
||||
/// <summary>
|
||||
/// Set-OpenXmlTheme cmdlet
|
||||
/// </summary>
|
||||
[Cmdlet(VerbsCommon.Set, "OpenXmlTheme", SupportsShouldProcess = true)]
|
||||
[OutputType("OpenXmlPowerToolsDocument")]
|
||||
public class SetOpenXmlThemeCmdlet : PowerToolsModifierCmdlet
|
||||
{
|
||||
#region Parameters
|
||||
|
||||
private string themePath;
|
||||
private OpenXmlPowerToolsDocument themePackage;
|
||||
|
||||
/// <summary>
|
||||
/// ThemePath parameter
|
||||
/// </summary>
|
||||
[Parameter(Position = 2,
|
||||
Mandatory = false,
|
||||
HelpMessage = "Theme path")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string ThemePath
|
||||
{
|
||||
set
|
||||
{
|
||||
themePath = SessionState.Path.GetResolvedPSPathFromPSPath(value).First().Path;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ThemePackage parameter
|
||||
/// </summary>
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
ValueFromPipeline = true,
|
||||
HelpMessage = "Theme path")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public OpenXmlPowerToolsDocument ThemePackage
|
||||
{
|
||||
get
|
||||
{
|
||||
return themePackage;
|
||||
}
|
||||
set
|
||||
{
|
||||
themePackage = value;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cmdlet Overrides
|
||||
|
||||
/// <summary>
|
||||
/// Entry point for PowerShell cmdlets
|
||||
/// </summary>
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
if (themePath != null)
|
||||
themePackage = OpenXmlPowerToolsDocument.FromFileName(themePath);
|
||||
if (themePackage == null)
|
||||
{
|
||||
WriteError(new ErrorRecord(new ArgumentException("No theme was specified."), "OpenXmlPowerTools", ErrorCategory.InvalidArgument, null));
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var document in AllDocuments("Set-OpenXmlTheme"))
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!(document is WmlDocument))
|
||||
throw new PowerToolsDocumentException("Not a wordprocessing document.");
|
||||
OutputDocument(ThemeAccessor.SetTheme((WmlDocument)document, themePackage));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, document));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
89
omegapro/PowerTools/Cmdlets/SetOpenXmlWatermarkCmdlet.cs
Normal file
89
omegapro/PowerTools/Cmdlets/SetOpenXmlWatermarkCmdlet.cs
Normal file
@@ -0,0 +1,89 @@
|
||||
/***************************************************************************
|
||||
|
||||
Copyright (c) Microsoft Corporation 2011.
|
||||
|
||||
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
|
||||
|
||||
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace OpenXmlPowerTools.Commands
|
||||
{
|
||||
/// <summary>
|
||||
/// Set-OpenXmlWatermark cmdlet
|
||||
/// </summary>
|
||||
[Cmdlet(VerbsCommon.Set, "OpenXmlWatermark", SupportsShouldProcess = true)]
|
||||
[OutputType("OpenXmlPowerToolsDocument")]
|
||||
public class SetOpenXmlWatermarkCmdlet : PowerToolsModifierCmdlet
|
||||
{
|
||||
#region Parameters
|
||||
|
||||
private bool diagonalOrientation;
|
||||
private string watermarkText;
|
||||
|
||||
/// <summary>
|
||||
/// WatermarkText parameter
|
||||
/// </summary>
|
||||
[Parameter(Position = 2,
|
||||
Mandatory = true,
|
||||
ValueFromPipeline = true,
|
||||
HelpMessage = "Text to show in the watermark")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string WatermarkText
|
||||
{
|
||||
get
|
||||
{
|
||||
return watermarkText;
|
||||
}
|
||||
set
|
||||
{
|
||||
watermarkText = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// DiagonalOrientation parameter
|
||||
/// </summary>
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
HelpMessage = "Specifies diagonal orientation for watermark")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public SwitchParameter DiagonalOrientation
|
||||
{
|
||||
get
|
||||
{
|
||||
return diagonalOrientation;
|
||||
}
|
||||
set
|
||||
{
|
||||
diagonalOrientation = value;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cmdlet Overrides
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
foreach (var document in AllDocuments("Set-OpenXmlWatermark"))
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!(document is WmlDocument))
|
||||
throw new PowerToolsDocumentException("Not a wordprocessing document.");
|
||||
OutputDocument(WatermarkAccessor.InsertWatermark((WmlDocument)document, watermarkText, diagonalOrientation));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, document));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
64
omegapro/PowerTools/Cmdlets/SplitOpenXmlDocumentCmdlet.cs
Normal file
64
omegapro/PowerTools/Cmdlets/SplitOpenXmlDocumentCmdlet.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
/***************************************************************************
|
||||
|
||||
Copyright (c) Microsoft Corporation 2011.
|
||||
|
||||
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
|
||||
|
||||
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using System.Management.Automation;
|
||||
using System.Xml.Linq;
|
||||
using DocumentFormat.OpenXml.Packaging;
|
||||
|
||||
namespace OpenXmlPowerTools.Commands
|
||||
{
|
||||
/// <summary>
|
||||
/// Split-OpenXmlDocument cmdlet
|
||||
/// </summary>
|
||||
[Cmdlet(VerbsCommon.Split, "OpenXmlDocument", SupportsShouldProcess = true)]
|
||||
[OutputType("OpenXmlPowerToolsDocument")]
|
||||
public class SplitOpenXmlDocumentCmdlet : PowerToolsModifierCmdlet
|
||||
{
|
||||
private int NextNumber;
|
||||
|
||||
#region Parameters
|
||||
|
||||
[Parameter(Position = 2,
|
||||
Mandatory = true,
|
||||
HelpMessage = "Defines the prefix name of resulting documents")
|
||||
]
|
||||
public string Prefix;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cmdlet Overrides
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
NextNumber = 1;
|
||||
foreach (var document in AllDocuments("Split-OpenXmlDocument"))
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!(document is WmlDocument))
|
||||
throw new PowerToolsDocumentException("Not a wordprocessing document.");
|
||||
foreach (WmlDocument item in DocumentBuilder.SplitOnSections((WmlDocument)document))
|
||||
{
|
||||
item.FileName = String.Format("{0}{1}.docx", Prefix, NextNumber++);
|
||||
OutputDocument(item);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, document));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user