iT邦幫忙

0

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

  • 分享至 

  • xImage

以下是程式碼

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邦好手 1 級 ‧ 2020-08-03 16:17:29 檢舉
試到答案了,SetTextAlignment(TextAlignment.CENTER)改為SetHorizontalAlignment(HorizontalAlignment.CENTER)
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答