FAQ - Frequently asked questions
Version 6.2.0
Questions and answers about MigraDoc.
In this article
- How can I bold certain text in a paragraph?
- How to set the background color of a Cell in a Table
- Keep table in one piece?
- How do I add preceding spaces to a String?
About
See the MigraDoc Overview for general information about MigraDoc.
See also:
General FAQ
PDFsharp FAQ
How can I bold certain text in a paragraph?
To mix different styles in a paragraph, use AddFormattedText.
var paragraph = section.AddParagraph("This is regular text. ");
paragraph.AddFormattedText("Text in Bold style.", TextFormat.Bold);
How to use an image from a byte[] or stream?
MigraDoc can only take strings as references to images.
But there is a trick: You can use the base64:
pseudo-protocol and provide the image data in the BASE64 format.
How to size a table to the page width?
MigraDoc uses default values for left and right margins if no margins have been set for your section.
Here is code that does the math.
var section = document.LastSection;
var pageWidth = section.PageSetup.Width ?? document.DefaultPageSetup.Width;
var leftMargin = section.PageSetup.LeftMargin ?? document.DefaultPageSetup.LeftMargin;
var rightMargin = section.PageSetup.rightMargin ?? document.DefaultPageSetup.rightMargin;
// Width of the document body.
var bodyWidth = pageWidth - leftMargin - rightMargin;
Note
The code snippet above is simplified, but covers most use-cases. The PageSetup of a section will inherit unset values from the previous section which in turn inherits from its previous section and finally the first section inherits from DefaultPageSetup.
How to set the background color of a Cell in a Table?
To set the shading of the complete cell, use Cell.Shading.
var cell = row.Cells[0];
cell.Shading.Color = Colors.Black;
cell.Font.Color = Colors.White;
To set the shading of all paragraphs in a cell, use Cell.Format.Shading.
var cell = row.Cells[0];
cell.Format.Shading.Color = Colors.Black;
cell.Format.Font.Color = Colors.White;
Keep table in one piece?
You can set the KeepWith property of a Table Row to specify blocks that must be kept together.
If you know the table fits on one page, you can set the KeepWith property of the first row to (table.Rows.Count - 1)
when the table is finished.
How do I add preceding spaces to a String?
To get more than one adjacent spaces with MigraDoc, simply use non-breaking spaces.
You can press Alt+<255> on your keyboard or use \A0
in a C# string.