惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
月光博客
月光博客
MyScale Blog
MyScale Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
爱范儿
爱范儿
P
Proofpoint News Feed
人人都是产品经理
人人都是产品经理
Last Week in AI
Last Week in AI
罗磊的独立博客
G
Google Developers Blog
Y
Y Combinator Blog
博客园 - 【当耐特】
WordPress大学
WordPress大学
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
J
Java Code Geeks
酷 壳 – CoolShell
酷 壳 – CoolShell
V
Visual Studio Blog
美团技术团队
宝玉的分享
宝玉的分享
Jina AI
Jina AI
小众软件
小众软件
T
Tailwind CSS Blog
A
About on SuperTechFans

博客园 - DODONG

c#获取硬件信息 一个读写csv文件的C#类 . C#操作Excel文件(读取Excel,写入Excel) . 解决全球化时区问题 利用ReportViewer读取Reporting Service数据 [转]用反射来处理多字段提交 查询数据表中重复的记录 CVS文件导入SQL ComponentArt.Web.UI中AJAX TreeView 抽象工厂(Abstract Factory)模式 简单工厂(Simple Factory)模式 工厂方法(Factory Method)模式 用.NET创建Windows服务 关于SharePoint中查询写法和注意的地方 关于&运算符和^ 初识WAP开发时.. C#操作XML XMLHttp客户端操作数据 asp.net网页智能导航SmartNavigation的替代实现方式
在现有PDF文件上添加水印
DODONG · 2012-05-09 · via 博客园 - DODONG

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
using iTextSharp.text;
using System.IO;
using System.Web.UI.HtmlControls;
using System.Drawing;

//itextsharp.dll version:5.1.10

protected void Button1_Click(object sender, EventArgs e)
     {
        string source =@"D:\My.Sample\C#NET\Exoport2PDF\Web2\Chap1011.pdf"; //模板路径
        string output = @"D:\My.Sample\C#NET\Exoport2PDF\Web2\Chap1012.pdf"; //导出水印背景后的PDF
        string watermark = @"D:\My.Sample\C#NET\Exoport2PDF\Web2\gp0.jpg";   // 水印图片


        

bool isSurrcess = PDFWatermark(source, output, watermark, 1010);
        
     }

    public  bool PDFWatermark(string inputfilepath, string outputfilepath, string ModelPicName, float top, float left)

    {
        //throw new NotImplementedException();
        PdfReader pdfReader = null;
        PdfStamper pdfStamper = null;
        try
        {
            pdfReader = new PdfReader(inputfilepath);

            int numberOfPages = pdfReader.NumberOfPages;

            iTextSharp.text.Rectangle psize = pdfReader.GetPageSize(1);

            float width = psize.Width;

            float height = psize.Height;

            pdfStamper = new PdfStamper(pdfReader, new FileStream(outputfilepath, FileMode.Create));

            PdfContentByte waterMarkContent;

            iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(ModelPicName);

            image.GrayFill = 20;//透明度,灰色填充
            
//image.Rotation//旋转
            
//image.RotationDegrees//旋转角度
            
//水印的位置 
            if (left < 0)
            {
                left = width - image.Width + left;
            }

            image.SetAbsolutePosition(left, (height - image.Height) - top);

            //每一页加水印,也可以设置某一页加水印 
            for (int i = 1; i <= numberOfPages; i++)
            {
                waterMarkContent = pdfStamper.GetUnderContent(i);

                waterMarkContent.AddImage(image);
            }
            //strMsg = "success";
            return true;
        }
        catch (Exception ex)
        {
             ex.Message.Trim();
            return false;
        }
        finally
        {

            if (pdfStamper != null)
                pdfStamper.Close();

            if (pdfReader != null)
                pdfReader.Close();
        }