Exemplo n.º 1
0
        private void Apply(
            PushButton field
            )
        {
            Document document = field.Document;
            Widget   widget   = field.Widgets[0];

            Appearance appearance = widget.Appearance;

            if (appearance == null)
            {
                widget.Appearance = appearance = new Appearance(document);
            }

            FormXObject normalAppearanceState;

            {
                SizeF size = widget.Box.Size;
                normalAppearanceState = new FormXObject(document, size);
                PrimitiveComposer composer = new PrimitiveComposer(normalAppearanceState);

                float      lineWidth = 1;
                RectangleF frame     = new RectangleF(lineWidth / 2, lineWidth / 2, size.Width - lineWidth, size.Height - lineWidth);
                if (GraphicsVisibile)
                {
                    composer.BeginLocalState();
                    composer.SetLineWidth(lineWidth);
                    composer.SetFillColor(BackColor);
                    composer.SetStrokeColor(ForeColor);
                    composer.DrawRectangle(frame, 5);
                    composer.FillStroke();
                    composer.End();
                }

                string title = (string)field.Value;
                if (title != null)
                {
                    BlockComposer blockComposer = new BlockComposer(composer);
                    blockComposer.Begin(frame, XAlignmentEnum.Center, YAlignmentEnum.Middle);
                    composer.SetFillColor(ForeColor);
                    composer.SetFont(
                        new StandardType1Font(
                            document,
                            StandardType1Font.FamilyEnum.Helvetica,
                            true,
                            false
                            ),
                        size.Height * 0.5
                        );
                    blockComposer.ShowText(title);
                    blockComposer.End();
                }

                composer.Flush();
            }
            appearance.Normal[null] = normalAppearanceState;
        }
Exemplo n.º 2
0
        private FormXObject CreateWatermark(Document document)
        {
            SKSize size = document.GetSize();

            // 1. Create an external form object to represent the watermark!
            FormXObject watermark = new FormXObject(document, size);

            // 2. Inserting the contents of the watermark...
            // 2.1. Create a content composer!
            PrimitiveComposer composer = new PrimitiveComposer(watermark);

            // 2.2. Inserting the contents...
            // Set the font to use!
            composer.SetFont(new fonts::StandardType1Font(document, fonts::StandardType1Font.FamilyEnum.Times, true, false), 120);
            // Set the color to fill the text characters!
            composer.SetFillColor(new DeviceRGBColor(115 / 255d, 164 / 255d, 232 / 255d));
            // Apply transparency!
            {
                ExtGState state = new ExtGState(document);
                state.FillAlpha = .3;
                composer.ApplyState(state);
            }
            // Show the text!
            composer.ShowText(
                "PdfClown",                                     // Text to show.
                new SKPoint(size.Width / 2f, size.Height / 2f), // Anchor location: page center.
                XAlignmentEnum.Center,                          // Horizontal placement (relative to the anchor): center.
                YAlignmentEnum.Middle,                          // Vertical placement (relative to the anchor): middle.
                50                                              // Rotation: 50-degree-counterclockwise.
                );
            // 2.3. Flush the contents into the watermark!
            composer.Flush();

            return(watermark);
        }
Exemplo n.º 3
0
        private void DrawText(PrimitiveComposer composer, string value, SKPoint location, XAlignmentEnum xAlignment, YAlignmentEnum yAlignment, float rotation)
        {
            // Show the anchor point!
            DrawCross(composer, location);

            composer.BeginLocalState();
            composer.SetFillColor(SampleColor);
            // Show the text onto the page!
            Quad textFrame = composer.ShowText(
                value,
                location,
                xAlignment,
                yAlignment,
                rotation);

            composer.End();

            // Draw the frame binding the shown text!
            DrawFrame(composer, textFrame.GetPoints());

            composer.BeginLocalState();
            composer.SetFont(composer.State.Font, 8);
            // Draw the rotation degrees!
            composer.ShowText(
                "(" + ((int)rotation) + " degrees)",
                new SKPoint(location.X + 70, location.Y),
                XAlignmentEnum.Left,
                YAlignmentEnum.Middle,
                0
                );
            composer.End();
        }
Exemplo n.º 4
0
        private void Apply(
            ComboBox field
            )
        {
            Document document = field.Document;
            Widget   widget   = field.Widgets[0];

            Appearance appearance = widget.Appearance;

            if (appearance == null)
            {
                widget.Appearance = appearance = new Appearance(document);
            }

            widget.BaseDataObject[PdfName.DA] = new PdfString("/Helv " + FontSize + " Tf 0 0 0 rg");

            FormXObject normalAppearanceState;

            {
                SizeF size = widget.Box.Size;
                normalAppearanceState = new FormXObject(document, size);
                PrimitiveComposer composer = new PrimitiveComposer(normalAppearanceState);

                float      lineWidth = 1;
                RectangleF frame     = new RectangleF(lineWidth / 2, lineWidth / 2, size.Width - lineWidth, size.Height - lineWidth);
                if (GraphicsVisibile)
                {
                    composer.BeginLocalState();
                    composer.SetLineWidth(lineWidth);
                    composer.SetFillColor(BackColor);
                    composer.SetStrokeColor(ForeColor);
                    composer.DrawRectangle(frame, 5);
                    composer.FillStroke();
                    composer.End();
                }

                composer.BeginMarkedContent(PdfName.Tx);
                composer.SetFont(
                    new StandardType1Font(
                        document,
                        StandardType1Font.FamilyEnum.Helvetica,
                        false,
                        false
                        ),
                    FontSize
                    );
                composer.ShowText(
                    (string)field.Value,
                    new PointF(0, size.Height / 2),
                    XAlignmentEnum.Left,
                    YAlignmentEnum.Middle,
                    0
                    );
                composer.End();

                composer.Flush();
            }
            appearance.Normal[null] = normalAppearanceState;
        }
Exemplo n.º 5
0
        /**
         * <summary>Populates a PDF file with contents.</summary>
         */
        private void Populate(
            Document document
            )
        {
            // 1. Add the page to the document!
            Page page = new Page(document); // Instantiates the page inside the document context.

            document.Pages.Add(page);       // Puts the page in the pages collection.

            // 2. Create a content composer for the page!
            PrimitiveComposer composer = new PrimitiveComposer(page);

            colors::Color textColor = new colors::DeviceRGBColor(115 / 255d, 164 / 255d, 232 / 255d);

            composer.SetFillColor(textColor);
            composer.SetLineDash(new LineDash(new double[] { 10 }));
            composer.SetLineWidth(.25);

            BlockComposer blockComposer = new BlockComposer(composer);

            blockComposer.Begin(new RectangleF(300, 400, 200, 100), XAlignmentEnum.Left, YAlignmentEnum.Middle);
            composer.SetFont(new fonts::StandardType1Font(document, fonts::StandardType1Font.FamilyEnum.Times, false, true), 12);
            blockComposer.ShowText("PrimitiveComposer.ShowText(...) methods return the actual bounding box of the text shown, allowing to precisely determine its location on the page.");
            blockComposer.End();

            // 3. Inserting contents...
            // Set the font to use!
            composer.SetFont(new fonts::StandardType1Font(document, fonts::StandardType1Font.FamilyEnum.Courier, true, false), 72);
            composer.DrawPolygon(
                composer.ShowText(
                    "Text frame",
                    new PointF(150, 360),
                    XAlignmentEnum.Left,
                    YAlignmentEnum.Middle,
                    45
                    ).Points
                );
            composer.Stroke();

            composer.SetFont(fonts::Font.Get(document, GetResourcePath("fonts" + System.IO.Path.DirectorySeparatorChar + "Ruritania-Outline.ttf")), 102);
            composer.DrawPolygon(
                composer.ShowText(
                    "Text frame",
                    new PointF(250, 600),
                    XAlignmentEnum.Center,
                    YAlignmentEnum.Middle,
                    -25
                    ).Points
                );
            composer.Stroke();

            // 4. Flush the contents into the page!
            composer.Flush();
        }
Exemplo n.º 6
0
        private void Stamp(
            Document document
            )
        {
            // 1. Instantiate the stamper!
            /* NOTE: The PageStamper is optimized for dealing with pages. */
            PageStamper stamper = new PageStamper();

            // 2. Numbering each page...
            StandardType1Font font = new StandardType1Font(
                document,
                StandardType1Font.FamilyEnum.Courier,
                true,
                false
                );
            DeviceRGBColor redColor = DeviceRGBColor.Get(SKColors.Red);
            int            margin   = 32;

            foreach (Page page in document.Pages)
            {
                // 2.1. Associate the page to the stamper!
                stamper.Page = page;

                // 2.2. Stamping the page number on the foreground...
                {
                    PrimitiveComposer foreground = stamper.Foreground;

                    foreground.SetFont(font, 16);
                    foreground.SetFillColor(redColor);

                    SKSize pageSize   = page.Size;
                    int    pageNumber = page.Number;
                    bool   pageIsEven = (pageNumber % 2 == 0);
                    foreground.ShowText(
                        pageNumber.ToString(),
                        new SKPoint(
                            (pageIsEven
                          ? margin
                          : pageSize.Width - margin),
                            pageSize.Height - margin
                            ),
                        (pageIsEven
                        ? XAlignmentEnum.Left
                        : XAlignmentEnum.Right),
                        YAlignmentEnum.Bottom,
                        0
                        );
                }

                // 2.3. End the stamping!
                stamper.Flush();
            }
        }
Exemplo n.º 7
0
        private void BuildWelcomePage(
            Document document,
            FormXObject template
            )
        {
            // Add welcome page to the document!
              Page page = new Page(document); // Instantiates the page inside the document context.
              document.Pages.Add(page); // Puts the page in the pages collection.
              SizeF pageSize = page.Size;

              PrimitiveComposer composer = new PrimitiveComposer(page);
              // Add the background template!
              composer.ShowXObject(template);
              // Wrap the content composer inside a block composer in order to achieve higher-level typographic control!
              /*
            NOTE: BlockComposer provides block-level typographic features as text and paragraph alignment.
            Flow-level typographic features are currently not supported: block-level typographic features
            are the foundations upon which flow-level typographic features will sit.
              */
              BlockComposer blockComposer = new BlockComposer(composer);

              SizeF breakSize = new SizeF(0,20); // Size of a paragraph break.
              // Instantiate the page body's font!
              fonts::Font font = fonts::Font.Get(
            document,
            GetResourcePath("fonts" + Path.DirectorySeparatorChar + "lazyDog.ttf")
            );

              // Showing the page title...
              // Define the box frame to force the page title within!
              RectangleF frame = new RectangleF(
            20,
            150,
            (float)pageSize.Width - 90,
            (float)pageSize.Height - 250
            );
              // Begin the block!
              blockComposer.Begin(frame,XAlignmentEnum.Center,YAlignmentEnum.Top);
              // Set the font to use!
              composer.SetFont(font,56);
              // Set the text rendering mode (outline only)!
              composer.SetTextRenderMode(TextRenderModeEnum.Stroke);
              // Show the page title!
              blockComposer.ShowText("Welcome");
              // End the block!
              blockComposer.End();

              // Showing the clown photo...
              // Instantiate a jpeg image object!
              entities::Image image = entities::Image.Get(GetResourcePath("images" + Path.DirectorySeparatorChar + "Clown.jpg")); // Abstract image (entity).
              PointF imageLocation = new PointF(
            blockComposer.BoundBox.X + blockComposer.BoundBox.Width - image.Width,
            blockComposer.BoundBox.Y + blockComposer.BoundBox.Height + 25
            );
              // Show the image!
              composer.ShowXObject(
            image.ToXObject(document),
            imageLocation
            );

              RectangleF descriptionFrame = new RectangleF(
            imageLocation.X,
            imageLocation.Y + image.Height + 5,
            image.Width,
            20
            );

              frame = new RectangleF(
            blockComposer.BoundBox.X,
            imageLocation.Y,
            blockComposer.BoundBox.Width - image.Width - 20,
            image.Height
            );
              blockComposer.Begin(frame,XAlignmentEnum.Left,YAlignmentEnum.Middle);
              {
            composer.SetFont(font,30);
            blockComposer.ShowText("This is a sample document that merely demonstrates some basic graphics features supported by PDF Clown.");
            blockComposer.ShowBreak(XAlignmentEnum.Center);
            blockComposer.ShowText("Enjoy!");
              }
              blockComposer.End();

              frame = new RectangleF(
            blockComposer.BoundBox.X,
            blockComposer.BoundBox.Y+blockComposer.BoundBox.Height,
            pageSize.Width - 90,
            pageSize.Height - 100 - (blockComposer.BoundBox.Y+blockComposer.BoundBox.Height)
            );
              blockComposer.Begin(frame,XAlignmentEnum.Justify,YAlignmentEnum.Bottom);
              {
            composer.SetFont(font,14);
            blockComposer.ShowText("PS: As promised, since version 0.0.3 PDF Clown has supported");
            // Begin local state!
            /*
              NOTE: Local state is a powerful feature of PDF format as it lets you nest
              multiple graphics contexts on the graphics state stack.
            */
            composer.BeginLocalState();
            {
              composer.SetFillColor(TextColor_Highlight);
              blockComposer.ShowText(" embedded latin OpenFont/TrueType and non-embedded Type 1 fonts");
            }
            composer.End();
            blockComposer.ShowText(" along with");
            composer.BeginLocalState();
            {
              composer.SetFillColor(TextColor_Highlight);
              blockComposer.ShowText(" paragraph construction facilities");
            }
            composer.End();
            blockComposer.ShowText(" through the BlockComposer class.");
            blockComposer.ShowBreak(breakSize);

            blockComposer.ShowText("Since version 0.0.4 the content stream stack has been completed, providing ");
            composer.BeginLocalState();
            {
              composer.SetFillColor(TextColor_Highlight);
              blockComposer.ShowText("fully object-oriented access to the graphics objects that describe the contents on a page.");
            }
            composer.End();
            blockComposer.ShowText(" It's a great step towards a whole bunch of possibilities, such as text extraction/replacement, that next releases will progressively exploit.");
            blockComposer.ShowBreak(breakSize);

            blockComposer.ShowText("Since version 0.0.6 it has supported ");
            composer.BeginLocalState();
            {
              composer.SetFillColor(TextColor_Highlight);
              blockComposer.ShowText("Unicode");
            }
            composer.End();
            blockComposer.ShowText(" for OpenFont/TrueType fonts.");
            blockComposer.ShowBreak(breakSize);

            composer.SetFont(font,8);
            blockComposer.ShowText("This page was crafted with the nice");
            composer.BeginLocalState();
            {
              composer.SetFont(font,10);
              blockComposer.ShowText(" LazyDog font");
            }
            composer.End();
            blockComposer.ShowText(" (by Paul Neave, www.neave.com)");
              }
              blockComposer.End();

              blockComposer.Begin(descriptionFrame,XAlignmentEnum.Right,YAlignmentEnum.Top);
              {
            composer.SetFont(font,8);
            blockComposer.ShowText("Source: http://www.wikipedia.org/");
              }
              blockComposer.End();

              composer.Flush();
        }
