pdf增長水印

/// <summary>
        /// 爲PDF添加水印或背景圖片
        /// </summary>
        /// <param name="strSourceFilePath">源文件路徑</param>
        /// <param name="strTargetFilePath">目標文件路徑</param>
        /// <param name="strWaterMarkPicPath">水印圖片路徑</param>
        /// <param name="strTargetFileTmpPath">目標臨時文件</param>
        /// <returns>添加結果</returns>
        public static bool AddWaterMarkForPdf(string strSourceFilePath, string strTargetFilePath, string strWaterMarkPicPath, FileInfo TargetFileTmp)
        {
            bool blnRt = false;
            PdfFileInfo pdf = new PdfFileInfo(strSourceFilePath);
            if (! pdf.BePdfFile)
            {
                throw  new ApplicationException("源文件不是有效的PDF文件");
            }
            //開始處理
            int intPageNums = pdf.NumberofPages;

            PdfFileStamp fileStamp = null;
            try
            {
                int intNumber = 650;
                int intCount = (int)Math.Ceiling(intPageNums / (decimal.Parse(intNumber.ToString())));
                for (int j = 0; j < intCount; j++)
                {
                    fileStamp = new PdfFileStamp(strSourceFilePath, strTargetFilePath);
                    for (int i = 1; i <= intNumber; i++)
                    {
                        if (j * intNumber + i > intPageNums)
                        {
                            break;
                        }
                        Stamp stamp = new Stamp();
                        if (!String.IsNullOrEmpty(strWaterMarkPicPath))
                        {
                            stamp.BindImage(strWaterMarkPicPath);
                        }
                        stamp.IsBackground = true;
                        stamp.PageNumber = j * intNumber + i;
                        stamp.SetImageSize(PageSize.A4.Height, PageSize.A4.Width);
                        fileStamp.AddStamp(stamp);
                    }
                    fileStamp.Close();
                    TargetFileTmp.Delete();
                    File.Copy(strTargetFilePath, TargetFileTmp.FullName);
                    strSourceFilePath = TargetFileTmp.FullName;
                }
                blnRt = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return blnRt;
        }
具體代碼

 注意,給PDF增長水晶報表,當頁碼過多時會報錯,或卡死。爲了防止這種狀況,咱們要分屢次對PDF進行增長水印。ide

如第一次加600頁,保存關閉當前文件,再拿加過水印的文件進行再追加水印,這樣這就不會了錯了。spa

相關文章
相關標籤/搜索