ClosedXML / ClosedXML/ClosedXML

Printed document does not respect actual font and font size

Open
#469 37 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
5.7k
Forks
933
Avg merge
12h 14m
Merged PRs (30d)
1

Description

**Do you want to request a *feature* or report a *bug*?**
- [x] Bug
- [ ] Feature

**Version of ClosedXML**

e.g. 0.88.0

**What is the current behavior?**

I am generating an excel with two worksheets. The first contains some business data, the second one contains the **Terms and Condition** of my company.

When the excel file is done generating, open it and directly go to the print option. Select **print the entire workbook** and scroll down the preview to see the pages of the second worksheet.

You will see that the font and font size of the preview is not the same as the one I setup in my code. If you go back to the file, open the second worksheet and then return to the print options, the preview now respects the font and font size.

Notice that I encounter the same issue when exporting to PDF or with the actually printed document. But once the second tab has been actually rendered by Excel, everything is back to normal.

Finally, I change my code (the sample below is the first version of the code, with two worksheets) to add the **Terms and Conditions** at the end of the first worksheet. But the problem is the same. If I don't scroll until Excel actually renders the **Terms and Conditions**, the font and font size are not respected during printing or pdf exporting.

I already post a question on [Stack Overflow](https://stackoverflow.com/questions/45982554/generated-excel-with-closedxml-does-not-use-the-specified-font-size-during-print?noredirect=1#comment79128653_45982554) which drives me here.

**What is the expected behavior or new feature?**

I expect the printed or exported document to respect the font and font size I setup in my code without having to force Excel to render if before.

I think it is a problem with merged cells, since I am merging a lots of rows to render my **Terms and Conditions**.

As I said in the Stack Overflow question, I am not sure it is a ClosedXML bug. I am suspecting an Excel bug instead. But may be there is a way to fix it in ClosedXML ?

**Did this work in previous versions of our tool? Which versions?**

I don't know.

**Code to reproduce problem:**
```c#
class Program
{
const int nbRowByPage = 67;
const int nbTotalColumn = 8;
const int nbParagraphByPage = 22;

const string baseTemplateFileName = "TemplateOffer.xlsx";
const string generatedExcelFileName = "Offer.xlsx";

const string header = "Lorem ipsum dolor sit amet";
const string paragraph = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed semper massa lacus, eget egestas lorem dapibus et. Sed at cursus tortor. Mauris sit amet dapibus felis. Integer elementum ante mi, a sollicitudin lorem fermentum eu. Duis pellentesque lobortis faucibus. Suspendisse sit amet velit nunc. Nunc eget pellentesque nunc.";

static void Main(string[] args)
{
// Deletes any existing excel file
var fi = new FileInfo(generatedExcelFileName);
if (fi.Exists)
{
fi.Delete();
}

var paragraphs = new List { header, paragraph, header, paragraph, header, paragraph, header, paragraph, header, paragraph, header, paragraph, header, paragraph, header, paragraph, header, paragraph, header, paragraph, header, paragraph, header, paragraph, header, paragraph, header, paragraph, header, paragraph, header, paragraph, header, paragraph };

using (var workbookDocument = new XLWorkbook(baseTemplateFileName))
{
// Gets the second worksheet
var conditionsWS = workbookDocument.Worksheet(2);

// Injects the paragraphs in the worksheets
PrintTerms(conditionsWS, paragraphs);

// Saves the excel
workbookDocument.SaveAs(generatedExcelFileName, true);
}

// Opens the generated excel with MS Excel
Process.Start(generatedExcelFileName);
}

static void PrintTerms(IXLWorksheet ws, List generalTerms)
{
int rowId = 1;
IXLCell currentCell = null;

for (int i = 0; i < generalTerms.Count; i++)
{
// needs to setup a new row
if ((i % nbParagraphByPage) == 0)
{
currentCell = ws.Cell(rowId, 1);

currentCell.Style.Alignment.SetHorizontal(XLAlignmentHorizontalValues.Justify);
currentCell.Style.Alignment.SetVertical(XLAlignmentVerticalValues.Top);
currentCell.Style.Font.SetFontName("Arial");
currentCell.Style.Font.SetFontSize(8);

ws.Range(rowId, 1, rowId + nbRowByPage - 1, nbTotalColumn).Merge();

rowId += nbRowByPage;
}

var text = currentCell.RichText.AddText(generalTerms[i]);

// Break line
currentCell.RichText.AddNewLine();
currentCell.RichText.AddNewLine();
}
}
}
```
Here is the Excel template used to generate the actual file. Please, check copy to bin folder before running.

[TemplateOffer.xlsx](https://github.com/ClosedXML/ClosedXML/files/1284828/TemplateOffer.xlsx)

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the issue with the provided Program and TemplateOffer.xlsx, focusing on PrintTerms, merged ranges, and rich-text font settings. Inspect the generated workbook and compare its print or PDF output before and after Excel renders the Terms and Conditions worksheet. Done means determining whether ClosedXML can produce output that preserves the configured font and size without manual rendering, or documenting that the behavior is an Excel issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.