コード例 #1
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();
            }
        }
コード例 #2
0
ファイル: AddOCG.cs プロジェクト: anqin0725/i7ns-samples
        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
            PdfCanvas   canvas = new PdfCanvas(pdfDoc.GetFirstPage());

            PdfLayer nested  = new PdfLayer("Nested layers", pdfDoc);
            PdfLayer nested1 = new PdfLayer("Nested layer 1", pdfDoc);
            PdfLayer nested2 = new PdfLayer("Nested layer 2", pdfDoc);

            nested2.SetLocked(true);
            nested.AddChild(nested1);
            nested.AddChild(nested2);

            canvas.BeginLayer(nested);
            Canvas canvasModel = new Canvas(canvas, pdfDoc.GetDefaultPageSize());

            canvasModel.ShowTextAligned("nested layers", 50, 765, TextAlignment.LEFT, 0);
            canvas.EndLayer();

            canvas.BeginLayer(nested1);
            canvasModel.ShowTextAligned("nested layers 1", 100, 800, TextAlignment.LEFT, 0);
            canvas.EndLayer();

            canvas.BeginLayer(nested2);
            canvasModel.ShowTextAligned("nested layers 2", 100, 750, TextAlignment.LEFT, 0);
            canvas.EndLayer();

            PdfLayer group  = PdfLayer.CreateTitle("Grouped layers", pdfDoc);
            PdfLayer layer1 = new PdfLayer("Group: layer 1", pdfDoc);
            PdfLayer layer2 = new PdfLayer("Group: layer 2", pdfDoc);

            group.AddChild(layer1);
            group.AddChild(layer2);

            canvas.BeginLayer(layer1);
            canvasModel.ShowTextAligned("layer 1 in the group", 50, 700, TextAlignment.LEFT, 0);
            canvas.EndLayer();

            canvas.BeginLayer(layer2);
            canvasModel.ShowTextAligned("layer 2 in the group", 50, 675, TextAlignment.LEFT, 0);
            canvas.EndLayer();

            PdfLayer radiogroup = PdfLayer.CreateTitle("Radio group", pdfDoc);
            PdfLayer radio1     = new PdfLayer("Radiogroup: layer 1", pdfDoc);

            radio1.SetOn(true);
            PdfLayer radio2 = new PdfLayer("Radiogroup: layer 2", pdfDoc);

            radio2.SetOn(false);
            PdfLayer radio3 = new PdfLayer("Radiogroup: layer 3", pdfDoc);

            radio3.SetOn(false);
            radiogroup.AddChild(radio1);
            radiogroup.AddChild(radio2);
            radiogroup.AddChild(radio3);
            IList <PdfLayer> options = new List <PdfLayer>();

            options.Add(radio1);
            options.Add(radio2);
            options.Add(radio3);
            PdfLayer.AddOCGRadioGroup(pdfDoc, options);

            canvas.BeginLayer(radio1);
            canvasModel.ShowTextAligned("option 1", 50, 600, TextAlignment.LEFT, 0);
            canvas.EndLayer();

            canvas.BeginLayer(radio2);
            canvasModel.ShowTextAligned("option 2", 50, 575, TextAlignment.LEFT, 0);
            canvas.EndLayer();

            canvas.BeginLayer(radio3);
            canvasModel.ShowTextAligned("option 3", 50, 550, TextAlignment.LEFT, 0);
            canvas.EndLayer();

            PdfLayer not_printed = new PdfLayer("not printed", pdfDoc);

            not_printed.SetOnPanel(false);
            not_printed.SetPrint("Print", false);

            canvas.BeginLayer(not_printed);
            canvasModel.ShowTextAligned("PRINT THIS PAGE", 300, 700, TextAlignment.CENTER,
                                        (float)MathUtil.ToRadians(90));
            canvas.EndLayer();

            PdfLayer zoom = new PdfLayer("Zoom 0.75-1.25", pdfDoc);

            zoom.SetOnPanel(false);
            zoom.SetZoom(0.75f, 1.25f);

            canvas.BeginLayer(zoom);
            canvasModel.ShowTextAligned("Only visible if the zoomfactor is between 75 and 125%",
                                        30, 530, TextAlignment.LEFT, (float)MathUtil.ToRadians(90));
            canvas.EndLayer();

            pdfDoc.Close();
        }