Inheritance: PdfDictionary, IPdfOCG
Beispiel #1
2
 private static void GetOCGOrder(PdfArray order, PdfLayer layer) {
     if (!layer.OnPanel)
         return;
     if (layer.Title == null)
         order.Add(layer.Ref);
     List<PdfLayer> children = layer.Children;
     if (children == null)
         return;
     PdfArray kids = new PdfArray();
     if (layer.Title != null)
         kids.Add(new PdfString(layer.Title, PdfObject.TEXT_UNICODE));
     for (int k = 0; k < children.Count; ++k) {
         GetOCGOrder(kids, children[k]);
     }
     if (kids.Size > 0)
         order.Add(kids);
 }
Beispiel #2
0
 // ---------------------------------------------------------------------------
 public byte[] CreatePdf(bool on)
 {
     using (MemoryStream ms = new MemoryStream()) {
     using (Document document = new Document()) {
       // step 2
       PdfWriter writer = PdfWriter.GetInstance(document, ms);
       writer.ViewerPreferences = PdfWriter.PageModeUseOC;
       writer.PdfVersion = PdfWriter.VERSION_1_5;
       // step 3
       document.Open();
       // step 4
       PdfLayer layer = new PdfLayer("Do you see me?", writer);
       layer.On = on;
       BaseFont bf = BaseFont.CreateFont();
       PdfContentByte cb = writer.DirectContent;
       cb.BeginText();
       cb.SetFontAndSize(bf, 18);
       cb.ShowTextAligned(Element.ALIGN_LEFT, "Do you see me?", 50, 790, 0);
       cb.BeginLayer(layer);
       cb.ShowTextAligned(Element.ALIGN_LEFT, "Peek-a-Boo!!!", 50, 766, 0);
       cb.EndLayer();
       cb.EndText();
     }
     return ms.ToArray();
       }
 }
Beispiel #3
0
 /**
 * Creates a title layer. A title layer is not really a layer but a collection of layers
 * under the same title heading.
 * @param title the title text
 * @param writer the <CODE>PdfWriter</CODE>
 * @return the title layer
 */    
 public static PdfLayer CreateTitle(String title, PdfWriter writer) {
     if (title == null)
         throw new ArgumentNullException(MessageLocalization.GetComposedMessage("title.cannot.be.null"));
     PdfLayer layer = new PdfLayer(title);
     writer.RegisterLayer(layer);
     return layer;
 }
Beispiel #4
0
 private static void ApplyWaterMark(string filePath)
 {
     Logger.LogI("ApplyWatermark -> " + filePath);
     var watermarkedFile = Path.GetFileNameWithoutExtension(filePath) + "-w.pdf";
     using (var reader1 = new PdfReader(filePath))
     {
         using (var fs = new FileStream(watermarkedFile, FileMode.Create, FileAccess.Write, FileShare.None))
         using (var stamper = new PdfStamper(reader1, fs))
         {
             var pageCount = reader1.NumberOfPages;
             var layer = new PdfLayer("WatermarkLayer", stamper.Writer);
             for (var i = 1; i <= pageCount; i++)
             {
                 var rect = reader1.GetPageSize(i);
                 var cb = stamper.GetUnderContent(i);
                 cb.BeginLayer(layer);
                 cb.SetFontAndSize(BaseFont.CreateFont(
                     BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 50);
                 var gState = new PdfGState {FillOpacity = 0.25f};
                 cb.SetGState(gState);
                 cb.SetColorFill(BaseColor.BLACK);
                 cb.BeginText();
                 cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER,
                     "(c)2015 ScrapEra", rect.Width/2, rect.Height/2, 45f);
                 cb.EndText();
                 cb.EndLayer();
             }
         }
     }
     File.Delete(filePath);
 }
        // ===========================================================================
        public void Write(Stream stream)
        {
            // step 1
              using (Document document = new Document()) {
            // step 2
            PdfWriter writer = PdfWriter.GetInstance(document, stream);
            writer.PdfVersion = PdfWriter.VERSION_1_6;
            // step 3
            document.Open();
            // step 4
            PdfContentByte cb = writer.DirectContent;

            PdfLayer dog = new PdfLayer("layer 1", writer);
            PdfLayer tiger = new PdfLayer("layer 2", writer);
            PdfLayer lion = new PdfLayer("layer 3", writer);
            PdfLayerMembership cat = new PdfLayerMembership(writer);
            PdfVisibilityExpression ve1 = new PdfVisibilityExpression(
              PdfVisibilityExpression.OR
            );
            ve1.Add(tiger);
            ve1.Add(lion);
            cat.VisibilityExpression = ve1;
            PdfLayerMembership no_cat = new PdfLayerMembership(writer);
            PdfVisibilityExpression ve2 = new PdfVisibilityExpression(
              PdfVisibilityExpression.NOT
            );
            ve2.Add(ve1);
            no_cat.VisibilityExpression = ve2;
            cb.BeginLayer(dog);
            ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT, new Phrase("dog"),
                50, 775, 0);
            cb.EndLayer();
            cb.BeginLayer(tiger);
            ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT, new Phrase("tiger"),
                50, 750, 0);
            cb.EndLayer();
            cb.BeginLayer(lion);
            ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT, new Phrase("lion"),
                50, 725, 0);
            cb.EndLayer();
            cb.BeginLayer(cat);
            ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT, new Phrase("cat"),
                50, 700, 0);
            cb.EndLayer();
            cb.BeginLayer(no_cat);
            ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT,
                new Phrase("no cat"), 50, 700, 0);
            cb.EndLayer();
              }
        }
Beispiel #6
0
 public void LayerCheckTest1()
 {
     string filename = OUT + "LayerCheckTest1.pdf";
     FileStream fos = new FileStream(filename, FileMode.Create);
     Document document = new Document();
     PdfWriter writer = PdfAWriter.GetInstance(document, fos, PdfAConformanceLevel.PDF_A_2B);
     writer.ViewerPreferences = PdfWriter.PageModeUseOC;
     writer.PdfVersion = PdfWriter.VERSION_1_5;
     document.Open();
     PdfLayer layer = new PdfLayer("Do you see me?", writer);
     layer.On = true;
     BaseFont bf = BaseFont.CreateFont(RESOURCES + "FreeMonoBold.ttf", BaseFont.WINANSI, true);
     PdfContentByte cb = writer.DirectContent;
     cb.BeginText();
     cb.SetFontAndSize(bf, 18);
     cb.ShowTextAligned(Element.ALIGN_LEFT, "Do you see me?", 50, 790, 0);
     cb.BeginLayer(layer);
     cb.ShowTextAligned(Element.ALIGN_LEFT, "Peek-a-Boo!!!", 50, 766, 0);
     cb.EndLayer();
     cb.EndText();
     document.Close();
 }
Beispiel #7
0
 /**
 * Adds a child layer. Nested layers can only have one parent.
 * @param child the child layer
 */    
 public void AddChild(PdfLayer child) {
     if (child.parent != null)
         throw new ArgumentException(MessageLocalization.GetComposedMessage("the.layer.1.already.has.a.parent", ((PdfString)child.Get(PdfName.NAME)).ToUnicodeString()));
     child.parent = this;
     if (children == null)
         children = new ArrayList();
     children.Add(child);
 }
        public void LayerCheckTest2() {
            string filename = OUT + "LayerCheckTest2.pdf";
            FileStream fos = new FileStream(filename, FileMode.Create);
            Document document = new Document();
            PdfWriter writer = PdfAWriter.GetInstance(document, fos, PdfAConformanceLevel.PDF_A_2B);
            writer.CreateXmpMetadata();
            writer.ViewerPreferences = PdfWriter.PageModeUseOC;
            writer.PdfVersion = PdfWriter.VERSION_1_5;
            document.Open();
            PdfContentByte cb = writer.DirectContent;
            PdfLayer nested = new PdfLayer("Nested layers", writer);
            PdfLayer nested_1 = new PdfLayer("Nested layer 1", writer);
            PdfLayer nested_2 = new PdfLayer("Nested layer 2", writer);
            nested.AddChild(nested_1);
            nested.AddChild(nested_2);
            writer.LockLayer(nested_2);
            cb.BeginLayer(nested);

            Font font = FontFactory.GetFont(RESOURCES + "FreeMonoBold.ttf", BaseFont.WINANSI, true);
            ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT, new Phrase("nested layers", font), 50, 775, 0);
            cb.EndLayer();
            cb.BeginLayer(nested_1);
            ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT, new Phrase("nested layer 1", font), 100, 800, 0);
            cb.EndLayer();
            cb.BeginLayer(nested_2);
            ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT, new Phrase("nested layer 2", font), 100, 750, 0);
            cb.EndLayer();

            FileStream iccProfileFileStream = File.Open(RESOURCES + "sRGB Color Space Profile.icm", FileMode.Open, FileAccess.Read, FileShare.Read);
            ICC_Profile icc = ICC_Profile.GetInstance(iccProfileFileStream);
            iccProfileFileStream.Close();

            writer.SetOutputIntents("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", icc);
            document.Close();
        }
        public void LayerCheckTest1() {
            string filename = OUT + "LayerCheckTest1.pdf";
            FileStream fos = new FileStream(filename, FileMode.Create);
            Document document = new Document();
            PdfWriter writer = PdfAWriter.GetInstance(document, fos, PdfAConformanceLevel.PDF_A_2B);
            writer.CreateXmpMetadata();
            writer.ViewerPreferences = PdfWriter.PageModeUseOC;
            writer.PdfVersion = PdfWriter.VERSION_1_5;
            document.Open();
            PdfLayer layer = new PdfLayer("Do you see me?", writer);
            layer.On = true;
            BaseFont bf = BaseFont.CreateFont(RESOURCES + "FreeMonoBold.ttf", BaseFont.WINANSI, true);
            PdfContentByte cb = writer.DirectContent;
            cb.BeginText();
            cb.SetFontAndSize(bf, 18);
            cb.ShowTextAligned(Element.ALIGN_LEFT, "Do you see me?", 50, 790, 0);
            cb.BeginLayer(layer);
            cb.ShowTextAligned(Element.ALIGN_LEFT, "Peek-a-Boo!!!", 50, 766, 0);
            cb.EndLayer();
            cb.EndText();

            FileStream iccProfileFileStream = File.Open(RESOURCES + "sRGB Color Space Profile.icm", FileMode.Open, FileAccess.Read, FileShare.Read);
            ICC_Profile icc = ICC_Profile.GetInstance(iccProfileFileStream);
            iccProfileFileStream.Close();

            writer.SetOutputIntents("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", icc);
            document.Close();
        }
        public void LayerStampingTest() {
            String outPdf = DestFolder + "out3.pdf";
            PdfReader reader =
                new PdfReader(TestResourceUtils.GetResourceAsStream(TestResourcesPath, "House_Plan_Final.pdf"));
            PdfStamper stamper = new PdfStamper(reader, File.Create(outPdf));

            PdfLayer logoLayer = new PdfLayer("Logos", stamper.Writer);
            PdfContentByte cb = stamper.GetUnderContent(1);
            cb.BeginLayer(logoLayer);

            Image iImage = Image.GetInstance(TestResourceUtils.GetResourceAsStream(TestResourcesPath, "Willi-1.jpg"));
            iImage.ScalePercent(24f);
            iImage.SetAbsolutePosition(100, 100);
            cb.AddImage(iImage);

            cb.EndLayer();
            stamper.Close();

            Assert.Null(new CompareTool().CompareByContent(outPdf, TestResourceUtils.GetResourceAsTempFile(TestResourcesPath, "cmp_House_Plan_Final.pdf"), DestFolder, "diff_"));
        }
        // ===========================================================================
        public void Write(Stream stream)
        {
            // step 1
              using (Document document = new Document()) {
            // step 2
            PdfWriter writer = PdfWriter.GetInstance(document, stream);
            writer.PdfVersion = PdfWriter.VERSION_1_5;
            // step 3
            document.Open();
            // step 4
            PdfLayer a1 = new PdfLayer("answer 1", writer);
            PdfLayer a2 = new PdfLayer("answer 2", writer);
            PdfLayer a3 = new PdfLayer("answer 3", writer);
            a1.On = false;
            a2.On = false;
            a3.On = false;

            BaseFont bf = BaseFont.CreateFont();
            PdfContentByte cb = writer.DirectContent;
            cb.BeginText();
            cb.SetFontAndSize(bf, 18);
            cb.ShowTextAligned(Element.ALIGN_LEFT,
            "Q1: Who is the director of the movie 'Paths of Glory'?", 50, 766, 0);
            cb.ShowTextAligned(Element.ALIGN_LEFT,
            "Q2: Who directed the movie 'Lawrence of Arabia'?", 50, 718, 0);
            cb.ShowTextAligned(Element.ALIGN_LEFT,
            "Q3: Who is the director of 'House of Flying Daggers'?", 50, 670, 0);
            cb.EndText();
            cb.SaveState();
            cb.SetRGBColorFill(0xFF, 0x00, 0x00);
            cb.BeginText();
            cb.BeginLayer(a1);
            cb.ShowTextAligned(Element.ALIGN_LEFT,
                "A1: Stanley Kubrick", 50, 742, 0);
            cb.EndLayer();
            cb.BeginLayer(a2);
            cb.ShowTextAligned(Element.ALIGN_LEFT,
                "A2: David Lean", 50, 694, 0);
            cb.EndLayer();
            cb.BeginLayer(a3);
            cb.ShowTextAligned(Element.ALIGN_LEFT,
                "A3: Zhang Yimou", 50, 646, 0);
            cb.EndLayer();
            cb.EndText();
            cb.RestoreState();

            List<Object> stateOn = new List<Object>();
            stateOn.Add("ON");
            stateOn.Add(a1);
            stateOn.Add(a2);
            stateOn.Add(a3);
            PdfAction actionOn = PdfAction.SetOCGstate(stateOn, true);
            List<Object> stateOff = new List<Object>();
            stateOff.Add("OFF");
            stateOff.Add(a1);
            stateOff.Add(a2);
            stateOff.Add(a3);
            PdfAction actionOff = PdfAction.SetOCGstate(stateOff, true);
            List<Object> stateToggle = new List<Object>();
            stateToggle.Add("Toggle");
            stateToggle.Add(a1);
            stateToggle.Add(a2);
            stateToggle.Add(a3);
            PdfAction actionToggle = PdfAction.SetOCGstate(stateToggle, true);
            Phrase p = new Phrase("Change the state of the answers:");
            Chunk on = new Chunk(" on ").SetAction(actionOn);
            p.Add(on);
            Chunk off = new Chunk("/ off ").SetAction(actionOff);
            p.Add(off);
            Chunk toggle = new Chunk("/ toggle").SetAction(actionToggle);
            p.Add(toggle);
            document.Add(p);
              }
        }
Beispiel #12
0
 /**
 * Use this method to lock an optional content group.
 * The state of a locked group cannot be changed through the user interface
 * of a viewer application. Producers can use this entry to prevent the visibility
 * of content that depends on these groups from being changed by users.
 * @param layer the layer that needs to be added to the array of locked OCGs
 * @since   2.1.2
 */    
 public void LockLayer(PdfLayer layer) {
     OCGLocked.Add(layer.Ref);
 }
Beispiel #13
0
 /**
 * Reads the OCProperties dictionary from the catalog of the existing document
 * and fills the documentOCG, documentOCGorder and OCGRadioGroup variables in PdfWriter.
 * Note that the original OCProperties of the existing document can contain more information.
 * @since    2.1.2
 */
 virtual protected void ReadOCProperties() {
     if (documentOCG.Count != 0) {
         return;
     }
     PdfDictionary dict = reader.Catalog.GetAsDict(PdfName.OCPROPERTIES);
     if (dict == null) {
         return;
     }
     PdfArray ocgs = dict.GetAsArray(PdfName.OCGS);
     PdfIndirectReference refi;
     PdfLayer layer;
     Dictionary<string,PdfLayer> ocgmap = new Dictionary<string,PdfLayer>();
     for (ListIterator<PdfObject> i = ocgs.GetListIterator(); i.HasNext();) {
         refi = (PdfIndirectReference)i.Next();
         layer = new PdfLayer(null);
         layer.Ref = refi;
         layer.OnPanel = false;
         layer.Merge((PdfDictionary)PdfReader.GetPdfObject(refi));
         ocgmap[refi.ToString()] = layer;
     }
     PdfDictionary d = dict.GetAsDict(PdfName.D);
     PdfArray off = d.GetAsArray(PdfName.OFF);
     if (off != null) {
         for (ListIterator<PdfObject> i = off.GetListIterator(); i.HasNext(); ) {
             refi = (PdfIndirectReference)i.Next();
             layer = ocgmap[refi.ToString()];
             layer.On = false;
         }
     }
     PdfArray order = d.GetAsArray(PdfName.ORDER);
     if (order != null) {
         AddOrder(null, order, ocgmap);
     }
     foreach (PdfLayer o in ocgmap.Values)
         documentOCG[o] = null;
     OCGRadioGroup = d.GetAsArray(PdfName.RBGROUPS);
     if (OCGRadioGroup == null)
         OCGRadioGroup = new PdfArray();
     OCGLocked = d.GetAsArray(PdfName.LOCKED);
     if (OCGLocked == null)
         OCGLocked = new PdfArray();
 }
        /// <summary>
        /// Event handler when the end of the page is reached
        /// </summary>
        void helper_PageEnd(object sender, PageEndEventArgs e)
        {
            writer.ViewerPreferences = PdfWriter.PageModeUseOC;
            writer.PdfVersion = PdfWriter.VERSION_1_5;
            PdfLayer layer = new PdfLayer("Pages", writer);
            layer.On = false;
            layer.PageElement = "HF";
            layer.OnPanel = false;
            layer.View = true;
            under.BeginLayer(layer);

            var th = new PdfStructureElement(root, PdfName.ARTIFACT);
            th.Put(PdfName.TYPE, new PdfNull());
            under.BeginMarkedContentSequence(th);

            under.SaveState();
            float bottom = .625f;
            float textBase = document.Bottom - bottom.ToPts();
            float textSize = 12;
            under.BeginText();
            under.SetFontAndSize(GrantFonts.bf_times, textSize);
            float adjust = GrantFonts.bf_times.GetWidthPoint("0", textSize);
            it.Rectangle pSize = document.PageSize;
            float width = pSize.Width / 2;
            under.SetTextMatrix(width - textSize - adjust, textBase);
            under.ShowText(e.page);
            under.EndText();
            under.RestoreState();
            under.EndMarkedContentSequence();

            under.EndLayer();

        }
Beispiel #15
0
        public static void ResetBackgroundColor(string originalFile, string watermarked, string color)
        {
            int page = 1;

              PdfReader reader = new PdfReader(originalFile);
              using (FileStream fs = new FileStream(watermarked, FileMode.Create, FileAccess.Write, FileShare.None))
              using (PdfStamper stamper = new PdfStamper(reader, fs))
              {
            PdfLayer layer = new PdfLayer("BackgroundColor", stamper.Writer);

            Rectangle rect = reader.GetPageSize(page);
            PdfContentByte cb = stamper.GetOverContent(page);

            cb.BeginLayer(layer);

            // set color
            CMYKColor chosenColor;
            CMYKColor green = new CMYKColor(0.0809f, 0f, 0.1915f, 0.0784f); //TO DO: get green color

            // set template color
            if (color.Equals("yellow", StringComparison.OrdinalIgnoreCase))
            {
              chosenColor = new CMYKColor(0f, 0.2092f, 0.7741f, 0.0627f);
            }
            else if (color.Equals("red", StringComparison.OrdinalIgnoreCase))
            {
              chosenColor = new CMYKColor(0f, 0.7564f, 0.7372f, 0.3882f);
            }
            else if (color.Equals("purple", StringComparison.OrdinalIgnoreCase))
            {
              chosenColor = new CMYKColor(0.5118f, 0.6693f, 0f, 0.5020f);
            }
            else
            {
              chosenColor = new CMYKColor(0f, 0f, 0f, 0f);
            }

            cb.SetColorFill(chosenColor);
            cb.SetColorStroke(chosenColor);

            // draw name label
            double widthDiff = 23;
            double startHeight = rect.Height - 3;
            double midHeight = rect.Height - 18;
            double endHeight = rect.Height - 27;
            double startWidth = rect.Width * 1 / 3;
            double firstMidWidth = startWidth + widthDiff;
            double endWidth = rect.Width;
            double secondMidWidth = endWidth - widthDiff;

            cb.MoveTo(rect.Width * 1 / 3, startHeight);
            cb.LineTo(rect.Width, startHeight);
            cb.LineTo(rect.Width, midHeight);
            cb.CurveTo(secondMidWidth + (endWidth - secondMidWidth) / 1.4, endHeight + (midHeight - endHeight) / 4, secondMidWidth, endHeight);
            cb.LineTo(firstMidWidth, endHeight);
            cb.CurveTo(firstMidWidth - (firstMidWidth - startWidth) / 1.4, endHeight + (midHeight - endHeight) / 4, startWidth, midHeight);
            cb.ClosePathFillStroke();

            // reset flag
            cb.SetColorFill(green);
            cb.SetColorStroke(green);

            startHeight = endHeight - 0.5;
            double heightDiff = 8.5;
            widthDiff = 13;

            cb.MoveTo(endWidth - widthDiff, startHeight);
            cb.LineTo(endWidth, startHeight);
            cb.LineTo(endWidth, startHeight - heightDiff);
            cb.LineTo(endWidth - widthDiff, startHeight - heightDiff);
            cb.ClosePathFillStroke();

            startHeight = startHeight - heightDiff - 2;
            cb.MoveTo(endWidth - widthDiff, startHeight);
            cb.LineTo(endWidth, startHeight);
            cb.LineTo(endWidth, startHeight - heightDiff);
            cb.LineTo(endWidth - widthDiff, startHeight - heightDiff);
            cb.ClosePathFillStroke();

            startHeight = startHeight - heightDiff - 2;
            cb.MoveTo(endWidth - widthDiff, startHeight);
            cb.LineTo(endWidth, startHeight);
            cb.LineTo(endWidth, startHeight - heightDiff);
            cb.LineTo(endWidth - widthDiff, startHeight - heightDiff);
            cb.ClosePathFillStroke();

            double mealLabelStartHeight = rect.Height - 30;
            double mealLabelHeight = 8;
            double mealLabelStartWidth = rect.Width / 3;
            double mealLabelWidth = 120;

            cb.MoveTo(mealLabelStartWidth, mealLabelStartHeight);
            cb.LineTo(mealLabelStartWidth + mealLabelWidth, mealLabelStartHeight);
            cb.LineTo(mealLabelStartWidth + mealLabelWidth, mealLabelStartHeight - mealLabelHeight);
            cb.LineTo(mealLabelStartWidth, mealLabelStartHeight - mealLabelHeight);
            cb.ClosePathFillStroke();

            double macrosStartHeight = rect.Height - 55;
            double macrosStartWidth = rect.Width / 3 + 5;
            double macrosWidth = 20;
            double macrosHeight = 10;

            for (int i = 0; i < 4; i++)
            {
              cb.MoveTo(macrosStartWidth, macrosStartHeight);
              cb.LineTo(macrosStartWidth + macrosWidth, macrosStartHeight);
              cb.LineTo(macrosStartWidth + macrosWidth, macrosStartHeight - macrosHeight);
              cb.LineTo(macrosStartWidth, macrosStartHeight - macrosHeight);
              cb.ClosePathFillStroke();

              macrosStartWidth = macrosStartWidth + macrosWidth + 13;
            }

            // Close the layer
            cb.EndLayer();
              }
        }
Beispiel #16
0
        public static void Watermark(string originalFile, string watermarked)
        {
            int page = 1;

              PdfReader reader = new PdfReader(originalFile);
              using (FileStream fs = new FileStream(watermarked, FileMode.Create, FileAccess.Write, FileShare.None))
              using (PdfStamper stamper = new PdfStamper(reader, fs))
              {
            PdfLayer layer = new PdfLayer("Text", stamper.Writer);

            Rectangle rect = reader.GetPageSize(page);
            PdfContentByte cb = stamper.GetOverContent(page);

            cb.EndLayer();
              }
        }
Beispiel #17
0
        public static void ResetMacrosDetails(string originalFile, string watermarked)
        {
            int page = 1;

              PdfReader reader = new PdfReader(originalFile);
              using (FileStream fs = new FileStream(watermarked, FileMode.Create, FileAccess.Write, FileShare.None))
              using (PdfStamper stamper = new PdfStamper(reader, fs))
              {
            PdfLayer layer = new PdfLayer("Text", stamper.Writer);

            Rectangle rect = reader.GetPageSize(page);
            PdfContentByte cb = stamper.GetOverContent(page);

            CMYKColor color = new CMYKColor(0f, 0f, 0f, 0f);
            cb.SetColorFill(color);
            cb.SetColorStroke(color);

            cb.BeginLayer(layer);

            double startHeight = rect.Height - 85;
            double height = 48;

            cb.MoveTo(0, startHeight);
            cb.LineTo(rect.Width, startHeight);
            cb.LineTo(rect.Width, startHeight - height);
            cb.LineTo(0, startHeight - height);
            cb.ClosePathFillStroke();

            startHeight = rect.Height - 143;
            double startWidth = rect.Width - 85;

            cb.MoveTo(startWidth, startHeight);
            cb.LineTo(rect.Width, startHeight);
            cb.LineTo(rect.Width, startHeight - height);
            cb.LineTo(startWidth, startHeight - height);
            cb.ClosePathFillStroke();

            startHeight = rect.Height - 150;
            double width = 65;
            height = 7;
            startWidth = 5;
            cb.MoveTo(startWidth, startHeight);
            cb.LineTo(startWidth + width, startHeight);
            cb.LineTo(startWidth + width, startHeight - height);
            cb.LineTo(startWidth, startHeight - height);
            cb.ClosePathFillStroke();

            startHeight = rect.Height - 179.5;
            height = 3;
            width = 4;
            startWidth = 115;

            for (int i = 0; i < 7; i++)
            {
              cb.MoveTo(startWidth, startHeight);
              cb.LineTo(startWidth + width, startHeight);
              cb.LineTo(startWidth + width, startHeight - height);
              cb.LineTo(startWidth, startHeight - height);
              cb.ClosePathFillStroke();

              startHeight = startHeight - 5.5;
            }

            startHeight = startHeight - 13;
            height = 2.7;

            for (int i = 0; i < 4; i++)
            {
              cb.MoveTo(startWidth, startHeight);
              cb.LineTo(startWidth + width, startHeight);
              cb.LineTo(startWidth + width, startHeight - height);
              cb.LineTo(startWidth, startHeight - height);
              cb.ClosePathFillStroke();

              startHeight = startHeight - 5.5;
            }

            cb.EndLayer();
              }
        }
Beispiel #18
0
        public static void ResetMacros(string originalFile, string watermarked)
        {
            int page = 1;

              PdfReader reader = new PdfReader(originalFile);
              using (FileStream fs = new FileStream(watermarked, FileMode.Create, FileAccess.Write, FileShare.None))
              using (PdfStamper stamper = new PdfStamper(reader, fs))
              {
            PdfLayer layer = new PdfLayer("Text", stamper.Writer);

            Rectangle rect = reader.GetPageSize(page);
            PdfContentByte cb = stamper.GetOverContent(page);

            CMYKColor color = new CMYKColor(0f, 0f, 0f, 0f);
            cb.SetColorFill(color);
            cb.SetColorStroke(color);

            cb.BeginLayer(layer);

            double mealLabelStartHeight = rect.Height - 30;
            double mealLabelHeight = 8;
            double mealLabelStartWidth = rect.Width / 3;
            double mealLabelWidth = 120;

            cb.MoveTo(mealLabelStartWidth, mealLabelStartHeight);
            cb.LineTo(mealLabelStartWidth + mealLabelWidth, mealLabelStartHeight);
            cb.LineTo(mealLabelStartWidth + mealLabelWidth, mealLabelStartHeight - mealLabelHeight);
            cb.LineTo(mealLabelStartWidth, mealLabelStartHeight - mealLabelHeight);
            cb.ClosePathFillStroke();

            double macrosStartHeight = rect.Height - 55;
            double macrosStartWidth = rect.Width / 3 + 5;
            double macrosWidth = 20;
            double macrosHeight = 10;

            for (int i = 0; i < 4; i++)
            {
              cb.MoveTo(macrosStartWidth, macrosStartHeight);
              cb.LineTo(macrosStartWidth + macrosWidth, macrosStartHeight);
              cb.LineTo(macrosStartWidth + macrosWidth, macrosStartHeight - macrosHeight);
              cb.LineTo(macrosStartWidth, macrosStartHeight - macrosHeight);
              cb.ClosePathFillStroke();

              macrosStartWidth = macrosStartWidth + macrosWidth + 13;
            }
            // Close the layer
            cb.EndLayer();
              }
        }
Beispiel #19
0
// ===========================================================================
    public void Write(Stream stream) {
      // step 1
      using (Document document = new Document()) {
        // step 2
        PdfWriter writer = PdfWriter.GetInstance(document, stream);
        writer.PdfVersion = PdfWriter.VERSION_1_5;
        // step 3
        document.Open();
        // step 4
        PdfContentByte cb = writer.DirectContent;
        PdfLayer nested = new PdfLayer("Nested layers", writer);
        PdfLayer nested_1 = new PdfLayer("Nested layer 1", writer);
        PdfLayer nested_2 = new PdfLayer("Nested layer 2", writer);
        nested.AddChild(nested_1);
        nested.AddChild(nested_2);
        writer.LockLayer(nested_2);
        cb.BeginLayer(nested);
        ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT,
          new Phrase("nested layers"), 50, 775, 0
        );
        cb.EndLayer();
        cb.BeginLayer(nested_1);
        ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT,
          new Phrase("nested layer 1"), 100, 800, 0
        );
        cb.EndLayer();
        cb.BeginLayer(nested_2);
        ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT, 
          new Phrase("nested layer 2"), 100, 750, 0
        );
        cb.EndLayer();

        PdfLayer group = PdfLayer.CreateTitle("Grouped layers", writer);
        PdfLayer layer1 = new PdfLayer("Group: layer 1", writer);
        PdfLayer layer2 = new PdfLayer("Group: layer 2", writer);
        group.AddChild(layer1);
        group.AddChild(layer2);
        cb.BeginLayer(layer1);
        ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT, 
          new Phrase( "layer 1 in the group"), 50, 700, 0
        );
        cb.EndLayer();
        cb.BeginLayer(layer2);
        ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT, 
          new Phrase("layer 2 in the group"), 50, 675, 0
        );
        cb.EndLayer();

        PdfLayer radiogroup = PdfLayer.CreateTitle("Radio group", writer);
        PdfLayer radio1 = new PdfLayer("Radiogroup: layer 1", writer);
        radio1.On = true;
        PdfLayer radio2 = new PdfLayer("Radiogroup: layer 2", writer);
        radio2.On = false;
        PdfLayer radio3 = new PdfLayer("Radiogroup: layer 3", writer);
        radio3.On = false;
        radiogroup.AddChild(radio1);
        radiogroup.AddChild(radio2);
        radiogroup.AddChild(radio3);
        List<PdfLayer> options = new List<PdfLayer>();
        options.Add(radio1);
        options.Add(radio2);
        options.Add(radio3);
        writer.AddOCGRadioGroup(options);
        cb.BeginLayer(radio1);
        ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT, 
          new Phrase("option 1"), 50, 600, 0
        );
        cb.EndLayer();
        cb.BeginLayer(radio2);
        ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT, 
          new Phrase("option 2"), 50, 575, 0
        );
        cb.EndLayer();
        cb.BeginLayer(radio3);
        ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT, 
          new Phrase(
                "option 3"
          ), 50, 550, 0
        );
        cb.EndLayer();

        PdfLayer not_printed = new PdfLayer("not printed", writer);
        not_printed.OnPanel = false;
        not_printed.SetPrint("Print", false);
        cb.BeginLayer(not_printed);
        ColumnText.ShowTextAligned(cb, Element.ALIGN_CENTER, 
          new Phrase(
                "PRINT THIS PAGE"
          ), 300, 700, 90
        );
        cb.EndLayer();

        PdfLayer zoom = new PdfLayer("Zoom 0.75-1.25", writer);
        zoom.OnPanel = false;
        zoom.SetZoom(0.75f, 1.25f);
        cb.BeginLayer(zoom);
        ColumnText.ShowTextAligned(
          cb, Element.ALIGN_LEFT, 
          new Phrase(
            "Only visible if the zoomfactor is between 75 and 125%"
          ), 30, 530, 90
        );
        cb.EndLayer();
      }
    }
 /**
 * Adds a new member to the layer.
 * @param layer the new member to the layer
 */    
 virtual public void AddMember(PdfLayer layer) {
     if (!layers.ContainsKey(layer)) {
         members.Add(layer.Ref);
         layers[layer] = null;
     }
 }
Beispiel #21
0
        public void LayerCheckTest2()
        {
            string filename = OUT + "LayerCheckTest2.pdf";
            FileStream fos = new FileStream(filename, FileMode.Create);
            Document document = new Document();
            PdfWriter writer = PdfAWriter.GetInstance(document, fos, PdfAConformanceLevel.PDF_A_2B);
            writer.ViewerPreferences = PdfWriter.PageModeUseOC;
            writer.PdfVersion = PdfWriter.VERSION_1_5;
            document.Open();
            PdfContentByte cb = writer.DirectContent;
            PdfLayer nested = new PdfLayer("Nested layers", writer);
            PdfLayer nested_1 = new PdfLayer("Nested layer 1", writer);
            PdfLayer nested_2 = new PdfLayer("Nested layer 2", writer);
            nested.AddChild(nested_1);
            nested.AddChild(nested_2);
            writer.LockLayer(nested_2);
            cb.BeginLayer(nested);

            Font font = FontFactory.GetFont(RESOURCES + "FreeMonoBold.ttf", BaseFont.WINANSI, true);
            ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT, new Phrase("nested layers", font), 50, 775, 0);
            cb.EndLayer();
            cb.BeginLayer(nested_1);
            ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT, new Phrase("nested layer 1", font), 100, 800, 0);
            cb.EndLayer();
            cb.BeginLayer(nested_2);
            ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT, new Phrase("nested layer 2", font), 100, 750, 0);
            cb.EndLayer();

            document.Close();
        }
Beispiel #22
0
 /**
 * Recursive method to reconstruct the documentOCGorder variable in the writer.
 * @param    parent  a parent PdfLayer (can be null)
 * @param    arr     an array possibly containing children for the parent PdfLayer
 * @param    ocgmap  a Hashtable with indirect reference Strings as keys and PdfLayer objects as values.
 * @since    2.1.2
 */
 private void AddOrder(PdfLayer parent, PdfArray arr, Dictionary<string,PdfLayer> ocgmap) {
     PdfObject obj;
     PdfLayer layer;
     for (int i = 0; i < arr.Size; i++) {
         obj = arr[i];
         if (obj.IsIndirect()) {
             layer = ocgmap[obj.ToString()];
             if (layer != null) {
                 layer.OnPanel = true;
                 RegisterLayer(layer);
                 if (parent != null) {
                     parent.AddChild(layer);
                 }
                 if (arr.Size > i + 1 && arr[i + 1].IsArray()) {
                     i++;
                     AddOrder(layer, (PdfArray)arr[i], ocgmap);
                 }
             }
         }
         else if (obj.IsArray()) {
             PdfArray sub = (PdfArray)obj;
             if (sub.IsEmpty()) return;
             obj = sub[0];
             if (obj.IsString()) {
                 layer = new PdfLayer(obj.ToString());
                 layer.OnPanel = true;
                 RegisterLayer(layer);
                 if (parent != null) {
                     parent.AddChild(layer);
                 }
                 PdfArray array = new PdfArray();
                 for (ListIterator<PdfObject> j = sub.GetListIterator(); j.HasNext(); ) {
                     array.Add(j.Next());
                 }
                 AddOrder(layer, array, ocgmap);
             }
             else {
                 AddOrder(parent, (PdfArray)obj, ocgmap);
             }
         }
     }
 }
