MigraDoc C# Align left and right on same line
Asked Answered
S

3

7

I have a table with a cell where i want two texts, the first, aligned on left and the second aligned on the right, in the same cell, on the same line.

I tried to reproduce this cell with MigraDoc without success. I only can add two texts aligned on left and right but not on same line.

Here my code:

Cell cellFooter1 = rowFooter.Cells[0];
Paragraph paraphTot = new Paragraph();
paraphTot.Format.Alignment = ParagraphAlignment.Left;
paraphTot.AddText("Left text");
cellFooter1.Add(paraphTot);
Paragraph paraphDetails = new Paragraph();
paraphDetails.Format.Alignment = ParagraphAlignment.Right;
paraphDetails.AddText("Right text");
cellFooter1.Add(paraphDetails);

A solution is presented here (http://forum.pdfsharp.net/viewtopic.php?f=2&t=2373) but I'm not able to do same with my table. I don't understand how it work.

Edit : Partial solution :

After a hard work to understand how it work, my code is partially working. partial because the only way I found to right align is to creat a TabStop with an approximative value... not fine.

Table table = new Table();
table.Borders.Width = 0.75;
Column myColumn = table.AddColumn(Unit.FromCentimeter(7));
Row myRow = table.AddRow();
Cell myCell = myRow.Cells[0];
Paragraph myParagraph = new Paragraph();
Style myStyle = doc.AddStyle("myStyle", "Normal");
myStyle.ParagraphFormat.Font.Size = 6.5;
myStyle.ParagraphFormat.Font.Bold = true;
myStyle.ParagraphFormat.TabStops.Clear();
myStyle.ParagraphFormat.AddTabStop(Unit.FromMillimeter(67), TabAlignment.Right);
myParagraph.Style = "myStyle";
myParagraph.Format.Alignment = ParagraphAlignment.Left;
myParagraph.AddFormattedText("left", "myStyle");
myParagraph.AddTab();
myParagraph.AddFormattedText("right", "myStyle");
myCell.Add(myParagraph);

It work but how to find the good value for the AddTab function ? I put 67 because 68to70 is not working.

Sandy answered 27/5, 2013 at 13:14 Comment(0)
S
11

The trick shown in the linked post is rather simple: you only need a single paragraph, left-aligned.

Then make sure there is only one tabstop defined, a right-aligned tabstop at the right edge of the cell.

To the paragraph, add the text you want left-aligned, then add a tabstop, then add the text you want right-aligned.

Sample code:

var table = section.AddTable();
table.AddColumn("8cm");
table.AddColumn("8cm");

var row = table.AddRow();
var paragraph = row.Cells[0].AddParagraph("Left text");
paragraph.AddTab();
paragraph.AddText("Right text");
paragraph.Format.ClearAll();
// TabStop at column width minus inner margins and borders:
paragraph.Format.AddTabStop("7.7cm", TabAlignment.Right);
row.Cells[1].AddParagraph("Second column");
table.Borders.Width = 1;
Scissors answered 27/5, 2013 at 14:59 Comment(2)
Thanks for explications. I'll test it again tomorrow at work but, like this, i don't see how. More news tomorrow.Sandy
Just added sample code. The right tab stop must take inner margins and borders of the table cell into account. Currently I have no better idea than trial and error for that (3 mm worked for me). It can be done without Style (as my sample code shows), but if it occurs more than once in your document, using styles is the recommended way.Scissors
X
4

On a single line you can "correct" the line height with the SpaceAfter property equal to the negative value of the fontsize.

Sample RightAlignedTitle style:

  // Define style: RightAlignedTitle
  style = document.Styles.AddStyle(Styles.RightAlignedTitle, StyleNames.Normal);
  style.Font.Size = new Unit(18, UnitType.Point);
  style.ParagraphFormat.Alignment = ParagraphAlignment.Right;
  style.ParagraphFormat.SpaceAfter = new Unit(-18, UnitType.Point);

Sample code:

  // First right aligned paragraph
  p = section.AddParagraph();
  p.Style = Styles.RightAlignedTitle;
  p.AddText("Right aligned text");

  // Second left aligned paragraph
  p = section.AddParagraph();
  p.Format.Alignment = ParagraphAlignment.Left;
  p.AddText("Left aligned text");
Xyster answered 28/6, 2013 at 15:27 Comment(0)
O
-1
   private void **PDF_DrawTextRight**(string text, PdfPage page, XGraphics gfx, XFont font, double x, double y, double x2, double y2)
    {
        var m = gfx.MeasureString(text, font);

        // Draw the text
        gfx.DrawString(text, font, XBrushes.Black,
          new XRect(x+x2-m.Width, y, x2, y2),
          XStringFormats.TopLeft);
    }

This is another way... Used in an invoice application, where numbers are right aligned, and item description left aligned.

Odaniel answered 27/12, 2017 at 19:16 Comment(1)
The question is about MigraDoc, your answer uses PDFsharp. Not helpful IMHO.Scissors

© 2022 - 2024 — McMap. All rights reserved.