iT邦幫忙

0

請問C#怎樣用iTextSharp產出pdf時有頁碼

  • 分享至 

  • xImage

最近在做網頁的案子,現在有一個需求是產出pdf檔要有頁碼,我的程式語言為C#,使用iTextSharp產出pdf,而我做的案子在產出pdf時並不是使用置換舊檔案的方式做的,是把要的東西新建一個pdf檔,目前我在網路上找的方法是讀取pdf檔案,然後增加頁碼,程式如下:

 public static void AddPageNumber(string fileIn, string fileOut)
        {
            byte[] bytes = System.IO.File.ReadAllBytes(fileIn);
            iTextSharp.text.Font blackFont = FontFactory.GetFont("Arial", 10, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);
            using (MemoryStream stream = new MemoryStream())
            {
                PdfReader reader = new PdfReader(bytes);
                using (PdfStamper stamper = new PdfStamper(reader, stream))
                {
                    int pages = reader.NumberOfPages;
                    for (int i = 1; i <= pages; i++)
                    {
                        ColumnText.ShowTextAligned(stamper.GetUnderContent(i), Element.ALIGN_RIGHT, new Phrase(i.ToString(), blackFont), 568f, 15f, 0);
                    }
                }
                bytes = stream.ToArray();
            }
            System.IO.File.WriteAllBytes(fileOut, bytes);
        }

我現在是做MVC網頁,有自己做一個測試的產生pdf程式,程式碼如下:

[HttpPost]
        public ActionResult ExportFreeSignup(int? FSID)
        {
           
            using (MemoryStream stream = new MemoryStream())
            {
               
                Document doc = new Document(PageSize.A4, 10, 10, 10, 10);

                PdfWriter writer = PdfWriter.GetInstance(doc, stream);
                doc.Open();                
                doc.Add(new Paragraph(14, "Good morning!"));

                doc.Close();
                stream.ToArray();
                Response.Clear();
                string filename = Server.UrlPathEncode("申請表.pdf");
                
                Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
                Response.ContentType = "application/octet-stream";

                Response.OutputStream.Write(stream.GetBuffer(), 0, stream.GetBuffer().Length);
                Response.OutputStream.Flush();
                Response.OutputStream.Close();
                Response.Flush();
                Response.End();
            }
            return View();
        }

現在的問題就是我在想這2個怎麼合併在一起,從PdfReader reader = new PdfReader(bytes); 這裡要怎麼改?請問要怎麼做比較好呢?或是有沒有更好的方法可以讓pdf檔產生頁碼呢?謝謝

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
johncoc
iT邦新手 3 級 ‧ 2022-11-29 17:11:07
最佳解答

用console測試有產生頁碼

            using (var ms1 = new MemoryStream())
            {
                Document doc = new Document(PageSize.A4, 10, 10, 10, 10);

                PdfWriter writer = PdfWriter.GetInstance(doc, ms1);
                doc.Open();
                doc.Add(new Paragraph(14, "Good morning!"));

                doc.Close();
                iTextSharp.text.Font blackFont = FontFactory.GetFont("Arial", 10, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);
                using (var ms2 = new MemoryStream())
                {
                    PdfReader reader = new PdfReader(ms1.ToArray());
                    using (PdfStamper stamper = new PdfStamper(reader, ms2))
                    {
                        int pages = reader.NumberOfPages;
                        for (int i = 1; i <= pages; i++)
                        {
                            ColumnText.ShowTextAligned(stamper.GetUnderContent(i), Element.ALIGN_RIGHT, new Phrase(i.ToString(), blackFont), 568f, 15f, 0);
                        }
                    }
                    System.IO.File.WriteAllBytes("Test.pdf", ms2.ToArray());
                }
            }

把你的程式照上述修改

[HttpPost]
        public ActionResult ExportFreeSignup(int? FSID)
        {
           
            using (MemoryStream stream = new MemoryStream())
            {
               
                Document doc = new Document(PageSize.A4, 10, 10, 10, 10);

                PdfWriter writer = PdfWriter.GetInstance(doc, stream);
                doc.Open();                
                doc.Add(new Paragraph(14, "Good morning!"));

                doc.Close();
                iTextSharp.text.Font blackFont = FontFactory.GetFont("Arial", 10, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);                
                using (var ms2 = new MemoryStream())
                {
                    PdfReader reader = new PdfReader(stream.ToArray());
                    using (PdfStamper stamper = new PdfStamper(reader, ms2))
                    {
                        int pages = reader.NumberOfPages;
                        for (int i = 1; i <= pages; i++)
                        {
                            ColumnText.ShowTextAligned(stamper.GetUnderContent(i), Element.ALIGN_RIGHT, new Phrase(i.ToString(), blackFont), 568f, 15f, 0);
                        }
                    }
                    ms2.ToArray();
                    Response.Clear();
                    string filename = Server.UrlPathEncode("申請表.pdf");
                
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
                    Response.ContentType = "application/octet-stream";

                    Response.OutputStream.Write(ms2.GetBuffer(), 0, ms2.GetBuffer().Length);
                    Response.OutputStream.Flush();
                    Response.OutputStream.Close();
                    Response.Flush();
                    Response.End();                    
                }
            }
            return View();
        }
baltic iT邦新手 4 級 ‧ 2022-11-29 18:00:53 檢舉

謝謝你的回覆,確實有加上頁碼

我要發表回答

立即登入回答