Beispiel #1
0
 public void CanProtectNewDocument()
 {
     using (var document = new DocxDocument())
     {
         document.Protect();
     }
 }
Beispiel #2
0
 public void GetCustomPropertyFromDocument()
 {
     using (var docxDocument = new DocxDocument(Resources.DocumentWithAttribute))
     {
         Assert.AreEqual("Working", docxDocument.GetCustomProperty("customAttributes"));
     }
 }
Beispiel #3
0
 public void CanUnprotectNewDocument()
 {
     using (var document = new DocxDocument())
     {
         Assert.DoesNotThrow(document.Unprotect);
     }
 }
Beispiel #4
0
 public void GetCustomPropertyFromDocumentReturnNullIfItDoesNotExists()
 {
     using (var docxDocument = new DocxDocument(Resources.DocumentWithoutAttributes))
     {
         Assert.Null(docxDocument.GetCustomProperty("customAttributes"));
     }
 }
Beispiel #5
0
 public void CanCorrectlyCountAllFieldsInDocument()
 {
     using (var document = new DocxDocument(Resources.WithSdtElements))
     {
         Assert.AreEqual(2, document.Fields.Count());
     }
 }
Beispiel #6
0
        /// <summary>
        /// Create builder for template
        /// </summary>
        /// <param name="docxDocument">Template document</param>
        /// <param name="tagVisibilityOptions">List of tags for show and hide</param>
        public DocxDocumentBuilder(DocxDocument docxDocument, TagVisibilityOptions tagVisibilityOptions = null)
            : base(docxDocument.GetWordDocument())
        {
            this.docxDocument         = docxDocument;
            this.tagVisibilityOptions = tagVisibilityOptions;

            MergeVanishedRuns();
        }
Beispiel #7
0
 public void DocumentsWithEqualInnerXmlMustBeEqual()
 {
     using (var document = new DocxDocument(Resources.DocumentWithoutParagraph))
         using (var otherDocument = new DocxDocument(Resources.DocumentWithoutParagraph))
         {
             Assert.IsTrue(new DocxDocumentEqualityComparer().Equals(document, otherDocument));
         }
 }
Beispiel #8
0
 public void CanFindTagIfSdtElementHasNoSdtAlias()
 {
     using (var document = new DocxDocument(Resources.SdtElementWithoutSdtAlias))
     {
         document.Fields.Contains(tag: "FirstTag");
         document.Fields.Contains(tag: "SecondTag");
     }
 }
        /// <summary>
        /// Create builder for template
        /// </summary>
        /// <param name="docxDocument">Template document</param>
        /// <param name="tagVisibilityOptions">List of tags for show and hide</param>
        public DocxDocumentBuilder(DocxDocument docxDocument, TagVisibilityOptions tagVisibilityOptions = null)
            : base(docxDocument.GetWordDocument())
        {
            this.docxDocument = docxDocument;
            this.tagVisibilityOptions = tagVisibilityOptions;

            MergeVanishedRuns();
        }
Beispiel #10
0
        public void PlaceholderGettingWhichNotExistsReturnsEmpty()
        {
            using (var document = new DocxDocument(Resources.WithMainContentTag))
            {
                var placeholders = DocumentPlaceholder.Get(document.GetWordDocument(), "NON_EXISTING");

                Assert.IsEmpty(placeholders);
            }
        }
Beispiel #11
0
        public void TagGettingWhichNotExistsReturnsEmpty()
        {
            using (var document = new DocxDocument(Resources.WithMainContentTag))
            {
                var tags = DocumentTag.Get(document.GetWordDocument(), "NON_EXISTING");

                Assert.IsEmpty(tags);
            }
        }
        public void ValidationForInvalidDocumentShouldFail()
        {
            using (var document = new DocxDocument(Resources.InvalidDocument))
            {
                var builder = CreateBuilder(document);

                Assert.False(builder.Validate());
            }
        }
        public void ValidationForValidDocumentShouldBeCorrect()
        {
            using (var document = new DocxDocument(Resources.WithMainContentTag))
            {
                var builder = CreateBuilder(document);

                Assert.True(builder.Validate());
            }
        }
