Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*BREAKING CHANGE* Improve Watermark with colors, rotation and other settings #181

Merged
merged 24 commits into from
Dec 31, 2023

Conversation

PrzemyslawKlys
Copy link
Member

@PrzemyslawKlys PrzemyslawKlys commented Dec 31, 2023

Solves:

This change adds:

  • Ability to modify watermark (colors, text, rotation, width, height)
  • Ability to remove watermark
  • Ability to find watermarks in document, sections, headers
  • Ability to add watermark to document/section which makes watermark show up only on single page
  • Ability to add watermark to headers/footers which makes watermark show up on whole section

Breaking changes

  • This change breaks how watermarks are added. If you add them directly within section/document it will only apply to single page/pages as the SdtBlock gets added to body directly. If you need watermark for the whole section/document you need to add watermark to header/footer for it to apply to given section.

This example shows per section in header:

public static void Watermark_Sample1(string folderPath, bool openWord) {
    Console.WriteLine("[*] Creating standard document with Watermark 2");
    string filePath = System.IO.Path.Combine(folderPath, "Basic Document with Watermark 4.docx");

    using (WordDocument document = WordDocument.Create(filePath)) {
        document.AddParagraph("Section 0");
        document.AddHeadersAndFooters();
        document.Sections[0].Header.Default.AddParagraph("Section 0 - In header");
        document.Sections[0].SetMargins(WordMargin.Normal);

        Console.WriteLine(document.Sections[0].Margins.Left.Value);
        Console.WriteLine(document.Sections[0].Margins.Right.Value);

        Console.WriteLine(document.Sections[0].Margins.Type);

        document.Sections[0].Margins.Type = WordMargin.Wide;

        Console.WriteLine(document.Sections[0].Margins.Type);

        Console.WriteLine("----");
        var watermark = document.Sections[0].Header.Default.AddWatermark(WordWatermarkStyle.Text, "Watermark");
        watermark.Color = Color.Red;

        // ColorHex normally returns hex colors, but for watermark it returns string as the underlying value is in string name, not hex
        Console.WriteLine(watermark.ColorHex);

        Console.WriteLine(watermark.Rotation);

        watermark.Rotation = 180;

        Console.WriteLine(watermark.Rotation);

        watermark.Stroked = true;

        Console.WriteLine(watermark.Height);
        Console.WriteLine(watermark.Width);

        // width and height in points (HTML wise)
        watermark.Height = 100.15;
        watermark.Width = 500.18;

        document.AddPageBreak();
        document.AddPageBreak();

        document.AddSection();

        document.AddParagraph("Section 1");

        document.Sections[1].AddHeadersAndFooters();
        document.Sections[1].Header.Default.AddParagraph("Section 1 - In header");
        document.Sections[1].Margins.Type = WordMargin.Narrow;
        Console.WriteLine("----");

        Console.WriteLine("Section 0 - Paragraphs Count: " + document.Sections[0].Header.Default.Paragraphs.Count);
        Console.WriteLine("Section 1 - Paragraphs Count: " + document.Sections[1].Header.Default.Paragraphs.Count);

        Console.WriteLine("----");
        document.Sections[1].AddParagraph("Test");
        document.Sections[1].Header.Default.AddWatermark(WordWatermarkStyle.Text, "Draft");

        Console.WriteLine(document.Sections[0].Margins.Left.Value);
        Console.WriteLine(document.Sections[0].Margins.Right.Value);

        Console.WriteLine(document.Sections[1].Margins.Left.Value);
        Console.WriteLine(document.Sections[1].Margins.Right.Value);

        Console.WriteLine(document.Sections[1].Margins.Type);


        document.Settings.SetBackgroundColor(Color.Azure);

        Console.WriteLine("----");

        Console.WriteLine("Watermarks in default header: " + document.Header.Default.Watermarks.Count);

        Console.WriteLine("Watermarks in default footer: " + document.Footer.Default.Watermarks.Count);

        Console.WriteLine("Watermarks in section 0: " + document.Sections[0].Watermarks.Count);
        Console.WriteLine("Watermarks in section 0 (header): " + document.Sections[0].Header.Default.Watermarks.Count);
        Console.WriteLine("Paragraphs in section 0 (header): " + document.Sections[0].Header.Default.Paragraphs.Count);

        Console.WriteLine("Watermarks in section 1: " + document.Sections[1].Watermarks.Count);
        Console.WriteLine("Watermarks in section 1 (header): " + document.Sections[1].Header.Default.Watermarks.Count);
        Console.WriteLine("Paragraphs in section 1 (header): " + document.Sections[1].Header.Default.Paragraphs.Count);

        Console.WriteLine("Watermarks in document: " + document.Watermarks.Count);

        document.Save(false);
    }

    using (WordDocument document = WordDocument.Load(filePath)) {
        //Console.WriteLine("----");
        //Console.WriteLine("Watermarks in default header: " + document.Header.Default.Watermarks.Count);

        //Console.WriteLine("Watermarks in default footer: " + document.Footer.Default.Watermarks.Count);

        //Console.WriteLine("Watermarks in section 0: " + document.Sections[0].Watermarks.Count);
        //Console.WriteLine("Watermarks in section 0 (header): " + document.Sections[0].Header.Default.Watermarks.Count);
        //Console.WriteLine("Paragraphs in section 0 (header): " + document.Sections[0].Header.Default.Paragraphs.Count);

        //Console.WriteLine("Watermarks in section 1: " + document.Sections[1].Watermarks.Count);

        //Console.WriteLine("Paragraphs in section 1 (header): " + document.Sections[1].Header.Default.Paragraphs.Count);

        //Console.WriteLine("Watermarks in document: " + document.Watermarks.Count);

        document.Save(openWord);
    }
}

