找了兩三篇文章再加一點修正才組出來的, 記錄一下, 要用 iTextSharp 哦.
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
---
string oldFile = Server.MapPath("/Content/PDF/300000419_20160929162658862.pdf"); //"oldFile.pdf";
string newFile = oldFile.Replace(".pdf", "_New11.pdf");
// open the reader
PdfReader reader = new PdfReader(oldFile);
Rectangle size = reader.GetPageSizeWithRotation(1);
Document document = new Document(size);
int NumberOfPages = reader.NumberOfPages;
// open the writer
FileStream fs = new FileStream(newFile, FileMode.Create, FileAccess.Write);
PdfWriter writer = PdfWriter.GetInstance(document, fs);
document.Open();
// the pdf content
PdfContentByte cb = writer.DirectContent;
string text = "Watermark...";
string windir = Environment.GetEnvironmentVariable("windir");
Chunk textAsChunk = new Chunk(text, new Font(BaseFont.CreateFont(windir + "\\Fonts\\mingliu.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED), 20, Font.NORMAL, new BaseColor(255,0,0)));
// create the new page and add it to the pdf
for (int i = 1; i<= NumberOfPages; i++)
{
if(i > 1)
{
document.NewPage();
}
ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT, new Phrase(textAsChunk), 0, 0, 0);
PdfImportedPage page = writer.GetImportedPage(reader, i);
cb.AddTemplate(page, 0, 0);
}
// close the streams and voilá the file should be changed :)
document.Close();
fs.Close();
writer.Close();
reader.Close();
Response.Write(newFile + "<br>");
Response.Write(NumberOfPages + "<br>");