Beispiel #14
0
        public void TagGettingWhichNotExistsReturnsEmpty()
        {
            using (var document = new DocxDocument(Resources.WithMainContentTag))
            {
                var tags = DocumentTag.Get(document.GetWordDocument(), "NON_EXISTING");

                Assert.IsEmpty(tags);
            }
        }
        public void OpeningDocumentViaBuilderNormalizesItsPlaceholderRuns()
        {
            using (var expected = new DocxDocument(Resources.WithPlaceholdersNormalized))
                using (var document = new DocxDocument(Resources.WithPlaceholdersDenormalized))
                {
                    CreateBuilder(document);

                    CompareDocuments(expected, document);
                }
        }
Beispiel #16
0
        public void ProtectAddWrightProtectionToFile()
        {
            using (var @protected = new DocxDocument(Resources.Protected))
                using (var @unprotected = new DocxDocument(Resources.Unprotected))
                {
                    @unprotected.Protect();

                    Assert.AreEqual(@protected.GetWordDocument().MainDocumentPart.DocumentSettingsPart.Settings.OuterXml, @unprotected.GetWordDocument().MainDocumentPart.DocumentSettingsPart.Settings.OuterXml);
                }
        }
Beispiel #17
0
        public void ManyTagsGettingFromDocumentCorrect()
        {
            using (var document = new DocxDocument(Resources.WithManyTags))
            {
                var tags = DocumentTag.Get(document.GetWordDocument(), "SUB");

                Assert.NotNull(tags);
                Assert.AreEqual(3, tags.Count());
            }
        }
Beispiel #18
0
        public void NotVisibilityContentIfDocumentWithContentTypeTag()
        {
            using (var initialDocument = new DocxDocument(Resources.DocumentWithContentTypeTag))
                using (var expectedDocument = new DocxDocument(Resources.DocumentWithContentTypeTag))
                {
                    initialDocument.SetTagVisibility("Tag", true);

                    Assert.IsTrue(new DocxDocumentEqualityComparer().Equals(expectedDocument, initialDocument));
                }
        }
Beispiel #19
0
        public void HidePlaceholderIfHideContentInTag()
        {
            using (var initialDocument = new DocxDocument(Resources.DocumentWithHideContentInPlaceholderInTag))
                using (var expectedDocument = new DocxDocument(Resources.DocumentWithVisibilityContentInPlaceholderInTag))
                {
                    initialDocument.SetTagVisibility("Tag", true);

                    Assert.IsTrue(new DocxDocumentEqualityComparer().Equals(expectedDocument, initialDocument));
                }
        }
Beispiel #20
0
        public void ManyPlaceholdersGettingFromDocumentCorrect()
        {
            using (var document = new DocxDocument(Resources.WithManyPlaceholders))
            {
                var placeholders = DocumentPlaceholder.Get(document.GetWordDocument(), "INNER");

                Assert.NotNull(placeholders);
                Assert.AreEqual(3, placeholders.Count());
            }
        }
Beispiel #21
0
        public void DocumentsWithDifferentInnerXmlMustBeNotEqual()
        {
            using (var document = new DocxDocument(Resources.DocumentWithoutParagraph))
                using (var otherDocument = new DocxDocument(Resources.DocumentWithParagraph))
                {
                    var comparer = new DocxDocumentEqualityComparer();

                    Assert.Throws <AssertionException>(() => comparer.Equals(document, otherDocument));
                }
        }
Beispiel #22
0
        public void ManyTagsGettingFromDocumentCorrect()
        {
            using (var document = new DocxDocument(Resources.WithManyTags))
            {
                var tags = DocumentTag.Get(document.GetWordDocument(), "SUB");

                Assert.NotNull(tags);
                Assert.AreEqual(3, tags.Count());
            }
        }
Beispiel #23
0
        public void ReplaceSingleTagWithTextBlocksDoesNothing()
        {
            using (var initialDocument = new DocxDocument(Resources.WithMainContentSingleTag))
            {
                var content = new[] { new TextBlock("Контент документа") };
                initialDocument.Replace("MAIN_CONTENT", content);

                Assert.IsTrue(new DocxDocumentEqualityComparer().Equals(initialDocument, initialDocument));
            }
        }
Beispiel #24
0
        public void TestCleanContent()
        {
            using (var withParagraph = new DocxDocument(Resources.DocumentWithParagraph))
                using (var withOutParagraph = new DocxDocument(Resources.DocumentWithoutParagraph))
                {
                    withParagraph.CleanContent("Edit");

                    Assert.IsTrue(new DocxDocumentEqualityComparer().Equals(withOutParagraph, withParagraph));
                }
        }
