Code Monkey home page Code Monkey logo

Comments (7)

PrzemyslawKlys avatar PrzemyslawKlys commented on May 25, 2024 1

Ye, as part of this PR:

I've added conversion of those \n and similar. It solved:

What you are seeing in your example is - there is already empty paragraph added in each cell. That means after you add 2 more there are 3.

image

So not only in your code \n adds another Run within Paragraph with break in it, but also you already have some more at each cell. Each run has it's own style.

So while the break doesn't do anything really, you have to be aware that it exists. In your case it's mostly that empty paragraph (paragraph 0) doing the hiding.

string filePath = System.IO.Path.Combine(folderPath, "Basic Document with Headers and Footers Default2.docx");
using (WordDocument document = WordDocument.Create(filePath)) {
    document.AddHeadersAndFooters();
    document.DifferentOddAndEvenPages = false;
    document.DifferentFirstPage = false;

    var headerTable = document.Header.Default.AddTable(1, 3, WordTableStyle.TableNormal);
    headerTable.WidthType = TableWidthUnitValues.Pct;
    headerTable.Width = 5000;

    var headers = new List<(string, string)>
    {
        ("Should be blue?\n", "#0000FF"), // this is the problem
        ("Should be red?", "#FF0000")
    };
    var count = 0;
    foreach (var header in headers) {
        var headerRow = headerTable.Rows[0].Cells[1].Paragraphs[count].SetText(header.Item1).SetColorHex(header.Item2);
        headerRow.ParagraphAlignment = JustificationValues.Center;
        headerRow.AddParagraph(); // probably some code to not add it if it's last in line
        count++;
    }
    document.Save();
}

from officeimo.

PrzemyslawKlys avatar PrzemyslawKlys commented on May 25, 2024
string filePath = System.IO.Path.Combine(folderPath, "Basic Document with Headers and Footers Default.docx");
using (WordDocument document = WordDocument.Create(filePath)) {
    document.AddHeadersAndFooters();
    document.DifferentOddAndEvenPages = false;
    document.DifferentFirstPage = false;

    var headerTable = document.Header.Default.AddTable(1, 3, WordTableStyle.TableNormal);
    headerTable.WidthType = TableWidthUnitValues.Pct;
    headerTable.Style = WordTableStyle.GridTable1Light;


    var header1 = headerTable.Rows[0].Cells[1].AddParagraph();
    header1.ParagraphAlignment = JustificationValues.Center;
    var text1 = header1.AddText("Should be blue?");
    text1.ColorHex = "0000FF";

    var header2 = headerTable.Rows[0].Cells[1].AddParagraph();
    header2.ParagraphAlignment = JustificationValues.Center;
    var text2 = header2.AddText("Should be red?");
    text2.ColorHex = "FF0000";
    document.Save();
}

Gets me this:

image

Looks ok to me.

from officeimo.

hisuwh avatar hisuwh commented on May 25, 2024

Am I loosing my mind, or are these two blocks of code identical:
image

Looking at whats in the document at the breakpoint. There a lot of empty paragraphs, but the key one doesn't have a colour set:

: 
:
Should be blue? : 021766
:
Should be red? : f64c36
:
Header 1 : 
:
:
Header 2 : f64c36

Except one works and one doesn't:
image

from officeimo.

hisuwh avatar hisuwh commented on May 25, 2024

Ah I've solved it. "Header 1" was actually "Header 1\n"
So I'm guessing AddText does some splitting and returns the last text block? If I trim the trailing \n then it gets the correct colour

from officeimo.

hisuwh avatar hisuwh commented on May 25, 2024

Is there a way to style all the text regardless of the linebreaks within it though? As I can foresee that still being a requirement.

from officeimo.

hisuwh avatar hisuwh commented on May 25, 2024

So this fails:

string filePath = System.IO.Path.Combine("C:\\temp", "Basic Document with Headers and Footers Default.docx");
using (WordDocument document = WordDocument.Create(filePath)) {
    document.AddHeadersAndFooters();
    document.DifferentOddAndEvenPages = false;
    document.DifferentFirstPage = false;

    var headerTable = document.Header.Default.AddTable(1, 3, WordTableStyle.TableNormal);
    headerTable.WidthType = TableWidthUnitValues.Pct;
    headerTable.Width = 5000;

    var headers = new List<(string, string)>
    {
        ("Should be blue?\n", "#0000FF"), // this is the problem
        ("Should be red?", "#FF0000")
    };

    foreach (var header in headers)
    {
        var headerRow = headerTable.Rows[0].Cells[1].AddParagraph();
        headerRow.ParagraphAlignment = JustificationValues.Center;
        var text = headerRow.AddText(header.Item1);
        text.ColorHex = header.Item2;
    }
    document.Save();
}

from officeimo.

hisuwh avatar hisuwh commented on May 25, 2024

Thanks @PrzemyslawKlys as always for being so responsive and helpful 🙇

from officeimo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.