Exemplo n.º 8
0
        private void BuildCurvesPage(
            Document document
            )
        {
            // 1. Add the page to the document!
            Page page = new Page(document); // Instantiates the page inside the document context.

            document.Pages.Add(page);       // Puts the page in the pages collection.

            SizeF pageSize = page.Size;

            // 2. Create a content composer for the page!
            PrimitiveComposer composer = new PrimitiveComposer(page);

            // 3. Drawing the page contents...
            composer.SetFont(
                new fonts::StandardType1Font(
                    document,
                    fonts::StandardType1Font.FamilyEnum.Courier,
                    true,
                    false
                    ),
                32
                );

            {
                BlockComposer blockComposer = new BlockComposer(composer);
                blockComposer.Begin(new RectangleF(30, 0, pageSize.Width - 60, 50), XAlignmentEnum.Center, YAlignmentEnum.Middle);
                blockComposer.ShowText("Curves");
                blockComposer.End();
            }

            // 3.1. Arcs.
            {
                float y = 100;
                for (
                    int rowIndex = 0;
                    rowIndex < 4;
                    rowIndex++
                    )
                {
                    int   angleStep  = 45;
                    int   startAngle = 0;
                    int   endAngle   = angleStep;
                    float x          = 100;
                    float diameterX;
                    float diameterY;
                    switch (rowIndex)
                    {
                    case 0:
                    default:
                        diameterX = 40;
                        diameterY = 40;
                        break;

                    case 1:
                        diameterX = 40;
                        diameterY = 20;
                        break;

                    case 2:
                        diameterX = 20;
                        diameterY = 40;
                        break;

                    case 3:
                        diameterX = 40;
                        diameterY = 40;
                        break;
                    }
                    for (
                        int index = 0,
                        length = 360 / angleStep;
                        index < length;
                        index++
                        )
                    {
                        RectangleF arcFrame = new RectangleF((float)x, (float)y, (float)diameterX, (float)diameterY);

                        // Drawing the arc frame...
                        composer.BeginLocalState();
                        composer.SetLineWidth(0.25f);
                        composer.SetLineDash(new LineDash(new double[] { 5, 5 }, 3));
                        composer.DrawRectangle(arcFrame);
                        composer.Stroke();
                        composer.End();

                        // Draw the arc!
                        composer.DrawArc(arcFrame, startAngle, endAngle);
                        composer.Stroke();

                        endAngle += angleStep;
                        switch (rowIndex)
                        {
                        case 3:
                            startAngle += angleStep;
                            break;
                        }

                        x += 50;
                    }

                    y += diameterY + 10;
                }
            }

            // 3.2. Circle.
            {
                RectangleF arcFrame = new RectangleF(100, 300, 100, 100);

                // Drawing the circle frame...
                composer.BeginLocalState();
                composer.SetLineWidth(0.25f);
                composer.SetLineDash(new LineDash(new double[] { 5, 5 }, 3));
                composer.DrawRectangle(arcFrame);
                composer.Stroke();
                composer.End();

                // Drawing the circle...
                composer.SetFillColor(DeviceRGBColor.Get(System.Drawing.Color.Red));
                composer.DrawEllipse(arcFrame);
                composer.FillStroke();
            }

            // 3.3. Horizontal ellipse.
            {
                RectangleF arcFrame = new RectangleF(210, 300, 100, 50);

                // Drawing the ellipse frame...
                composer.BeginLocalState();
                composer.SetLineWidth(0.25f);
                composer.SetLineDash(new LineDash(new double[] { 5, 5 }, 3));
                composer.DrawRectangle(arcFrame);
                composer.Stroke();
                composer.End();

                // Drawing the ellipse...
                composer.SetFillColor(DeviceRGBColor.Get(System.Drawing.Color.Green));
                composer.DrawEllipse(arcFrame);
                composer.FillStroke();
            }

            // 3.4. Vertical ellipse.
            {
                RectangleF arcFrame = new RectangleF(320, 300, 50, 100);

                // Drawing the ellipse frame...
                composer.BeginLocalState();
                composer.SetLineWidth(0.25f);
                composer.SetLineDash(new LineDash(new double[] { 5, 5 }, 3));
                composer.DrawRectangle(arcFrame);
                composer.Stroke();
                composer.End();

                // Drawing the ellipse...
                composer.SetFillColor(DeviceRGBColor.Get(System.Drawing.Color.Blue));
                composer.DrawEllipse(arcFrame);
                composer.FillStroke();
            }

            // 3.5. Spirals.
            {
                float y           = 500;
                float spiralWidth = 100;
                composer.SetLineWidth(.5f);
                for (
                    int rowIndex = 0;
                    rowIndex < 3;
                    rowIndex++
                    )
                {
                    float x           = 150;
                    float branchWidth = .5f;
                    float branchRatio = 1;
                    for (
                        int spiralIndex = 0;
                        spiralIndex < 4;
                        spiralIndex++
                        )
                    {
                        float spiralTurnsCount;
                        switch (rowIndex)
                        {
                        case 0:
                        default:
                            spiralTurnsCount = spiralWidth / (branchWidth * 8);
                            break;

                        case 1:
                            spiralTurnsCount = (float)(spiralWidth / (branchWidth * 8 * (spiralIndex * 1.15 + 1)));
                            break;
                        }
                        switch (rowIndex)
                        {
                        case 2:
                            composer.SetLineDash(new LineDash(new double[] { 10, 5 }));
                            composer.SetLineCap(LineCapEnum.Round);
                            break;

                        default:
                            break;
                        }

                        composer.DrawSpiral(
                            new PointF((float)x, (float)y),
                            0,
                            360 * spiralTurnsCount,
                            branchWidth,
                            branchRatio
                            );
                        composer.Stroke();

                        x += spiralWidth + 10;

                        switch (rowIndex)
                        {
                        case 0:
                        default:
                            branchWidth += 1;
                            break;

                        case 1:
                            branchRatio += .035f;
                            break;
                        }
                        switch (rowIndex)
                        {
                        case 2:
                            composer.SetLineWidth(composer.State.LineWidth + .5f);
                            break;
                        }
                    }

                    y += spiralWidth + 10;
                }
            }

            // 4. Flush the contents into the page!
            composer.Flush();
        }
Exemplo n.º 9
0
        private void BuildMiscellaneousPage(
            Document document
            )
        {
            // 1. Add the page to the document!
              Page page = new Page(document); // Instantiates the page inside the document context.
              document.Pages.Add(page); // Puts the page in the pages collection.

              SizeF pageSize = page.Size;

              // 2. Create a content composer for the page!
              PrimitiveComposer composer = new PrimitiveComposer(page);

              // 3. Drawing the page contents...
              composer.SetFont(
            new fonts::StandardType1Font(
              document,
              fonts::StandardType1Font.FamilyEnum.Courier,
              true,
              false
              ),
            32
            );

              {
            BlockComposer blockComposer = new BlockComposer(composer);
            blockComposer.Begin(new RectangleF(30,0,pageSize.Width-60,50),XAlignmentEnum.Center,YAlignmentEnum.Middle);
            blockComposer.ShowText("Miscellaneous");
            blockComposer.End();
              }

              composer.BeginLocalState();
              composer.SetLineJoin(LineJoinEnum.Round);
              composer.SetLineCap(LineCapEnum.Round);

              // 3.1. Polygon.
              composer.DrawPolygon(
            new PointF[]
            {
              new PointF(100,200),
              new PointF(150,150),
              new PointF(200,150),
              new PointF(250,200)
            }
            );

              // 3.2. Polyline.
              composer.DrawPolyline(
            new PointF[]
            {
              new PointF(300,200),
              new PointF(350,150),
              new PointF(400,150),
              new PointF(450,200)
            }
            );

              composer.Stroke();

              // 3.3. Rectangle (both squared and rounded).
              int x = 50;
              int radius = 0;
              while(x < 500)
              {
            if(x > 300)
            {
              composer.SetLineDash(new LineDash(new double[]{5,5}, 3));
            }

            composer.SetFillColor(new DeviceRGBColor(1, x / 500d, x / 500d));
            composer.DrawRectangle(
              new RectangleF(x, 250, 150, 100),
              radius // NOTE: radius parameter determines the rounded angle size.
              );
            composer.FillStroke();

            x += 175;
            radius += 10;
              }
              composer.End(); // End local state.

              composer.BeginLocalState();
              composer.SetFont(
            composer.State.Font,
            12
            );

              // 3.4. Line cap parameter.
              int y = 400;
              foreach(LineCapEnum lineCap
            in (LineCapEnum[])Enum.GetValues(typeof(LineCapEnum)))
              {
            composer.ShowText(
              lineCap + ":",
              new PointF(50,y),
              XAlignmentEnum.Left,
              YAlignmentEnum.Middle,
              0
              );
            composer.SetLineWidth(12);
            composer.SetLineCap(lineCap);
            composer.DrawLine(
              new PointF(120,y),
              new PointF(220,y)
              );
            composer.Stroke();

            composer.BeginLocalState();
            composer.SetLineWidth(1);
            composer.SetStrokeColor(DeviceRGBColor.White);
            composer.SetLineCap(LineCapEnum.Butt);
            composer.DrawLine(
              new PointF(120,y),
              new PointF(220,y)
              );
            composer.Stroke();
            composer.End(); // End local state.

            y += 30;
              }

              // 3.5. Line join parameter.
              y += 50;
              foreach(LineJoinEnum lineJoin
            in (LineJoinEnum[])Enum.GetValues(typeof(LineJoinEnum)))
              {
            composer.ShowText(
              lineJoin + ":",
              new PointF(50,y),
              XAlignmentEnum.Left,
              YAlignmentEnum.Middle,
              0
              );
            composer.SetLineWidth(12);
            composer.SetLineJoin(lineJoin);
            PointF[] points = new PointF[]
              {
            new PointF(120,y+25),
            new PointF(150,y-25),
            new PointF(180,y+25)
              };
            composer.DrawPolyline(points);
            composer.Stroke();

            composer.BeginLocalState();
            composer.SetLineWidth(1);
            composer.SetStrokeColor(DeviceRGBColor.White);
            composer.SetLineCap(LineCapEnum.Butt);
            composer.DrawPolyline(points);
            composer.Stroke();
            composer.End(); // End local state.

            y += 50;
              }
              composer.End(); // End local state.

              // 3.6. Clipping.
              /*
            NOTE: Clipping should be conveniently enclosed within a local state
            in order to easily resume the unaltered drawing area after the operation completes.
              */
              composer.BeginLocalState();
              composer.DrawPolygon(
            new PointF[]
            {
              new PointF(220,410),
              new PointF(300,490),
              new PointF(450,360),
              new PointF(430,520),
              new PointF(590,565),
              new PointF(420,595),
              new PointF(460,730),
              new PointF(380,650),
              new PointF(330,765),
              new PointF(310,640),
              new PointF(220,710),
              new PointF(275,570),
              new PointF(170,500),
              new PointF(275,510)
            }
            );
              composer.Clip();
              // Showing a clown image...
              // Instantiate a jpeg image object!
              entities::Image image = entities::Image.Get(GetResourcePath("images" + System.IO.Path.DirectorySeparatorChar + "Clown.jpg")); // Abstract image (entity).
              xObjects::XObject imageXObject = image.ToXObject(document);
              // Show the image!
              composer.ShowXObject(
            imageXObject,
            new PointF(170, 320),
            GeomUtils.Scale(imageXObject.Size, new SizeF(450,0))
            );
              composer.End(); // End local state.

              // 4. Flush the contents into the page!
              composer.Flush();
        }
Exemplo n.º 10
0
        /**
         * <summary>Populates a PDF file with contents.</summary>
         */
        private void Populate(Document document)
        {
            // Initialize a new page!
            Page page = new Page(document);

            document.Pages.Add(page);

            // Initialize the primitive composer (within the new page context)!
            PrimitiveComposer composer = new PrimitiveComposer(page);

            composer.SetFont(PdfType1Font.Load(document, PdfType1Font.FamilyEnum.Helvetica, true, false), 12);

            // Initialize the block composer (wrapping the primitive one)!
            BlockComposer blockComposer = new BlockComposer(composer);

            // Initialize the document layer configuration!
            LayerDefinition layerDefinition = document.Layer;

            document.ViewerPreferences.PageMode = ViewerPreferences.PageModeEnum.Layers; // Shows the layers tab on document opening.

            // Get the root collection of the layers displayed to the user!
            UILayers uiLayers = layerDefinition.UILayers;

            // Nested layers.
            Layer parentLayer;
            {
                parentLayer = new Layer(document, "Parent layer");
                uiLayers.Add(parentLayer);
                var childLayers = parentLayer.Children;

                var childLayer1 = new Layer(document, "Child layer 1");
                childLayers.Add(childLayer1);

                var childLayer2 = new Layer(document, "Child layer 2");
                childLayers.Add(childLayer2);
                childLayer2.Locked = true;

                /*
                 * NOTE: Graphical content can be controlled through layers in two ways:
                 * 1) marking content within content streams (that is content within the page body);
                 * 2) associating annotations and external objects (XObject) to the layers.
                 */

                XObject imageXObject = entities::Image.Get(GetResourcePath("images" + Path.DirectorySeparatorChar + "gnu.jpg")).ToXObject(document);
                imageXObject.Layer = childLayer1; // Associates the image to the layer.

                composer.ShowXObject(imageXObject, new SKPoint(200, 75));

                composer.BeginLayer(parentLayer); // Opens a marked block associating its contents with the specified layer.
                composer.ShowText(parentLayer.Title, new SKPoint(50, 50));
                composer.End();                   // Closes the marked block.

                composer.BeginLayer(childLayer1);
                composer.ShowText(childLayer1.Title, new SKPoint(50, 75));
                composer.End();

                composer.BeginLayer(childLayer2);
                composer.ShowText(childLayer2.Title, new SKPoint(50, 100));
                composer.End();
            }

            // Simple layer collection (labeled collection of inclusive-state layers).
            Layer simpleLayer1;
            {
                var simpleLayerCollection = new LayerCollection(document, "Simple layer collection");
                uiLayers.Add(simpleLayerCollection);

                simpleLayer1 = new Layer(document, "Simple layer 1");
                simpleLayerCollection.Add(simpleLayer1);

                var simpleLayer2 = new Layer(document, "Simple layer 2 (Design)");

                /*
                 * NOTE: Intent limits layer use in determining visibility to specific use contexts. In this
                 * case, we want to mark content as intended to represent a document designer's structural
                 * organization of artwork, hence it's outside the interactive use by document consumers.
                 */
                simpleLayer2.Intents = new HashSet <PdfName> {
                    IntentEnum.Design.Name()
                };
                simpleLayerCollection.Add(simpleLayer2);

                var simpleLayer3 = new Layer(document, "Simple layer 3");
                simpleLayerCollection.Add(simpleLayer3);

                blockComposer.Begin(SKRect.Create(50, 125, 200, 75), XAlignmentEnum.Left, YAlignmentEnum.Middle);

                composer.BeginLayer(simpleLayer1);
                blockComposer.ShowText(simpleLayer1.Title);
                composer.End();

                blockComposer.ShowBreak(new SKSize(0, 10));

                composer.BeginLayer(simpleLayer2);
                blockComposer.ShowText(simpleLayer2.Title);
                composer.End();

                blockComposer.ShowBreak(new SKSize(0, 10));

                composer.BeginLayer(simpleLayer3);
                blockComposer.ShowText(simpleLayer3.Title);
                composer.End();

                blockComposer.End();
            }

            // Radio layer collection (labeled collection of exclusive-state layers).
            Layer radioLayer2;

            {
                var radioLayerCollection = new LayerCollection(document, "Radio layer collection");
                uiLayers.Add(radioLayerCollection);

                var radioLayer1 = new Layer(document, "Radio layer 1")
                {
                    Visible = true
                };
                radioLayerCollection.Add(radioLayer1);

                radioLayer2 = new Layer(document, "Radio layer 2")
                {
                    Visible = false
                };
                radioLayerCollection.Add(radioLayer2);

                var radioLayer3 = new Layer(document, "Radio layer 3")
                {
                    Visible = false
                };
                radioLayerCollection.Add(radioLayer3);

                // Register this option group in the layer configuration!
                var optionGroup = new OptionGroup(document)
                {
                    radioLayer1, radioLayer2, radioLayer3
                };
                layerDefinition.OptionGroups.Add(optionGroup);

                blockComposer.Begin(SKRect.Create(50, 200, 200, 75), XAlignmentEnum.Left, YAlignmentEnum.Middle);

                composer.BeginLayer(radioLayer1);
                blockComposer.ShowText(radioLayer1.Title);
                composer.End();

                blockComposer.ShowBreak(new SKSize(0, 10));

                composer.BeginLayer(radioLayer2);
                blockComposer.ShowText(radioLayer2.Title);
                composer.End();

                blockComposer.ShowBreak(new SKSize(0, 10));

                composer.BeginLayer(radioLayer3);
                blockComposer.ShowText(radioLayer3.Title);
                composer.End();

                blockComposer.End();
            }

            // Layer state action.
            {
                var actionLayer = new Layer(document, "Action layer")
                {
                    Printable = false
                };

                composer.BeginLayer(actionLayer);
                composer.BeginLocalState();
                composer.SetFillColor(colors::DeviceRGBColor.Get(SKColors.Blue));
                composer.ShowText(
                    "Layer state action:\n * deselect \"" + simpleLayer1.Title + "\"\n   and \"" + radioLayer2.Title + "\"\n * toggle \"" + parentLayer.Title + "\"",
                    new SKPoint(400, 200),
                    XAlignmentEnum.Left,
                    YAlignmentEnum.Middle,
                    0,
                    new SetLayerState(
                        document,
                        new SetLayerState.LayerState(SetLayerState.StateModeEnum.Off, simpleLayer1, radioLayer2),
                        new SetLayerState.LayerState(SetLayerState.StateModeEnum.Toggle, parentLayer)
                        )
                    );
                composer.End();
                composer.End();
            }

            // Zoom-restricted layer.
            {
                var zoomRestrictedLayer = new Layer(document, "Zoom-restricted layer")
                {
                    ZoomRange = new Interval <double>(.75, 1.251)
                };                                                // NOTE: Change this interval to test other magnification ranges.

                composer.BeginLayer(zoomRestrictedLayer);
                new TextMarkup(
                    page,
                    composer.ShowText(zoomRestrictedLayer.Title + ": this text is only visible if zoom between 75% and 125%", new SKPoint(50, 290)),
                    "This is a highlight annotation visible only if zoom is between 75% and 125%",
                    TextMarkupType.Highlight
                    )
                {
                    Layer = zoomRestrictedLayer /* Associates the annotation to the layer. */
                };
                composer.End();
            }

            // Print-only layer.
            {
                var printOnlyLayer = new Layer(document, "Print-only layer")
                {
                    Visible   = false,
                    Printable = true
                };

                composer.BeginLayer(printOnlyLayer);
                composer.BeginLocalState();
                composer.SetFillColor(colors::DeviceRGBColor.Get(SKColors.Red));
                composer.ShowText(printOnlyLayer.Title, new SKPoint(25, 300), XAlignmentEnum.Left, YAlignmentEnum.Top, 90);
                composer.End();
                composer.End();
            }

            // Language-specific layer.
            {
                var languageLayer = new Layer(document, "Language-specific layer")
                {
                    Language          = new LanguageIdentifier("en-GB"), // NOTE: Change this to test other languages and locales.
                    LanguagePreferred = true,                            // Matches any system locale (e.g. en-US, en-AU...) if layer language (en-GB) doesn't match exactly the system language.
                    Printable         = false
                };

                blockComposer.Begin(SKRect.Create(50, 320, 500, 75), XAlignmentEnum.Left, YAlignmentEnum.Top);

                composer.BeginLayer(languageLayer);
                blockComposer.ShowText(languageLayer.Title + ": this text is visible only if current system language is english (\"en\", any locale (\"en-US\", \"en-GB\", \"en-NZ\", etc.)) and is hidden on print.");
                composer.End();

                blockComposer.End();
            }

            // User-specific layer.
            {
                var userLayer = new Layer(document, "User-specific layer")
                {
                    Users = new List <string> {
                        "Lizbeth", "Alice", "Stefano", "Johann"
                    },                                                                    // NOTE: Change these entries to test other user names.
                    UserType = Layer.UserTypeEnum.Individual
                };

                blockComposer.Begin(SKRect.Create(blockComposer.BoundBox.Left, blockComposer.BoundBox.Bottom + 15, blockComposer.BoundBox.Width, 75), XAlignmentEnum.Left, YAlignmentEnum.Top);

                composer.BeginLayer(userLayer);
                blockComposer.ShowText(userLayer.Title + ": this text is visible only to " + String.Join(", ", userLayer.Users) + " (exact match).");
                composer.End();

                blockComposer.End();
            }

            // Layer membership (composite layer visibility).
            {
                var layerMembership = new LayerMembership(document)
                {
                    /*
                     * NOTE: VisibilityExpression is a more flexible alternative to the combination of
                     * VisibilityPolicy and VisibilityMembers. However, for compatibility purposes is preferable
                     * to provide both of them (the latter combination will work as a fallback in case of older
                     * viewer application).
                     */
                    VisibilityExpression = new VisibilityExpression(
                        document,
                        VisibilityExpression.OperatorEnum.And,
                        new VisibilityExpression(
                            document,
                            VisibilityExpression.OperatorEnum.Not,
                            new VisibilityExpression(
                                document,
                                VisibilityExpression.OperatorEnum.Or,
                                simpleLayer1,
                                radioLayer2
                                )
                            ),
                        parentLayer
                        ),
                    VisibilityPolicy  = LayerMembership.VisibilityPolicyEnum.AnyOff,
                    VisibilityMembers = new List <Layer> {
                        simpleLayer1, radioLayer2, parentLayer
                    }
                };

                blockComposer.Begin(SKRect.Create(blockComposer.BoundBox.Left, blockComposer.BoundBox.Bottom + 15, blockComposer.BoundBox.Width, 75), XAlignmentEnum.Left, YAlignmentEnum.Top);

                composer.BeginLayer(layerMembership);
                blockComposer.ShowText(String.Format("Layer membership: the visibility of this text is computed combining multiple layer states into an expression (\"{0}\" and \"{1}\" must be OFF while \"{2}\" must be ON).", simpleLayer1.Title, radioLayer2.Title, parentLayer.Title));
                composer.End();

                blockComposer.End();
            }

            composer.Flush();
        }
Exemplo n.º 11
0
        private void Apply(
      CheckBox field
      )
        {
            Document document = field.Document;
              foreach(Widget widget in field.Widgets)
              {
            {
              PdfDictionary widgetDataObject = widget.BaseDataObject;
              widgetDataObject[PdfName.DA] = new PdfString("/ZaDb 0 Tf 0 0 0 rg");
              widgetDataObject[PdfName.MK] = new PdfDictionary(
            new PdfName[]
            {
              PdfName.BG,
              PdfName.BC,
              PdfName.CA
            },
            new PdfDirectObject[]
            {
              new PdfArray(new PdfDirectObject[]{PdfReal.Get(0.9412), PdfReal.Get(0.9412), PdfReal.Get(0.9412)}),
              new PdfArray(new PdfDirectObject[]{PdfInteger.Default, PdfInteger.Default, PdfInteger.Default}),
              new PdfString("4")
            }
            );
              widgetDataObject[PdfName.BS] = new PdfDictionary(
            new PdfName[]
            {
              PdfName.W,
              PdfName.S
            },
            new PdfDirectObject[]
            {
              PdfReal.Get(0.8),
              PdfName.S
            }
            );
              widgetDataObject[PdfName.H] = PdfName.P;
            }

            Appearance appearance = widget.Appearance;
            if(appearance == null)
            {widget.Appearance = appearance = new Appearance(document);}

            SizeF size = widget.Box.Size;

            AppearanceStates normalAppearance = appearance.Normal;
            FormXObject onState = new FormXObject(document, size);
            normalAppearance[PdfName.Yes] = onState;

              //TODO:verify!!!
              //   appearance.getRollover().put(PdfName.Yes,onState);
              //   appearance.getDown().put(PdfName.Yes,onState);
              //   appearance.getRollover().put(PdfName.Off,offState);
              //   appearance.getDown().put(PdfName.Off,offState);

            float lineWidth = 1;
            RectangleF frame = new RectangleF(lineWidth / 2, lineWidth / 2, size.Width - lineWidth, size.Height - lineWidth);
            {
              PrimitiveComposer composer = new PrimitiveComposer(onState);

              if(GraphicsVisibile)
              {
            composer.BeginLocalState();
            composer.SetLineWidth(lineWidth);
            composer.SetFillColor(BackColor);
            composer.SetStrokeColor(ForeColor);
            composer.DrawRectangle(frame, 5);
            composer.FillStroke();
            composer.End();
              }

              BlockComposer blockComposer = new BlockComposer(composer);
              blockComposer.Begin(frame,XAlignmentEnum.Center,YAlignmentEnum.Middle);
              composer.SetFillColor(ForeColor);
              composer.SetFont(
            new StandardType1Font(
              document,
              StandardType1Font.FamilyEnum.ZapfDingbats,
              true,
              false
              ),
            size.Height * 0.8
            );
              blockComposer.ShowText(new String(new char[]{CheckSymbol}));
              blockComposer.End();

              composer.Flush();
            }

            FormXObject offState = new FormXObject(document, size);
            normalAppearance[PdfName.Off] = offState;
            {
              if(GraphicsVisibile)
              {
            PrimitiveComposer composer = new PrimitiveComposer(offState);

            composer.BeginLocalState();
            composer.SetLineWidth(lineWidth);
            composer.SetFillColor(BackColor);
            composer.SetStrokeColor(ForeColor);
            composer.DrawRectangle(frame, 5);
            composer.FillStroke();
            composer.End();

            composer.Flush();
              }
            }
              }
        }
Exemplo n.º 12
0
        private void Apply(
      ComboBox field
      )
        {
            Document document = field.Document;
              Widget widget = field.Widgets[0];

              Appearance appearance = widget.Appearance;
              if(appearance == null)
              {widget.Appearance = appearance = new Appearance(document);}

              widget.BaseDataObject[PdfName.DA] = new PdfString("/Helv " + FontSize + " Tf 0 0 0 rg");

              FormXObject normalAppearanceState;
              {
            SizeF size = widget.Box.Size;
            normalAppearanceState = new FormXObject(document, size);
            PrimitiveComposer composer = new PrimitiveComposer(normalAppearanceState);

            float lineWidth = 1;
            RectangleF frame = new RectangleF(lineWidth / 2, lineWidth / 2, size.Width - lineWidth, size.Height - lineWidth);
            if(GraphicsVisibile)
            {
              composer.BeginLocalState();
              composer.SetLineWidth(lineWidth);
              composer.SetFillColor(BackColor);
              composer.SetStrokeColor(ForeColor);
              composer.DrawRectangle(frame, 5);
              composer.FillStroke();
              composer.End();
            }

            composer.BeginMarkedContent(PdfName.Tx);
            composer.SetFont(
              new StandardType1Font(
            document,
            StandardType1Font.FamilyEnum.Helvetica,
            false,
            false
            ),
              FontSize
              );
            composer.ShowText(
              (string)field.Value,
              new PointF(0,size.Height/2),
              XAlignmentEnum.Left,
              YAlignmentEnum.Middle,
              0
              );
            composer.End();

            composer.Flush();
              }
              appearance.Normal[null] = normalAppearanceState;
        }
Exemplo n.º 13
0
        /*
         * TODO: refresh should happen just before serialization, on document event (e.g. OnWrite())
         */
        private void RefreshAppearance(
            )
        {
            FormXObject normalAppearance;
            RectangleF  box = org.pdfclown.objects.Rectangle.Wrap(BaseDataObject[PdfName.Rect]).ToRectangleF();
            {
                AppearanceStates normalAppearances = Appearance.Normal;
                normalAppearance = normalAppearances[null];
                if (normalAppearance != null)
                {
                    normalAppearance.Box = box;
                    normalAppearance.BaseDataObject.Body.SetLength(0);
                }
                else
                {
                    normalAppearances[null] = normalAppearance = new FormXObject(Document, box);
                }
            }

            PrimitiveComposer composer = new PrimitiveComposer(normalAppearance);

            {
                float          yOffset    = box.Height - Page.Box.Height;
                MarkupTypeEnum markupType = MarkupType;
                switch (markupType)
                {
                case MarkupTypeEnum.Highlight:
                {
                    ExtGState defaultExtGState;
                    {
                        ExtGStateResources extGStates = normalAppearance.Resources.ExtGStates;
                        defaultExtGState = extGStates[HighlightExtGStateName];
                        if (defaultExtGState == null)
                        {
                            if (extGStates.Count > 0)
                            {
                                extGStates.Clear();
                            }

                            extGStates[HighlightExtGStateName] = defaultExtGState = new ExtGState(Document);
                            defaultExtGState.AlphaShape        = false;
                            defaultExtGState.BlendMode         = new List <BlendModeEnum>(new BlendModeEnum[] { BlendModeEnum.Multiply });
                        }
                    }

                    composer.ApplyState(defaultExtGState);
                    composer.SetFillColor(Color);
                    {
                        foreach (Quad markupBox in MarkupBoxes)
                        {
                            PointF[] points          = markupBox.Points;
                            float    markupBoxHeight = points[3].Y - points[0].Y;
                            float    markupBoxMargin = GetMarkupBoxMargin(markupBoxHeight);
                            composer.DrawCurve(
                                new PointF(points[3].X, points[3].Y + yOffset),
                                new PointF(points[0].X, points[0].Y + yOffset),
                                new PointF(points[3].X - markupBoxMargin, points[3].Y - markupBoxMargin + yOffset),
                                new PointF(points[0].X - markupBoxMargin, points[0].Y + markupBoxMargin + yOffset)
                                );
                            composer.DrawLine(
                                new PointF(points[1].X, points[1].Y + yOffset)
                                );
                            composer.DrawCurve(
                                new PointF(points[2].X, points[2].Y + yOffset),
                                new PointF(points[1].X + markupBoxMargin, points[1].Y + markupBoxMargin + yOffset),
                                new PointF(points[2].X + markupBoxMargin, points[2].Y - markupBoxMargin + yOffset)
                                );
                            composer.Fill();
                        }
                    }
                }
                break;

                case MarkupTypeEnum.Squiggly:
                {
                    composer.SetStrokeColor(Color);
                    composer.SetLineCap(LineCapEnum.Round);
                    composer.SetLineJoin(LineJoinEnum.Round);
                    {
                        foreach (Quad markupBox in MarkupBoxes)
                        {
                            PointF[] points          = markupBox.Points;
                            float    markupBoxHeight = points[3].Y - points[0].Y;
                            float    lineWidth       = markupBoxHeight * .05f;
                            float    step            = markupBoxHeight * .125f;
                            float    boxXOffset      = points[3].X;
                            float    boxYOffset      = points[3].Y + yOffset - lineWidth;
                            bool     phase           = false;
                            composer.SetLineWidth(lineWidth);
                            for (float x = 0, xEnd = points[2].X - boxXOffset; x < xEnd || !phase; x += step)
                            {
                                PointF point = new PointF(x + boxXOffset, (phase ? -step : 0) + boxYOffset);
                                if (x == 0)
                                {
                                    composer.StartPath(point);
                                }
                                else
                                {
                                    composer.DrawLine(point);
                                }
                                phase = !phase;
                            }
                        }
                        composer.Stroke();
                    }
                }
                break;

                case MarkupTypeEnum.StrikeOut:
                case MarkupTypeEnum.Underline:
                {
                    composer.SetStrokeColor(Color);
                    {
                        float lineYRatio = 0;
                        switch (markupType)
                        {
                        case MarkupTypeEnum.StrikeOut:
                            lineYRatio = .5f;
                            break;

                        case MarkupTypeEnum.Underline:
                            lineYRatio = .9f;
                            break;

                        default:
                            throw new NotImplementedException();
                        }
                        foreach (Quad markupBox in MarkupBoxes)
                        {
                            PointF[] points          = markupBox.Points;
                            float    markupBoxHeight = points[3].Y - points[0].Y;
                            float    boxYOffset      = markupBoxHeight * lineYRatio + yOffset;
                            composer.SetLineWidth(markupBoxHeight * .065);
                            composer.DrawLine(
                                new PointF(points[3].X, points[0].Y + boxYOffset),
                                new PointF(points[2].X, points[1].Y + boxYOffset)
                                );
                        }
                        composer.Stroke();
                    }
                }
                break;

                default:
                    throw new NotImplementedException();
                }
            }
            composer.Flush();
        }
Exemplo n.º 14
0
        private void BuildLegend(PrimitiveComposer composer, string[] steps, colorSpaces::Color[] colors, SKSize pageSize)
        {
            float maxCtmInversionApproximation;
            {
                float[] ctmInversionApproximations = new float[9];
                {
                    float[] initialCtmValues, finalCtmValues;
                    {
                        var state = composer.Scanner.State;
                        initialCtmValues = state.GetInitialCtm().Values;
                        finalCtmValues   = state.Ctm.Values;
                    }
                    for (
                        int index = 0,
                        length = finalCtmValues.Length;
                        index < length;
                        index++
                        )
                    {
                        ctmInversionApproximations[index] = Math.Abs(finalCtmValues[index]) - initialCtmValues[index];
                    }
                }
                maxCtmInversionApproximation = Max(ctmInversionApproximations);
            }

            BlockComposer blockComposer = new BlockComposer(composer);

            blockComposer.LineSpace = new Length(.25, Length.UnitModeEnum.Relative);

            composer.BeginLocalState();
            composer.SetFillColor(
                new colorSpaces::DeviceRGBColor(115 / 255d, 164 / 255d, 232 / 255d)
                );
            SKRect frame = SKRect.Create(
                18,
                18,
                pageSize.Width * .5f,
                pageSize.Height * .5f
                );

            blockComposer.Begin(frame, XAlignmentEnum.Left, YAlignmentEnum.Top);
            composer.SetFont(ResourceName_DefaultFont, 24);
            blockComposer.ShowText("Page coordinates sample");
            SKSize breakSize = new SKSize(0, 8);

            blockComposer.ShowBreak(breakSize);
            composer.SetFont(ResourceName_DefaultFont, 8);
            blockComposer.ShowText(
                "This sample shows the effects of the manipulation of the CTM (Current Transformation Matrix), "
                + "that is the mathematical device which affects the page coordinate system used to place "
                + "graphic contents onto the canvas."
                );
            blockComposer.ShowBreak(breakSize);
            blockComposer.ShowText(
                "The following steps represent the operations applied to this page's CTM in order to alter it. "
                + "Each step writes the word \"Step\" at the lower-left corner of the current page frame:"
                );
            blockComposer.ShowBreak(breakSize);
            for (int i = 0; i < steps.Length; i++)
            {
                composer.SetFillColor(colors[i]);
                blockComposer.ShowText("Step " + i + ")");
                composer.SetFillColor(
                    new colorSpaces::DeviceRGBColor(115 / 255d, 164 / 255d, 232 / 255d)
                    );
                blockComposer.ShowText(" " + steps[i]);
                blockComposer.ShowBreak(breakSize);
            }
            blockComposer.ShowText("Note that the approximation (" + maxCtmInversionApproximation + ") of the CTM components at step 4 is due to floating point precision limits; their exact values should be 1.0, 0.0, 0.0, 1.0, 0.0, 0.0.");
            blockComposer.End();
            composer.End();
        }
Exemplo n.º 15
0
        private void BuildLegend(
            PrimitiveComposer composer,
            string[] steps,
            colorSpaces::Color[] colors,
            SizeF pageSize
            )
        {
            float maxCtmInversionApproximation;
              {
            float[] ctmInversionApproximations = new float[6];
            {
              float[] initialCtmValues, finalCtmValues;
              {
            ContentScanner.GraphicsState state = composer.Scanner.State;
            initialCtmValues = state.GetInitialCtm().Elements;
            finalCtmValues = state.Ctm.Elements;
              }
              for(
            int index = 0,
              length = finalCtmValues.Length;
            index < length;
            index++
            )
              {ctmInversionApproximations[index] = Math.Abs(finalCtmValues[index]) - initialCtmValues[index];}
            }
            maxCtmInversionApproximation = Max(ctmInversionApproximations);
              }

              BlockComposer blockComposer = new BlockComposer(composer);
              blockComposer.LineSpace = new Length(.25, Length.UnitModeEnum.Relative);

              composer.BeginLocalState();
              composer.SetFillColor(
            new colorSpaces::DeviceRGBColor(115 / 255d, 164 / 255d, 232 / 255d)
            );
              RectangleF frame = new RectangleF(
            18,
            18,
            pageSize.Width * .5f,
            pageSize.Height * .5f
            );
              blockComposer.Begin(frame,XAlignmentEnum.Left,YAlignmentEnum.Top);
              composer.SetFont(ResourceName_DefaultFont,24);
              blockComposer.ShowText("Page coordinates sample");
              SizeF breakSize = new SizeF(0,8);
              blockComposer.ShowBreak(breakSize);
              composer.SetFont(ResourceName_DefaultFont,8);
              blockComposer.ShowText(
            "This sample shows the effects of the manipulation of the CTM (Current Transformation Matrix), "
              + "that is the mathematical device which affects the page coordinate system used to place "
              + "graphic contents onto the canvas."
            );
              blockComposer.ShowBreak(breakSize);
              blockComposer.ShowText(
            "The following steps represent the operations applied to this page's CTM in order to alter it. "
              + "Each step writes the word \"Step\" at the lower-left corner of the current page frame:"
            );
              blockComposer.ShowBreak(breakSize);
              for(int i = 0; i < steps.Length; i++)
              {
            composer.SetFillColor(colors[i]);
            blockComposer.ShowText("Step " + i + ")");
            composer.SetFillColor(
              new colorSpaces::DeviceRGBColor(115 / 255d, 164 / 255d, 232 / 255d)
              );
            blockComposer.ShowText(" " + steps[i]);
            blockComposer.ShowBreak(breakSize);
              }
              blockComposer.ShowText("Note that the approximation (" + maxCtmInversionApproximation + ") of the CTM components at step 4 is due to floating point precision limits; their exact values should be 1.0, 0.0, 0.0, 1.0, 0.0, 0.0.");
              blockComposer.End();
              composer.End();
        }
Exemplo n.º 16
0
        private void BuildSteps(
            PrimitiveComposer composer,
            string[] steps,
            colorSpaces::Color[] colors,
            SizeF pageSize
            )
        {
            composer.SetFont(ResourceName_DefaultFont,32);
              RectangleF frame = new RectangleF(
            0,
            0,
            pageSize.Width,
            pageSize.Height
            );

              // Step 0.
              {
            colors[0] = new colorSpaces::DeviceRGBColor(30 / 255d, 10 / 255d, 0);
            composer.SetFillColor(colors[0]);
            composer.SetStrokeColor(colors[0]);

            // Draw the page frame!
            composer.DrawRectangle(frame);
            composer.Stroke();

            // Draw the lower-left corner mark!
            composer.ShowText(
              "Step 0",
              new PointF(0,pageSize.Height),
              XAlignmentEnum.Left,
              YAlignmentEnum.Bottom,
              0
              );

            steps[0] = GetStepNote(composer,"default");
              }

              // Step 1.
              {
            colors[1] = new colorSpaces::DeviceRGBColor(80 / 255d, 25 / 255d, 0);
            composer.SetFillColor(colors[1]);
            composer.SetStrokeColor(colors[1]);

            // Transform the coordinate space, applying translation!
            composer.Translate(72,72);

            // Draw the page frame!
            composer.DrawRectangle(frame);
            composer.Stroke();

            // Draw the lower-left corner mark!
            composer.ShowText(
              "Step 1",
              new PointF(0,pageSize.Height),
              XAlignmentEnum.Left,
              YAlignmentEnum.Bottom,
              0
              );

            steps[1] = GetStepNote(composer,"after translate(72,72)");
              }

              // Step 2.
              {
            colors[2] = new colorSpaces::DeviceRGBColor(130 / 255d, 45 / 255d, 0);
            composer.SetFillColor(colors[2]);
            composer.SetStrokeColor(colors[2]);

            // Transform the coordinate space, applying clockwise rotation!
            composer.Rotate(-20);

            // Draw the page frame!
            composer.DrawRectangle(frame);
            composer.Stroke();

            // Draw the coordinate space origin mark!
            composer.ShowText("Origin 2");

            // Draw the lower-left corner mark!
            composer.ShowText(
              "Step 2",
              new PointF(0,pageSize.Height),
              XAlignmentEnum.Left,
              YAlignmentEnum.Bottom,
              0
              );

            steps[2] = GetStepNote(composer,"after rotate(20)");
              }

              // Step 3.
              {
            colors[3] = new colorSpaces::DeviceRGBColor(180 / 255d, 60 / 255d, 0);
            composer.SetFillColor(colors[3]);
            composer.SetStrokeColor(colors[3]);

            // Transform the coordinate space, applying translation and scaling!
            composer.Translate(0,72);
            composer.Scale(.5f,.5f);

            // Draw the page frame!
            composer.DrawRectangle(frame);
            composer.Stroke();

            // Draw the lower-left corner mark!
            composer.ShowText(
              "Step 3",
              new PointF(0,pageSize.Height),
              XAlignmentEnum.Left,
              YAlignmentEnum.Bottom,
              0
              );

            steps[3] = GetStepNote(composer,"after translate(0,72) and scale(.5,.5)");
              }

              // Step 4.
              {
            colors[4] = new colorSpaces::DeviceRGBColor(230 / 255d, 75 / 255d, 0);
            composer.SetFillColor(colors[4]);
            composer.SetStrokeColor(colors[4]);

            // Transform the coordinate space, restoring its initial CTM!
            composer.Add(
              ModifyCTM.GetResetCTM(
            composer.Scanner.State
            )
              );

            // Draw the page frame!
            composer.DrawRectangle(frame);
            composer.Stroke();

            // Draw the lower-left corner mark!
            composer.ShowText(
              "Step 4",
              new PointF(0,pageSize.Height),
              XAlignmentEnum.Left,
              YAlignmentEnum.Bottom,
              0
              );

            steps[4] = GetStepNote(composer,"after resetting CTM");
              }
        }
Exemplo n.º 17
0
        private void RefreshAppearance(
            )
        {
            Widget      widget = Widgets[0];
            FormXObject normalAppearance;
            {
                AppearanceStates normalAppearances = widget.Appearance.Normal;
                normalAppearance = normalAppearances[null];
                if (normalAppearance == null)
                {
                    normalAppearances[null] = normalAppearance = new FormXObject(Document, widget.Box.Size);
                }
            }
            PdfName fontName = null;
            double  fontSize = 0;
            {
                PdfString defaultAppearanceState = DefaultAppearanceState;
                if (defaultAppearanceState == null)
                {
                    // Retrieving the font to define the default appearance...
                    fonts::Font defaultFont     = null;
                    PdfName     defaultFontName = null;
                    {
                        // Field fonts.
                        FontResources normalAppearanceFonts = normalAppearance.Resources.Fonts;
                        foreach (KeyValuePair <PdfName, fonts::Font> entry in normalAppearanceFonts)
                        {
                            if (!entry.Value.Symbolic)
                            {
                                defaultFont     = entry.Value;
                                defaultFontName = entry.Key;
                                break;
                            }
                        }
                        if (defaultFontName == null)
                        {
                            // Common fonts.
                            FontResources formFonts = Document.Form.Resources.Fonts;
                            foreach (KeyValuePair <PdfName, fonts::Font> entry in formFonts)
                            {
                                if (!entry.Value.Symbolic)
                                {
                                    defaultFont     = entry.Value;
                                    defaultFontName = entry.Key;
                                    break;
                                }
                            }
                            if (defaultFontName == null)
                            {
                                //TODO:manage name collision!
                                formFonts[
                                    defaultFontName = new PdfName("default")
                                ] = defaultFont = new fonts::StandardType1Font(
                                    Document,
                                    fonts::StandardType1Font.FamilyEnum.Helvetica,
                                    false,
                                    false
                                    );
                            }
                            normalAppearanceFonts[defaultFontName] = defaultFont;
                        }
                    }
                    bytes::Buffer buffer = new bytes::Buffer();
                    new SetFont(defaultFontName, IsMultiline ? 10 : 0).WriteTo(buffer, Document);
                    widget.BaseDataObject[PdfName.DA] = defaultAppearanceState = new PdfString(buffer.ToByteArray());
                }

                // Retrieving the font to use...
                ContentParser parser = new ContentParser(defaultAppearanceState.ToByteArray());
                foreach (ContentObject content in parser.ParseContentObjects())
                {
                    if (content is SetFont)
                    {
                        SetFont setFontOperation = (SetFont)content;
                        fontName = setFontOperation.Name;
                        fontSize = setFontOperation.Size;
                        break;
                    }
                }
                normalAppearance.Resources.Fonts[fontName] = Document.Form.Resources.Fonts[fontName];
            }

            // Refreshing the field appearance...

            /*
             * TODO: resources MUST be resolved both through the apperance stream resource dictionary and
             * from the DR-entry acroform resource dictionary
             */
            PrimitiveComposer baseComposer = new PrimitiveComposer(normalAppearance);
            BlockComposer     composer     = new BlockComposer(baseComposer);
            ContentScanner    currentLevel = composer.Scanner;
            bool textShown = false;

            while (currentLevel != null)
            {
                if (!currentLevel.MoveNext())
                {
                    currentLevel = currentLevel.ParentLevel;
                    continue;
                }

                ContentObject content = currentLevel.Current;
                if (content is MarkedContent)
                {
                    MarkedContent markedContent = (MarkedContent)content;
                    if (PdfName.Tx.Equals(((BeginMarkedContent)markedContent.Header).Tag))
                    {
                        // Remove old text representation!
                        markedContent.Objects.Clear();
                        // Add new text representation!
                        baseComposer.Scanner = currentLevel.ChildLevel; // Ensures the composer places new contents within the marked content block.
                        ShowText(composer, fontName, fontSize);
                        textShown = true;
                    }
                }
                else if (content is Text)
                {
                    currentLevel.Remove();
                }
                else if (currentLevel.ChildLevel != null)
                {
                    currentLevel = currentLevel.ChildLevel;
                }
            }
            if (!textShown)
            {
                baseComposer.BeginMarkedContent(PdfName.Tx);
                baseComposer.SetFillColor(new DeviceGrayColor(0)); // workaround for problem when input box fill color remains set
                ShowText(composer, fontName, fontSize);
                baseComposer.End();
            }
            baseComposer.Flush();
        }
Exemplo n.º 18
0
        private void BuildCurvesPage(
            Document document
            )
        {
            // 1. Add the page to the document!
              Page page = new Page(document); // Instantiates the page inside the document context.
              document.Pages.Add(page); // Puts the page in the pages collection.

              SizeF pageSize = page.Size;

              // 2. Create a content composer for the page!
              PrimitiveComposer composer = new PrimitiveComposer(page);

              // 3. Drawing the page contents...
              composer.SetFont(
            new fonts::StandardType1Font(
              document,
              fonts::StandardType1Font.FamilyEnum.Courier,
              true,
              false
              ),
            32
            );

              {
            BlockComposer blockComposer = new BlockComposer(composer);
            blockComposer.Begin(new RectangleF(30,0,pageSize.Width-60,50),XAlignmentEnum.Center,YAlignmentEnum.Middle);
            blockComposer.ShowText("Curves");
            blockComposer.End();
              }

              // 3.1. Arcs.
              {
            float y = 100;
            for(
              int rowIndex = 0;
              rowIndex < 4;
              rowIndex++
              )
            {
              int angleStep = 45;
              int startAngle = 0;
              int endAngle = angleStep;
              float x = 100;
              float diameterX;
              float diameterY;
              switch(rowIndex)
              {
            case 0: default:
              diameterX = 40;
              diameterY = 40;
              break;
            case 1:
              diameterX = 40;
              diameterY = 20;
              break;
            case 2:
              diameterX = 20;
              diameterY = 40;
              break;
            case 3:
              diameterX = 40;
              diameterY = 40;
              break;
              }
              for(
            int index = 0,
              length = 360/angleStep;
            index < length;
            index++
            )
              {
            RectangleF arcFrame = new RectangleF((float)x,(float)y,(float)diameterX,(float)diameterY);

            // Drawing the arc frame...
            composer.BeginLocalState();
            composer.SetLineWidth(0.25f);
            composer.SetLineDash(new LineDash(new double[]{5,5}, 3));
            composer.DrawRectangle(arcFrame);
            composer.Stroke();
            composer.End();

            // Draw the arc!
            composer.DrawArc(arcFrame,startAngle,endAngle);
            composer.Stroke();

            endAngle += angleStep;
            switch(rowIndex)
            {
              case 3:
                startAngle += angleStep;
                break;
            }

            x += 50;
              }

              y += diameterY + 10;
            }
              }

              // 3.2. Circle.
              {
            RectangleF arcFrame = new RectangleF(100, 300, 100, 100);

            // Drawing the circle frame...
            composer.BeginLocalState();
            composer.SetLineWidth(0.25f);
            composer.SetLineDash(new LineDash(new double[]{5,5}, 3));
            composer.DrawRectangle(arcFrame);
            composer.Stroke();
            composer.End();

            // Drawing the circle...
            composer.SetFillColor(DeviceRGBColor.Get(System.Drawing.Color.Red));
            composer.DrawEllipse(arcFrame);
            composer.FillStroke();
              }

              // 3.3. Horizontal ellipse.
              {
            RectangleF arcFrame = new RectangleF(210, 300, 100, 50);

            // Drawing the ellipse frame...
            composer.BeginLocalState();
            composer.SetLineWidth(0.25f);
            composer.SetLineDash(new LineDash(new double[]{5,5}, 3));
            composer.DrawRectangle(arcFrame);
            composer.Stroke();
            composer.End();

            // Drawing the ellipse...
            composer.SetFillColor(DeviceRGBColor.Get(System.Drawing.Color.Green));
            composer.DrawEllipse(arcFrame);
            composer.FillStroke();
              }

              // 3.4. Vertical ellipse.
              {
            RectangleF arcFrame = new RectangleF(320, 300, 50, 100);

            // Drawing the ellipse frame...
            composer.BeginLocalState();
            composer.SetLineWidth(0.25f);
            composer.SetLineDash(new LineDash(new double[]{5,5}, 3));
            composer.DrawRectangle(arcFrame);
            composer.Stroke();
            composer.End();

            // Drawing the ellipse...
            composer.SetFillColor(DeviceRGBColor.Get(System.Drawing.Color.Blue));
            composer.DrawEllipse(arcFrame);
            composer.FillStroke();
              }

              // 3.5. Spirals.
              {
            float y = 500;
            float spiralWidth = 100;
            composer.SetLineWidth(.5f);
            for(
              int rowIndex = 0;
              rowIndex < 3;
              rowIndex++
              )
            {
              float x = 150;
              float branchWidth = .5f;
              float branchRatio = 1;
              for(
            int spiralIndex = 0;
            spiralIndex < 4;
            spiralIndex++
            )
              {
            float spiralTurnsCount;
            switch(rowIndex)
            {
              case 0: default:
                spiralTurnsCount = spiralWidth/(branchWidth*8);
                break;
              case 1:
                spiralTurnsCount = (float)(spiralWidth/(branchWidth*8*(spiralIndex*1.15+1)));
                break;
            }
            switch(rowIndex)
            {
              case 2:
                composer.SetLineDash(new LineDash(new double[]{10,5}));
                composer.SetLineCap(LineCapEnum.Round);
                break;
              default:
                break;
            }

            composer.DrawSpiral(
              new PointF((float)x,(float)y),
              0,
              360*spiralTurnsCount,
              branchWidth,
              branchRatio
              );
            composer.Stroke();

            x += spiralWidth + 10;

            switch(rowIndex)
            {
              case 0: default:
                branchWidth += 1;
                break;
              case 1:
                branchRatio += .035f;
                break;
            }
            switch(rowIndex)
            {
              case 2:
                composer.SetLineWidth(composer.State.LineWidth+.5f);
                break;
            }
              }

              y += spiralWidth + 10;
            }
              }

              // 4. Flush the contents into the page!
              composer.Flush();
        }