Beispiel #25
0
        public void AppendParagraphAddsNewParagraphToTheEndOfDocument()
        {
            using (var expected = new DocxDocument(Resources.DocumentWithAddedParagraph))
                using (var document = new DocxDocument(Resources.DocumentWithoutParagraph))
                {
                    document.AppendParagraph("New paragraph content");

                    Assert.AreEqual(GetParagraphs(expected).Count(), GetParagraphs(document).Count());
                    Assert.AreEqual(GetParagraphs(expected).Last().InnerText, GetParagraphs(document).Last().InnerText);
                }
        }
Beispiel #26
0
        public void InsertTagContentToOpenCloseTag()
        {
            using (var initialDocument = new DocxDocument(Resources.WithMainContentTag))
                using (var expectedDocument = new DocxDocument(Resources.WithMainContentInserted))
                {
                    var content = new[] { new TextBlock("Контент документа") };
                    initialDocument.InsertTagContent("MAIN_CONTENT", content);

                    Assert.IsTrue(new DocxDocumentEqualityComparer().Equals(expectedDocument, initialDocument));
                }
        }
Beispiel #27
0
        public void CanCheckFieldAvailabilityUsingItsName()
        {
            using (var document = new DocxDocument(Resources.WithSdtElements))
            {
                var hasBlockField  = document.Fields.Contains("BlockField");
                var hasInlineField = document.Fields.Contains("InlineField");

                Assert.True(hasBlockField);
                Assert.True(hasInlineField);
            }
        }
Beispiel #28
0
        public void CanCheckFieldAvailabilityUsingItsTag()
        {
            using (var document = new DocxDocument(Resources.WithSdtElements))
            {
                var hasBlockField  = document.Fields.Contains(tag: "FirstTag");
                var hasInlineField = document.Fields.Contains(tag: "SecondTag");

                Assert.True(hasBlockField);
                Assert.True(hasInlineField);
            }
        }
Beispiel #29
0
 public void ReplacePairsFromDictionary()
 {
     using (var initialDocument = new DocxDocument(Resources.DocumentWithTitle))
         using (var expectedDocument = new DocxDocument(Resources.DocumentWithReplacedTitle))
         {
             initialDocument.Replace(new Dictionary <string, string>
             {
                 { "Title", "Hello" }
             });
             Assert.IsTrue(new DocxDocumentEqualityComparer().Equals(expectedDocument, initialDocument));
         }
 }
Beispiel #30
0
        public void PropertiesAppliedToInlineFieldInHeadersAndFooters()
        {
            using (var expected = new DocxDocument(Resources.FieldsInHeadersAndFootersReplaced))
                using (var document = new DocxDocument(Resources.FieldsInHeadersAndFooters))
                {
                    var builder = new DocxDocumentBuilder(document);

                    builder.InlineField("Signer.ShortNameThisOrSubstitute", x => x.Text("Первый").Text("Второй"));

                    Assert.IsTrue(new DocxDocumentEqualityComparer().Equals(expected, document));
                }
        }
Beispiel #31
0
        public void PropertiesAppliedToInlineFieldShouldPersistAfterSettingContent()
        {
            using (var expected = new DocxDocument(Resources.WithStyledInlineSdtElementContentInserted))
                using (var document = new DocxDocument(Resources.WithStyledInlineSdtElement))
                {
                    var builder = new DocxDocumentBuilder(document);

                    builder.InlineField("InlineField", x => x.Text("Первый").Text("Второй"));

                    Assert.IsTrue(new DocxDocumentEqualityComparer().Equals(expected, document));
                }
        }
Beispiel #32
0
        /// <summary>
        /// Construct a Document instance. 
        /// </summary>
        public static Document New(string pathAndName)
        {
            Document newDoc;
            FileInfo i = new FileInfo(pathAndName);
            switch (i.Extension.ToLowerInvariant())
            {

                case ".docx":
                    newDoc = new DocxDocument(pathAndName);
                    break;

                case ".pptx":
                    newDoc = new PptxDocument(pathAndName);
                    break;

                case ".xlsx":
                    newDoc = new XlsxDocument(pathAndName);
                    break;

                case ".ppt":
                case ".xls":
                case ".doc":
                case ".pdf":
                    newDoc = new FilterDocument(pathAndName);
                    newDoc.Extension = i.Extension.ToLowerInvariant();
                    break;

                case ".txt":
                    newDoc = new TextDocument(pathAndName);
                    break;

                case ".xml":
                case ".htm":
                case ".html":
                case ".xhtml":
                    newDoc = new HtmlDocument(pathAndName);
                    break;

                case ".jpeg":
                case ".jpg":
                    newDoc = new JpegDocument(pathAndName);
                    break;

                default:
                    //could be rtf or other format
                    newDoc = new IgnoreDocument(pathAndName);
                    // Console.WriteLine("Unrecognized format encountered :" + i.Extension);
                    // Zasz: TODO: Raise Progress Event
                    break;
            }
            newDoc.Length = i.Length;
            return newDoc;
        }
