且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PDF去水印资源索引

更新时间:2021-12-03 02:48:47

1、用iTextSharp去除文字水印。(水印文字需在contents中)

————Removing Watermark from a PDF using iTextSharp

2、用iTextSharp去除图片水印。(水印图片需为xobject对象)

————Remove mask image“Watermark” from PDF itextsharp

3、使用PDFlib-6.0.2库生成PDF文档的水印清除方法


example code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
  
namespace ClearWatermark
{
    class Program
    {
        static void Main(string[] args)
        {
            string pathfile_in = "D:\\test\\input\\test.pdf"
            string pathfile_out = "D:\\test\\output\\test.pdf"
              
            ProcOne4Contents(pathfile_in, pathfile_out);
            ProcOne4Xobject(pathfile_in, pathfile_out);
  
            Console.ReadKey();
        }       
  
        //去文字水印示例(walker)
        static bool ProcOne4Contents(string pathfile_in, string pathfile_out)
        {
            PdfReader pdfReader = new PdfReader(pathfile_in);
  
            PRStream stream;
            String content;
            PdfDictionary page;
            PdfArray contentarray;
            int pageCount = pdfReader.NumberOfPages;
            for (int i = 1; i <= pageCount; ++i)
            {
                page = pdfReader.GetPageN(i);
                contentarray = page.GetAsArray(PdfName.CONTENTS);
                if (contentarray != null)
                {
                    for (int j = 0; j < contentarray.Size; j++)
                    {
                        stream = (PRStream)contentarray.GetAsStream(j);                        
                        content = System.Text.Encoding.ASCII.GetString(PdfReader.GetStreamBytes(stream));
  
                        //if (content.IndexOf("/OC") >= 0 && content.IndexOf("I'm watermark") >= 0)
                        if (content.Length == 123 && content.IndexOf("I'm watermark") >= 0)
                        {
                            stream.Put(PdfName.LENGTH, new PdfNumber(0));
                            stream.SetData(new byte[0]);
                        }
                    }
                }
            }
            pdfReader.RemoveUnusedObjects();
  
            //写到输出文件
            using (FileStream outStream = new FileStream(pathfile_out, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                using (PdfStamper stamper = new PdfStamper(pdfReader, outStream))
                {
                }
            }
  
            return true;
        }
          
        //去图片水印示例(walker)
        static bool ProcOne4XObject(string pathfile_in, string pathfile_out)
        {
            string pdfTempFilename = pathfile_out;
            FileStream outStream = new FileStream(pdfTempFilename, FileMode.Create);
            PdfReader pdfReader = new PdfReader(pathfile_in);
            PdfStamper stp = new PdfStamper(pdfReader, outStream);   //关联输入和输出
            PdfDictionary page1 = pdfReader.GetPageN(1);
            PdfDictionary res = (PdfDictionary)PdfReader.GetPdfObject(page1.Get(PdfName.RESOURCES));
            PdfDictionary xobjDict = (PdfDictionary)PdfReader.GetPdfObject(res.Get(PdfName.XOBJECT));
  
            if (xobjDict != null)
            {
                foreach (PdfName name in xobjDict.Keys)
                {
                    //Console.WriteLine("name:" + name.ToString());
                    PdfObject obj = xobjDict.Get(name);
                    if (obj.IsIndirect())
                    {
                        PdfDictionary objDict = (PdfDictionary)PdfReader.GetPdfObject(obj);
                        PdfName type = objDict.GetAsName(PdfName.SUBTYPE);
                        //Console.WriteLine("type:" + type);
  
                        if (PdfName.IMAGE.Equals(type) && name.ToString().IndexOf("I1") > 0)
                        {
                            PdfReader.KillIndirect(obj);
                        }
                    }
                }
            }
            pdfReader.RemoveUnusedObjects();
            stp.Close();
  
            return true;
        }
         
        /*通过提取内容创建新PDF来变相去水印(walker)
         * 参考:http://www.sharejs.com/codes/csharp/8619
         * http://www.4guysfromrolla.com/articles/030911-1.aspx
         * https://gist.github.com/7shi/805326
        */
        static void ProcOneByExtract(string pathfile_in, string pathfile_out)
        {
            PdfReader pdfReader = new PdfReader(pathfile_in);
 
            Document outDocument = new Document();
            FileStream outStream = new FileStream(pathfile_out, FileMode.Create);
            PdfWriter pdfWriter = PdfWriter.GetInstance(outDocument, outStream);
            outDocument.Open();
             
            int pageNum = pdfReader.NumberOfPages;
            Console.WriteLine("pageNum:" + pageNum);
            for (int i = 1; i <= pageNum; i++)
            {
                PdfDictionary pg = pdfReader.GetPageN(i);
                PdfDictionary res = PdfReader.GetPdfObject(pg.Get(PdfName.RESOURCES)) as PdfDictionary;
                PdfDictionary xobj = PdfReader.GetPdfObject(res.Get(PdfName.XOBJECT)) as PdfDictionary;
                if (xobj == null)
                {
                    Console.WriteLine("xobj == null");
                    continue;
                }
 
                var keys = xobj.Keys;
                if (keys.Count == 0)
                {
                    Console.WriteLine("keys.Count == 0");
                    continue;
                }
 
                PdfObject obj = xobj.Get(keys.ElementAt(0));
                if (!obj.IsIndirect())
                {
                    Console.WriteLine("!obj.IsIndirect()");
                    continue;
                }
 
                PdfDictionary tg = PdfReader.GetPdfObject(obj) as PdfDictionary;
                PdfName type = PdfReader.GetPdfObject(tg.Get(PdfName.SUBTYPE)) as PdfName;
                Console.WriteLine("type:" + type);
                if (!PdfName.IMAGE.Equals(type))
                {                    
                    continue;
                }
 
                int XrefIndex = (obj as PRIndirectReference).Number;
                PRStream pdfStream = pdfReader.GetPdfObject(XrefIndex) as PRStream;
                byte[] data = PdfReader.GetStreamBytesRaw(pdfStream);
 
                Image img = Image.GetInstance(obj as PRIndirectReference);
 
                float pageWidth = pdfReader.GetPageSize(i).Width;
                float pageHeight = pdfReader.GetPageSize(i).Height;
 
                float imgWidth = img.Width;
                float imgHeight = img.Height;
 
                outDocument.SetPageSize(new Rectangle(pageWidth, pageHeight));
                outDocument.NewPage();
 
                img.ScaleToFit(pageWidth, pageHeight);
                img.SetAbsolutePosition(0, 0);
 
                outDocument.Add(img);
            }
 
            outDocument.Close();
        }
    }
}


*** walker ***

本文转自walker snapshot博客51CTO博客,原文链接http://blog.51cto.com/walkerqt/1408792如需转载请自行联系原作者

RQSLT