[.Net C#]利用iTextSharp在PDF上新增浮水印(WaterMark)

public void Watermark(string inputPath, string outputPath, string watermarkPath)        
{  
    try
    {  
        PdfReader reader = new PdfReader(inputPath);
        iTextSharp.text.Document document = new iTextSharp.text.Document();
        PdfWriter writer = PdfWriter.GetInstance(document,new FileStream(outputPath, FileMode.Create));   
        Watermark watermark = new Watermark(iTextSharp.text.Image.GetInstance(watermarkPath), 255, 785);

        document.Add(watermark);
        document.Open();
        PdfContentByte cb = writer.DirectContent;   
        PdfImportedPage newPage; 
        int iPageNum = reader.NumberOfPages;   
        for(int j=1; j<= iPageNum;j++)    
        { 
            document.NewPage();   
            newPage = writer.GetImportedPage(reader,j);   
            cb.AddTemplate(newPage,0,0); 
        }    
        document.Close();   
        writer.Close();
        reader.Close();
    }  
    catch (Exception ex)  
    {
        WriteLog.Log(ex.ToString());
        throw ex;
    }
}


转自(http://www.dotblogs.com.tw/kiwifruit0612/archive/2009/08/17/10103.aspx)

你可能感兴趣的:(.net,exception,String,C#)