using PdfSharpCore.Drawing;
class Program
{
static void Main(string[] args)
{
// PDFドキュメントを作成
PdfDocument document = new PdfDocument();
PdfPage page = document.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(page);
// フォントとテキストを指定
XFont font = new XFont("Arial", 12);
string text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
// テキストの描画
XRect rect = new XRect(100, 100, 400, 200); // 描画領域
gfx.DrawString(text, font, XBrushes.Black, rect, XStringFormats.TopLeft);
// 両端揃えのためのカスタムロジックを実装
rect = new XRect(100, 300, 400, 200); // 描画領域
DrawJustifiedText(gfx, text, font, XBrushes.Black, rect);
// PDFファイルに保存
document.Save("output.pdf");
}
static void DrawJustifiedText(XGraphics gfx, string text, XFont font, XBrush brush, XRect rect)
{
string[] words = text.Split(' ');
float totalWidth = gfx.MeasureString(text, font).Width;
float spaceWidth = (rect.Width - totalWidth) / (words.Length - 1);
float x = rect.X;
float y = rect.Y;
foreach (string word in words)
{
gfx.DrawString(word, font, brush, new XPoint(x, y));
x += gfx.MeasureString(word, font).Width + spaceWidth;
}
}
}
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme