Exemplo n.º 1
0
 /// <summary>
 /// Class constructor
 /// </summary>
 /// <param name="document">Document to perform operations on</param>
 /// <param name="fullName">Full path name of document</param>
 public PTWordprocessingDocument(OpenXmlSDK.WordprocessingDocument document, string fullName)
     : base()
 {
     Document         = document;
     FullName         = fullName;
     InnerContent     = new WordprocessingDocumentManager(this);
     Comments         = new CommentAccessor(this);
     Changes          = new ChangeAccessor(this);
     Headers          = new HeaderAccessor(this);
     Footer           = new FooterAccessor(this);
     Setting          = new SettingAccessor(this);
     CustomXml        = new CustomXmlAccessor(this);
     Background       = new BackgroundAccessor(this);
     Style            = new StyleAccessor(this);
     Format           = new ContentFormatAccessor(this);
     Picture          = new PictureAccessor(this);
     Watermark        = new WatermarkAccesor(this);
     Theme            = new ThemeAccessor(this);
     TOC              = new TOCAccessor(this);
     TOF              = new TOFAccessor(this);
     TOA              = new TOAAccessor(this);
     Index            = new IndexAccessor(this);
     CoreProperties   = new CorePropertiesAccesor(this);
     CustomProperties = new CustomPropertiesAccesor(this);
 }
Exemplo n.º 2
0
 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));
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// Class constructor
        /// </summary>
        /// <param name="filePath">Path of document to load</param>
        /// <param name="createNew">Whether create a new document or load contents from existing one</param>
        public PTWordprocessingDocument(string filePath, bool createNew)
            : base()
        {
            try
            {
                if (createNew)
                {
                    Document = OpenXmlSDK.WordprocessingDocument.Create(filePath, DocumentFormat.OpenXml.WordprocessingDocumentType.Document);
                }
                else
                {
                    Document = OpenXmlSDK.WordprocessingDocument.Open(filePath, true);
                }
                FullName = filePath;

                InnerContent = new WordprocessingDocumentManager(this);
                Comments     = new CommentAccessor(this);
                Changes      = new ChangeAccessor(this);
                Headers      = new HeaderAccessor(this);
                Footer       = new FooterAccessor(this);
                Setting      = new SettingAccessor(this);
                CustomXml    = new CustomXmlAccessor(this);
                Background   = new BackgroundAccessor(this);
                Style        = new StyleAccessor(this);
                Format       = new ContentFormatAccessor(this);
                Picture      = new PictureAccessor(this);
                Watermark    = new WatermarkAccesor(this);
                Theme        = new ThemeAccessor(this);
                TOC          = new TOCAccessor(this);
                TOF          = new TOFAccessor(this);
                TOA          = new TOAAccessor(this);
                Index        = new IndexAccessor(this);
            }
            catch (OpenXmlSDK.OpenXmlPackageException ope)
            {
                throw new Exception("Bad formed OpenXml package", ope);
            }
            catch (FileFormatException ffe) {
                throw new Exception("File contains corrupted data", ffe);
            }
            catch (Exception)
            {
                throw;
            }
        }
Exemplo n.º 4
0
        /// <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));
                }
            }
        }
Exemplo n.º 5
0
 /// <summary>
 /// WordprocessingDocument (Constructor)
 /// </summary>
 public PTWordprocessingDocument()
     : base()
 {
     memoryStream = new MemoryStream();
     Document     = OpenXmlSDK.WordprocessingDocument.Create(memoryStream, DocumentFormat.OpenXml.WordprocessingDocumentType.Document);
     InnerContent = new WordprocessingDocumentManager(this);
     Comments     = new CommentAccessor(this);
     Changes      = new ChangeAccessor(this);
     Headers      = new HeaderAccessor(this);
     Footer       = new FooterAccessor(this);
     Setting      = new SettingAccessor(this);
     CustomXml    = new CustomXmlAccessor(this);
     Background   = new BackgroundAccessor(this);
     Style        = new StyleAccessor(this);
     Format       = new ContentFormatAccessor(this);
     Picture      = new PictureAccessor(this);
     Watermark    = new WatermarkAccesor(this);
     Theme        = new ThemeAccessor(this);
     TOC          = new TOCAccessor(this);
     TOF          = new TOFAccessor(this);
 }