1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

pdfsharpcore

Posted at
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;
        }
    }
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?