I have some code below, using PdfSharpCore (not PdfSharp) to generate a Pdf with some information, line by line. The _client string for instance, is a long string of client names. However, the problem is sometimes if several clients have been selected, their names can go over the total width of the PDF page. This means that the underline is sometimes being drawn through the clients. Can you help?
private void RenderInfo(XGraphics xGraphics)
{
XSolidBrush labelBrush = new XSolidBrush(InfoLabelColour);
XSolidBrush valueBrush = new XSolidBrush(InfoValueColour);
XFont labelFont = new XFont(InfoLabelFontFamily, InfoLabelFontSize, InfoLabelXFontStyle);
XFont valueFont = new XFont(InfoValueFontFamily, InfoValueFontSize, InfoValueXFontStyle);
XRect labelColumnRect = new XRect(Constants.Page.LeftMarginWidth, InfoTopSpacing, InfoLabelColumnWidth, RowHeight);
XRect valueColumnRect = new XRect(labelColumnRect.Right + InfoColumnSeperatorWidth, InfoTopSpacing,
Constants.Page.Width - labelColumnRect.Right - InfoColumnSeperatorWidth - Constants.Page.RightMarginWidth, RowHeight);
XPen valueLinePen = new XPen(InfoValueLineColour, InfoValueLineWidth);
XPoint valueLineStart = new XPoint(valueColumnRect.Left, valueColumnRect.Bottom);
XPoint valueLineEnd = new XPoint(valueColumnRect.Right, valueColumnRect.Bottom);
List<Tuple<string, string>> DataList = new List<Tuple<string, string>>
{
new Tuple<string, string>("Case ID",_caseNumber),
new Tuple<string, string>("Client",_client),
new Tuple<string, string>("Advisor",_advisor),
new Tuple<string, string>("Company",_company),
new Tuple<string, string>("Print Date",_printDate.ToString(Constants.Page.DateFormatString))
};
DataList.Add(new Tuple<string, string>("Fact Find Created Date", _creationDate?.ToString(Constants.Page.DateFormatString) ?? string.Empty));
XTextFormatter tf = new XTextFormatter(xGraphics);
foreach (Tuple<string, string> Data in DataList)
{
xGraphics.DrawString(Data.Item1, labelFont, labelBrush, labelColumnRect, XStringFormats.TopRight);
// Define the rectangle where the text should be formatted within
// Note: The height is set arbitrarily high; XTextFormatter will not draw beyond the actual text height
XRect valueTextRect = new XRect(valueColumnRect.Left, valueColumnRect.Top, valueColumnRect.Width, 1000);
// Draw the value text within the rect, allowing it to wrap as needed
tf.DrawString(Data.Item2, valueFont, valueBrush, valueTextRect, XStringFormats.TopLeft);
// Measure the actual height of the drawn text to place the underline correctly
XSize size = xGraphics.MeasureString(Data.Item2, valueFont);
// Draw the underline below the wrapped text
xGraphics.DrawLine(valueLinePen, new XPoint(valueColumnRect.Left, valueColumnRect.Top), new XPoint(valueColumnRect.Right, valueColumnRect.Top));
// Adjust rects for the next iteration
// Increase offset by the height of the text plus some space for padding between entries
double offset = (RowSpacing - size.Height);
labelColumnRect.Offset(0, offset);
valueColumnRect.Offset(0, offset);
}
}