@it-yeq
Please review the information about font metrics. This is necessary to understand the proposed solution.
To align text to the top line of numeric characters it should to take into account ‘internal leading’ value. See: 1920px-Font_metrics.svg.png
Body height = Descent + Ascent Ascent = Cap Height + Internal Leading // Number characters glyphs have height close to the Cap Height value
Different font families has different ratio of ‘cap heigth’ to ‘body height’. It is about of 0.582 for the “Arial”. (Refer to the font documentation or special tools to get the value for other fonts.) The ratio is independent from font size. But measured values of ‘cap heigth’ and ‘internal leading’ depends from the font size.
Please consider the following code:
public static double GetFontMetricsInternalLeading(string fontName, int fontSize)
{
int emSize; // font family body height in design units
int descent; // font family descent in design units
double capHeightRatio = 0.582; // cap height ratio for Arial font. It may differ for other font families
double internalLeading; // internal leading in design units
double internalLeadingPoints; // internal leading converted to points
FontFamily fontFamily = new FontFamily(fontName);
emSize = fontFamily.GetEmHeight(System.Drawing.FontStyle.Regular);
descent = fontFamily.GetCellDescent(System.Drawing.FontStyle.Regular);
internalLeading = emSize - descent - emSize * capHeightRatio;
internalLeadingPoints = fontSize * internalLeading / emSize;
return internalLeadingPoints;
}
string fontName = "Arial";
Font vFont = Aspose.Pdf.Text.FontRepository.FindFont(fontName);
using (var vPDFDoc = new Aspose.Pdf.Document())
{
vPDFDoc.DisableFontLicenseVerifications = true;
vPDFDoc.PageLayout = Aspose.Pdf.PageLayout.SinglePage;
var vPageSize = new Aspose.Pdf.PageSize(Aspose.Pdf.PageSize.A3.Width, Aspose.Pdf.PageSize.A3.Height) { IsLandscape = true };
var vPDFPage = vPDFDoc.Pages.Add();
vPDFPage.SetPageSize(vPageSize.Width, vPageSize.Height);
vPDFPage.PageInfo.Margin = new Aspose.Pdf.MarginInfo(0, 0, 0, 0);
var vPageGrossRECT = vPDFPage.GetPageRect(true);
vPageGrossRECT = new Aspose.Pdf.Rectangle((vPageGrossRECT.Width * 15) / 1000,
(vPageGrossRECT.Height * 15) / 1000, (vPageGrossRECT.Width * 985) / 1000,
(vPageGrossRECT.Height * 985) / 1000);
var vPageTextBuilder = new Aspose.Pdf.Text.TextBuilder(vPDFPage);
var vCurrW = 0.0;
var vCurrH = 0.0;
var vText = "29";
var vFontSize = 163;
var vParagraphTEMP = GetTextParagraphModified(vText, vFont, vFontSize, vPageGrossRECT);
vCurrW = GetTextWidthFromParagraph(vParagraphTEMP);
vCurrH = GetTextHeightFromParagraph(vParagraphTEMP);
var subTextInternalLeading = GetFontMetricsInternalLeading(fontName, vFontSize);
var vCurrLLY = vPageGrossRECT.URY - vCurrH;
var vCurrLLX = vPageGrossRECT.URX - vCurrW;
vPageTextBuilder.AppendParagraph(GetTextParagraphModified(vText, vFont, vFontSize,
new Aspose.Pdf.Rectangle(vCurrLLX, vCurrLLY, vCurrLLX + vCurrW, vCurrLLY + vCurrH)));
vText = "5";
vFontSize = 280;
vParagraphTEMP = GetTextParagraphModified(vText, vFont, vFontSize, vPageGrossRECT);
vCurrW = GetTextWidthFromParagraph(vParagraphTEMP);
vCurrH = GetTextHeightFromParagraph(vParagraphTEMP);
var mainTextInternalLeading = GetFontMetricsInternalLeading(fontName, vFontSize);
vCurrLLY = vPageGrossRECT.URY - vCurrH + (mainTextInternalLeading - subTextInternalLeading);
vCurrLLX = vCurrLLX - vCurrW;
vPageTextBuilder.AppendParagraph(GetTextParagraphModified(vText, vFont, vFontSize,
new Aspose.Pdf.Rectangle(vCurrLLX, vCurrLLY, vCurrLLX + vCurrW, vCurrLLY + vCurrH)));
vText = "Netto";
vFontSize = 14;
vParagraphTEMP = GetTextParagraph(vText, vFont, vFontSize, vPageGrossRECT);
vCurrW = GetTextWidthFromParagraph(vParagraphTEMP);
vCurrH = GetTextHeightFromParagraph(vParagraphTEMP);
vCurrLLX = vPageGrossRECT.URX - vCurrW;
vPageTextBuilder.AppendParagraph(GetTextParagraph(vText, vFont, vFontSize,
new Aspose.Pdf.Rectangle(vCurrLLX, vCurrLLY, vCurrLLX + vCurrW, vCurrLLY + vCurrH)));
using (System.IO.MemoryStream vMStream = new System.IO.MemoryStream())
{
vPDFDoc.Save(vMStream);
vMStream.Position = 0;
System.IO.File.WriteAllBytes(System.IO.Path.Combine(dataDir, "50303_out.pdf"), vMStream.ToArray());
};
};
It produces expected result.
1920px-Font_metrics.svg.png (21.1 KB)
50303_out.pdf (74.3 KB)