-
-
Notifications
You must be signed in to change notification settings - Fork 47
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
Color applied to first paragraph in header/footer ignored #168
Comments
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: Looks ok to me. |
Ah I've solved it. |
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. |
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();
} |
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. 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();
} |
Thanks @PrzemyslawKlys as always for being so responsive and helpful 🙇 |
Summary
We are trying to apply some colours to text in headers and footers. Though we are doing something slightly weird potentially in that we're adding a table to the header then. For some reason it's not applying the color to the first paragraph
Code
This is roughly what we're doing though I have simplified it to just show the sequence of calls to office IMO methods:
Screenshots
Header 1 should be blue
Header 2 should be red
It seems to only be applying the style to the second paragraph.
Header:

Footer:

The text was updated successfully, but these errors were encountered: