C# 讀取EXCEL的數據批量插入單個PDF里的多個位置
using System;
using System. Collections. Generic;
using System. ComponentModel;
using System. Data;
using System. Diagnostics;
using System. Drawing;
using System. IO;
using System. Linq;
using System. Reflection;
using System. Text;
using System. Threading. Tasks;
using System. Windows. Forms;
using iTextSharp. text. pdf;
using iTextSharp. text;
using Font = iTextSharp. text. Font;
using Microsoft. Office. Interop. Excel;
using Application = Microsoft. Office. Interop. Excel. Application;
using Excel = Microsoft. Office. Interop. Excel;
using Rectangle = iTextSharp. text. Rectangle; namespace EXCEL批量插入PDF指定位置
{ public partial class Form1 : Form{ InsertPdf insertPdf = new InsertPdf( ) ; List< InsertPdf> pudfPathList = new List< InsertPdf> ( ) ; public Form1( ) { InitializeComponent( ) ; } private void btn_chose_Click( object sender, EventArgs e) { string OpenFilePath; // 存儲選擇到的文件的完整路徑OpenFileDialog ofd = new OpenFileDialog( ) ; ofd. Title = "選擇Excel文件" ; // 設置對話框標題欄的內容ofd. Filter = "Excel文件(*.xls*)|*.xls*;" ; // 這是設置對話框內顯示的指定后綴類型文件( 可設置多個) if ( ofd. ShowDialog( ) == DialogResult. OK) { OpenFilePath = ofd. FileName; input . Text = OpenFilePath; } else { return ; } OPENEXCEL( ) ; } // / < summary> // / 打開Excel文件// / < / summary> // / < param name= "sender" > < / param> // / < param name= "e" > < / param> private void OPENEXCEL( ) { if ( input . Text!= "" ) { string excelPath = input . Text; object missing= Missing. Value; Excel. Application excel = new Excel. Application( ) ; // 創建Excel對象try { if ( excel == null) { MessageBox. Show( "Excel對象為空,請檢查是否已安裝Excel" ) ; return ; } else { excel. Visible = false; // 顯示Excelexcel. DisplayAlerts = false; // 不顯示警告信息excel. UserControl = false; // 不顯示用戶界面excel. ScreenUpdating = false; // 屏幕刷新Excel. Workbook wb = excel. Application. Workbooks. Open( excelPath, missing, true, missing, missing, missing, missing, missing, missing, true, missing, missing, missing, missing, missing) ; // 打開Excel文件Excel. Worksheet ws = ( Excel. Worksheet) wb. Worksheets[ 1 ] ; // 獲取當前工作表int rowCount = ws. UsedRange. Rows. Count; // 獲取當前工作表的行數int colCount = ws. UsedRange. Columns. Count; // 獲取當前工作表的列數dataGridView1. Rows. Clear( ) ; dataGridView1. Columns. Clear( ) ; for ( int i = 1 ; i <= colCount; i+ + ) { if ( ws. Cells[ 1 , i] . Value2 == null) { break ; } string colName = ws. Cells[ 1 , i] . Value2. ToString( ) . Trim( ) ; dataGridView1. Columns. Add( "column" + i, colName) ; } for ( int i = 2 ; i <= rowCount; i+ + ) { int index = dataGridView1. Rows. Add( ) ; if ( ws. Cells[ i, 1 ] . Value2 != null) { for ( int j = 1 ; j <= colCount; j+ + ) { if ( ws. Cells[ i, j] . Value2 == null) { continue ; } dataGridView1. Rows[ index] . Cells[ j - 1 ] . Value = ws. Cells[ i, j] . Value2. ToString( ) . Trim( ) ; } } } } } catch ( Exception ex) { MessageBox. Show( "打開Excel文件失敗!" + ex. Message) ; // throw; } finally { CloseExcel( excel) ; MessageBox. Show( "加載成功!" ) ; } } } private void CloseExcel( Application excel) { excel. Quit( ) ; excel = null; Process[ ] excels = Process. GetProcessesByName( "excel" ) ; foreach ( Process p in excels) { p. Kill( ) ; // 殺掉excel進程} GC. Collect( ) ; } // / < summary> // / 處理單個頁面填寫多個地方的PDF// / < / summary> // / < param name= "pdfPath" > PDF文件路徑< / param> // / < param name= "pdfsavePath" > 存PDF文件路徑< / param> // / < param name= "xnum" > 填寫字的坐標x< / param> // / < param name= "ynum" > 填寫字的坐標y< / param> // / < param name= "fontnum" > 字體大小< / param> // / < param name= "textstr" > 要填入的文字< / param> // / private void PDFadd( string pdfPath, string pdfsavePath, InsertPdf insertPdf) { if ( dataGridView1. Rows. Count > 0 ) { for ( int i = 0 ; i < dataGridView1. Rows. Count; i+ + ) { insertPdf. xnum = Convert. ToInt32( dataGridView1. Rows[ i] . Cells[ 0 ] . Value) ; insertPdf. ynum = Convert. ToInt32( dataGridView1. Rows[ i] . Cells[ 1 ] . Value) ; insertPdf. fontnum = Convert. ToInt32( dataGridView1. Rows[ i] . Cells[ 2 ] . Value) ; insertPdf. textstr = dataGridView1. Rows[ i] . Cells[ 3 ] . Value. ToString( ) ; pudfPathList. Add( insertPdf) ; } } // 原文件地址string url = @pdfPath; // 最后要保存的文件地址string urlNew= @pdfsavePath; // 讀取原pdf文件PdfReader reader = new PdfReader( url) ; PdfStamper stamper = new PdfStamper( reader, new FileStream( urlNew, FileMode. Create, FileAccess. Write, FileShare. None ) ) ; setOnePage( pudfPathList, reader, stamper) ; } // / < summary> // / 處理單個頁面多個填寫的地方// / < / summary> // / < param name= "pdfPathList" > < / param> private void setOnePage( List< InsertPdf> pdfPathList, PdfReader reader, PdfStamper stamper) { if ( pdfPathList. Count == 1 ) { insertPdf = pdfPathList[ 1 ] ; // 獲取系統的字體BaseFont baseFont = BaseFont. CreateFont( "C:\\Windows\\Fonts\\simhei.ttf" , BaseFont. IDENTITY_H, BaseFont. NOT_EMBEDDED) ; // 字體屬性及大小Font font = new Font( baseFont, insertPdf. fontnum) ; // 需要添加的文字Phrase addName = new Phrase( insertPdf. textstr, font) ; // 獲取pdf總頁數int pagesCount = reader. NumberOfPages; // 獲取pdf頁面大小Rectangle pageSize = reader. GetPageSize( 1 ) ; // 設置pdf的寬float width = pageSize. Width; // 設置pdf的高float height = pageSize. Height; // 遍歷所有頁,從第一頁開始for ( int i = 1 ; i <= pagesCount; i+ + ) // 遍歷所有頁{ if ( i == 1 ) { // 第一頁// 設置當前頁PdfContentByte canvas = stamper. GetOverContent( i) ; // 將文本添加到每頁pdf的右上角ColumnText. ShowTextAligned( canvas, Element. ALIGN_RIGHT, addName, insertPdf. xnum, insertPdf. ynum, 0 ) ; // 900 , 435 , x, y} } // 釋放stamper. Close( ) ; MessageBox. Show( "添加成功" ) ; } else if ( pdfPathList. Count > 1 ) { // 獲取系統的字體BaseFont baseFont = BaseFont. CreateFont( "C:\\Windows\\Fonts\\simhei.ttf" , BaseFont. IDENTITY_H, BaseFont. NOT_EMBEDDED) ; // 獲取pdf總頁數int pagesCount = reader. NumberOfPages; // 獲取pdf頁面大小Rectangle pageSize = reader. GetPageSize( 1 ) ; // 設置pdf的寬float width = pageSize. Width; // 設置pdf的高float height = pageSize. Height; PdfContentByte canvas = stamper. GetOverContent( pagesCount) ; for ( int i = 0 ; i < pdfPathList. Count; i+ + ) { insertPdf = pdfPathList[ i] ; // 字體屬性及大小Font font = new Font( baseFont, insertPdf. fontnum) ; // 需要添加的文字Phrase addName = new Phrase( insertPdf. textstr, font) ; ColumnText. ShowTextAligned( canvas, Element. ALIGN_RIGHT, addName, insertPdf. xnum, insertPdf. ynum, 0 ) ; } stamper. Close( ) ; MessageBox. Show( "添加成功" ) ; } } } }