情况如下:有一份PDF文件使用itext加水印,共五页,需要在每页的右上角位置添加水印,第1、2、4、5页加水印正确,第3页没有加到水印,纠结中,后来我分析了下,第3页有可能是一张图片,那么如何获取到每页的尺寸?或者是如何满足每页右上角位置有水印?代码如下:
private string AddInfo2PDF(string sourceFilePath, string targetFilePath, string signLocation, string engineerSignImagePath, string fileNum, string fileRev, string fileContent, string rootModel)
{
string message = "";
try
{
PdfReader reader = new PdfReader(sourceFilePath);
int pageTotalCount = reader.NumberOfPages;
PdfStamper stamp = new PdfStamper(reader, new FileStream(targetFilePath, FileMode.Create));
int i = 0;
PdfContentByte under;
iTextSharp.text.Image mm;
float PDfWidth = 595.22F;
float PDFHeight = 842.0F;
int xpos = 0;
int ypos = 0;
List<string> arrayWaterList = new List<string>();
//創建圖片
arrayWaterList = CreateWaterList(pageTotalCount, engineerSignImagePath, fileNum, fileRev, fileContent, rootModel);
while (i < pageTotalCount)
{
i += 1;
mm = iTextSharp.text.Image.GetInstance(arrayWaterList[i - 1]);
iTextSharp.text.Rectangle psize = reader.GetPageSizeWithRotation(i);
PDfWidth = psize.Width;
PDFHeight = psize.Height;
switch (signLocation)
{
case "左上":
xpos = (int)(PDfWidth * (float)0.01);
ypos = (int)(PDFHeight * (float)0.99 - mm.Height);
if (PDfWidth > 1000)//橫排排版
{
mm.SetAbsolutePosition(xpos, ypos);
mm.ScaleAbsolute(mm.Width, mm.Height);
}
else//豎排排版
{
mm.SetAbsolutePosition(xpos, ypos);
mm.ScaleAbsolute((float)(mm.Width / 1.5), (float)(mm.Height / 1.5));
}
break;
case "中上":
xpos = (int)(PDfWidth * (float)0.5 - mm.Width / 2);
ypos = (int)(PDFHeight * (float)0.99 - mm.Height);
if (PDfWidth > 1000)//橫排排版
{
mm.SetAbsolutePosition(xpos, ypos);
mm.ScaleAbsolute(mm.Width, mm.Height);
}
else//豎排排版
{
mm.SetAbsolutePosition(xpos + 50, ypos);
mm.ScaleAbsolute((float)(mm.Width / 1.5), (float)(mm.Height / 1.5));
}
break;
case "右上":
//xpos = (int)(PDfWidth * (float)0.99 - mm.Width);
//ypos = (int)(PDFHeight * (float)0.99 - mm.Height);
//if (PDfWidth > 1000)//橫排排版
//{
// if (PDfWidth == 1224 && PDFHeight == 792)
// {
// xpos = (int)(PDfWidth * (float)0.6 - mm.Width / 10);
// ypos = (int)(PDFHeight * (float)0.6 - mm.Height / 10);
// mm.SetAbsolutePosition(xpos, ypos);
// mm.ScaleAbsolute((float)(mm.Width / 2), (float)(mm.Height / 2));
// }
// else
// {
// mm.SetAbsolutePosition(xpos, ypos);
// mm.ScaleAbsolute(mm.Width, mm.Height);
// }
//}
//else//豎排排版
//{
// mm.SetAbsolutePosition(xpos + 130, ypos + 25);
// mm.ScaleAbsolute((float)(mm.Width / 1.5), (float)(mm.Height / 1.5));
//}
float mmWidth = mm.Width / (float)1.5;
float mmHeight = mm.Height / (float)1.5;
xpos = (int)(PDfWidth - mmWidth - (float)20);
ypos = (int)(PDFHeight - mmHeight - (float)20);
mm.SetAbsolutePosition(xpos, ypos);
mm.ScaleAbsolute(mmWidth, mmHeight);
break;
case "左中":
xpos = (int)(PDfWidth * (float)0.01);
ypos = (int)(PDFHeight * (float)0.5 - mm.Height / 2);
if (PDfWidth > 1000)//橫排排版
{
mm.SetAbsolutePosition(xpos, ypos);
mm.ScaleAbsolute(mm.Width, mm.Height);
}
else//豎排排版
{
mm.SetAbsolutePosition(xpos, ypos);
mm.ScaleAbsolute((float)(mm.Width / 1.5), (float)(mm.Height / 1.5));
}
break;
case "中":
xpos = (int)(PDfWidth * (float)0.5 - mm.Width / 2);
ypos = (int)(PDFHeight * (float)0.5 - mm.Height / 2);
if (PDfWidth > 1000)//橫排排版
{
mm.SetAbsolutePosition(xpos, ypos);
mm.ScaleAbsolute(mm.Width, mm.Height);
}
else//豎排排版
{
mm.SetAbsolutePosition(xpos + 50, ypos);
mm.ScaleAbsolute((float)(mm.Width / 1.5), (float)(mm.Height / 1.5));
}
break;
case "右中":
xpos = (int)(PDfWidth * (float)0.99 - mm.Width);
ypos = (int)(PDFHeight * (float)0.5 - mm.Height / 2);
if (PDfWidth > 1000)//橫排排版
{
mm.SetAbsolutePosition(xpos, ypos);
mm.ScaleAbsolute(mm.Width, mm.Height);
}
else//豎排排版
{
mm.SetAbsolutePosition(xpos + 130, ypos);
mm.ScaleAbsolute((float)(mm.Width / 1.5), (float)(mm.Height / 1.5));
}
break;
case "左下":
xpos = (int)(PDfWidth * (float)0.01);
ypos = (int)(PDFHeight * (float)0.01);
if (PDfWidth > 1000)//橫排排版
{
mm.SetAbsolutePosition(xpos, ypos);
mm.ScaleAbsolute(mm.Width, mm.Height);
}
else//豎排排版
{
mm.SetAbsolutePosition(xpos, ypos);
mm.ScaleAbsolute((float)(mm.Width / 1.5), (float)(mm.Height / 1.5));
}
break;
case "中下":
xpos = (int)(PDfWidth * (float)0.5 - mm.Width / 2);
ypos = (int)(PDFHeight * (float)0.01);
if (PDfWidth > 1000)//橫排排版
{
mm.SetAbsolutePosition(xpos, ypos);
mm.ScaleAbsolute(mm.Width, mm.Height);
}
else//豎排排版
{
mm.SetAbsolutePosition(xpos + 50, ypos);
mm.ScaleAbsolute((float)(mm.Width / 1.5), (float)(mm.Height / 1.5));
}
break;
case "右下":
xpos = (int)(PDfWidth * (float)0.99 - mm.Width);
ypos = (int)(PDFHeight * (float)0.01);
if (PDfWidth > 1000)//橫排排版
{
mm.SetAbsolutePosition(xpos, ypos);
mm.ScaleAbsolute(mm.Width, mm.Height);
}
else//豎排排版
{
mm.SetAbsolutePosition(xpos + 130, ypos);
mm.ScaleAbsolute((float)(mm.Width / 1.5), (float)(mm.Height / 1.5));
}
break;
default:
break;
}
under = stamp.GetOverContent(i);
//图片
under.AddImage(mm);
}
stamp.Close();
reader.Close();
DeleteWaterPNG(pageTotalCount);
}
catch (Exception ex)
{
message = ex.Message.ToString();
}
return message;
}