C#使用iTextSharp给PDF文件添加水印,PDF文件加密,PDF文件旋转

 

//添加水印

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);
        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

漫步的社会人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值