Beispiel #23
0
 /**
 * Creates a title layer. A title layer is not really a layer but a collection of layers
 * under the same title heading.
 * @param title the title text
 * @param writer the <CODE>PdfWriter</CODE>
 * @return the title layer
 */
 public static PdfLayer CreateTitle(String title, PdfWriter writer)
 {
     if (title == null)
         throw new ArgumentNullException("Title cannot be null.");
     PdfLayer layer = new PdfLayer(title);
     writer.RegisterLayer(layer);
     return layer;
 }
Beispiel #24
0
 /**
 * Adds a child layer. Nested layers can only have one parent.
 * @param child the child layer
 */
 public void AddChild(PdfLayer child)
 {
     if (child.parent != null)
         throw new ArgumentException("The layer '" + ((PdfString)child.Get(PdfName.NAME)).ToUnicodeString() + "' already has a parent.");
     child.parent = this;
     if (children == null)
         children = new ArrayList();
     children.Add(child);
 }
        public static double[] CreateEmptyLabel(string originalFile, string watermarked)
        {
            int page = 1;
              double[] dimensions = new double[2];

              PdfReader reader = new PdfReader(originalFile);
              using (FileStream fs = new FileStream(watermarked, FileMode.Create, FileAccess.Write, FileShare.None))
              using (PdfStamper stamper = new PdfStamper(reader, fs))
              {
            PdfLayer layer = new PdfLayer("BackgroundColor", stamper.Writer);

            Rectangle rect = reader.GetPageSize(page);
            PdfContentByte cb = stamper.GetOverContent(page);

            cb.BeginLayer(layer);

            CMYKColor green = new CMYKColor(0.0809f, 0f, 0.1915f, 0.0784f); //TO DO: get green color
            CMYKColor white = new CMYKColor(0f, 0f, 0f, 0f);
            cb.SetColorFill(white);
            cb.SetColorStroke(white);

            // draw name label
            double widthDiff = 23;
            double startHeight = rect.Height - 3;
            double midHeight = rect.Height - 18;
            double endHeight = rect.Height - 27;
            double startWidth = rect.Width * 1 / 3;
            double firstMidWidth = startWidth + widthDiff;
            double endWidth = rect.Width;
            double secondMidWidth = endWidth - widthDiff;

            dimensions[0] = rect.Width;
            dimensions[1] = rect.Height;

            cb.MoveTo(rect.Width * 1 / 3, startHeight);
            cb.LineTo(rect.Width, startHeight);
            cb.LineTo(rect.Width, midHeight);
            cb.CurveTo(secondMidWidth + (endWidth - secondMidWidth) / 1.4, endHeight + (midHeight - endHeight) / 4, secondMidWidth, endHeight);
            cb.LineTo(firstMidWidth, endHeight);
            cb.CurveTo(firstMidWidth - (firstMidWidth - startWidth) / 1.4, endHeight + (midHeight - endHeight) / 4, startWidth, midHeight);
            cb.ClosePathFillStroke();

            // reset flag
            cb.SetColorFill(green);
            cb.SetColorStroke(green);

            startHeight = endHeight - 0.5;
            double heightDiff = 8.5;
            widthDiff = 13;

            cb.MoveTo(endWidth - widthDiff, startHeight);
            cb.LineTo(endWidth, startHeight);
            cb.LineTo(endWidth, startHeight - heightDiff);
            cb.LineTo(endWidth - widthDiff, startHeight - heightDiff);
            cb.ClosePathFillStroke();

            startHeight = startHeight - heightDiff - 2;
            cb.MoveTo(endWidth - widthDiff, startHeight);
            cb.LineTo(endWidth, startHeight);
            cb.LineTo(endWidth, startHeight - heightDiff);
            cb.LineTo(endWidth - widthDiff, startHeight - heightDiff);
            cb.ClosePathFillStroke();

            startHeight = startHeight - heightDiff - 2;
            cb.MoveTo(endWidth - widthDiff, startHeight);
            cb.LineTo(endWidth, startHeight);
            cb.LineTo(endWidth, startHeight - heightDiff);
            cb.LineTo(endWidth - widthDiff, startHeight - heightDiff);
            cb.ClosePathFillStroke();

            cb.SetColorFill(white);
            cb.SetColorStroke(white);

            double mealLabelStartHeight = rect.Height - 30;
            double mealLabelHeight = 8;
            double mealLabelStartWidth = rect.Width / 3;
            double mealLabelWidth = 120;

            cb.MoveTo(mealLabelStartWidth, mealLabelStartHeight);
            cb.LineTo(mealLabelStartWidth + mealLabelWidth, mealLabelStartHeight);
            cb.LineTo(mealLabelStartWidth + mealLabelWidth, mealLabelStartHeight - mealLabelHeight);
            cb.LineTo(mealLabelStartWidth, mealLabelStartHeight - mealLabelHeight);
            cb.ClosePathFillStroke();

            double macrosStartHeight = rect.Height - 55;
            double macrosStartWidth = rect.Width / 3 + 5;
            double macrosWidth = 20;
            double macrosHeight = 10;

            for (int i = 0; i < 4; i++)
            {
              cb.MoveTo(macrosStartWidth, macrosStartHeight);
              cb.LineTo(macrosStartWidth + macrosWidth, macrosStartHeight);
              cb.LineTo(macrosStartWidth + macrosWidth, macrosStartHeight - macrosHeight);
              cb.LineTo(macrosStartWidth, macrosStartHeight - macrosHeight);
              cb.ClosePathFillStroke();

              macrosStartWidth = macrosStartWidth + macrosWidth + 13;
            }

            startHeight = rect.Height - 85;
            double height = 48;

            cb.MoveTo(0, startHeight);
            cb.LineTo(rect.Width, startHeight);
            cb.LineTo(rect.Width, startHeight - height);
            cb.LineTo(0, startHeight - height);
            cb.ClosePathFillStroke();

            startHeight = rect.Height - 143;
            startWidth = rect.Width - 85;

            cb.MoveTo(startWidth, startHeight);
            cb.LineTo(rect.Width, startHeight);
            cb.LineTo(rect.Width, startHeight - height);
            cb.LineTo(startWidth, startHeight - height);
            cb.ClosePathFillStroke();

            startHeight = rect.Height - 150;
            double width = 65;
            height = 7;
            startWidth = 5;
            cb.MoveTo(startWidth, startHeight);
            cb.LineTo(startWidth + width, startHeight);
            cb.LineTo(startWidth + width, startHeight - height);
            cb.LineTo(startWidth, startHeight - height);
            cb.ClosePathFillStroke();

            startHeight = rect.Height - 179.5;
            height = 3.2;
            width = 4;
            startWidth = 115;

            for (int i = 0; i < 7; i++)
            {
              cb.MoveTo(startWidth, startHeight);
              cb.LineTo(startWidth + width, startHeight);
              cb.LineTo(startWidth + width, startHeight - height);
              cb.LineTo(startWidth, startHeight - height);
              cb.ClosePathFillStroke();

              startHeight = startHeight - 5.5;
            }

            startHeight = startHeight - 13;
            height = 2.7;

            for (int i = 0; i < 4; i++)
            {
              cb.MoveTo(startWidth, startHeight);
              cb.LineTo(startWidth + width, startHeight);
              cb.LineTo(startWidth + width, startHeight - height);
              cb.LineTo(startWidth, startHeight - height);
              cb.ClosePathFillStroke();

              startHeight = startHeight - 5.5;
            }

            startHeight = rect.Height - 179.5;
            height = 3.23;
            width = 17;
            startWidth = 25;

            cb.MoveTo(startWidth, startHeight);
            cb.LineTo(startWidth + width, startHeight);
            cb.LineTo(startWidth + width, startHeight - height);
            cb.LineTo(startWidth, startHeight - height);
            cb.ClosePathFillStroke();

            startHeight = startHeight - 5.5;
            startWidth = 37;

            cb.MoveTo(startWidth, startHeight);
            cb.LineTo(startWidth + width, startHeight);
            cb.LineTo(startWidth + width, startHeight - height);
            cb.LineTo(startWidth, startHeight - height);
            cb.ClosePathFillStroke();

            startHeight = startHeight - 5.5;
            startWidth = 29;

            cb.MoveTo(startWidth, startHeight);
            cb.LineTo(startWidth + width, startHeight);
            cb.LineTo(startWidth + width, startHeight - height);
            cb.LineTo(startWidth, startHeight - height);
            cb.ClosePathFillStroke();

            startHeight = startHeight - 5.5;
            startWidth = 32;
            cb.MoveTo(startWidth, startHeight);
            cb.LineTo(startWidth + width, startHeight);
            cb.LineTo(startWidth + width, startHeight - height);
            cb.LineTo(startWidth, startHeight - height);
            cb.ClosePathFillStroke();

            startHeight = startHeight - 5.5;
            startWidth = 24;

            cb.MoveTo(startWidth, startHeight);
            cb.LineTo(startWidth + width, startHeight);
            cb.LineTo(startWidth + width, startHeight - height);
            cb.LineTo(startWidth, startHeight - height);
            cb.ClosePathFillStroke();

            startHeight = startHeight - 5.5;
            startWidth = 51;

            cb.MoveTo(startWidth, startHeight);
            cb.LineTo(startWidth + width, startHeight);
            cb.LineTo(startWidth + width, startHeight - height);
            cb.LineTo(startWidth, startHeight - height);
            cb.ClosePathFillStroke();

            startHeight = startHeight - 5.5;
            startWidth = 36;

            cb.MoveTo(startWidth, startHeight);
            cb.LineTo(startWidth + width, startHeight);
            cb.LineTo(startWidth + width, startHeight - height);
            cb.LineTo(startWidth, startHeight - height);
            cb.ClosePathFillStroke();

            startHeight = startHeight - 5.5;
            startWidth = 23;

            cb.MoveTo(startWidth, startHeight);
            cb.LineTo(startWidth + width, startHeight);
            cb.LineTo(startWidth + width, startHeight - height);
            cb.LineTo(startWidth, startHeight - height);
            cb.ClosePathFillStroke();

            startHeight = startHeight - 5.5;
            startWidth = 22;

            cb.MoveTo(startWidth, startHeight);
            cb.LineTo(startWidth + width, startHeight);
            cb.LineTo(startWidth + width, startHeight - height);
            cb.LineTo(startWidth, startHeight - height);
            cb.ClosePathFillStroke();

            startHeight = rect.Height - 167;
            height = 3;
            width = 17;
            startWidth = 25;

            cb.MoveTo(startWidth, startHeight);
            cb.LineTo(startWidth + width, startHeight);
            cb.LineTo(startWidth + width, startHeight - height);
            cb.LineTo(startWidth, startHeight - height);
            cb.ClosePathFillStroke();

            width = 12;
            startWidth = 112;

            cb.MoveTo(startWidth, startHeight);
            cb.LineTo(startWidth + width, startHeight);
            cb.LineTo(startWidth + width, startHeight - height);
            cb.LineTo(startWidth, startHeight - height);
            cb.ClosePathFillStroke();

            cb.EndLayer();
              }
              return dimensions;
        }