PDF按模板出力,多个PDF合并
| const string TEMP_PREXFIX = "Temp_"; | ||||||||||||||||
| string _tempfilename01; | ||||||||||||||||
| PdfReader _pdfReader = null; | ||||||||||||||||
| BaseFont _baseFont = null; | ||||||||||||||||
| PdfStamper _pdfStamper = null; | ||||||||||||||||
| string _sourcefilename01 = ".\\PDF\\faxcre01.pdf"; | ||||||||||||||||
| string _targetfilename01 = ".\\PDF\\faxcre01_生成.pdf"; | ||||||||||||||||
| string fileName01 = Path.GetFileName(_targetfilename01); | ||||||||||||||||
| string path = Path.GetDirectoryName(_targetfilename01); | ||||||||||||||||
| _tempfilename01 = path + @"\TEMP\" + TEMP_PREXFIX + fileName01; | ||||||||||||||||
| _baseFont = BaseFont.CreateFont(@"c:\windows\fonts\msmincho.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); | ||||||||||||||||
| BaseFont bfTimes = BaseFont.CreateFont(@"c:\windows\fonts\msmincho.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); | ||||||||||||||||
| iTextSharp.text.Font times = new Font(bfTimes, 12, iTextSharp.text.Font.BOLD, iTextSharp.text.BaseColor.RED); | ||||||||||||||||
| Font palatino = FontFactory.GetFont(@"c:\windows\fonts\msmincho.ttc,0", BaseFont.CP1252, BaseFont.EMBEDDED, 10, Font.ITALIC, BaseColor.GREEN); | ||||||||||||||||
| //--------------------template1-------------------- | ||||||||||||||||
| FileStream ReadPdf = new FileStream(_tempfilename01, FileMode.Create); | ||||||||||||||||
| //pdf reader | ||||||||||||||||
| _pdfReader = new PdfReader(_sourcefilename01); | ||||||||||||||||
| _pdfStamper = new PdfStamper(_pdfReader, ReadPdf); | ||||||||||||||||
| _pdfStamper.AcroFields.AddSubstitutionFont(_baseFont); | ||||||||||||||||
| ////set field: MSゴシック | ||||||||||||||||
| //_pdfStamper.AcroFields.SetFieldProperty("f_print_date", "textfont", _baseFont, null); | ||||||||||||||||
| //_pdfStamper.AcroFields.SetFieldProperty("f_print_time", "textfont", _baseFont, null); | ||||||||||||||||
| //_pdfStamper.AcroFields.SetFieldProperty("f_jusyo_kana", "textfont", _baseFont, null); | ||||||||||||||||
| //_pdfStamper.AcroFields.SetFieldProperty("f_hoken_syurui", "textfont", _baseFont, null); | ||||||||||||||||
| //_pdfStamper.AcroFields.SetFieldProperty("f_sfx_info1", "textfont", _baseFont, null); | ||||||||||||||||
| //_pdfStamper.AcroFields.SetFieldProperty("f_sfx_info2", "textfont", _baseFont, null); | ||||||||||||||||
| //Font font = FontFactory.GetFont(FontFactory.COURIER, 8f, iTextSharp.text.Font.BOLD); | ||||||||||||||||
| ////_pdfStamper.AcroFields.SetFieldProperty("f_sfx_info2", "textfont", font.BaseFont, null); | ||||||||||||||||
| //_pdfStamper.AcroFields.SetFieldProperty("f_sfx_info2", "textfont", times.ToString(), null); | ||||||||||||||||
| Font font = new Font(_baseFont, 8f, iTextSharp.text.Font.BOLD); | ||||||||||||||||
| FontFactory.Register(@"c:\windows\fonts\msmincho.ttc,0", "myFont"); | ||||||||||||||||
| //FontFactory.GetFont(FontFactory.GetFont("myFont").ToString(), 28,iTextSharp.text.BaseColor.RED); | ||||||||||||||||
| Font myFont = FontFactory.GetFont("myFont"); | ||||||||||||||||
| //Font myFont = FontFactory.GetFont("myFont", 56, Font.BOLD); | ||||||||||||||||
| BaseFont bf = myFont.BaseFont; | ||||||||||||||||
| _pdfStamper.AcroFields.SetFieldProperty("f_print_date", "textfont", bf, null); | ||||||||||||||||
| _pdfStamper.AcroFields.SetFieldProperty("f_print_time", "textfont", bf, null); | ||||||||||||||||
| _pdfStamper.AcroFields.SetFieldProperty("f_jusyo_kana", "textfont", bf, null); | ||||||||||||||||
| _pdfStamper.AcroFields.SetFieldProperty("f_hoken_syurui", "textfont", bf, null); | ||||||||||||||||
| _pdfStamper.AcroFields.SetFieldProperty("f_sfx_info1", "textfont", bf, null); | ||||||||||||||||
| _pdfStamper.AcroFields.SetFieldProperty("f_sfx_info2", "textfont", _baseFont, null); | ||||||||||||||||
| _pdfStamper.AcroFields.SetField("f_print_date", "2016/7/07"); | ||||||||||||||||
| _pdfStamper.AcroFields.SetField("f_print_time", "13:21"); | ||||||||||||||||
| _pdfStamper.AcroFields.SetField("f_jikoseiribango", "1332658-9"); | ||||||||||||||||
| _pdfStamper.AcroFields.SetField("f_karino", "040A0001664"); | ||||||||||||||||
| //編集不可設定 | ||||||||||||||||
| _pdfStamper.FormFlattening = true; | ||||||||||||||||
| _pdfStamper.Close(); | ||||||||||||||||
| _pdfReader.Close(); | ||||||||||||||||
| ReadPdf.Close(); | ||||||||||||||||
| // 合成pdf文件 | ||||||||||||||||
| string _targetfilename = "faxcre_000.pdf"; | ||||||||||||||||
| string _tempfilename = path + @"\TEMP\" + TEMP_PREXFIX + _targetfilename; | ||||||||||||||||
| PdfReader reader01; | ||||||||||||||||
| Document document = new Document(); | ||||||||||||||||
| PdfSmartCopy copy = new PdfSmartCopy(document, new FileStream(_tempfilename, FileMode.Create)); | ||||||||||||||||
| document.Open(); | ||||||||||||||||
| reader01 = new PdfReader(_tempfilename01); | ||||||||||||||||
| copy.AddDocument(reader01); | ||||||||||||||||
| reader01.Close(); | ||||||||||||||||
| if (File.Exists(_tempfilename01)) | ||||||||||||||||
| { | ||||||||||||||||
| File.Delete(_tempfilename01); | ||||||||||||||||
| } | ||||||||||||||||
| _pdfStamper.Dispose(); | ||||||||||||||||
| _pdfReader.Dispose(); | ||||||||||||||||
| ReadPdf.Dispose(); | ||||||||||||||||
| document.Close(); | ||||||||||||||||
弄丢的小可爱🌸

浙公网安备 33010602011771号