最近在做網頁的案子,現在有一個需求是產出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檔產生頁碼呢?謝謝
用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();
}