通過 iTextSharp 實現PDF 審核蓋章

來源:互聯網
上載者:User

最近需要做一個PDF自動審核蓋章的工作,其實就是讀取PDF,然後再最後一頁加入一個審核章印圖 片上去。看起來很簡單,不過在開發過程中,還是遇到了一些問題,在這裡記錄一下。

主要遇到的問題是頁面的旋轉 和 內容的旋轉 的分開的,需要分別操作。

準備工作需要下載 iTextSharp.dll 然後加入引用

using iTextSharp.text;

using iTextSharp.text.pdf;

string path = @"D:\28727_LOG001_FOLIOLE COMPANY LIMITED_STOCK_PI";                // 建立一個PdfReader對象                PdfReader reader = new PdfReader(path + ".pdf");                    // 獲得文檔頁數                int n = reader.NumberOfPages;                // 獲得第一頁的大小                Rectangle psize = reader.GetPageSize(1);                    float width = psize.Width;                float height = psize.Height;                    // 建立一個文檔變數                Document document = new Document(psize);                    // 建立該文檔 產生物理檔案                PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(path + "_APPROVE.pdf", FileMode.OpenOrCreate));                    // 開啟文檔                document.Open();                    // 新增內容                PdfContentByte cb = writer.DirectContent;                    for (int i = 0; i < n; )                {                        i++;                    //設定指定頁的PagSize 包含Rotation(頁面旋轉度)                    document.SetPageSize(reader.GetPageSizeWithRotation(i));                        //建立一個新的頁面,需要注意的調用NewPage() ,PdfContentByte cb 對象會預設清空                    document.NewPage();                        //擷取指定頁面的旋轉度                    int rotation = reader.GetPageRotation(i);                        //擷取載入PDF的指定頁內容                    PdfImportedPage page1 = writer.GetImportedPage(reader, i);                        //新增內容頁到新的頁面,並更加旋轉度設定對應的旋轉                    switch (rotation)                    {                        case 90:                            cb.AddTemplate(page1, 0, -1, 1, 0, 0, reader.GetPageSizeWithRotation(i).Height);                            break;                        case 180:                            cb.AddTemplate(page1, -1, 0, 0, -1, reader.GetPageSizeWithRotation(i).Width, reader.GetPageSizeWithRotation(i).Height);                           break;                        case 270:                            cb.AddTemplate(page1, 0, 1, -1, 0, reader.GetPageSizeWithRotation(i).Width, 0);                            break;                        default:                            cb.AddTemplate(page1, 1, 0, 0, 1, 0, 0);//等同於 cb.AddTemplate(page1, 0,0)                            break;                    }                        if (i == n)//如果是最後一頁加入指定的圖片                    {               //不同旋轉度的頁面 圖片位置left距離的調整                       int imgLeft = 350;               if(rotation==90 || rotation==270)               {    imgLeft = 550;    }                             //建立一個圖片對象                                            iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(new Uri(@"d:\Lock-icon.png"));                            //設定圖片的指定大小                        //img.ScaleToFit(140F, 320F);                            //按比例縮放                        //img.ScalePercent(50);                                       //把圖片增加到內容頁的指定位子  b width c height  e bottom f left                        cb.AddImage(img, 0, 32F, 32F, 0, 50F, imgLeft);                            //開始增加文本                        cb.BeginText();                            BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA_OBLIQUE, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);                        //設定字型 大小                        cb.SetFontAndSize(bf, 9);                            //指定添加文字的絕對位置                        cb.SetTextMatrix(imgLeft, 200);                        //增加文本                        cb.ShowText("GW INDUSTRIAL LTD");                            //結束                        cb.EndText();                                               }                                                                          }                // 關閉文檔                    document.Close();                                }            catch (Exception de)            {                Console.Error.WriteLine(de.Message);                Console.Error.WriteLine(de.StackTrace);            }

查看本欄目更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/csharp/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.