i have to render a PDF with 3 columns layout. But i have to know when i dont have enough space for the next paragraph in the column and switch to the next one. I tried to use the XTextFormatterEx what the owner of the library using, but im still don't have accuracy what i need.
Asked
Active
Viewed 81 times
-1
-
`XTextFormatterEx` is not from the owner of the library, but it returns the height of the text when used as intended. You do not show any code, so we cannot identify what you are doing wrong. – I liked the old Stack Overflow May 15 '23 at 15:21
-
https://pastebin.com/GSCQE8Pm This is the code. Here is the picture of the wanted behavior https://pasteboard.co/H47cXZjlJUlg.png – iFURY May 15 '23 at 20:51
-
I just see a code snippet. Where does "_xGraphics" come from? What is "MeasureStringExact"? Where do you use `XTextFormatterEx`? BTW: You do not need a new `Section` just to add a page break. Your use of `SpaceAfter` seems superfluous - tables rows will automatically align vertically. – I liked the old Stack Overflow May 16 '23 at 06:51
-
You chose a complicated approach that leads to complicated problems in the end. Maybe start from scratch with a different approach that may seem more complicated first, but avoids the problems that show later: Create a table with two columns that fits in the width of 1/3 A4 page and let MigraDoc do the page breaks. Then render three "pages" of this table side by side on the pages of the final document. With this approach you do not have to measure a single string and avoid the hassle you are facing now. – I liked the old Stack Overflow May 16 '23 at 07:01
-
Thank you for the response, im fill the columns one by one starting from the left to the right. If the space in the first column is not enough for the next paragraph, then im changing the column. That is the idea. The space after is required in my example because i can have 5 lines with information about pages, but the name can be one line, and i have to add that "SpaceAfter" with the idea next name with pages information to start at the same level. – iFURY May 16 '23 at 07:22
-
Im using only one row per column, because the row height what i can modify is applied to the whole table and when i change the column, the text didnt start from the top of the table. I looked the way with rendering previously generated 3 pages into one, side by side, but that leads me again to string measurements because i also have like this template where i didnt want to split paragraphs to different columns. Its long and strange requirements to type here, i just wanted to know if there is a way to measure the text after wrapping. If not i have to think about different approach. – iFURY May 16 '23 at 07:29
-
You do not have to use MeasureString when you use MigraDoc as intended. The `XTextFormatterEx` class is an addon for PDFsharp and results may differ from what MigraDoc does. Maybe leave out MigraDoc for the Index page and instead use PDFsharp and `XTextFormatterEx` for total control over everything that is being drawn. Then the measurements of `XTextFormatterEx` will be exact and you avoid playing with `SpaceAfter` and other hacks. – I liked the old Stack Overflow May 16 '23 at 07:40
-
That is great idea, but that way i will have problems with the header and footer, i have to manually set where everything to be drawn(with PdfSharp), if im understanding correct if i need really customization capabilities i have to use PdfSharp because that way i have full control over everything. – iFURY May 16 '23 at 15:04
-
No, you can mix MigraDoc and PDFsharp. First create the Index on smaller pages to calculate how many pages you need. Create a MigraDoc document with header and footer and empty pages for the index. Then draw the index pages, three side by side, onto the empty pages. See here: https://forum.pdfsharp.net/viewtopic.php?f=8&t=3172 After calling `RenderPage` to create header/footer, use gfx and `DrawImage` to draw a page from the index PDF created earlier. Or use `RenderPage` with transformations (more efficient, more difficult). http://pdfsharp.net/wiki/MixMigraDocAndPdfSharp-sample.ashx – I liked the old Stack Overflow May 16 '23 at 22:38
2 Answers
0
PDFsharp: When using XTextFormatterEx
to measure text and to draw text, then XTextFormatterEx
is the way to calculate the exact height of text strings.
MigraDoc: With MigraDoc you design documents that can be rendered for several outputs, so at the design stage there are no pages and no "text heights".
When MigraDoc is used as intended, there is no need to use MeasureString at all.
MeasureString can be used to determine the height of a single line for PDF, but the height of multi-line paragraphs may still be different.

I liked the old Stack Overflow
- 20,651
- 8
- 87
- 153
-1
for horizontal size look here : How to determine the size of a string given a font
for vertical size, search for "font metrics" there is plenty of schema in google image. Here is a sample of how to get theses metrics :
//Get "arial Regular" Metrics in DesignUnit
var lFont = new FontFamily("Arial");
var lEm_height = lFont.GetEmHeight(System.Drawing.FontStyle.Regular);
var lAscent = lFont.GetCellAscent(System.Drawing.FontStyle.Regular);
var lDescent = lFont.GetCellDescent(System.Drawing.FontStyle.Regular);
var lLineSpacing = lFont.GetLineSpacing(System.Drawing.FontStyle.Regular);
//in milimeter
double lFontSize = 12; //in pt
var lFontSizeMM = U_Unit.Convert_From_pt.To_mm(lFontSize); //convert Pt to MM, from my personal lib
var lMM_Ascent = lFontSizeMM * (lAscent / lEm_height);
var lMM_Descent = lFontSizeMM * (lDescent / lEm_height);
var lMM_LineSpacing = lFontSizeMM * (lLineSpacing / lEm_height);
var lMM_Em_height = lFontSizeMM * (lEm_height / lEm_height);
//in px
var lFontSizePx = U_Unit.Convert_From_pt.To_px(lFontSize); //convert Pt to Px, from my personal lib
var lPx_Ascent = lFontSizePx * (lAscent / lEm_height);
var lPx_Descent = lFontSizePx * (lDescent / lEm_height);
var lPx_LineSpacing = lFontSizePx * (lLineSpacing / lEm_height);
var lPx_Em_height = lFontSizePx * (lEm_height / lEm_height);

Arnaud
- 64
- 5
-
edit just testing this old code from my lib, LineSpacing is the line height, nevermind the rest. it's a litel bit more tricky if the font is not the same on the lines. – Arnaud May 15 '23 at 15:23