Comments (7)
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();
}
from officeimo.
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.
from officeimo.
Am I loosing my mind, or are these two blocks of code identical:
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:
from officeimo.
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.
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.
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.
Thanks @PrzemyslawKlys as always for being so responsive and helpful 🙇
from officeimo.
Related Issues (20)
- InvalidProgramException HOT 5
- Open Office Writer can't open document created with OfficeIMO HOT 17
- Add Combo Chart HOT 1
- Upgrading DocumentFormat.OpenXML to version 3 HOT 1
- dotnet 8.0 error HOT 13
- Any way to implement numbered list (a., b., c., ....)? HOT 4
- Setting font size for list symbol HOT 7
- Any way to insert rich text HTML content? HOT 8
- Remove continue numbering & restart numbering for lists
- Add ability to merge & remove lists
- Image layout options HOT 8
- Mislabeled Behavior? HOT 8
- Potential Bug: Does iteration over TableCell's Paragraphs property allow mutation? HOT 21
- Stable and unlikely to change vs unstable and subject to change vs depreciated api HOT 13
- How to add paragraph without pagebreak? HOT 4
- ContentControls.Add() is trimming space from user selection HOT 1
- How to display the specific gravity value in a pie chart HOT 2
- How to add image watermark? HOT 3
- how can I add a table with two rows and each row had diff columns and width? HOT 1
- Issue with Text Replacement in Multi-Line Text Boxes HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from officeimo.