This example shows per page:

public static void Watermark_Sample3(string folderPath, bool openWord) {
    Console.WriteLine("[*] Creating standard document with watermark");
    string filePath = System.IO.Path.Combine(folderPath, "Basic Document with watermark and sections.docx");

    using (WordDocument document = WordDocument.Create(filePath)) {

        document.AddParagraph("Section 0");
        document.Sections[0].AddWatermark(WordWatermarkStyle.Text, "Confidential");

        document.AddPageBreak();
        document.AddPageBreak();

        var section = document.AddSection();
        section.AddWatermark(WordWatermarkStyle.Text, "Second Mark");

        document.AddParagraph("Section 1");

        document.AddPageBreak();
        document.AddPageBreak();

        var section1 = document.AddSection();

        document.AddParagraph("Section 2");

        document.Sections[2].AddWatermark(WordWatermarkStyle.Text, "New");

        document.AddPageBreak();
        document.AddPageBreak();

        Console.WriteLine("----");
        Console.WriteLine("Watermarks: " + document.Watermarks.Count);
        Console.WriteLine("Watermarks section 0: " + document.Sections[0].Watermarks.Count);
        Console.WriteLine("Watermarks section 1: " + document.Sections[1].Watermarks.Count);
        Console.WriteLine("Watermarks section 2: " + document.Sections[2].Watermarks.Count);

        Console.WriteLine("Paragraphs: " + document.Paragraphs.Count);

        Console.WriteLine("Removing last watermark");

        document.Sections[2].Watermarks[0].Remove();

        Console.WriteLine("Watermarks: " + document.Watermarks.Count);
        Console.WriteLine("Watermarks section 0: " + document.Sections[0].Watermarks.Count);
        Console.WriteLine("Watermarks section 1: " + document.Sections[1].Watermarks.Count);
        Console.WriteLine("Watermarks section 2: " + document.Sections[2].Watermarks.Count);
        Console.WriteLine("Paragraphs: " + document.Paragraphs.Count);

        document.Save(openWord);
    }
}

@PrzemyslawKlys PrzemyslawKlys linked an issue Dec 31, 2023 that may be closed by this pull request
Improved readability by inserting line breaks in properties for clarity. Enhanced functionality of WordHeaderFooter by introducing a Watermarks property, retrieving watermarks from either header or footer sections as applicable. This change streamlines watermark access, facilitating document manipulation tasks.
Introduced a new property to list all watermarks across the document's sections. This update simplifies access to watermarks for users seeking to analyze or modify them without iterating over each section manually. The enhancement boosts code readability and efficiency when working with documents containing multiple watermarks.
Refactored WordSection class to include XML summary comments for public properties, improving code documentation and developer experience. Removed unused using directives and legacy commented-out code, resulting in cleaner and more readable sources. The changes enhance code maintainability by providing clear descriptions of properties for paragraphs, page breaks, lists, embedded documents, and watermarks.
Enhanced document processing capabilities by adding a method for transforming standardized blocks into watermark objects, supporting richer content handling in documents. Concurrently, deprecated an unused paragraph conversion method to improve maintainability without impacting functionality—preparing for future removal if confirmed unnecessary.
@PrzemyslawKlys PrzemyslawKlys linked an issue Dec 31, 2023 that may be closed by this pull request
Implemented new internal utility methods for document element conversions such as converting tables to WordTables, SdtBlocks to WordWatermarks, and paragraphs to WordParagraphs. Enhanced the section data retrieval by adding methods to get the list of paragraphs and SdtBlocks within sections. These changes provide a structured way to manipulate document elements and improve the ease of document section data access in preparation for future document manipulation features.

Refactors ensure consistency and extend our library's functionality, potentially improving performance and maintainability. The introduced conversion methods prepare the groundwork for upcoming features that require more granular control over document elements.
Removed unnecessary comment clutter around the watermark rotation parsing logic, streamlining code readability. Introduced a new constructor that allows adding text watermarks to sections with or without existing paragraphs, enhancing versatility. Additionally, implemented a Remove method to facilitate watermark deletion, improving object manageability. These enhancements ensure that watermarks can be easily added or removed, catering to more dynamic document manipulation requirements.
Refactored parsing of watermark width and height properties to correctly interpret the numerical values in an invariant culture context. This change prevents potential issues with locale-specific decimal separators, guaranteeing that the watermark dimensions are consistent regardless of the user's regional settings. Fixes potential bugs with international usage where '.' is not the decimal separator.
@PrzemyslawKlys PrzemyslawKlys changed the title Improve Watermark with colors, rotation and other settings *BREAKING CHANGE* Improve Watermark with colors, rotation and other settings Dec 31, 2023
@PrzemyslawKlys PrzemyslawKlys merged commit b2887d9 into master Dec 31, 2023
3 checks passed
@PrzemyslawKlys PrzemyslawKlys deleted the ImproveWatermark branch December 31, 2023 16:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Watermark font and size Watermark only a page
1 participant