private void Form1_Load(object sender, EventArgs e)
        {
            string workingFolder     = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            string startFile         = Path.Combine(workingFolder, "StartFile.pdf");
            string watermarkedFile   = Path.Combine(workingFolder, "Watermarked.pdf");
            string unwatermarkedFile = Path.Combine(workingFolder, "Un-watermarked.pdf");
            string watermarkText     = "This is a test";

            //SECTION 1
            //Create a 5 page PDF, nothing special here
            using (FileStream fs = new FileStream(startFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
                using (Document doc = new Document(PageSize.LETTER)) {
                    using (PdfWriter witier = PdfWriter.GetInstance(doc, fs)) {
                        doc.Open();

                        for (int i = 1; i <= 5; i++)
                        {
                            doc.NewPage();
                            doc.Add(new Paragraph(String.Format("This is page {0}", i)));
                        }

                        doc.Close();
                    }
                }
            }

            //SECTION 2
            //Create our watermark on a separate layer. The only different here is that we are adding the watermark to a PdfLayer which is an OCG or Optional Content Group
            PdfReader reader1 = new PdfReader(startFile);

            using (FileStream fs = new FileStream(watermarkedFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
                using (PdfStamper stamper = new PdfStamper(reader1, fs)) {
                    int pageCount1 = reader1.NumberOfPages;
                    //Create a new layer
                    PdfLayer layer = new PdfLayer("WatermarkLayer", stamper.Writer);
                    for (int i = 1; i <= pageCount1; i++)
                    {
                        iTextSharp.text.Rectangle rect = reader1.GetPageSize(i);
                        //Get the ContentByte object
                        PdfContentByte cb = stamper.GetUnderContent(i);
                        //Tell the CB that the next commands should be "bound" to this new layer
                        cb.BeginLayer(layer);
                        cb.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 50);
                        PdfGState gState = new PdfGState();
                        gState.FillOpacity = 0.25f;
                        cb.SetGState(gState);
                        cb.SetColorFill(BaseColor.BLACK);
                        cb.BeginText();
                        cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, watermarkText, rect.Width / 2, rect.Height / 2, 45f);
                        cb.EndText();
                        //"Close" the layer
                        cb.EndLayer();
                    }
                }
            }

            //SECTION 3
            //Remove the layer created above
            //First we bind a reader to the watermarked file, then strip out a bunch of things, and finally use a simple stamper to write out the edited reader
            PdfReader reader2 = new PdfReader(watermarkedFile);

            //NOTE, This will destroy all layers in the document, only use if you don't have additional layers
            //Remove the OCG group completely from the document.
            //reader2.Catalog.Remove(PdfName.OCPROPERTIES);

            //Clean up the reader, optional
            reader2.RemoveUnusedObjects();

            //Placeholder variables
            PRStream      stream;
            String        content;
            PdfDictionary page;
            PdfArray      contentarray;

            //Get the page count
            int pageCount2 = reader2.NumberOfPages;

            //Loop through each page
            for (int i = 1; i <= pageCount2; i++)
            {
                //Get the page
                page = reader2.GetPageN(i);
                //Get the raw content
                contentarray = page.GetAsArray(PdfName.CONTENTS);
                if (contentarray != null)
                {
                    //Loop through content
                    for (int j = 0; j < contentarray.Size; j++)
                    {
                        //Get the raw byte stream
                        stream = (PRStream)contentarray.GetAsStream(j);
                        //Convert to a string. NOTE, you might need a different encoding here
                        content = System.Text.Encoding.ASCII.GetString(PdfReader.GetStreamBytes(stream));
                        //Look for the OCG token in the stream as well as our watermarked text
                        if (content.IndexOf("/OC") >= 0 && content.IndexOf(watermarkText) >= 0)
                        {
                            //Remove it by giving it zero length and zero data
                            stream.Put(PdfName.LENGTH, new PdfNumber(0));
                            stream.SetData(new byte[0]);
                        }
                    }
                }
            }

            //Write the content out
            using (FileStream fs = new FileStream(unwatermarkedFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
                using (PdfStamper stamper = new PdfStamper(reader2, fs)) {
                }
            }
            this.Close();
        }
Exemplo n.º 2
0
        public Example5()
        {
            string appRootDir = new DirectoryInfo(Environment.CurrentDirectory).Parent.Parent.FullName;

            string startFile         = appRootDir + "/PDFs/" + "Chapter1_Example5.pdf";
            string watermarkedFile   = appRootDir + "/PDFs/" + "Chapter1_Example5_Watermarked.pdf";
            string unwatermarkedFile = appRootDir + "/PDFs/" + "Chapter1_Example5_Un-Watermarked.pdf";

            string watermarkText = "This is a Test";

            // Creating a Five paged PDF
            using (FileStream fs = new FileStream(startFile, FileMode.Create, FileAccess.Write, FileShare.None))
                using (Document doc = new Document(PageSize.LETTER))
                    using (PdfWriter writer = PdfWriter.GetInstance(doc, fs))
                    {
                        doc.Open();
                        for (int i = 1; i <= 5; i++)
                        {
                            doc.NewPage();
                            doc.Add(new Paragraph(string.Format("This is a page {0}", i)));
                        }
                        doc.Close();
                    }

            // Creating watermark on a separate layer
            // Creating iTextSharp.text.pdf.PdfReader object to read the Existing PDF Document produced by 1 no.
            PdfReader reader1 = new PdfReader(startFile);

            using (FileStream fs = new FileStream(watermarkedFile, FileMode.Create, FileAccess.Write, FileShare.None))
                // Creating iTextSharp.text.pdf.PdfStamper object to write Data from iTextSharp.text.pdf.PdfReader object to FileStream object
                using (PdfStamper stamper = new PdfStamper(reader1, fs))
                {
                    // Getting total number of pages of the Existing Document
                    int pageCount = reader1.NumberOfPages;

                    // Create New Layer for Watermark
                    PdfLayer layer = new PdfLayer("WatermarkLayer", stamper.Writer);
                    // Loop through each Page
                    for (int i = 1; i <= pageCount; i++)
                    {
                        // Getting the Page Size
                        Rectangle rect = reader1.GetPageSize(i);

                        // Get the ContentByte object
                        PdfContentByte cb = stamper.GetUnderContent(i);

                        // Tell the cb that the next commands should be "bound" to this new layer
                        cb.BeginLayer(layer);
                        cb.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 50);

                        PdfGState gState = new PdfGState();
                        gState.FillOpacity = 0.25f;
                        cb.SetGState(gState);

                        cb.SetColorFill(BaseColor.BLACK);

                        cb.BeginText();

                        cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, watermarkText, rect.Width / 2, rect.Height / 2, 45f);

                        cb.EndText();

                        // Close the layer
                        cb.EndLayer();
                    }
                }

            // Removing the layer created above
            // 1. First we bind a reader to the watermarked file
            // 2. Then strip out a branch of things
            // 3. Finally use a simple stamper to write out the edited reader
            PdfReader reader2 = new PdfReader(watermarkedFile);

            // NOTE: This will destroy all layers in the Document, only use if you don't have any addtional layers
            // Remove the OCG group completely from the Document: reader2.Catalog.Remove(PdfName.OCPROPERTIES);

            // Clean up the reader, optional
            reader2.RemoveUnusedObjects();

            // Placeholder variables
            PRStream      stream;
            string        content;
            PdfDictionary page;
            PdfArray      contentArray;

            // Get the number of pages
            int pageCount2 = reader2.NumberOfPages;

            // Loop through each page
            for (int i = 1; i <= pageCount2; i++)
            {
                // Get the page
                page = reader2.GetPageN(i);

                // Get the raw content
                contentArray = page.GetAsArray(PdfName.CONTENTS);

                if (contentArray != null)
                {
                    // Loop through content
                    for (int j = 0; j < contentArray.Size; j++)
                    {
                        stream = (PRStream)contentArray.GetAsStream(j);

                        // Convert to a String, NOTE: you might need a different encoding here
                        content = System.Text.Encoding.ASCII.GetString(PdfReader.GetStreamBytes(stream));

                        //Look for the OCG token in the stream as well as our watermarked text
                        if (content.IndexOf("/OC") >= 0 && content.IndexOf(watermarkText) >= 0)
                        {
                            //Remove it by giving it zero length and zero data
                            stream.Put(PdfName.LENGTH, new PdfNumber(0));
                            stream.SetData(new byte[0]);
                        }
                    }
                }
            }

            // Write the content out
            using (FileStream fs = new FileStream(unwatermarkedFile, FileMode.Create, FileAccess.Write, FileShare.None))
                using (PdfStamper stamper = new PdfStamper(reader2, fs)) { }
        }