问题
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.
回答1:
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;
回答2:
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");
回答3:
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.
来源:https://stackoverflow.com/questions/16774066/migradoc-c-sharp-align-left-and-right-on-same-line