Beispiel #33
0
        public void CanSetContentToBlockFieldConsistingOfTwoParagraphs()
        {
            using (var expected = new DocxDocument(Resources.WithTwoParagraphsInBlockField))
                using (var document = new DocxDocument(Resources.WithSdtElements))
                {
                    var builder = new DocxDocumentBuilder(document);

                    builder.BlockField("BlockField", x => x.Paragraph("Первый").Paragraph("Второй"));

                    Assert.IsTrue(new DocxDocumentEqualityComparer().Equals(expected, document));
                }
        }
Beispiel #34
0
        public void CanSetContentToInlineFieldConsistingOfTwoRuns()
        {
            using (var expected = new DocxDocument(Resources.WithTwoRunsInInlineField))
                using (var document = new DocxDocument(Resources.WithSdtElements))
                {
                    var builder = new DocxDocumentBuilder(document);

                    builder.InlineField("InlineField", x => x.Text("Первый").Text("Второй"));

                    Assert.IsTrue(new DocxDocumentEqualityComparer().Equals(expected, document));
                }
        }
        public void PropertiesAppliedToInlineFieldInHeadersAndFooters()
        {
            using (var expected = new DocxDocument(Resources.FieldsInHeadersAndFootersReplaced))
            using (var document = new DocxDocument(Resources.FieldsInHeadersAndFooters))
            {
                var builder = new DocxDocumentBuilder(document);

                builder.InlineField("Signer.ShortNameThisOrSubstitute", x => x.Text("Первый").Text("Второй"));

                Assert.IsTrue(new DocxDocumentEqualityComparer().Equals(expected, document));
            }
        }
        public void PropertiesAppliedToInlineFieldShouldPersistAfterSettingContent()
        {
            using (var expected = new DocxDocument(Resources.WithStyledInlineSdtElementContentInserted))
            using (var document = new DocxDocument(Resources.WithStyledInlineSdtElement))
            {
                var builder = new DocxDocumentBuilder(document);

                builder.InlineField("InlineField", x => x.Text("Первый").Text("Второй"));

                Assert.IsTrue(new DocxDocumentEqualityComparer().Equals(expected, document));
            }
        }
        public void CanSetContentToInlineFieldConsistingOfTwoRuns()
        {
            using (var expected = new DocxDocument(Resources.WithTwoRunsInInlineField))
            using (var document = new DocxDocument(Resources.WithSdtElements))
            {
                var builder = new DocxDocumentBuilder(document);

                builder.InlineField("InlineField", x => x.Text("Первый").Text("Второй"));

                Assert.IsTrue(new DocxDocumentEqualityComparer().Equals(expected, document));
            }
        }
        public void CanSetContentToBlockFieldConsistingOfTwoParagraphs()
        {
            using (var expected = new DocxDocument(Resources.WithTwoParagraphsInBlockField))
            using (var document = new DocxDocument(Resources.WithSdtElements))
            {
                var builder = new DocxDocumentBuilder(document);

                builder.BlockField("BlockField", x => x.Paragraph("Первый").Paragraph("Второй"));

                Assert.IsTrue(new DocxDocumentEqualityComparer().Equals(expected, document));
            }
        }
 /// <summary>
 /// Factory method
 /// </summary>
 /// <param name="docxDocument">Template document</param>
 public static IDocumentBuilder Create(DocxDocument docxDocument)
 {
     return new DocxDocumentBuilder(docxDocument);
 }
Beispiel #40
0
 public SingleTagReplacer(string name, DocxDocument document)
     : base(name, document)
 {
 }
Beispiel #41
0
 protected TagReplacer(string name, DocxDocument document)
 {
     Document = document;
     Name = name;
 }