Exemplo n.º 19
0
        private void BuildSimpleTextPage(
            Document document
            )
        {
            // 1. Add the page to the document!
              Page page = new Page(document); // Instantiates the page inside the document context.
              document.Pages.Add(page); // Puts the page in the pages collection.

              SizeF pageSize = page.Size;

              // 2. Create a content composer for the page!
              PrimitiveComposer composer = new PrimitiveComposer(page);
              // 3. Inserting contents...
              // Set the font to use!
              composer.SetFont(
            new fonts::StandardType1Font(
              document,
              fonts::StandardType1Font.FamilyEnum.Courier,
              true,
              false
              ),
            32
            );

              XAlignmentEnum[] xAlignments = (XAlignmentEnum[])Enum.GetValues(typeof(XAlignmentEnum));
              YAlignmentEnum[] yAlignments = (YAlignmentEnum[])Enum.GetValues(typeof(YAlignmentEnum));
              int step = (int)(pageSize.Height) / ((xAlignments.Length-1) * yAlignments.Length+1);

              BlockComposer blockComposer = new BlockComposer(composer);
              RectangleF frame = new RectangleF(
            30,
            0,
            pageSize.Width-60,
            step/2
            );
              blockComposer.Begin(frame,XAlignmentEnum.Center,YAlignmentEnum.Middle);
              blockComposer.ShowText("Simple alignment");
              blockComposer.End();

              frame = new RectangleF(
            30,
            pageSize.Height-step/2,
            pageSize.Width-60,
            step/2 -10
            );
              blockComposer.Begin(frame,XAlignmentEnum.Left,YAlignmentEnum.Bottom);
              composer.SetFont(composer.State.Font,10);
              blockComposer.ShowText(
            "NOTE: showText(...) methods return the actual bounding box of the text shown.\n"
              + "NOTE: The rotation parameter can be freely defined as a floating point value."
            );
              blockComposer.End();

              composer.SetFont(composer.State.Font,12);
              int x = 30;
              int y = step;
              int alignmentIndex = 0;
              foreach(XAlignmentEnum xAlignment
            in (XAlignmentEnum[])Enum.GetValues(typeof(XAlignmentEnum)))
              {
            /*
              NOTE: As text shown through PrimitiveComposer has no bounding box constraining its extension,
              applying the justified alignment has no effect (it degrades to center alignment);
              in order to get such an effect, use BlockComposer instead.
            */
            if(xAlignment.Equals(XAlignmentEnum.Justify))
              continue;

            foreach(YAlignmentEnum yAlignment
              in (YAlignmentEnum[])Enum.GetValues(typeof(YAlignmentEnum)))
            {
              if(alignmentIndex % 2 == 0)
              {
            composer.BeginLocalState();
            composer.SetFillColor(BackColor);
            composer.DrawRectangle(
              new RectangleF(
                0,
                y-step/2,
                pageSize.Width,
                step
                )
              );
            composer.Fill();
            composer.End();
              }

              composer.ShowText(
            xAlignment + " " + yAlignment + ":",
            new PointF(x,y),
            XAlignmentEnum.Left,
            YAlignmentEnum.Middle,
            0
            );

              y+=step;
              alignmentIndex++;
            }
              }

              float rotationStep = 0;
              float rotation = 0;
              for(
            int columnIndex = 0;
            columnIndex < 2;
            columnIndex++
            )
              {
            switch(columnIndex)
            {
              case 0:
            x = 200;
            rotationStep = 0;
            break;
              case 1:
            x = (int)pageSize.Width / 2 + 100;
            rotationStep = 360 / ((xAlignments.Length-1) * yAlignments.Length-1);
            break;
            }
            y = step;
            rotation = 0;
            foreach(XAlignmentEnum xAlignment
              in (XAlignmentEnum[])Enum.GetValues(typeof(XAlignmentEnum)))
            {
              /*
            NOTE: As text shown through PrimitiveComposer has no bounding box constraining its extension,
            applying the justified alignment has no effect (it degrades to center alignment);
            in order to get such an effect, use BlockComposer instead.
              */
              if(xAlignment.Equals(XAlignmentEnum.Justify))
            continue;

              foreach(YAlignmentEnum yAlignment
            in (YAlignmentEnum[])Enum.GetValues(typeof(YAlignmentEnum)))
              {
            float startArcAngle = 0;
            switch(xAlignment)
            {
              case XAlignmentEnum.Left:
                // OK -- NOOP.
                break;
              case XAlignmentEnum.Right:
              case XAlignmentEnum.Center:
                startArcAngle = 180;
                break;
            }

            composer.DrawArc(
              new RectangleF(
                x-10,
                y-10,
                20,
                20
                ),
              startArcAngle,
              startArcAngle+rotation
              );

            DrawText(
              composer,
              "PDF Clown",
              new PointF(x,y),
              xAlignment,
              yAlignment,
              rotation
              );
            y+=step;
            rotation+=rotationStep;
              }
            }
              }

              // 4. Flush the contents into the page!
              composer.Flush();
        }
Exemplo n.º 20
0
        /**
          <summary>Populates a PDF file with contents.</summary>
        */
        private void Populate(
            Document document
            )
        {
            // 1. Add the page to the document!
              Page page = new Page(document); // Instantiates the page inside the document context.
              document.Pages.Add(page); // Puts the page in the pages collection.

              // 2. Create a content composer for the page!
              PrimitiveComposer composer = new PrimitiveComposer(page);

              colors::Color textColor = new colors::DeviceRGBColor(115 / 255d, 164 / 255d, 232 / 255d);
              composer.SetFillColor(textColor);

              BlockComposer blockComposer = new BlockComposer(composer);
              blockComposer.Begin(new RectangleF(300, 400, 200, 100), XAlignmentEnum.Left, YAlignmentEnum.Middle);
              composer.SetFont(
            new fonts::StandardType1Font(
              document,
              fonts::StandardType1Font.FamilyEnum.Times,
              false,
              true
              ),
            12
            );
              blockComposer.ShowText("PrimitiveComposer.ShowText(...) methods return the actual bounding box of the text shown, allowing to precisely determine its location on the page.");
              blockComposer.End();

              // 3. Inserting contents...
              // Set the font to use!
              composer.SetFont(
            new fonts::StandardType1Font(
              document,
              fonts::StandardType1Font.FamilyEnum.Courier,
              true,
              false
              ),
            72
            );
              composer.DrawPolygon(
            composer.ShowText(
              "Text frame",
              new PointF(150, 360),
              XAlignmentEnum.Left,
              YAlignmentEnum.Middle,
              45
              ).Points
            );
              composer.Stroke();

              composer.SetFont(
            fonts::Font.Get(
              document,
              GetResourcePath("fonts" + System.IO.Path.DirectorySeparatorChar + "Ruritania-Outline.ttf")
              ),
            102
            );
              composer.DrawPolygon(
            composer.ShowText(
              "Text frame",
              new PointF(300, 600),
              XAlignmentEnum.Center,
              YAlignmentEnum.Middle,
              -25
              ).Points
            );
              composer.Stroke();

              // 4. Flush the contents into the page!
              composer.Flush();
        }
Exemplo n.º 21
0
        private void BuildLinks(Document document)
        {
            Pages pages = document.Pages;
            Page  page  = new Page(document);

            pages.Add(page);

            PrimitiveComposer composer      = new PrimitiveComposer(page);
            BlockComposer     blockComposer = new BlockComposer(composer);

            PdfType1Font font = PdfType1Font.Load(document, PdfType1Font.FamilyEnum.Courier, true, false);

            /*
             * 2.1. Goto-URI link.
             */
            {
                blockComposer.Begin(SKRect.Create(30, 100, 200, 50), XAlignmentEnum.Left, YAlignmentEnum.Middle);
                composer.SetFont(font, 12);
                blockComposer.ShowText("Go-to-URI link");
                composer.SetFont(font, 8);
                blockComposer.ShowText("\nIt allows you to navigate to a network resource.");
                composer.SetFont(font, 5);
                blockComposer.ShowText("\n\nClick on the box to go to the project's SourceForge.net repository.");
                blockComposer.End();

                /*
                 * NOTE: This statement instructs the PDF viewer to navigate to the given URI when the link is clicked.
                 */
                new Link(page, SKRect.Create(240, 100, 100, 50), "Link annotation",
                         new GoToURI(document, new Uri("http://www.sourceforge.net/projects/clown")))
                {
                    Border = new Border(3, BorderStyleType.Beveled)
                };
            }

            /*
             * 2.2. Embedded-goto link.
             */
            {
                string filePath = PromptFileChoice("Please select a PDF file to attach");

                /*
                 * NOTE: These statements instruct PDF Clown to attach a PDF file to the current document.
                 * This is necessary in order to test the embedded-goto functionality,
                 * as you can see in the following link creation (see below).
                 */
                int    fileAttachmentPageIndex = page.Index;
                string fileAttachmentName      = "attachedSamplePDF";
                string fileName = System.IO.Path.GetFileName(filePath);
                new FileAttachment(
                    page,
                    SKRect.Create(0, -20, 10, 10),
                    "File attachment annotation",
                    FileSpecification.Get(EmbeddedFile.Get(document, filePath),
                                          fileName
                                          )
                    )
                {
                    Name           = fileAttachmentName,
                    AttachmentName = FileAttachmentImageType.PaperClip
                };

                blockComposer.Begin(SKRect.Create(30, 170, 200, 50), XAlignmentEnum.Left, YAlignmentEnum.Middle);
                composer.SetFont(font, 12);
                blockComposer.ShowText("Go-to-embedded link");
                composer.SetFont(font, 8);
                blockComposer.ShowText("\nIt allows you to navigate to a destination within an embedded PDF file.");
                composer.SetFont(font, 5);
                blockComposer.ShowText("\n\nClick on the button to go to the 2nd page of the attached PDF file (" + fileName + ").");
                blockComposer.End();

                /*
                 * NOTE: This statement instructs the PDF viewer to navigate to the page 2 of a PDF file
                 * attached inside the current document as described by the FileAttachment annotation on page 1 of the current document.
                 */
                new Link(
                    page,
                    SKRect.Create(240, 170, 100, 50),
                    "Link annotation",
                    new GoToEmbedded(
                        document,
                        new GoToEmbedded.PathElement(
                            document,
                            fileAttachmentPageIndex, // Page of the current document containing the file attachment annotation of the target document.
                            fileAttachmentName,      // Name of the file attachment annotation corresponding to the target document.
                            null                     // No sub-target.
                            ),                       // Target represents the document to go to.
                        new RemoteDestination(
                            document,
                            1,                        // Show the page 2 of the target document.
                            Destination.ModeEnum.Fit, // Show the target document page entirely on the screen.
                            null,
                            null
                            ) // The destination must be within the target document.
                        )
                    )
                {
                    Border = new Border(1, new LineDash(new float[] { 8, 5, 2, 5 }))
                };
            }

            /*
             * 2.3. Textual link.
             */
            {
                blockComposer.Begin(SKRect.Create(30, 240, 200, 50), XAlignmentEnum.Left, YAlignmentEnum.Middle);
                composer.SetFont(font, 12);
                blockComposer.ShowText("Textual link");
                composer.SetFont(font, 8);
                blockComposer.ShowText("\nIt allows you to expose any kind of link (including the above-mentioned types) as text.");
                composer.SetFont(font, 5);
                blockComposer.ShowText("\n\nClick on the text links to go either to the project's SourceForge.net repository or to the project's home page.");
                blockComposer.End();

                composer.BeginLocalState();
                composer.SetFont(font, 10);
                composer.SetFillColor(DeviceRGBColor.Get(SKColors.Blue));
                composer.ShowText(
                    "PDF Clown Project's repository at SourceForge.net",
                    new SKPoint(240, 265),
                    XAlignmentEnum.Left,
                    YAlignmentEnum.Middle,
                    0,
                    new GoToURI(
                        document,
                        new Uri("http://www.sourceforge.net/projects/clown")
                        )
                    );
                composer.ShowText(
                    "PDF Clown Project's home page",
                    new SKPoint(240, 285),
                    XAlignmentEnum.Left,
                    YAlignmentEnum.Bottom,
                    -90,
                    new GoToURI(
                        document,
                        new Uri("http://www.pdfclown.org")
                        )
                    );
                composer.End();
            }

            composer.Flush();
        }
Exemplo n.º 22
0
        private void BuildSteps(
            PrimitiveComposer composer,
            string[] steps,
            colorSpaces::Color[] colors,
            SKSize pageSize
            )
        {
            composer.SetFont(ResourceName_DefaultFont, 32);
            SKRect frame = SKRect.Create(
                0,
                0,
                pageSize.Width,
                pageSize.Height
                );

            // Step 0.
            {
                colors[0] = new colorSpaces::DeviceRGBColor(30 / 255d, 10 / 255d, 0);
                composer.SetFillColor(colors[0]);
                composer.SetStrokeColor(colors[0]);

                // Draw the page frame!
                composer.DrawRectangle(frame);
                composer.Stroke();

                // Draw the lower-left corner mark!
                composer.ShowText(
                    "Step 0",
                    new SKPoint(0, pageSize.Height),
                    XAlignmentEnum.Left,
                    YAlignmentEnum.Bottom,
                    0
                    );

                steps[0] = GetStepNote(composer, "default");
            }

            // Step 1.
            {
                colors[1] = new colorSpaces::DeviceRGBColor(80 / 255d, 25 / 255d, 0);
                composer.SetFillColor(colors[1]);
                composer.SetStrokeColor(colors[1]);

                // Transform the coordinate space, applying translation!
                composer.Translate(72, 72);

                // Draw the page frame!
                composer.DrawRectangle(frame);
                composer.Stroke();

                // Draw the lower-left corner mark!
                composer.ShowText(
                    "Step 1",
                    new SKPoint(0, pageSize.Height),
                    XAlignmentEnum.Left,
                    YAlignmentEnum.Bottom,
                    0
                    );

                steps[1] = GetStepNote(composer, "after translate(72,72)");
            }

            // Step 2.
            {
                colors[2] = new colorSpaces::DeviceRGBColor(130 / 255d, 45 / 255d, 0);
                composer.SetFillColor(colors[2]);
                composer.SetStrokeColor(colors[2]);

                // Transform the coordinate space, applying clockwise rotation!
                composer.Rotate(-20);

                // Draw the page frame!
                composer.DrawRectangle(frame);
                composer.Stroke();

                // Draw the coordinate space origin mark!
                composer.ShowText("Origin 2");

                // Draw the lower-left corner mark!
                composer.ShowText(
                    "Step 2",
                    new SKPoint(0, pageSize.Height),
                    XAlignmentEnum.Left,
                    YAlignmentEnum.Bottom,
                    0
                    );

                steps[2] = GetStepNote(composer, "after rotate(20)");
            }

            // Step 3.
            {
                colors[3] = new colorSpaces::DeviceRGBColor(180 / 255d, 60 / 255d, 0);
                composer.SetFillColor(colors[3]);
                composer.SetStrokeColor(colors[3]);

                // Transform the coordinate space, applying translation and scaling!
                composer.Translate(0, 72);
                composer.Scale(.5f, .5f);

                // Draw the page frame!
                composer.DrawRectangle(frame);
                composer.Stroke();

                // Draw the lower-left corner mark!
                composer.ShowText(
                    "Step 3",
                    new SKPoint(0, pageSize.Height),
                    XAlignmentEnum.Left,
                    YAlignmentEnum.Bottom,
                    0
                    );

                steps[3] = GetStepNote(composer, "after translate(0,72) and scale(.5,.5)");
            }

            // Step 4.
            {
                colors[4] = new colorSpaces::DeviceRGBColor(230 / 255d, 75 / 255d, 0);
                composer.SetFillColor(colors[4]);
                composer.SetStrokeColor(colors[4]);

                // Transform the coordinate space, restoring its initial CTM!
                composer.Add(
                    ModifyCTM.GetResetCTM(
                        composer.Scanner.State
                        )
                    );

                // Draw the page frame!
                composer.DrawRectangle(frame);
                composer.Stroke();

                // Draw the lower-left corner mark!
                composer.ShowText(
                    "Step 4",
                    new SKPoint(0, pageSize.Height),
                    XAlignmentEnum.Left,
                    YAlignmentEnum.Bottom,
                    0
                    );

                steps[4] = GetStepNote(composer, "after resetting CTM");
            }
        }
Exemplo n.º 23
0
        private void Apply(
      PushButton field
      )
        {
            Document document = field.Document;
              Widget widget = field.Widgets[0];

              Appearance appearance = widget.Appearance;
              if(appearance == null)
              {widget.Appearance = appearance = new Appearance(document);}

              FormXObject normalAppearanceState;
              {
            SizeF size = widget.Box.Size;
            normalAppearanceState = new FormXObject(document, size);
            PrimitiveComposer composer = new PrimitiveComposer(normalAppearanceState);

            float lineWidth = 1;
            RectangleF frame = new RectangleF(lineWidth / 2, lineWidth / 2, size.Width - lineWidth, size.Height - lineWidth);
            if(GraphicsVisibile)
            {
              composer.BeginLocalState();
              composer.SetLineWidth(lineWidth);
              composer.SetFillColor(BackColor);
              composer.SetStrokeColor(ForeColor);
              composer.DrawRectangle(frame, 5);
              composer.FillStroke();
              composer.End();
            }

            string title = (string)field.Value;
            if(title != null)
            {
              BlockComposer blockComposer = new BlockComposer(composer);
              blockComposer.Begin(frame,XAlignmentEnum.Center,YAlignmentEnum.Middle);
              composer.SetFillColor(ForeColor);
              composer.SetFont(
            new StandardType1Font(
              document,
              StandardType1Font.FamilyEnum.Helvetica,
              true,
              false
              ),
            size.Height * 0.5
            );
              blockComposer.ShowText(title);
              blockComposer.End();
            }

            composer.Flush();
              }
              appearance.Normal[null] = normalAppearanceState;
        }
