日期:2014-05-17  浏览次数:21317 次

给pdf添加水印,当pdf里面有图片时会被遮住,求解
C# code
      #region 添加水印文字
        private void DoWaterMatkText(String waterMarkName, PdfContentByte under)
        {
            bool bRet = false;
            try
            {
                // 设置字体     
                // BaseFont basefont = BaseFont.CreateFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);//字体:宋体
                BaseFont basefont = BaseFont.CreateFont(@"c:/windows/fonts/SIMYOU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
                //透明度
                PdfGState gstate = new PdfGState();
                gstate.FillOpacity = (1f);
                gstate.StrokeOpacity = (1f);

                //水印文字长度
                int j = waterMarkName.Length;
                under.BeginText();
                //字体设计
                under.SetFontAndSize(basefont, 30);

                under.SetGState(gstate);
                //under.SetTextMatrix(
                under.SaveState();
                
               

                // 画一个圆  
                // under.ellipse(250, 450, 350, 550);  
                // under.setLineWidth(1f);  
                // under.stroke();  
                float mPageW = under.PdfDocument.Right + under.PdfDocument.RightMargin;
                float mPageH = under.PdfDocument.Top + under.PdfDocument.TopMargin;
                float rotation = 45f;
                float x = mPageW / 2;
                float y = mPageH / 2;
                under.SetRGBColorFillF(0.8f, 0.8f, 0.8f);

                if (j >= 25)
                {
                    //总行数
                    int a = j / 25 + 1;

                    for (int k = 0; k < a; k++)
                    {
                        if (k == 0)
                        {
                            under.ShowTextAligned(PdfContentByte.ALIGN_CENTER, waterMarkName.Substring(0, 24), x, y - 80, rotation);
                        }
                        else
                        {
                            string b = waterMarkName.Substring(j - (j - (24 * k)));
                            under.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, b, x, y - (80 + (50 * k)), rotation);
                        }
                    }
                }
                else
                {
                    under.ShowTextAligned(PdfContentByte.ALIGN_CENTER, waterMarkName, x, y - 80, rotation);
                }
                under.EndText();
                under.RestoreState();
                bRet = true;
            }
            catch (DocumentException de)
            {

            }
        }
        #endregion

代码如上,现有2个问题:
一:如果Pdf里面有图片的时候会把水印遮住
二: 我不想要水印斜着,该调整哪里

以上2个问题,有知道的希望能指导下小弟,不胜感激,在线等.呵呵

------解决方案--------------------
1.把水印放到最后放上去?
2.你那套生成pdf的类库该有设置的吧,查查api.