私は何年もの間、これに髪の毛を引っ張ってきたので、助けを求めようと思っていました...私のasp.net Webアプリを介してダウンロードされたときのドキュメント。
私の問題は、ユーザーが透かしのテキストを定義できる必要があるため、固定のテキストサイズを使用できず、pdf ページのサイズが変更される可能性があることです。
Arial と太字を使用していて、cm/mm/inch/pt でページ幅を見つけることができると仮定すると、入力されたテキストが拡大/縮小して幅を埋めるために必要なフォントの emSize を計算するにはどうすればよいですか?
PDFsharp XFont コンストラクターは、フォント名と emSize を取ります。
編集:提案してくれてありがとう、これが私が最終的に実装したものです:
PdfDocument doc = PdfReader.Open(stream, PdfDocumentOpenMode.Modify);
foreach (PdfPage page in doc.Pages)
{
double watermarkWidth = (Math.Sqrt(Math.Pow(page.Height.Point, 2) + Math.Pow(page.Width.Point, 2)));
//reduce by 10% so that the wording doesn't go right into the corners
watermarkWidth -= (watermarkWidth / 10);
XGraphics gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Prepend);
double emSize = 150;
XFont font = new XFont("Arial", emSize, XFontStyle.Bold);
XSize size = gfx.MeasureString(watermark, font);
while (size.Width > watermarkWidth && emSize > 10)
{
emSize -= 5;
font = new XFont("Arial", emSize, XFontStyle.Bold);
size = gfx.MeasureString(watermark, font);
}
gfx.TranslateTransform(page.Width / 2, page.Height / 2);
gfx.RotateTransform(-Math.Atan(page.Height / page.Width) * 180 / Math.PI);
gfx.TranslateTransform(-page.Width / 2, -page.Height / 2);
XGraphicsPath path = new XGraphicsPath();
path.AddString(watermark, font.FontFamily, XFontStyle.Bold, emSize,
new XPoint((page.Width - size.Width) / 2, (page.Height - size.Height) / 2),
XStringFormats.Default);
XPen pen = new XPen(XColor.FromArgb(75, 255, 0, 0), 2);
gfx.DrawPath(pen, path);
}
doc.Save(stream, false);