Exemplo n.º 24
0
        private void Apply(
            CheckBox field
            )
        {
            Document document = field.Document;

            foreach (Widget widget in field.Widgets)
            {
                {
                    PdfDictionary widgetDataObject = widget.BaseDataObject;
                    widgetDataObject[PdfName.DA] = new PdfString("/ZaDb 0 Tf 0 0 0 rg");
                    widgetDataObject[PdfName.MK] = new PdfDictionary(
                        new PdfName[]
                    {
                        PdfName.BG,
                        PdfName.BC,
                        PdfName.CA
                    },
                        new PdfDirectObject[]
                    {
                        new PdfArray(new PdfDirectObject[] { new PdfReal(0.9412), new PdfReal(0.9412), new PdfReal(0.9412) }),
                        new PdfArray(new PdfDirectObject[] { new PdfInteger(0), new PdfInteger(0), new PdfInteger(0) }),
                        new PdfString("4")
                    }
                        );
                    widgetDataObject[PdfName.BS] = new PdfDictionary(
                        new PdfName[]
                    {
                        PdfName.W,
                        PdfName.S
                    },
                        new PdfDirectObject[]
                    {
                        new PdfReal(0.8),
                        PdfName.S
                    }
                        );
                    widgetDataObject[PdfName.H] = PdfName.P;
                }

                Appearance appearance = widget.Appearance;
                if (appearance == null)
                {
                    widget.Appearance = appearance = new Appearance(document);
                }

                AppearanceStates normalAppearance = appearance.Normal;
                FormXObject      onState          = new FormXObject(document);
                normalAppearance[PdfName.Yes] = onState;

                //TODO:verify!!!
                //   appearance.getRollover().put(PdfName.Yes,onState);
                //   appearance.getDown().put(PdfName.Yes,onState);
                //   appearance.getRollover().put(PdfName.Off,offState);
                //   appearance.getDown().put(PdfName.Off,offState);

                SizeF      size  = widget.Box.Size;
                RectangleF frame = new RectangleF(0, 0, size.Width, size.Height);
                {
                    onState.Size = new Size((int)size.Width, (int)size.Height);

                    PrimitiveComposer composer = new PrimitiveComposer(onState);

                    composer.BeginLocalState();
                    composer.SetFillColor(BackColor);
                    composer.SetStrokeColor(ForeColor);
                    composer.DrawRectangle(frame);
                    composer.FillStroke();
                    composer.End();

                    BlockComposer blockComposer = new BlockComposer(composer);
                    blockComposer.Begin(frame, AlignmentXEnum.Center, AlignmentYEnum.Middle);
                    composer.SetFillColor(ForeColor);
                    composer.SetFont(
                        new StandardType1Font(
                            document,
                            StandardType1Font.FamilyEnum.ZapfDingbats,
                            true,
                            false
                            ),
                        size.Height * 0.8f
                        );
                    blockComposer.ShowText(new String(new char[] { CheckSymbol }));
                    blockComposer.End();

                    composer.Flush();
                }

                FormXObject offState = new FormXObject(document);
                normalAppearance[PdfName.Off] = offState;
                {
                    offState.Size = new Size((int)size.Width, (int)size.Height);

                    PrimitiveComposer composer = new PrimitiveComposer(offState);

                    composer.BeginLocalState();
                    composer.SetFillColor(BackColor);
                    composer.SetStrokeColor(ForeColor);
                    composer.DrawRectangle(frame);
                    composer.FillStroke();
                    composer.End();

                    composer.Flush();
                }
            }
        }
Exemplo n.º 25
0
        private void Apply(
      ListBox field
      )
        {
            Document document = field.Document;
              Widget widget = field.Widgets[0];

              Appearance appearance = widget.Appearance;
              if(appearance == null)
              {widget.Appearance = appearance = new Appearance(document);}

              {
            PdfDictionary widgetDataObject = widget.BaseDataObject;
            widgetDataObject[PdfName.DA] = new PdfString("/Helv " + FontSize + " Tf 0 0 0 rg");
            widgetDataObject[PdfName.MK] = new PdfDictionary(
              new PdfName[]
              {
            PdfName.BG,
            PdfName.BC
              },
              new PdfDirectObject[]
              {
            new PdfArray(new PdfDirectObject[]{PdfReal.Get(.9), PdfReal.Get(.9), PdfReal.Get(.9)}),
            new PdfArray(new PdfDirectObject[]{PdfInteger.Default, PdfInteger.Default, PdfInteger.Default})
              }
              );
              }

              FormXObject normalAppearanceState;
              {
            SizeF size = widget.Box.Size;
            normalAppearanceState = new FormXObject(document, size);
            PrimitiveComposer composer = new PrimitiveComposer(normalAppearanceState);

            float lineWidth = 1;
            RectangleF frame = new RectangleF(lineWidth / 2, lineWidth / 2, size.Width - lineWidth, size.Height - lineWidth);
            if(GraphicsVisibile)
            {
              composer.BeginLocalState();
              composer.SetLineWidth(lineWidth);
              composer.SetFillColor(BackColor);
              composer.SetStrokeColor(ForeColor);
              composer.DrawRectangle(frame, 5);
              composer.FillStroke();
              composer.End();
            }

            composer.BeginLocalState();
            if(GraphicsVisibile)
            {
              composer.DrawRectangle(frame, 5);
              composer.Clip(); // Ensures that the visible content is clipped within the rounded frame.
            }
            composer.BeginMarkedContent(PdfName.Tx);
            composer.SetFont(
              new StandardType1Font(
            document,
            StandardType1Font.FamilyEnum.Helvetica,
            false,
            false
            ),
              FontSize
              );
            double y = 3;
            foreach(ChoiceItem item in field.Items)
            {
              composer.ShowText(
            item.Text,
            new PointF(0, (float)y)
            );
              y += FontSize * 1.175;
              if(y > size.Height)
            break;
            }
            composer.End();
            composer.End();

            composer.Flush();
              }
              appearance.Normal[null] = normalAppearanceState;
        }
Exemplo n.º 26
0
        private void BuildWelcomePage(Document document, FormXObject template)
        {
            // Add welcome page to the document!
            Page page = new Page(document); // Instantiates the page inside the document context.

            document.Pages.Add(page);       // Puts the page in the pages collection.
            SKSize pageSize = page.Size;

            PrimitiveComposer composer = new PrimitiveComposer(page);

            // Add the background template!
            composer.ShowXObject(template);
            // Wrap the content composer inside a block composer in order to achieve higher-level typographic control!

            /*
             * NOTE: BlockComposer provides block-level typographic features as text and paragraph alignment.
             * Flow-level typographic features are currently not supported: block-level typographic features
             * are the foundations upon which flow-level typographic features will sit.
             */
            BlockComposer blockComposer = new BlockComposer(composer);

            SKSize breakSize = new SKSize(0, 20); // Size of a paragraph break.
                                                  // Instantiate the page body's font!
            fonts::Font font = fonts::PdfType0Font.Load(document, GetResourcePath("fonts" + Path.DirectorySeparatorChar + "lazyDog.ttf"));

            // Showing the page title...
            // Define the box frame to force the page title within!
            SKRect frame = SKRect.Create(
                20,
                150,
                (float)pageSize.Width - 90,
                (float)pageSize.Height - 250
                );

            // Begin the block!
            blockComposer.Begin(frame, XAlignmentEnum.Center, YAlignmentEnum.Top);
            // Set the font to use!
            composer.SetFont(font, 56);
            // Set the text rendering mode (outline only)!
            composer.SetTextRenderMode(TextRenderModeEnum.Stroke);
            // Show the page title!
            blockComposer.ShowText("Welcome");
            // End the block!
            blockComposer.End();

            // Showing the clown photo...
            // Instantiate a jpeg image object!
            entities::Image image         = entities::Image.Get(GetResourcePath("images" + Path.DirectorySeparatorChar + "Clown.jpg")); // Abstract image (entity).
            SKPoint         imageLocation = new SKPoint(
                blockComposer.BoundBox.Left + blockComposer.BoundBox.Width - image.Width,
                blockComposer.BoundBox.Top + blockComposer.BoundBox.Height + 25
                );

            // Show the image!
            composer.ShowXObject(
                image.ToXObject(document),
                imageLocation
                );

            SKRect descriptionFrame = SKRect.Create(
                imageLocation.X,
                imageLocation.Y + image.Height + 5,
                image.Width,
                20
                );

            frame = SKRect.Create(
                blockComposer.BoundBox.Left,
                imageLocation.Y,
                blockComposer.BoundBox.Width - image.Width - 20,
                image.Height
                );
            blockComposer.Begin(frame, XAlignmentEnum.Left, YAlignmentEnum.Middle);
            {
                composer.SetFont(font, 30);
                blockComposer.ShowText("This is a sample document that merely demonstrates some basic graphics features supported by PDF Clown.");
                blockComposer.ShowBreak(XAlignmentEnum.Center);
                blockComposer.ShowText("Enjoy!");
            }
            blockComposer.End();

            frame = SKRect.Create(
                blockComposer.BoundBox.Left,
                blockComposer.BoundBox.Top + blockComposer.BoundBox.Height,
                pageSize.Width - 90,
                pageSize.Height - 100 - (blockComposer.BoundBox.Top + blockComposer.BoundBox.Height)
                );
            blockComposer.Begin(frame, XAlignmentEnum.Justify, YAlignmentEnum.Bottom);
            {
                composer.SetFont(font, 14);
                blockComposer.ShowText("PS: As promised, since version 0.0.3 PDF Clown has supported");
                // Begin local state!

                /*
                 * NOTE: Local state is a powerful feature of PDF format as it lets you nest
                 * multiple graphics contexts on the graphics state stack.
                 */
                composer.BeginLocalState();
                {
                    composer.SetFillColor(TextColor_Highlight);
                    blockComposer.ShowText(" embedded latin OpenFont/TrueType and non-embedded Type 1 fonts");
                }
                composer.End();
                blockComposer.ShowText(" along with");
                composer.BeginLocalState();
                {
                    composer.SetFillColor(TextColor_Highlight);
                    blockComposer.ShowText(" paragraph construction facilities");
                }
                composer.End();
                blockComposer.ShowText(" through the BlockComposer class.");
                blockComposer.ShowBreak(breakSize);

                blockComposer.ShowText("Since version 0.0.4 the content stream stack has been completed, providing ");
                composer.BeginLocalState();
                {
                    composer.SetFillColor(TextColor_Highlight);
                    blockComposer.ShowText("fully object-oriented access to the graphics objects that describe the contents on a page.");
                }
                composer.End();
                blockComposer.ShowText(" It's a great step towards a whole bunch of possibilities, such as text extraction/replacement, that next releases will progressively exploit.");
                blockComposer.ShowBreak(breakSize);

                blockComposer.ShowText("Since version 0.0.6 it has supported ");
                composer.BeginLocalState();
                {
                    composer.SetFillColor(TextColor_Highlight);
                    blockComposer.ShowText("Unicode");
                }
                composer.End();
                blockComposer.ShowText(" for OpenFont/TrueType fonts.");
                blockComposer.ShowBreak(breakSize);

                composer.SetFont(font, 8);
                blockComposer.ShowText("This page was crafted with the nice");
                composer.BeginLocalState();
                {
                    composer.SetFont(font, 10);
                    blockComposer.ShowText(" LazyDog font");
                }
                composer.End();
                blockComposer.ShowText(" (by Paul Neave, www.neave.com)");
            }
            blockComposer.End();

            blockComposer.Begin(descriptionFrame, XAlignmentEnum.Right, YAlignmentEnum.Top);
            {
                composer.SetFont(font, 8);
                blockComposer.ShowText("Source: http://www.wikipedia.org/");
            }
            blockComposer.End();

            composer.Flush();
        }
Exemplo n.º 27
0
        private FormXObject CreateWatermark(
            Document document
            )
        {
            SizeF size = document.GetSize();

              // 1. Create an external form object to represent the watermark!
              FormXObject watermark = new FormXObject(document, size);

              // 2. Inserting the contents of the watermark...
              // 2.1. Create a content composer!
              PrimitiveComposer composer = new PrimitiveComposer(watermark);
              // 2.2. Inserting the contents...
              // Set the font to use!
              composer.SetFont(new fonts::StandardType1Font(document, fonts::StandardType1Font.FamilyEnum.Times, true, false), 120);
              // Set the color to fill the text characters!
              composer.SetFillColor(new DeviceRGBColor(115 / 255d, 164 / 255d, 232 / 255d));
              // Apply transparency!
              {
            ExtGState state = new ExtGState(document);
            state.FillAlpha = .3;
            composer.ApplyState(state);
              }
              // Show the text!
              composer.ShowText(
            "PDFClown", // Text to show.
            new PointF(size.Width / 2f, size.Height / 2f), // Anchor location: page center.
            XAlignmentEnum.Center, // Horizontal placement (relative to the anchor): center.
            YAlignmentEnum.Middle, // Vertical placement (relative to the anchor): middle.
            50 // Rotation: 50-degree-counterclockwise.
            );
              // 2.3. Flush the contents into the watermark!
              composer.Flush();

              return watermark;
        }
Exemplo n.º 28
0
        private void Apply(
            ListBox field
            )
        {
            Document document = field.Document;
            Widget   widget   = field.Widgets[0];

            Appearance appearance = widget.Appearance;
            {
                PdfDictionary widgetDataObject = widget.BaseDataObject;
                widgetDataObject[PdfName.DA] = new PdfString("/Helv " + FontSize + " Tf 0 0 0 rg");
                widgetDataObject[PdfName.MK] = new PdfDictionary(
                    new PdfName[]
                {
                    PdfName.BG,
                    PdfName.BC
                },
                    new PdfDirectObject[]
                {
                    new PdfArray(new PdfDirectObject[] { PdfReal.Get(.9), PdfReal.Get(.9), PdfReal.Get(.9) }),
                    new PdfArray(new PdfDirectObject[] { PdfInteger.Default, PdfInteger.Default, PdfInteger.Default })
                }
                    );
            }

            FormXObject normalAppearanceState;

            {
                SKSize size = widget.Box.Size;
                normalAppearanceState = new FormXObject(document, size);
                PrimitiveComposer composer = new PrimitiveComposer(normalAppearanceState);

                float  lineWidth = 1;
                SKRect frame     = SKRect.Create(lineWidth / 2, lineWidth / 2, size.Width - lineWidth, size.Height - lineWidth);
                if (GraphicsVisibile)
                {
                    composer.BeginLocalState();
                    composer.SetLineWidth(lineWidth);
                    composer.SetFillColor(BackColor);
                    composer.SetStrokeColor(ForeColor);
                    composer.DrawRectangle(frame, 5);
                    composer.FillStroke();
                    composer.End();
                }

                composer.BeginLocalState();
                if (GraphicsVisibile)
                {
                    composer.DrawRectangle(frame, 5);
                    composer.Clip(); // Ensures that the visible content is clipped within the rounded frame.
                }
                composer.BeginMarkedContent(PdfName.Tx);
                composer.SetFont(
                    new StandardType1Font(
                        document,
                        StandardType1Font.FamilyEnum.Helvetica,
                        false,
                        false
                        ),
                    FontSize
                    );
                double y = 3;
                foreach (ChoiceItem item in field.Items)
                {
                    composer.ShowText(
                        item.Text,
                        new SKPoint(0, (float)y)
                        );
                    y += FontSize * 1.175;
                    if (y > size.Height)
                    {
                        break;
                    }
                }
                composer.End();
                composer.End();

                composer.Flush();
            }
            appearance.Normal[null] = normalAppearanceState;
        }
Exemplo n.º 29
0
        private void BuildLinks(
            Document document
            )
        {
            Pages pages = document.Pages;
              Page page = new Page(document);
              pages.Add(page);

              StandardType1Font font = new StandardType1Font(
            document,
            StandardType1Font.FamilyEnum.Courier,
            true,
            false
            );

              PrimitiveComposer composer = new PrimitiveComposer(page);
              BlockComposer blockComposer = new BlockComposer(composer);

              /*
            2.1. Goto-URI link.
              */
              {
            blockComposer.Begin(new RectangleF(30,100,200,50),XAlignmentEnum.Left,YAlignmentEnum.Middle);
            composer.SetFont(font,12);
            blockComposer.ShowText("Go-to-URI link");
            composer.SetFont(font,8);
            blockComposer.ShowText("\nIt allows you to navigate to a network resource.");
            composer.SetFont(font,5);
            blockComposer.ShowText("\n\nClick on the box to go to the project's SourceForge.net repository.");
            blockComposer.End();

            try
            {
              /*
            NOTE: This statement instructs the PDF viewer to navigate to the given URI when the link is clicked.
              */
              annotations::Link link = new annotations::Link(
            page,
            new Rectangle(240,100,100,50),
            "Link annotation",
            new GoToURI(
              document,
              new Uri("http://www.sourceforge.net/projects/clown")
              )
            );
              link.Border = new annotations::Border(
            document,
            3,
            annotations::Border.StyleEnum.Beveled,
            null
            );
            }
            catch(Exception exception)
            {throw new Exception("",exception);}
              }

              /*
            2.2. Embedded-goto link.
              */
              {
            string filePath = PromptFileChoice("Please select a PDF file to attach");

            /*
              NOTE: These statements instruct PDF Clown to attach a PDF file to the current document.
              This is necessary in order to test the embedded-goto functionality,
              as you can see in the following link creation (see below).
            */
            int fileAttachmentPageIndex = page.Index;
            string fileAttachmentName = "attachedSamplePDF";
            string fileName = System.IO.Path.GetFileName(filePath);
            annotations::FileAttachment attachment = new annotations::FileAttachment(
              page,
              new Rectangle(0, -20, 10, 10),
              "File attachment annotation",
              FileSpecification.Get(
            EmbeddedFile.Get(
              document,
              filePath
              ),
            fileName
            )
              );
            attachment.Name = fileAttachmentName;
            attachment.IconType = annotations::FileAttachment.IconTypeEnum.PaperClip;

            blockComposer.Begin(new RectangleF(30,170,200,50),XAlignmentEnum.Left,YAlignmentEnum.Middle);
            composer.SetFont(font,12);
            blockComposer.ShowText("Go-to-embedded link");
            composer.SetFont(font,8);
            blockComposer.ShowText("\nIt allows you to navigate to a destination within an embedded PDF file.");
            composer.SetFont(font,5);
            blockComposer.ShowText("\n\nClick on the button to go to the 2nd page of the attached PDF file (" + fileName + ").");
            blockComposer.End();

            /*
              NOTE: This statement instructs the PDF viewer to navigate to the page 2 of a PDF file
              attached inside the current document as described by the FileAttachment annotation on page 1 of the current document.
            */
            annotations::Link link = new annotations::Link(
              page,
              new Rectangle(240,170,100,50),
              "Link annotation",
              new GoToEmbedded(
            document,
            new GoToEmbedded.PathElement(
              document,
              fileAttachmentPageIndex, // Page of the current document containing the file attachment annotation of the target document.
              fileAttachmentName, // Name of the file attachment annotation corresponding to the target document.
              null // No sub-target.
              ), // Target represents the document to go to.
            new RemoteDestination(
              document,
              1, // Show the page 2 of the target document.
              Destination.ModeEnum.Fit, // Show the target document page entirely on the screen.
              null,
              null
              ) // The destination must be within the target document.
            )
              );
            link.Border = new annotations::Border(
              document,
              1,
              annotations::Border.StyleEnum.Dashed,
              new LineDash(new double[]{8,5,2,5})
              );
              }

              /*
            2.3. Textual link.
              */
              {
            blockComposer.Begin(new RectangleF(30,240,200,50),XAlignmentEnum.Left,YAlignmentEnum.Middle);
            composer.SetFont(font,12);
            blockComposer.ShowText("Textual link");
            composer.SetFont(font,8);
            blockComposer.ShowText("\nIt allows you to expose any kind of link (including the above-mentioned types) as text.");
            composer.SetFont(font,5);
            blockComposer.ShowText("\n\nClick on the text links to go either to the project's SourceForge.net repository or to the project's home page.");
            blockComposer.End();

            try
            {
              composer.BeginLocalState();
              composer.SetFont(font,10);
              composer.SetFillColor(DeviceRGBColor.Get(System.Drawing.Color.Blue));
              composer.ShowText(
            "PDF Clown Project's repository at SourceForge.net",
            new PointF(240,265),
            XAlignmentEnum.Left,
            YAlignmentEnum.Middle,
            0,
            new GoToURI(
              document,
              new Uri("http://www.sourceforge.net/projects/clown")
              )
            );
              composer.ShowText(
            "PDF Clown Project's home page",
            new PointF(240,285),
            XAlignmentEnum.Left,
            YAlignmentEnum.Bottom,
            -90,
            new GoToURI(
              document,
              new Uri("http://www.pdfclown.org")
              )
            );
              composer.End();
            }
            catch
            {}
              }

              composer.Flush();
        }
Exemplo n.º 30
0
        private void DrawText(
            PrimitiveComposer composer,
            string value,
            PointF location,
            XAlignmentEnum xAlignment,
            YAlignmentEnum yAlignment,
            float rotation
            )
        {
            // Show the anchor point!
              DrawCross(composer,location);

              composer.BeginLocalState();
              composer.SetFillColor(SampleColor);
              // Show the text onto the page!
              Quad textFrame = composer.ShowText(
            value,
            location,
            xAlignment,
            yAlignment,
            rotation
            );
              composer.End();

              // Draw the frame binding the shown text!
              DrawFrame(
            composer,
            textFrame.Points
            );

              composer.BeginLocalState();
              composer.SetFont(composer.State.Font,8);
              // Draw the rotation degrees!
              composer.ShowText(
            "(" + ((int)rotation) + " degrees)",
            new PointF(
              location.X+70,
              location.Y
              ),
            XAlignmentEnum.Left,
            YAlignmentEnum.Middle,
            0
            );
              composer.End();
        }
Exemplo n.º 31
0
        private FormXObject BuildTemplate(Document document, DateTime creationDate)
        {
            // Create a template (form)!
            FormXObject template     = new FormXObject(document, document.PageSize.Value);
            SKSize      templateSize = template.Size;

            // Get form content stream!
            PrimitiveComposer composer = new PrimitiveComposer(template);

            // Showing the header image inside the common content stream...
            // Instantiate a jpeg image object!
            entities::Image image = entities::Image.Get(GetResourcePath("images" + Path.DirectorySeparatorChar + "mountains.jpg")); // Abstract image (entity).

            // Show the image inside the common content stream!
            composer.ShowXObject(
                image.ToXObject(document),
                new SKPoint(0, 0),
                new SKSize(templateSize.Width - 50, 125)
                );

            // Showing the 'PdfClown' label inside the common content stream...
            composer.BeginLocalState();
            composer.SetFillColor(new colorSpaces::DeviceRGBColor(115f / 255, 164f / 255, 232f / 255));
            // Set the font to use!
            composer.SetFont(fonts::PdfType1Font.Load(document, fonts::PdfType1Font.FamilyEnum.Times, true, false), 120);
            // Show the text!
            composer.ShowText(
                "PdfClown",
                new SKPoint(
                    0,
                    templateSize.Height - (float)composer.State.Font.GetAscent(composer.State.FontSize)
                    )
                );

            // Drawing the side rectangle...
            composer.DrawRectangle(
                SKRect.Create(
                    (float)templateSize.Width - 50,
                    0,
                    50,
                    (float)templateSize.Height
                    )
                );
            composer.Fill();
            composer.End();

            // Showing the side text inside the common content stream...
            composer.BeginLocalState();
            {
                composer.SetFont(fonts::PdfType1Font.Load(document, fonts::PdfType1Font.FamilyEnum.Helvetica, false, false), 8);
                composer.SetFillColor(colorSpaces::DeviceRGBColor.White);
                composer.BeginLocalState();
                {
                    composer.Rotate(
                        90,
                        new SKPoint(
                            templateSize.Width - 50,
                            templateSize.Height - 25
                            )
                        );
                    BlockComposer blockComposer = new BlockComposer(composer);
                    blockComposer.Begin(
                        SKRect.Create(0, 0, 300, 50),
                        XAlignmentEnum.Left,
                        YAlignmentEnum.Middle
                        );
                    {
                        blockComposer.ShowText("Generated by PDF Clown on " + creationDate);
                        blockComposer.ShowBreak();
                        blockComposer.ShowText("For more info, visit http://www.pdfclown.org");
                    }
                    blockComposer.End();
                }
                composer.End();
            }
            composer.End();

            composer.Flush();

            return(template);
        }
Exemplo n.º 32
0
        /*
          TODO: refresh should happen just before serialization, on document event (e.g. OnWrite())
        */
        private void RefreshAppearance(
      )
        {
            FormXObject normalAppearance;
              RectangleF box = org.pdfclown.objects.Rectangle.Wrap(BaseDataObject[PdfName.Rect]).ToRectangleF();
              {
            AppearanceStates normalAppearances = Appearance.Normal;
            normalAppearance = normalAppearances[null];
            if(normalAppearance != null)
            {
              normalAppearance.Box = box;
              normalAppearance.BaseDataObject.Body.SetLength(0);
            }
            else
            {normalAppearances[null] = normalAppearance = new FormXObject(Document, box);}
              }

              PrimitiveComposer composer = new PrimitiveComposer(normalAppearance);
              {
            float yOffset = box.Height - Page.Box.Height;
            MarkupTypeEnum markupType = MarkupType;
            switch(markupType)
            {
              case MarkupTypeEnum.Highlight:
              {
            ExtGState defaultExtGState;
            {
              ExtGStateResources extGStates = normalAppearance.Resources.ExtGStates;
              defaultExtGState = extGStates[HighlightExtGStateName];
              if(defaultExtGState == null)
              {
                if(extGStates.Count > 0)
                {extGStates.Clear();}

                extGStates[HighlightExtGStateName] = defaultExtGState = new ExtGState(Document);
                defaultExtGState.AlphaShape = false;
                defaultExtGState.BlendMode = new List<BlendModeEnum>(new BlendModeEnum[]{BlendModeEnum.Multiply});
              }
            }

            composer.ApplyState(defaultExtGState);
            composer.SetFillColor(Color);
            {
              foreach(Quad markupBox in MarkupBoxes)
              {
                PointF[] points = markupBox.Points;
                float markupBoxHeight = points[3].Y - points[0].Y;
                float markupBoxMargin = GetMarkupBoxMargin(markupBoxHeight);
                composer.DrawCurve(
                  new PointF(points[3].X, points[3].Y + yOffset),
                  new PointF(points[0].X, points[0].Y + yOffset),
                  new PointF(points[3].X - markupBoxMargin, points[3].Y - markupBoxMargin + yOffset),
                  new PointF(points[0].X - markupBoxMargin, points[0].Y + markupBoxMargin + yOffset)
                  );
                composer.DrawLine(
                  new PointF(points[1].X, points[1].Y + yOffset)
                  );
                composer.DrawCurve(
                  new PointF(points[2].X, points[2].Y + yOffset),
                  new PointF(points[1].X + markupBoxMargin, points[1].Y + markupBoxMargin + yOffset),
                  new PointF(points[2].X + markupBoxMargin, points[2].Y - markupBoxMargin + yOffset)
                  );
                composer.Fill();
              }
            }
              }
            break;
              case MarkupTypeEnum.Squiggly:
              {
            composer.SetStrokeColor(Color);
            composer.SetLineCap(LineCapEnum.Round);
            composer.SetLineJoin(LineJoinEnum.Round);
            {
              foreach(Quad markupBox in MarkupBoxes)
              {
                PointF[] points = markupBox.Points;
                float markupBoxHeight = points[3].Y - points[0].Y;
                float lineWidth = markupBoxHeight * .02f;
                float step = markupBoxHeight * .125f;
                float boxXOffset = points[3].X;
                float boxYOffset = points[3].Y + yOffset - lineWidth;
                bool phase = false;
                composer.SetLineWidth(lineWidth);
                for(float x = 0, xEnd = points[2].X - boxXOffset; x < xEnd || !phase; x += step)
                {
                  PointF point = new PointF(x + boxXOffset, (phase ? -step : 0) + boxYOffset);
                  if(x == 0)
                  {composer.StartPath(point);}
                  else
                  {composer.DrawLine(point);}
                  phase = !phase;
                }
              }
              composer.Stroke();
            }
              }
            break;
              case MarkupTypeEnum.StrikeOut:
              case MarkupTypeEnum.Underline:
              {
            composer.SetStrokeColor(Color);
            {
              float lineYRatio = 0;
              switch(markupType)
              {
                case MarkupTypeEnum.StrikeOut:
                  lineYRatio = .575f;
                  break;
                case MarkupTypeEnum.Underline:
                  lineYRatio = .85f;
                  break;
                default:
                  throw new NotImplementedException();
              }
              foreach(Quad markupBox in MarkupBoxes)
              {
                PointF[] points = markupBox.Points;
                float markupBoxHeight = points[3].Y - points[0].Y;
                float boxYOffset = markupBoxHeight * lineYRatio + yOffset;
                composer.SetLineWidth(markupBoxHeight * .065);
                composer.DrawLine(
                  new PointF(points[3].X, points[0].Y + boxYOffset),
                  new PointF(points[2].X, points[1].Y + boxYOffset)
                  );
              }
              composer.Stroke();
            }
              }
            break;
              default:
            throw new NotImplementedException();
            }
              }
              composer.Flush();
        }
Exemplo n.º 33
0
        private void Apply(
            CheckBox field
            )
        {
            Document document = field.Document;

            foreach (Widget widget in field.Widgets)
            {
                {
                    PdfDictionary widgetDataObject = widget.BaseDataObject;
                    widgetDataObject[PdfName.DA] = new PdfString("/ZaDb 0 Tf 0 0 0 rg");
                    widgetDataObject[PdfName.MK] = new PdfDictionary(
                        new PdfName[] { PdfName.BG, PdfName.BC, PdfName.CA },
                        new PdfDirectObject[]
                    {
                        new PdfArray(new PdfDirectObject[] { PdfReal.Get(0.9412), PdfReal.Get(0.9412), PdfReal.Get(0.9412) }),
                        new PdfArray(new PdfDirectObject[] { PdfInteger.Default, PdfInteger.Default, PdfInteger.Default }),
                        new PdfString("4")
                    }
                        );
                    widgetDataObject[PdfName.BS] = new PdfDictionary(
                        new PdfName[] { PdfName.W, PdfName.S },
                        new PdfDirectObject[] { PdfReal.Get(0.8), PdfName.S }
                        );
                    widgetDataObject[PdfName.H] = PdfName.P;
                }

                Appearance       appearance       = widget.Appearance;
                AppearanceStates normalAppearance = appearance.Normal;
                SKSize           size             = widget.Box.Size;
                FormXObject      onState          = new FormXObject(document, size);
                normalAppearance[PdfName.Yes] = onState;

                //TODO:verify!!!
                //   appearance.getRollover().put(PdfName.Yes,onState);
                //   appearance.getDown().put(PdfName.Yes,onState);
                //   appearance.getRollover().put(PdfName.Off,offState);
                //   appearance.getDown().put(PdfName.Off,offState);

                float  lineWidth = 1;
                SKRect frame     = SKRect.Create(lineWidth / 2, lineWidth / 2, size.Width - lineWidth, size.Height - lineWidth);
                {
                    PrimitiveComposer composer = new PrimitiveComposer(onState);

                    if (GraphicsVisibile)
                    {
                        composer.BeginLocalState();
                        composer.SetLineWidth(lineWidth);
                        composer.SetFillColor(BackColor);
                        composer.SetStrokeColor(ForeColor);
                        composer.DrawRectangle(frame, 5);
                        composer.FillStroke();
                        composer.End();
                    }

                    BlockComposer blockComposer = new BlockComposer(composer);
                    blockComposer.Begin(frame, XAlignmentEnum.Center, YAlignmentEnum.Middle);
                    composer.SetFillColor(ForeColor);
                    composer.SetFont(
                        new StandardType1Font(
                            document,
                            StandardType1Font.FamilyEnum.ZapfDingbats,
                            true,
                            false
                            ),
                        size.Height * 0.8
                        );
                    blockComposer.ShowText(new String(new char[] { CheckSymbol }));
                    blockComposer.End();

                    composer.Flush();
                }

                FormXObject offState = new FormXObject(document, size);
                normalAppearance[PdfName.Off] = offState;
                {
                    if (GraphicsVisibile)
                    {
                        PrimitiveComposer composer = new PrimitiveComposer(offState);

                        composer.BeginLocalState();
                        composer.SetLineWidth(lineWidth);
                        composer.SetFillColor(BackColor);
                        composer.SetStrokeColor(ForeColor);
                        composer.DrawRectangle(frame, 5);
                        composer.FillStroke();
                        composer.End();

                        composer.Flush();
                    }
                }
            }
        }
        public FormXObject Build(
            )
        {
            bool   isRound         = type == TypeEnum.Round;
            bool   isStriped       = type == TypeEnum.Striped;
            double textScale       = .5;
            double borderWidth     = this.borderWidth.GetValue(width);
            double doubleBorderGap = borderDoubled ? borderWidth : 0;
            double fontSize        = 10;

            fontSize *= ((width - (isStriped ? 2 : doubleBorderGap * 2 + (borderWidth * (borderDoubled ? 1.5 : 1) * 2) + width * (isRound ?  .15 : .05))) / textScale) / font.GetWidth(text, fontSize);
            float height = (float)(isRound ? width : (font.GetAscent(fontSize) * 1.2 + doubleBorderGap * 2 + (borderWidth * (borderDoubled ? 1.5 : 1) * 2)));
            SizeF size   = new SizeF(width, height);

            FormXObject appearance = new FormXObject(document, size);

            {
                PrimitiveComposer composer = new PrimitiveComposer(appearance);
                if (color != null)
                {
                    composer.SetStrokeColor(color);
                    composer.SetFillColor(color);
                }
                composer.SetTextScale(textScale);
                composer.SetFont(font, fontSize);
                composer.ShowText(text, new PointF(size.Width / 2, (float)(size.Height / 2 - font.GetDescent(fontSize) * .4)), XAlignmentEnum.Center, YAlignmentEnum.Middle, 0);

                double     borderRadius  = isRound ? 0 : this.borderRadius.GetValue((size.Width + size.Height) / 2);
                RectangleF prevBorderBox = appearance.Box;
                for (int borderStep = 0, borderStepLimit = (borderDoubled ? 2 : 1); borderStep < borderStepLimit; borderStep++)
                {
                    if (borderStep == 0)
                    {
                        composer.SetLineWidth(borderWidth);
                    }
                    else
                    {
                        composer.SetLineWidth(composer.State.LineWidth / 2);
                    }

                    float      lineWidth = (float)(borderStep > 0 ? composer.State.LineWidth / 2 : borderWidth);
                    float      marginY   = (float)(lineWidth / 2 + (borderStep > 0 ? composer.State.LineWidth + doubleBorderGap : 0));
                    float      marginX   = isStriped ? 0 : marginY;
                    RectangleF borderBox = new RectangleF(prevBorderBox.X + marginX, prevBorderBox.Y + marginY, prevBorderBox.Width - marginX * 2, prevBorderBox.Height - marginY * 2);

                    if (isRound)
                    {
                        composer.DrawEllipse(borderBox);
                    }
                    else
                    {
                        if (isStriped)
                        {
                            composer.DrawLine(new PointF(borderBox.Left, borderBox.Top), new PointF(borderBox.Right, borderBox.Top));
                            composer.DrawLine(new PointF(borderBox.Left, borderBox.Bottom), new PointF(borderBox.Right, borderBox.Bottom));
                        }
                        else
                        {
                            composer.DrawRectangle(borderBox, borderRadius * (1 - .5 * borderStep));
                        }
                    }
                    composer.Stroke();
                    prevBorderBox = borderBox;
                }
                composer.Flush();
            }
            return(appearance);
        }
Exemplo n.º 35
0
        private void BuildMiscellaneousPage(
            Document document
            )
        {
            // 1. Add the page to the document!
            Page page = new Page(document); // Instantiates the page inside the document context.

            document.Pages.Add(page);       // Puts the page in the pages collection.

            SizeF pageSize = page.Size;

            // 2. Create a content composer for the page!
            PrimitiveComposer composer = new PrimitiveComposer(page);

            // 3. Drawing the page contents...
            composer.SetFont(
                new fonts::StandardType1Font(
                    document,
                    fonts::StandardType1Font.FamilyEnum.Courier,
                    true,
                    false
                    ),
                32
                );

            {
                BlockComposer blockComposer = new BlockComposer(composer);
                blockComposer.Begin(new RectangleF(30, 0, pageSize.Width - 60, 50), XAlignmentEnum.Center, YAlignmentEnum.Middle);
                blockComposer.ShowText("Miscellaneous");
                blockComposer.End();
            }

            composer.BeginLocalState();
            composer.SetLineJoin(LineJoinEnum.Round);
            composer.SetLineCap(LineCapEnum.Round);

            // 3.1. Polygon.
            composer.DrawPolygon(
                new PointF[]
            {
                new PointF(100, 200),
                new PointF(150, 150),
                new PointF(200, 150),
                new PointF(250, 200)
            }
                );

            // 3.2. Polyline.
            composer.DrawPolyline(
                new PointF[]
            {
                new PointF(300, 200),
                new PointF(350, 150),
                new PointF(400, 150),
                new PointF(450, 200)
            }
                );

            composer.Stroke();

            // 3.3. Rectangle (both squared and rounded).
            int x      = 50;
            int radius = 0;

            while (x < 500)
            {
                if (x > 300)
                {
                    composer.SetLineDash(new LineDash(new double[] { 5, 5 }, 3));
                }

                composer.SetFillColor(new DeviceRGBColor(1, x / 500d, x / 500d));
                composer.DrawRectangle(
                    new RectangleF(x, 250, 150, 100),
                    radius // NOTE: radius parameter determines the rounded angle size.
                    );
                composer.FillStroke();

                x      += 175;
                radius += 10;
            }
            composer.End(); // End local state.

            composer.BeginLocalState();
            composer.SetFont(
                composer.State.Font,
                12
                );

            // 3.4. Line cap parameter.
            int y = 400;

            foreach (LineCapEnum lineCap
                     in (LineCapEnum[])Enum.GetValues(typeof(LineCapEnum)))
            {
                composer.ShowText(
                    lineCap + ":",
                    new PointF(50, y),
                    XAlignmentEnum.Left,
                    YAlignmentEnum.Middle,
                    0
                    );
                composer.SetLineWidth(12);
                composer.SetLineCap(lineCap);
                composer.DrawLine(
                    new PointF(120, y),
                    new PointF(220, y)
                    );
                composer.Stroke();

                composer.BeginLocalState();
                composer.SetLineWidth(1);
                composer.SetStrokeColor(DeviceRGBColor.White);
                composer.SetLineCap(LineCapEnum.Butt);
                composer.DrawLine(
                    new PointF(120, y),
                    new PointF(220, y)
                    );
                composer.Stroke();
                composer.End(); // End local state.

                y += 30;
            }

            // 3.5. Line join parameter.
            y += 50;
            foreach (LineJoinEnum lineJoin
                     in (LineJoinEnum[])Enum.GetValues(typeof(LineJoinEnum)))
            {
                composer.ShowText(
                    lineJoin + ":",
                    new PointF(50, y),
                    XAlignmentEnum.Left,
                    YAlignmentEnum.Middle,
                    0
                    );
                composer.SetLineWidth(12);
                composer.SetLineJoin(lineJoin);
                PointF[] points = new PointF[]
                {
                    new PointF(120, y + 25),
                    new PointF(150, y - 25),
                    new PointF(180, y + 25)
                };
                composer.DrawPolyline(points);
                composer.Stroke();

                composer.BeginLocalState();
                composer.SetLineWidth(1);
                composer.SetStrokeColor(DeviceRGBColor.White);
                composer.SetLineCap(LineCapEnum.Butt);
                composer.DrawPolyline(points);
                composer.Stroke();
                composer.End(); // End local state.

                y += 50;
            }
            composer.End(); // End local state.

            // 3.6. Clipping.

            /*
             * NOTE: Clipping should be conveniently enclosed within a local state
             * in order to easily resume the unaltered drawing area after the operation completes.
             */
            composer.BeginLocalState();
            composer.DrawPolygon(
                new PointF[]
            {
                new PointF(220, 410),
                new PointF(300, 490),
                new PointF(450, 360),
                new PointF(430, 520),
                new PointF(590, 565),
                new PointF(420, 595),
                new PointF(460, 730),
                new PointF(380, 650),
                new PointF(330, 765),
                new PointF(310, 640),
                new PointF(220, 710),
                new PointF(275, 570),
                new PointF(170, 500),
                new PointF(275, 510)
            }
                );
            composer.Clip();
            // Showing a clown image...
            // Instantiate a jpeg image object!
            entities::Image   image        = entities::Image.Get(GetResourcePath("images" + System.IO.Path.DirectorySeparatorChar + "Clown.jpg")); // Abstract image (entity).
            xObjects::XObject imageXObject = image.ToXObject(document);

            // Show the image!
            composer.ShowXObject(
                imageXObject,
                new PointF(170, 320),
                GeomUtils.Scale(imageXObject.Size, new SizeF(450, 0))
                );
            composer.End(); // End local state.

            // 4. Flush the contents into the page!
            composer.Flush();
        }
Exemplo n.º 36
0
        private void RefreshAppearance(
            )
        {
            Widget widget = Widgets[0];
              FormXObject normalAppearance;
              {
            AppearanceStates normalAppearances = widget.Appearance.Normal;
            normalAppearance = normalAppearances[null];
            if(normalAppearance == null)
            {normalAppearances[null] = normalAppearance = new FormXObject(Document, widget.Box.Size);}
              }
              PdfName fontName = null;
              double fontSize = 0;
              {
            PdfString defaultAppearanceState = DefaultAppearanceState;
            if(defaultAppearanceState == null)
            {
              // Retrieving the font to define the default appearance...
              fonts::Font defaultFont = null;
              PdfName defaultFontName = null;
              {
            // Field fonts.
            FontResources normalAppearanceFonts = normalAppearance.Resources.Fonts;
            foreach(KeyValuePair<PdfName,fonts::Font> entry in normalAppearanceFonts)
            {
              if(!entry.Value.Symbolic)
              {
                defaultFont = entry.Value;
                defaultFontName = entry.Key;
                break;
              }
            }
            if(defaultFontName == null)
            {
              // Common fonts.
              FontResources formFonts = Document.Form.Resources.Fonts;
              foreach(KeyValuePair<PdfName,fonts::Font> entry in formFonts)
              {
                if(!entry.Value.Symbolic)
                {
                  defaultFont = entry.Value;
                  defaultFontName = entry.Key;
                  break;
                }
              }
              if(defaultFontName == null)
              {
                //TODO:manage name collision!
                formFonts[
                  defaultFontName = new PdfName("default")
                  ] = defaultFont = new fonts::StandardType1Font(
                    Document,
                    fonts::StandardType1Font.FamilyEnum.Helvetica,
                    false,
                    false
                    );
              }
              normalAppearanceFonts[defaultFontName] = defaultFont;
            }
              }
              bytes::Buffer buffer = new bytes::Buffer();
              new SetFont(defaultFontName, IsMultiline ? 10 : 0).WriteTo(buffer, Document);
              widget.BaseDataObject[PdfName.DA] = defaultAppearanceState = new PdfString(buffer.ToByteArray());
            }

            // Retrieving the font to use...
            ContentParser parser = new ContentParser(defaultAppearanceState.ToByteArray());
            foreach(ContentObject content in parser.ParseContentObjects())
            {
              if(content is SetFont)
              {
            SetFont setFontOperation = (SetFont)content;
            fontName = setFontOperation.Name;
            fontSize = setFontOperation.Size;
            break;
              }
            }
            normalAppearance.Resources.Fonts[fontName] = Document.Form.Resources.Fonts[fontName];
              }

              // Refreshing the field appearance...
              /*
               * TODO: resources MUST be resolved both through the apperance stream resource dictionary and
               * from the DR-entry acroform resource dictionary
               */
              PrimitiveComposer baseComposer = new PrimitiveComposer(normalAppearance);
              BlockComposer composer = new BlockComposer(baseComposer);
              ContentScanner currentLevel = composer.Scanner;
              bool textShown = false;
              while(currentLevel != null)
              {
            if(!currentLevel.MoveNext())
            {
              currentLevel = currentLevel.ParentLevel;
              continue;
            }

            ContentObject content = currentLevel.Current;
            if(content is MarkedContent)
            {
              MarkedContent markedContent = (MarkedContent)content;
              if(PdfName.Tx.Equals(((BeginMarkedContent)markedContent.Header).Tag))
              {
            // Remove old text representation!
            markedContent.Objects.Clear();
            // Add new text representation!
            baseComposer.Scanner = currentLevel.ChildLevel; // Ensures the composer places new contents within the marked content block.
            ShowText(composer, fontName, fontSize);
            textShown = true;
              }
            }
            else if(content is Text)
            {currentLevel.Remove();}
            else if(currentLevel.ChildLevel != null)
            {currentLevel = currentLevel.ChildLevel;}
              }
              if(!textShown)
              {
            baseComposer.BeginMarkedContent(PdfName.Tx);
            baseComposer.SetFillColor(new DeviceGrayColor(0)); // workaround for problem when input box fill color remains set
            ShowText(composer, fontName, fontSize);
            baseComposer.End();
              }
              baseComposer.Flush();
        }
Exemplo n.º 37
0
        private void BuildSimpleTextPage(
            Document document
            )
        {
            // 1. Add the page to the document!
            Page page = new Page(document); // Instantiates the page inside the document context.

            document.Pages.Add(page);       // Puts the page in the pages collection.

            SizeF pageSize = page.Size;

            // 2. Create a content composer for the page!
            PrimitiveComposer composer = new PrimitiveComposer(page);

            // 3. Inserting contents...
            // Set the font to use!
            composer.SetFont(
                new fonts::StandardType1Font(
                    document,
                    fonts::StandardType1Font.FamilyEnum.Courier,
                    true,
                    false
                    ),
                32
                );

            XAlignmentEnum[] xAlignments = (XAlignmentEnum[])Enum.GetValues(typeof(XAlignmentEnum));
            YAlignmentEnum[] yAlignments = (YAlignmentEnum[])Enum.GetValues(typeof(YAlignmentEnum));
            int step = (int)(pageSize.Height) / ((xAlignments.Length - 1) * yAlignments.Length + 1);

            BlockComposer blockComposer = new BlockComposer(composer);
            RectangleF    frame         = new RectangleF(
                30,
                0,
                pageSize.Width - 60,
                step / 2
                );

            blockComposer.Begin(frame, XAlignmentEnum.Center, YAlignmentEnum.Middle);
            blockComposer.ShowText("Simple alignment");
            blockComposer.End();

            frame = new RectangleF(
                30,
                pageSize.Height - step / 2,
                pageSize.Width - 60,
                step / 2 - 10
                );
            blockComposer.Begin(frame, XAlignmentEnum.Left, YAlignmentEnum.Bottom);
            composer.SetFont(composer.State.Font, 10);
            blockComposer.ShowText(
                "NOTE: showText(...) methods return the actual bounding box of the text shown.\n"
                + "NOTE: The rotation parameter can be freely defined as a floating point value."
                );
            blockComposer.End();

            composer.SetFont(composer.State.Font, 12);
            int x = 30;
            int y = step;
            int alignmentIndex = 0;

            foreach (XAlignmentEnum xAlignment
                     in (XAlignmentEnum[])Enum.GetValues(typeof(XAlignmentEnum)))
            {
                /*
                 * NOTE: As text shown through PrimitiveComposer has no bounding box constraining its extension,
                 * applying the justified alignment has no effect (it degrades to center alignment);
                 * in order to get such an effect, use BlockComposer instead.
                 */
                if (xAlignment.Equals(XAlignmentEnum.Justify))
                {
                    continue;
                }

                foreach (YAlignmentEnum yAlignment
                         in (YAlignmentEnum[])Enum.GetValues(typeof(YAlignmentEnum)))
                {
                    if (alignmentIndex % 2 == 0)
                    {
                        composer.BeginLocalState();
                        composer.SetFillColor(BackColor);
                        composer.DrawRectangle(
                            new RectangleF(
                                0,
                                y - step / 2,
                                pageSize.Width,
                                step
                                )
                            );
                        composer.Fill();
                        composer.End();
                    }

                    composer.ShowText(
                        xAlignment + " " + yAlignment + ":",
                        new PointF(x, y),
                        XAlignmentEnum.Left,
                        YAlignmentEnum.Middle,
                        0
                        );

                    y += step;
                    alignmentIndex++;
                }
            }

            float rotationStep = 0;
            float rotation     = 0;

            for (
                int columnIndex = 0;
                columnIndex < 2;
                columnIndex++
                )
            {
                switch (columnIndex)
                {
                case 0:
                    x            = 200;
                    rotationStep = 0;
                    break;

                case 1:
                    x            = (int)pageSize.Width / 2 + 100;
                    rotationStep = 360 / ((xAlignments.Length - 1) * yAlignments.Length - 1);
                    break;
                }
                y        = step;
                rotation = 0;
                foreach (XAlignmentEnum xAlignment
                         in (XAlignmentEnum[])Enum.GetValues(typeof(XAlignmentEnum)))
                {
                    /*
                     * NOTE: As text shown through PrimitiveComposer has no bounding box constraining its extension,
                     * applying the justified alignment has no effect (it degrades to center alignment);
                     * in order to get such an effect, use BlockComposer instead.
                     */
                    if (xAlignment.Equals(XAlignmentEnum.Justify))
                    {
                        continue;
                    }

                    foreach (YAlignmentEnum yAlignment
                             in (YAlignmentEnum[])Enum.GetValues(typeof(YAlignmentEnum)))
                    {
                        float startArcAngle = 0;
                        switch (xAlignment)
                        {
                        case XAlignmentEnum.Left:
                            // OK -- NOOP.
                            break;

                        case XAlignmentEnum.Right:
                        case XAlignmentEnum.Center:
                            startArcAngle = 180;
                            break;
                        }

                        composer.DrawArc(
                            new RectangleF(
                                x - 10,
                                y - 10,
                                20,
                                20
                                ),
                            startArcAngle,
                            startArcAngle + rotation
                            );

                        DrawText(
                            composer,
                            "PDF Clown",
                            new PointF(x, y),
                            xAlignment,
                            yAlignment,
                            rotation
                            );
                        y        += step;
                        rotation += rotationStep;
                    }
                }
            }

            // 4. Flush the contents into the page!
            composer.Flush();
        }
Exemplo n.º 38
0
        private FormXObject BuildTemplate(
            Document document,
            DateTime creationDate
            )
        {
            // Create a template (form)!
              FormXObject template = new FormXObject(document, document.PageSize.Value);
              SizeF templateSize = template.Size;

              // Get form content stream!
              PrimitiveComposer composer = new PrimitiveComposer(template);

              // Showing the header image inside the common content stream...
              // Instantiate a jpeg image object!
              entities::Image image = entities::Image.Get(GetResourcePath("images" + Path.DirectorySeparatorChar + "mountains.jpg")); // Abstract image (entity).
              // Show the image inside the common content stream!
              composer.ShowXObject(
            image.ToXObject(document),
            new PointF(0,0),
            new SizeF(templateSize.Width - 50, 125)
            );

              // Showing the 'PDFClown' label inside the common content stream...
              composer.BeginLocalState();
              composer.SetFillColor(new colorSpaces::DeviceRGBColor(115f / 255, 164f / 255, 232f / 255));
              // Set the font to use!
              composer.SetFont(
            new fonts::StandardType1Font(
              document,
              fonts::StandardType1Font.FamilyEnum.Times,
              true,
              false
              ),
            120
            );
              // Show the text!
              composer.ShowText(
            "PDFClown",
            new PointF(
              0,
              templateSize.Height - (float)composer.State.Font.GetAscent(composer.State.FontSize)
              )
            );

              // Drawing the side rectangle...
              composer.DrawRectangle(
            new RectangleF(
              (float)templateSize.Width - 50,
              0,
              50,
              (float)templateSize.Height
              )
            );
              composer.Fill();
              composer.End();

              // Showing the side text inside the common content stream...
              composer.BeginLocalState();
              {
            composer.SetFont(
              new fonts::StandardType1Font(
            document,
            fonts::StandardType1Font.FamilyEnum.Helvetica,
            false,
            false
            ),
              8
              );
            composer.SetFillColor(colorSpaces::DeviceRGBColor.White);
            composer.BeginLocalState();
            {
              composer.Rotate(
            90,
            new PointF(
              templateSize.Width - 50,
              templateSize.Height - 25
              )
            );
              BlockComposer blockComposer = new BlockComposer(composer);
              blockComposer.Begin(
            new RectangleF(0,0,300,50),
            XAlignmentEnum.Left,
            YAlignmentEnum.Middle
            );
              {
            blockComposer.ShowText("Generated by PDF Clown on " + creationDate);
            blockComposer.ShowBreak();
            blockComposer.ShowText("For more info, visit http://www.pdfclown.org");
              }
              blockComposer.End();
            }
            composer.End();
              }
              composer.End();

              composer.Flush();

              return template;
        }