//添加水印
public void AddWaterMark(string filePath, string text)
{
iTextSharp.text.pdf.PdfReader pdfReader = null;
iTextSharp.text.pdf.PdfStamper pdfStamper = null;
string tempPath = Path.GetDirectoryName(filePath) + Path.GetFileNameWithoutExtension(filePath) + "_temp.pdf";
try
{
pdfReader = new iTextSharp.text.pdf.PdfReader(filePath);
pdfStamper = new iTextSharp.text.pdf.PdfStamper(pdfReader, new FileStream(tempPath, FileMode.Create));
int total = pdfReader.NumberOfPages + 1;
iTextSharp.text.Rectangle psize = pdfReader.GetPageSize(1);
float width = psize.Width;
float height = psize.Height;
iTextSharp.text.pdf.PdfContentByte content;
iTextSharp.text.pdf.BaseFont font = iTextSharp.text.pdf.BaseFont.CreateFont(@"C:\WINDOWS\Fonts\SIMFANG.TTF", iTextSharp.text.pdf.BaseFont.IDENTITY_H, iTextSharp.text.pdf.BaseFont.EMBEDDED);
iTextSharp.text.pdf.PdfGState gs = new iTextSharp.text.pdf.PdfGState();
//for (int i = 1; i < total; i++)
float jg_w = width / 5;
float jg_h = width / 5;
content = pdfStamper.GetOverContent(1);//在内容上方加水印
//content = pdfStamper.GetUnderContent(1);//在内容下方加水印
//透明度
gs.FillOpacity = 0.2f;
content.SetGState(gs);
iTextSharp.text.BaseColor nn = new iTextSharp.text.BaseColor(192,192,192,0);
content.SetColorFill(nn);
content.SetFontAndSize(font, 30);
content.SetTextMatrix(0, 0);
for (int i = 1; i < 6; i++)
{
//开始写入文本
content.BeginText();
content.ShowTextAligned(iTextSharp.text.Element.ALIGN_CENTER, text, jg_w, jg_h*i, 45);
content.ShowTextAligned(iTextSharp.text.Element.ALIGN_CENTER, text, jg_w*2, jg_h * i, 45);
content.ShowTextAligned(iTextSharp.text.Element.ALIGN_CENTER, text, jg_w * 3, jg_h * i, 45);
content.ShowTextAligned(iTextSharp.text.Element.ALIGN_CENTER, text, jg_w * 4, jg_h * i, 45);
content.ShowTextAligned(iTextSharp.text.Element.ALIGN_CENTER, text, jg_w*5, jg_h * i, 45);
content.EndText();
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (pdfStamper != null)
pdfStamper.Close();
if (pdfReader != null)
pdfReader.Close();
System.IO.File.Copy(tempPath, filePath, true);
System.IO.File.Delete(tempPath);
}
}
//加密
public void AddEncryptText(string path,string destPath)
{
iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(path, System.Text.Encoding.Default.GetBytes("adminSjy@123")); //读取要加密的PDF文件
int n = reader.NumberOfPages; //获取PDF文件的页数
iTextSharp.text.Rectangle pagesize = reader.GetPageSize(1);
iTextSharp.text.Document document = new iTextSharp.text.Document(pagesize);
string destPath = Path.GetDirectoryName(path) + "\\" + Path.GetFileNameWithoutExtension(path) + "_加密.pdf";
FileStream stream = new FileStream(destPath, FileMode.Create);
//写文件
iTextSharp.text.pdf.PdfCopy copy = new iTextSharp.text.pdf.PdfCopy(document, stream);
//copy.SetEncryption(iTextSharp.text.pdf.PdfWriter.STRENGTH40BITS, "", "adminSjy@123", iTextSharp.text.pdf.PdfWriter.AllowDegradedPrinting | iTextSharp.text.pdf.PdfWriter.AllowScreenReaders | iTextSharp.text.pdf.PdfWriter.AllowPrinting);
copy.SetEncryption(iTextSharp.text.pdf.PdfWriter.STRENGTH40BITS, "", "123", iTextSharp.text.pdf.PdfWriter.AllowScreenReaders | iTextSharp.text.pdf.PdfWriter.AllowPrinting);
document.Open(); ;
for (int i = 1; i <= n; i++)
{
iTextSharp.text.pdf.PdfImportedPage page = copy.GetImportedPage(reader, i);
copy.AddPage(page);
}
document.Close();
}
//旋转PDF
private void RotatePDF(string inputFile, int desiredRot)
{
string outputFile = Path.GetDirectoryName(inputFile) + Path.GetFileNameWithoutExtension(inputFile) + "_temp.pdf";
using (FileStream outStream = new FileStream(outputFile, FileMode.Append))
{
iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(inputFile);
iTextSharp.text.pdf.PdfStamper stamper = new iTextSharp.text.pdf.PdfStamper(reader, outStream);
iTextSharp.text.pdf.PdfDictionary pageDict = reader.GetPageN(1);
iTextSharp.text.pdf.PdfNumber rotation = pageDict.GetAsNumber(iTextSharp.text.pdf.PdfName.ROTATE);
if (rotation != null)
{
desiredRot += rotation.IntValue;
desiredRot %= 360; // must be 0, 90, 180, or 270
}
pageDict.Put(iTextSharp.text.pdf.PdfName.ROTATE, new iTextSharp.text.pdf.PdfNumber(desiredRot));
stamper.Close();
}
System.IO.File.Copy(outputFile, inputFile, true);
System.IO.File.Delete(outputFile);
}