Woohoo! Think I've finally managed to get Alignment being read from the TextFields, It might not be the most efficient or best way but it's worked with all my tests, feel free to let me know if it doesn't work. So after you have made the changes in my above post, you will need to make the following changes too:
Step 1:
Change DrawToPDF function declaration fromCode:
internal void DrawToPDF(PdfRectangle rect, PdfPage elementPage, XFont font){ ... }
To
Code:
internal void DrawToPDF(PdfRectangle rect, PdfPage elementPage, XFont font, XStringFormat format){ ... }
Step 2:
Create a GetAlignment function like so:Code:
internal XStringFormat GetAlignment(DictionaryElements dict)
{
PdfItem item = dict.GetValue("/Q");
if (item != null)
{
int alignment = Int32.Parse(item.ToString());
switch (alignment)
{
case 0:
return XStringFormats.TopLeft;
case 1:
return XStringFormats.TopCenter;
case 2:
return XStringFormats.TopRight;
default:
return XStringFormats.TopLeft;
}
}
else
{
return XStringFormats.TopLeft;
}
}
Step 3:
Add your calls to the GetAlignment function and pass it to the DrawToPDF function.Code:
if (HasKids)
{
for(int i = 0; i < Fields.Elements.Count; i++)
{
var rect = Fields[i].Rectangle;
var page = Fields[i].Page;
var font = GetFontFromElement(Fields[i]);
XStringFormat format = GetAlignment(Fields[i].Elements); //Add Call to Get Alignment
DrawToPDF(rect, page, font, format); //Pass format to DrawToPDF
}
}
else
{
var rect = Rectangle;
var page = Page;
XStringFormat format = GetAlignment(Elements); //Add Call to Get Alignment
DrawToPDF(rect, page, Font, format); //Pass format to DrawToPDF
}
}
Step 4:
Remove this line from DrawToPDFCode:
var format = XStringFormats.TopLeft;
And modify the following two lines to use the new parameterCode:
gfx.DrawString(text, font, new XSolidBrush(ForeColor), xRect + new XPoint(0, 1.5), format); //Pass format Parameter to DrawString
Code:
gfx.DrawString(text, font, new XSolidBrush(ForeColor), xRect + new XPoint(2, 2), format); //Pass format Parameter to DrawString
Feel free to implement this is the PDFSharp Source!
RBrNx