Last active
July 29, 2019 07:26
-
-
Save edward-hsu-1994/2377691f8353ed3dd6f08299e1e839df to your computer and use it in GitHub Desktop.
PDF套版
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using iTextSharp.text; | |
using iTextSharp.text.pdf; | |
using System; | |
using System.Collections; | |
using System.IO; | |
namespace PDFForm { | |
class Program { | |
static void Main(string[] args) { | |
Stream pdfStream = new FileStream(path: "01.pdf", | |
mode: FileMode.Open); | |
PdfReader pdfReader = new PdfReader(pdfStream); | |
PdfStamper pdfStamper = new PdfStamper(pdfReader, File.Create($"01-out-{DateTime.Now.Ticks}.pdf")); | |
// 標楷體 | |
BaseFont chBaseFont = BaseFont.CreateFont(@"C:\windows\fonts\kaiu.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); | |
AcroFields acroFields = pdfStamper.AcroFields; | |
// 設定字體 | |
acroFields.SetFieldProperty("no", "textfont", chBaseFont, null); | |
acroFields.SetFieldProperty("time", "textfont", chBaseFont, null); | |
acroFields.SetFieldProperty("name", "textfont", chBaseFont, null); | |
acroFields.SetFieldProperty("score", "textfont", chBaseFont, null); | |
// 設定欄位值 | |
acroFields.SetField("no", "001"); | |
acroFields.SetField("time", "108年7月29日"); | |
acroFields.SetField("name", "王小明"); | |
acroFields.SetField("score", "100"); | |
pdfStamper.SetFullCompression(); // 壓縮 | |
pdfStamper.FormFlattening = true; // 平面化 | |
pdfStream.Close(); | |
pdfStamper.Close(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment