MigraDoc C # Align left and right on the same line

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

I tried to play this cell using MigraDoc without success. I can add only two texts aligned left and right, but not on the same line.

Here is 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); 

Here is a solution ( http://forum.pdfsharp.net/viewtopic.php?f=2&t=2373 ), but I can not do the same with my table. I do not understand how this works.

Edit: Partial Solution:

After hard work, to understand how it works, my code partially works. in part, because the only way I found to align to the right is to create a TabStop with an approximate value ... not very good.

 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 works, but how to find a good value for the AddTab function? I put 67 because 68to70 is not working.

+4
source share
3 answers

The trick shown in the linked post is pretty simple: you only need one paragraph aligned to the left.

Then make sure that only one tabstop is defined, right-aligned on the right-hand edge of the cell.

In the paragraph, add the text that you want to align to the left, then add tabstop, then add the text that you want to align to the right.

Code example:

 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; 
+7
source

In one line, you can "fix" the height of the line using the SpaceAfter property, which is equal to the negative value of the font.

RightAlignedTitle style example:

  // 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); 

Code example:

  // 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"); 
+2
source
  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 the invoice application, where the numbers are right-aligned and the position description is left-aligned.

-1
source

Source: https://habr.com/ru/post/1482976/


All Articles