iT邦幫忙

0

[C#] 使用 iText 7 匯出 pdf 置中圖片效果無效?

以下是程式碼

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
using iText.Kernel.Pdf.Canvas.Draw;
using iText.IO.Image;

namespace GeneratePdfDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfWriter writer = new PdfWriter("D:\\demo.pdf");
            PdfDocument pdf = new PdfDocument(writer);
            Document document = new Document(pdf);
            Paragraph header = new Paragraph("HEADER")
                .SetTextAlignment(TextAlignment.CENTER)
                .SetFontSize(20);

            document.Add(header);

            Paragraph subheader = new Paragraph("SUB HEADER")
                .SetTextAlignment(TextAlignment.CENTER)
                .SetFontSize(15);
            document.Add(subheader);

            LineSeparator ls = new LineSeparator(new SolidLine());
            document.Add(ls);

            Paragraph paragraph1 = new Paragraph("Lorem ipsum " +
            "dolor sit amet, consectetur adipiscing elit, " +
            "sed do eiusmod tempor incididunt ut labore " +
            "et dolore magna aliqua.");
            document.Add(paragraph1);

            Image img = new Image(ImageDataFactory
                .Create(@"image.png"))
                .SetTextAlignment(TextAlignment.CENTER);
            document.Add(img);
            document.Close();
        }
    }
}

輸出結果圖:
https://ithelp.ithome.com.tw/upload/images/20200803/20117357IjpLarmgEL.png
請問為什麼pdf中的圖沒有置中?

wrxue iT邦研究生 3 級 ‧ 2020-08-03 16:17:29 檢舉
試到答案了,SetTextAlignment(TextAlignment.CENTER)改為SetHorizontalAlignment(HorizontalAlignment.CENTER)

尚未有邦友回答

立即登入回答