コード例 #1
0
ファイル: Border.cs プロジェクト: wangchengqun/itext7-dotnet
 /// <summary>
 /// All borders are supposed to be drawn in such way, that inner content of the element is on the right from the
 /// drawing direction.
 /// </summary>
 /// <remarks>
 /// All borders are supposed to be drawn in such way, that inner content of the element is on the right from the
 /// drawing direction. Borders are drawn in this order: top, right, bottom, left.
 /// <para />
 /// Given points specify the line which lies on the border of the content area,
 /// therefore the border itself should be drawn to the left from the drawing direction.
 /// <para />
 /// <c>borderWidthBefore</c> and <c>borderWidthAfter</c> parameters are used to
 /// define the widths of the borders that are before and after the current border, e.g. for
 /// the bottom border, <c>borderWidthBefore</c> specifies width of the right border and
 /// <c>borderWidthAfter</c> - width of the left border. Those width are used to handle areas
 /// of border joins.
 /// <para />
 /// <c>borderRadius</c> is used to draw rounded borders.
 /// </remarks>
 /// <param name="canvas">PdfCanvas to be written to</param>
 /// <param name="x1">x coordinate of the beginning point of the element side, that should be bordered</param>
 /// <param name="y1">y coordinate of the beginning point of the element side, that should be bordered</param>
 /// <param name="x2">x coordinate of the ending point of the element side, that should be bordered</param>
 /// <param name="y2">y coordinate of the ending point of the element side, that should be bordered</param>
 /// <param name="borderRadius">defines the radius of the element's corners</param>
 /// <param name="defaultSide">
 /// the
 /// <see cref="Side"/>
 /// , that we will fallback to, if it cannot be determined by border coordinates
 /// </param>
 /// <param name="borderWidthBefore">defines width of the border that is before the current one</param>
 /// <param name="borderWidthAfter">defines width of the border that is after the current one</param>
 public virtual void Draw(PdfCanvas canvas, float x1, float y1, float x2, float y2, float borderRadius, Border.Side
                          defaultSide, float borderWidthBefore, float borderWidthAfter)
 {
     Draw(canvas, x1, y1, x2, y2, borderRadius, borderRadius, borderRadius, borderRadius, defaultSide, borderWidthBefore
          , borderWidthAfter);
 }
コード例 #2
0
        /// <summary>Places the barcode in a <c>PdfCanvas</c>.</summary>
        /// <remarks>
        /// Places the barcode in a <c>PdfCanvas</c>. The
        /// barcode is always placed at coordinates (0, 0). Use the
        /// translation matrix to move it elsewhere.
        /// <para />
        /// The bars and text are written in the following colors:
        /// <table border="1" summary="">
        /// <tr>
        /// <th><c>barColor</c></th>
        /// <th><c>textColor</c></th>
        /// <th>Result</th>
        /// </tr>
        /// <tr>
        /// <td><c>null</c></td>
        /// <td><c>null</c></td>
        /// <td>bars and text painted with current fill color</td>
        /// </tr>
        /// <tr>
        /// <td><c>barColor</c></td>
        /// <td><c>null</c></td>
        /// <td>bars and text painted with <c>barColor</c></td>
        /// </tr>
        /// <tr>
        /// <td><c>null</c></td>
        /// <td><c>textColor</c></td>
        /// <td>bars painted with current color<br />text painted with <c>textColor</c></td>
        /// </tr>
        /// <tr>
        /// <td><c>barColor</c></td>
        /// <td><c>textColor</c></td>
        /// <td>bars painted with <c>barColor</c><br />text painted with <c>textColor</c></td>
        /// </tr>
        /// </table>
        /// </remarks>
        /// <param name="canvas">the <c>PdfCanvas</c> where the barcode will be placed</param>
        /// <param name="barColor">the color of the bars. It can be <c>null</c></param>
        /// <param name="textColor">the color of the text. It can be <c>null</c></param>
        /// <returns>the dimensions the barcode occupies</returns>
        public override Rectangle PlaceBarcode(PdfCanvas canvas, Color barColor, Color textColor)
        {
            String fullCode = this.code;

            if (this.checksumText)
            {
                fullCode = fullCode + JavaUtil.IntegerToString(GetChecksum(this.code));
            }
            float fontX = 0.0f;

            if (this.font != null)
            {
                String var10001 = this.altText != null ? this.altText : fullCode;
                fullCode = this.altText != null ? this.altText : fullCode;
                fontX    = this.font.GetWidth(var10001, this.size);
            }
            String bCode = this.code;

            if (this.generateChecksum)
            {
                bCode += GetChecksum(bCode);
            }
            int idx;

            idx = bCode.Length;
            float fullWidth  = (float)((idx + 2) * 11) * this.x + 2.0f * this.x;
            float barStartX  = 0.0f;
            float textStartX = 0.0f;

            switch (this.textAlignment)
            {
            case 1: {
                break;
            }

            case 2: {
                if (fontX > fullWidth)
                {
                    barStartX = fontX - fullWidth;
                }
                else
                {
                    textStartX = fullWidth - fontX;
                }
                break;
            }

            default: {
                if (fontX > fullWidth)
                {
                    barStartX = (fontX - fullWidth) / 2.0f;
                }
                else
                {
                    textStartX = (fullWidth - fontX) / 2.0f;
                }
                break;
            }
            }
            float barStartY  = 0.0f;
            float textStartY = 0.0f;

            if (this.font != null)
            {
                if (this.baseline <= 0.0f)
                {
                    textStartY = this.barHeight - this.baseline;
                }
                else
                {
                    textStartY = -this.GetDescender();
                    barStartY  = textStartY + this.baseline;
                }
            }
            byte[] bars = GetBarsMSI(bCode);
            if (barColor != null)
            {
                canvas.SetFillColor(barColor);
            }
            for (int k = 0; k < bars.Length; ++k)
            {
                float w = (float)bars[k] * this.x;
                if (bars[k] == 1)
                {
                    canvas.Rectangle((double)barStartX, (double)barStartY, (double)(w - this.inkSpreading), (double)this.barHeight
                                     );
                }
                barStartX += this.x;
            }
            canvas.Fill();
            if (this.font != null)
            {
                if (textColor != null)
                {
                    canvas.SetFillColor(textColor);
                }
                canvas.BeginText();
                canvas.SetFontAndSize(this.font, this.size);
                canvas.SetTextMatrix(textStartX, textStartY);
                canvas.ShowText(fullCode);
                canvas.EndText();
            }
            return(this.GetBarcodeSize());
        }
コード例 #3
0
 private void DrawLine(PdfCanvas canvas, float x1, float y1, float x2, float y2)
 {
     canvas.SaveState().SetLineWidth(0.5f).SetLineDash(3).MoveTo(x1, y1).LineTo(x2, y2).Stroke().RestoreState();
 }
コード例 #4
0
 public virtual void Draw(PdfCanvas canvas, Rectangle drawArea)
 {
     canvas.SaveState().SetStrokeColor(color).SetLineWidth(lineWidth).MoveTo(drawArea.GetX(), drawArea.GetY() +
                                                                             lineWidth / 2).LineTo(drawArea.GetX() + drawArea.GetWidth(), drawArea.GetY() + lineWidth / 2).Stroke(
         ).RestoreState();
 }
コード例 #5
0
        public virtual void SmartModeSameResourcesCopyingModifyingAndFlushing_ensureObjectFresh()
        {
            String outFile = destinationFolder + "smartModeSameResourcesCopyingModifyingAndFlushing_ensureObjectFresh.pdf";
            String cmpFile = sourceFolder + "cmp_smartModeSameResourcesCopyingModifyingAndFlushing_ensureObjectFresh.pdf";

            String[]    srcFiles    = new String[] { sourceFolder + "indirectResourcesStructure.pdf", sourceFolder + "indirectResourcesStructure2.pdf" };
            PdfDocument outputDoc   = new PdfDocument(new PdfWriter(outFile, new WriterProperties().UseSmartMode()));
            int         lastPageNum = 1;
            PdfFont     font        = PdfFontFactory.CreateFont();

            foreach (String srcFile in srcFiles)
            {
                PdfDocument sourceDoc = new PdfDocument(new PdfReader(srcFile));
                for (int i = 1; i <= sourceDoc.GetNumberOfPages(); ++i)
                {
                    PdfDictionary srcRes = sourceDoc.GetPage(i).GetPdfObject().GetAsDictionary(PdfName.Resources);
                    // Ensures that objects copied to the output document are fresh,
                    // i.e. are not reused from already copied objects cache.
                    bool ensureObjectIsFresh = true;
                    // it's crucial to copy first inner objects and then the container object!
                    foreach (PdfObject v in srcRes.Values())
                    {
                        if (v.GetIndirectReference() != null)
                        {
                            // We are not interested in returned copied objects instances, they will be picked up by
                            // general copying mechanism from copied objects cache by default.
                            v.CopyTo(outputDoc, ensureObjectIsFresh);
                        }
                    }
                    if (srcRes.GetIndirectReference() != null)
                    {
                        srcRes.CopyTo(outputDoc, ensureObjectIsFresh);
                    }
                }
                sourceDoc.CopyPagesTo(1, sourceDoc.GetNumberOfPages(), outputDoc);
                sourceDoc.Close();
                int i_1;
                for (i_1 = lastPageNum; i_1 <= outputDoc.GetNumberOfPages(); ++i_1)
                {
                    PdfPage   page   = outputDoc.GetPage(i_1);
                    PdfCanvas canvas = new PdfCanvas(page);
                    canvas.BeginText().MoveText(36, 36).SetFontAndSize(font, 12).ShowText("Page " + i_1).EndText();
                }
                lastPageNum = i_1;
                outputDoc.FlushCopiedObjects(sourceDoc);
            }
            outputDoc.Close();
            PdfDocument          assertDoc       = new PdfDocument(new PdfReader(outFile));
            PdfIndirectReference page1ResFontObj = assertDoc.GetPage(1).GetPdfObject().GetAsDictionary(PdfName.Resources
                                                                                                       ).GetAsDictionary(PdfName.Font).GetIndirectReference();
            PdfIndirectReference page2ResFontObj = assertDoc.GetPage(2).GetPdfObject().GetAsDictionary(PdfName.Resources
                                                                                                       ).GetAsDictionary(PdfName.Font).GetIndirectReference();
            PdfIndirectReference page3ResFontObj = assertDoc.GetPage(3).GetPdfObject().GetAsDictionary(PdfName.Resources
                                                                                                       ).GetAsDictionary(PdfName.Font).GetIndirectReference();

            NUnit.Framework.Assert.IsFalse(page1ResFontObj.Equals(page2ResFontObj));
            NUnit.Framework.Assert.IsFalse(page1ResFontObj.Equals(page3ResFontObj));
            NUnit.Framework.Assert.IsFalse(page2ResFontObj.Equals(page3ResFontObj));
            assertDoc.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFile, cmpFile, destinationFolder));
        }
コード例 #6
0
 public static void DrawPdfACircle(PdfCanvas canvas, float width, float height, float moveX, float moveY)
 {
     DrawPdfAAppearanceString(canvas, width, height, moveX, moveY, circle);
 }
コード例 #7
0
 /// <summary>
 /// Method that will set properties to be inherited by this branch renderer's
 /// children and will iterate over all children in order to draw them.
 /// </summary>
 /// <param name="context">
 /// the object that knows the place to draw this element and
 /// maintains its state
 /// </param>
 protected internal override void DoDraw(SvgDrawContext context)
 {
     if (GetChildren().Count > 0)
     {
         // if branch has no children, don't do anything
         PdfCanvas currentCanvas = context.GetCurrentCanvas();
         if (performRootTransformations)
         {
             currentCanvas.BeginText();
             //Current transformation matrix results in the character glyphs being mirrored, correct with inverse tf
             AffineTransform rootTf;
             if (this.ContainsAbsolutePositionChange())
             {
                 rootTf = GetTextTransform(this.GetAbsolutePositionChanges(), context);
             }
             else
             {
                 rootTf = new AffineTransform(TEXTFLIP);
             }
             currentCanvas.SetTextMatrix(rootTf);
             //Reset context of text move
             context.ResetTextMove();
             //Apply relative move
             if (this.ContainsRelativeMove())
             {
                 float[] rootMove = this.GetRelativeTranslation();
                 context.AddTextMove(rootMove[0], -rootMove[1]);
             }
             //-y to account for the text-matrix transform we do in the text root to account for the coordinates
             //handle white-spaces
             if (!whiteSpaceProcessed)
             {
                 SvgTextUtil.ProcessWhiteSpace(this, true);
             }
         }
         ApplyTextRenderingMode(currentCanvas);
         if (this.attributesAndStyles != null)
         {
             ResolveFontSize();
             ResolveFont(context);
             currentCanvas.SetFontAndSize(font, fontSize);
             foreach (ISvgTextNodeRenderer c in children)
             {
                 float childLength = c.GetTextContentLength(fontSize, font);
                 if (c.ContainsAbsolutePositionChange())
                 {
                     //TODO(DEVSIX-2507) support rotate and other attributes
                     float[][]       absolutePositions = c.GetAbsolutePositionChanges();
                     AffineTransform newTransform      = GetTextTransform(absolutePositions, context);
                     //overwrite the last transformation stored in the context
                     context.SetLastTextTransform(newTransform);
                     //Apply transformation
                     currentCanvas.SetTextMatrix(newTransform);
                     //Absolute position changes requires resetting the current text move in the context
                     context.ResetTextMove();
                 }
                 //Handle Text-Anchor declarations
                 float textAnchorCorrection = GetTextAnchorAlignmentCorrection(childLength);
                 if (!CssUtils.CompareFloats(0f, textAnchorCorrection))
                 {
                     context.AddTextMove(textAnchorCorrection, 0);
                 }
                 //Move needs to happen before the saving of the state in order for it to cascade beyond
                 if (c.ContainsRelativeMove())
                 {
                     float[] childMove = c.GetRelativeTranslation();
                     context.AddTextMove(childMove[0], -childMove[1]);
                 }
                 //-y to account for the text-matrix transform we do in the text root to account for the coordinates
                 currentCanvas.SaveState();
                 c.Draw(context);
                 context.AddTextMove(childLength, 0);
                 currentCanvas.RestoreState();
                 //Restore transformation matrix
                 if (!context.GetLastTextTransform().IsIdentity())
                 {
                     currentCanvas.SetTextMatrix(context.GetLastTextTransform());
                 }
             }
             if (performRootTransformations)
             {
                 currentCanvas.EndText();
             }
         }
     }
 }
コード例 #8
0
 protected internal override TableBorders DrawHorizontalBorder(int i, float startX, float y1, PdfCanvas canvas
                                                               , float[] countedColumnWidth)
 {
     return(this);
 }
コード例 #9
0
 protected internal override TableBorders DrawVerticalBorder(int i, float startY, float x1, PdfCanvas canvas
                                                             , IList <float> heights)
 {
     return(this);
 }
コード例 #10
0
        /// <summary>Reads and processes all the data of the InputMeta.</summary>
        /// <exception cref="System.IO.IOException"/>
        public virtual void ReadAll()
        {
            if (@in.ReadInt() != unchecked ((int)(0x9AC6CDD7)))
            {
                throw new PdfException(PdfException.NotAPlaceableWindowsMetafile);
            }
            @in.ReadWord();
            left   = @in.ReadShort();
            top    = @in.ReadShort();
            right  = @in.ReadShort();
            bottom = @in.ReadShort();
            inch   = @in.ReadWord();
            state.SetScalingX((float)(right - left) / (float)inch * 72f);
            state.SetScalingY((float)(bottom - top) / (float)inch * 72f);
            state.SetOffsetWx(left);
            state.SetOffsetWy(top);
            state.SetExtentWx(right - left);
            state.SetExtentWy(bottom - top);
            @in.ReadInt();
            @in.ReadWord();
            @in.Skip(18);
            int tsize;
            int function;

            cb.SetLineCapStyle(PdfCanvasConstants.LineCapStyle.ROUND);
            cb.SetLineJoinStyle(PdfCanvasConstants.LineJoinStyle.ROUND);
            for (; ;)
            {
                int lenMarker = @in.GetLength();
                tsize = @in.ReadInt();
                if (tsize < 3)
                {
                    break;
                }
                function = @in.ReadWord();
                switch (function)
                {
                case 0: {
                    break;
                }

                case META_CREATEPALETTE:
                case META_CREATEREGION:
                case META_DIBCREATEPATTERNBRUSH: {
                    state.AddMetaObject(new MetaObject());
                    break;
                }

                case META_CREATEPENINDIRECT: {
                    MetaPen pen = new MetaPen();
                    pen.Init(@in);
                    state.AddMetaObject(pen);
                    break;
                }

                case META_CREATEBRUSHINDIRECT: {
                    MetaBrush brush = new MetaBrush();
                    brush.Init(@in);
                    state.AddMetaObject(brush);
                    break;
                }

                case META_CREATEFONTINDIRECT: {
                    MetaFont font = new MetaFont();
                    font.Init(@in);
                    state.AddMetaObject(font);
                    break;
                }

                case META_SELECTOBJECT: {
                    int idx = @in.ReadWord();
                    state.SelectMetaObject(idx, cb);
                    break;
                }

                case META_DELETEOBJECT: {
                    int idx = @in.ReadWord();
                    state.DeleteMetaObject(idx);
                    break;
                }

                case META_SAVEDC: {
                    state.SaveState(cb);
                    break;
                }

                case META_RESTOREDC: {
                    int idx = @in.ReadShort();
                    state.RestoreState(idx, cb);
                    break;
                }

                case META_SETWINDOWORG: {
                    state.SetOffsetWy(@in.ReadShort());
                    state.SetOffsetWx(@in.ReadShort());
                    break;
                }

                case META_SETWINDOWEXT: {
                    state.SetExtentWy(@in.ReadShort());
                    state.SetExtentWx(@in.ReadShort());
                    break;
                }

                case META_MOVETO: {
                    int   y = @in.ReadShort();
                    Point p = new Point(@in.ReadShort(), y);
                    state.SetCurrentPoint(p);
                    break;
                }

                case META_LINETO: {
                    int   y = @in.ReadShort();
                    int   x = @in.ReadShort();
                    Point p = state.GetCurrentPoint();
                    cb.MoveTo(state.TransformX((int)p.GetX()), state.TransformY((int)p.GetY()));
                    cb.LineTo(state.TransformX(x), state.TransformY(y));
                    cb.Stroke();
                    state.SetCurrentPoint(new Point(x, y));
                    break;
                }

                case META_POLYLINE: {
                    state.SetLineJoinPolygon(cb);
                    int len = @in.ReadWord();
                    int x   = @in.ReadShort();
                    int y   = @in.ReadShort();
                    cb.MoveTo(state.TransformX(x), state.TransformY(y));
                    for (int k = 1; k < len; ++k)
                    {
                        x = @in.ReadShort();
                        y = @in.ReadShort();
                        cb.LineTo(state.TransformX(x), state.TransformY(y));
                    }
                    cb.Stroke();
                    break;
                }

                case META_POLYGON: {
                    if (IsNullStrokeFill(false))
                    {
                        break;
                    }
                    int len = @in.ReadWord();
                    int sx  = @in.ReadShort();
                    int sy  = @in.ReadShort();
                    cb.MoveTo(state.TransformX(sx), state.TransformY(sy));
                    for (int k = 1; k < len; ++k)
                    {
                        int x = @in.ReadShort();
                        int y = @in.ReadShort();
                        cb.LineTo(state.TransformX(x), state.TransformY(y));
                    }
                    cb.LineTo(state.TransformX(sx), state.TransformY(sy));
                    StrokeAndFill();
                    break;
                }

                case META_POLYPOLYGON: {
                    if (IsNullStrokeFill(false))
                    {
                        break;
                    }
                    int   numPoly = @in.ReadWord();
                    int[] lens    = new int[numPoly];
                    for (int k = 0; k < lens.Length; ++k)
                    {
                        lens[k] = @in.ReadWord();
                    }
                    for (int j = 0; j < lens.Length; ++j)
                    {
                        int len = lens[j];
                        int sx  = @in.ReadShort();
                        int sy  = @in.ReadShort();
                        cb.MoveTo(state.TransformX(sx), state.TransformY(sy));
                        for (int k = 1; k < len; ++k)
                        {
                            int x = @in.ReadShort();
                            int y = @in.ReadShort();
                            cb.LineTo(state.TransformX(x), state.TransformY(y));
                        }
                        cb.LineTo(state.TransformX(sx), state.TransformY(sy));
                    }
                    StrokeAndFill();
                    break;
                }

                case META_ELLIPSE: {
                    if (IsNullStrokeFill(state.GetLineNeutral()))
                    {
                        break;
                    }
                    int b = @in.ReadShort();
                    int r = @in.ReadShort();
                    int t = @in.ReadShort();
                    int l = @in.ReadShort();
                    cb.Arc(state.TransformX(l), state.TransformY(b), state.TransformX(r), state.TransformY(t), 0, 360);
                    StrokeAndFill();
                    break;
                }

                case META_ARC: {
                    if (IsNullStrokeFill(state.GetLineNeutral()))
                    {
                        break;
                    }
                    float yend   = state.TransformY(@in.ReadShort());
                    float xend   = state.TransformX(@in.ReadShort());
                    float ystart = state.TransformY(@in.ReadShort());
                    float xstart = state.TransformX(@in.ReadShort());
                    float b      = state.TransformY(@in.ReadShort());
                    float r      = state.TransformX(@in.ReadShort());
                    float t      = state.TransformY(@in.ReadShort());
                    float l      = state.TransformX(@in.ReadShort());
                    float cx     = (r + l) / 2;
                    float cy     = (t + b) / 2;
                    float arc1   = GetArc(cx, cy, xstart, ystart);
                    float arc2   = GetArc(cx, cy, xend, yend);
                    arc2 -= arc1;
                    if (arc2 <= 0)
                    {
                        arc2 += 360;
                    }
                    cb.Arc(l, b, r, t, arc1, arc2);
                    cb.Stroke();
                    break;
                }

                case META_PIE: {
                    if (IsNullStrokeFill(state.GetLineNeutral()))
                    {
                        break;
                    }
                    float yend   = state.TransformY(@in.ReadShort());
                    float xend   = state.TransformX(@in.ReadShort());
                    float ystart = state.TransformY(@in.ReadShort());
                    float xstart = state.TransformX(@in.ReadShort());
                    float b      = state.TransformY(@in.ReadShort());
                    float r      = state.TransformX(@in.ReadShort());
                    float t      = state.TransformY(@in.ReadShort());
                    float l      = state.TransformX(@in.ReadShort());
                    float cx     = (r + l) / 2;
                    float cy     = (t + b) / 2;
                    float arc1   = GetArc(cx, cy, xstart, ystart);
                    float arc2   = GetArc(cx, cy, xend, yend);
                    arc2 -= arc1;
                    if (arc2 <= 0)
                    {
                        arc2 += 360;
                    }
                    IList <double[]> ar = PdfCanvas.BezierArc(l, b, r, t, arc1, arc2);
                    if (ar.Count == 0)
                    {
                        break;
                    }
                    double[] pt = ar[0];
                    cb.MoveTo(cx, cy);
                    cb.LineTo(pt[0], pt[1]);
                    for (int k = 0; k < ar.Count; ++k)
                    {
                        pt = ar[k];
                        cb.CurveTo(pt[2], pt[3], pt[4], pt[5], pt[6], pt[7]);
                    }
                    cb.LineTo(cx, cy);
                    StrokeAndFill();
                    break;
                }

                case META_CHORD: {
                    if (IsNullStrokeFill(state.GetLineNeutral()))
                    {
                        break;
                    }
                    float yend   = state.TransformY(@in.ReadShort());
                    float xend   = state.TransformX(@in.ReadShort());
                    float ystart = state.TransformY(@in.ReadShort());
                    float xstart = state.TransformX(@in.ReadShort());
                    float b      = state.TransformY(@in.ReadShort());
                    float r      = state.TransformX(@in.ReadShort());
                    float t      = state.TransformY(@in.ReadShort());
                    float l      = state.TransformX(@in.ReadShort());
                    float cx     = (r + l) / 2;
                    float cy     = (t + b) / 2;
                    float arc1   = GetArc(cx, cy, xstart, ystart);
                    float arc2   = GetArc(cx, cy, xend, yend);
                    arc2 -= arc1;
                    if (arc2 <= 0)
                    {
                        arc2 += 360;
                    }
                    IList <double[]> ar = PdfCanvas.BezierArc(l, b, r, t, arc1, arc2);
                    if (ar.Count == 0)
                    {
                        break;
                    }
                    double[] pt = ar[0];
                    cx = (float)pt[0];
                    cy = (float)pt[1];
                    cb.MoveTo(cx, cy);
                    for (int k = 0; k < ar.Count; ++k)
                    {
                        pt = ar[k];
                        cb.CurveTo(pt[2], pt[3], pt[4], pt[5], pt[6], pt[7]);
                    }
                    cb.LineTo(cx, cy);
                    StrokeAndFill();
                    break;
                }

                case META_RECTANGLE: {
                    if (IsNullStrokeFill(true))
                    {
                        break;
                    }
                    float b = state.TransformY(@in.ReadShort());
                    float r = state.TransformX(@in.ReadShort());
                    float t = state.TransformY(@in.ReadShort());
                    float l = state.TransformX(@in.ReadShort());
                    cb.Rectangle(l, b, r - l, t - b);
                    StrokeAndFill();
                    break;
                }

                case META_ROUNDRECT: {
                    if (IsNullStrokeFill(true))
                    {
                        break;
                    }
                    float h = state.TransformY(0) - state.TransformY(@in.ReadShort());
                    float w = state.TransformX(@in.ReadShort()) - state.TransformX(0);
                    float b = state.TransformY(@in.ReadShort());
                    float r = state.TransformX(@in.ReadShort());
                    float t = state.TransformY(@in.ReadShort());
                    float l = state.TransformX(@in.ReadShort());
                    cb.RoundRectangle(l, b, r - l, t - b, (h + w) / 4);
                    StrokeAndFill();
                    break;
                }

                case META_INTERSECTCLIPRECT: {
                    float b = state.TransformY(@in.ReadShort());
                    float r = state.TransformX(@in.ReadShort());
                    float t = state.TransformY(@in.ReadShort());
                    float l = state.TransformX(@in.ReadShort());
                    cb.Rectangle(l, b, r - l, t - b);
                    cb.EoClip();
                    cb.NewPath();
                    break;
                }

                case META_EXTTEXTOUT: {
                    int y     = @in.ReadShort();
                    int x     = @in.ReadShort();
                    int count = @in.ReadWord();
                    int flag  = @in.ReadWord();
                    int x1    = 0;
                    int y1    = 0;
                    int x2    = 0;
                    int y2    = 0;
                    if ((flag & (MetaFont.ETO_CLIPPED | MetaFont.ETO_OPAQUE)) != 0)
                    {
                        x1 = @in.ReadShort();
                        y1 = @in.ReadShort();
                        x2 = @in.ReadShort();
                        y2 = @in.ReadShort();
                    }
                    byte[] text = new byte[count];
                    int    k;
                    for (k = 0; k < count; ++k)
                    {
                        byte c = (byte)@in.ReadByte();
                        if (c == 0)
                        {
                            break;
                        }
                        text[k] = c;
                    }
                    String s;
                    try {
                        s = iText.IO.Util.JavaUtil.GetStringForBytes(text, 0, k, "Cp1252");
                    }
                    catch (ArgumentException) {
                        s = iText.IO.Util.JavaUtil.GetStringForBytes(text, 0, k);
                    }
                    OutputText(x, y, flag, x1, y1, x2, y2, s);
                    break;
                }

                case META_TEXTOUT: {
                    int    count = @in.ReadWord();
                    byte[] text  = new byte[count];
                    int    k;
                    for (k = 0; k < count; ++k)
                    {
                        byte c = (byte)@in.ReadByte();
                        if (c == 0)
                        {
                            break;
                        }
                        text[k] = c;
                    }
                    String s;
                    try {
                        s = iText.IO.Util.JavaUtil.GetStringForBytes(text, 0, k, "Cp1252");
                    }
                    catch (ArgumentException) {
                        s = iText.IO.Util.JavaUtil.GetStringForBytes(text, 0, k);
                    }
                    count = count + 1 & 0xfffe;
                    @in.Skip(count - k);
                    int y = @in.ReadShort();
                    int x = @in.ReadShort();
                    OutputText(x, y, 0, 0, 0, 0, 0, s);
                    break;
                }

                case META_SETBKCOLOR: {
                    state.SetCurrentBackgroundColor(@in.ReadColor());
                    break;
                }

                case META_SETTEXTCOLOR: {
                    state.SetCurrentTextColor(@in.ReadColor());
                    break;
                }

                case META_SETTEXTALIGN: {
                    state.SetTextAlign(@in.ReadWord());
                    break;
                }

                case META_SETBKMODE: {
                    state.SetBackgroundMode(@in.ReadWord());
                    break;
                }

                case META_SETPOLYFILLMODE: {
                    state.SetPolyFillMode(@in.ReadWord());
                    break;
                }

                case META_SETPIXEL: {
                    Color color = @in.ReadColor();
                    int   y     = @in.ReadShort();
                    int   x     = @in.ReadShort();
                    cb.SaveState();
                    cb.SetFillColor(color);
                    cb.Rectangle(state.TransformX(x), state.TransformY(y), .2f, .2f);
                    cb.Fill();
                    cb.RestoreState();
                    break;
                }

                case META_DIBSTRETCHBLT:
                case META_STRETCHDIB: {
                    int rop = @in.ReadInt();
                    if (function == META_STRETCHDIB)
                    {
                        /*int usage = */
                        @in.ReadWord();
                    }
                    int    srcHeight  = @in.ReadShort();
                    int    srcWidth   = @in.ReadShort();
                    int    ySrc       = @in.ReadShort();
                    int    xSrc       = @in.ReadShort();
                    float  destHeight = state.TransformY(@in.ReadShort()) - state.TransformY(0);
                    float  destWidth  = state.TransformX(@in.ReadShort()) - state.TransformX(0);
                    float  yDest      = state.TransformY(@in.ReadShort());
                    float  xDest      = state.TransformX(@in.ReadShort());
                    byte[] b          = new byte[tsize * 2 - (@in.GetLength() - lenMarker)];
                    for (int k = 0; k < b.Length; ++k)
                    {
                        b[k] = (byte)@in.ReadByte();
                    }
                    try {
                        cb.SaveState();
                        cb.Rectangle(xDest, yDest, destWidth, destHeight);
                        cb.Clip();
                        cb.NewPath();
                        ImageData       bmpImage     = ImageDataFactory.CreateBmp(b, true, b.Length);
                        PdfImageXObject imageXObject = new PdfImageXObject(bmpImage);
                        float           width        = destWidth * bmpImage.GetWidth() / srcWidth;
                        float           height       = -destHeight *bmpImage.GetHeight() / srcHeight;

                        float x = xDest - destWidth * xSrc / srcWidth;
                        float y = yDest + destHeight * ySrc / srcHeight - height;
                        cb.AddXObject(imageXObject, new Rectangle(x, y, width, height));
                        cb.RestoreState();
                    }
                    catch (Exception) {
                    }
                    // empty on purpose
                    break;
                }
                }
                @in.Skip(tsize * 2 - (@in.GetLength() - lenMarker));
            }
            state.Cleanup(cb);
        }
コード例 #11
0
 public abstract void Draw(PdfCanvas arg1);
コード例 #12
0
 /// <summary>Creates a MetaDo instance.</summary>
 /// <param name="in">inputstream containing the data</param>
 /// <param name="cb">PdfCanvas</param>
 public MetaDo(Stream @in, PdfCanvas cb)
 {
     this.cb  = cb;
     this.@in = new InputMeta(@in);
 }
コード例 #13
0
ファイル: Border.cs プロジェクト: wangchengqun/itext7-dotnet
        /// <summary>Perform drawing operations to draw discontinuous borders.</summary>
        /// <remarks>
        /// Perform drawing operations to draw discontinuous borders. Used by
        /// <see cref="DashedBorder"/>
        /// ,
        /// <see cref="DottedBorder"/>
        /// and
        /// <see cref="RoundDotsBorder"/>.
        /// </remarks>
        /// <param name="canvas">canvas to draw on</param>
        /// <param name="boundingRectangle">rectangle representing the bounding box of the drawing operations</param>
        /// <param name="horizontalRadii">the horizontal radius of the border's two corners</param>
        /// <param name="verticalRadii">the vertical radius of the border's two corners</param>
        /// <param name="defaultSide">
        /// the
        /// <see cref="Side"/>
        /// , that we will fallback to, if it cannot be determined by border coordinates
        /// </param>
        /// <param name="borderWidthBefore">defines width of the border that is before the current one</param>
        /// <param name="borderWidthAfter">defines width of the border that is after the current one</param>
        protected internal virtual void DrawDiscontinuousBorders(PdfCanvas canvas, Rectangle boundingRectangle, float
                                                                 [] horizontalRadii, float[] verticalRadii, Border.Side defaultSide, float borderWidthBefore, float borderWidthAfter
                                                                 )
        {
            float x1 = boundingRectangle.GetX();
            float y1 = boundingRectangle.GetY();
            float x2 = boundingRectangle.GetRight();
            float y2 = boundingRectangle.GetTop();
            float horizontalRadius1 = horizontalRadii[0];
            float horizontalRadius2 = horizontalRadii[1];
            float verticalRadius1   = verticalRadii[0];
            float verticalRadius2   = verticalRadii[1];
            // Points (x0, y0) and (x3, y3) are used to produce Bezier curve
            float x0 = boundingRectangle.GetX();
            float y0 = boundingRectangle.GetY();
            float x3 = boundingRectangle.GetRight();
            float y3 = boundingRectangle.GetTop();
            float innerRadiusBefore;
            float innerRadiusFirst;
            float innerRadiusSecond;
            float innerRadiusAfter;
            float widthHalf = width / 2;
            Point clipPoint1;
            Point clipPoint2;
            Point clipPoint;

            Border.Side borderSide = GetBorderSide(x1, y1, x2, y2, defaultSide);
            switch (borderSide)
            {
            case Border.Side.TOP: {
                innerRadiusBefore = Math.Max(0, horizontalRadius1 - borderWidthBefore);
                innerRadiusFirst  = Math.Max(0, verticalRadius1 - width);
                innerRadiusSecond = Math.Max(0, verticalRadius2 - width);
                innerRadiusAfter  = Math.Max(0, horizontalRadius2 - borderWidthAfter);
                x0        -= borderWidthBefore / 2;
                y0        -= innerRadiusFirst;
                x3        += borderWidthAfter / 2;
                y3        -= innerRadiusSecond;
                clipPoint1 = GetIntersectionPoint(new Point(x1 - borderWidthBefore, y1 + width), new Point(x1, y1), new Point
                                                      (x0, y0), new Point(x0 + 10, y0));
                clipPoint2 = GetIntersectionPoint(new Point(x2 + borderWidthAfter, y2 + width), new Point(x2, y2), new Point
                                                      (x3, y3), new Point(x3 - 10, y3));
                if (clipPoint1.x > clipPoint2.x)
                {
                    clipPoint = GetIntersectionPoint(new Point(x1 - borderWidthBefore, y1 + width), clipPoint1, clipPoint2, new
                                                     Point(x2 + borderWidthAfter, y2 + width));
                    canvas.MoveTo(x1 - borderWidthBefore, y1 + width).LineTo(clipPoint.x, clipPoint.y).LineTo(x2 + borderWidthAfter
                                                                                                              , y2 + width).LineTo(x1 - borderWidthBefore, y1 + width);
                }
                else
                {
                    canvas.MoveTo(x1 - borderWidthBefore, y1 + width).LineTo(clipPoint1.x, clipPoint1.y).LineTo(clipPoint2.x,
                                                                                                                clipPoint2.y).LineTo(x2 + borderWidthAfter, y2 + width).LineTo(x1 - borderWidthBefore, y1 + width);
                }
                canvas.Clip().EndPath();
                x1 += innerRadiusBefore;
                y1 += widthHalf;
                x2 -= innerRadiusAfter;
                y2 += widthHalf;
                canvas.MoveTo(x0, y0).CurveTo(x0, y0 + innerRadiusFirst * CURV, x1 - innerRadiusBefore * CURV, y1, x1, y1)
                .LineTo(x2, y2).CurveTo(x2 + innerRadiusAfter * CURV, y2, x3, y3 + innerRadiusSecond * CURV, x3, y3);
                break;
            }

            case Border.Side.RIGHT: {
                innerRadiusBefore = Math.Max(0, verticalRadius1 - borderWidthBefore);
                innerRadiusFirst  = Math.Max(0, horizontalRadius1 - width);
                innerRadiusSecond = Math.Max(0, horizontalRadius2 - width);
                innerRadiusAfter  = Math.Max(0, verticalRadius2 - borderWidthAfter);
                x0        -= innerRadiusFirst;
                y0        += borderWidthBefore / 2;
                x3        -= innerRadiusSecond;
                y3        -= borderWidthAfter / 2;
                clipPoint1 = GetIntersectionPoint(new Point(x1 + width, y1 + borderWidthBefore), new Point(x1, y1), new Point
                                                      (x0, y0), new Point(x0, y0 - 10));
                clipPoint2 = GetIntersectionPoint(new Point(x2 + width, y2 - borderWidthAfter), new Point(x2, y2), new Point
                                                      (x3, y3), new Point(x3, y3 - 10));
                if (clipPoint1.y < clipPoint2.y)
                {
                    clipPoint = GetIntersectionPoint(new Point(x1 + width, y1 + borderWidthBefore), clipPoint1, clipPoint2, new
                                                     Point(x2 + width, y2 - borderWidthAfter));
                    canvas.MoveTo(x1 + width, y1 + borderWidthBefore).LineTo(clipPoint.x, clipPoint.y).LineTo(x2 + width, y2 -
                                                                                                              borderWidthAfter).LineTo(x1 + width, y1 + borderWidthBefore).Clip().EndPath();
                }
                else
                {
                    canvas.MoveTo(x1 + width, y1 + borderWidthBefore).LineTo(clipPoint1.x, clipPoint1.y).LineTo(clipPoint2.x,
                                                                                                                clipPoint2.y).LineTo(x2 + width, y2 - borderWidthAfter).LineTo(x1 + width, y1 + borderWidthBefore).Clip
                        ().EndPath();
                }
                canvas.Clip().EndPath();
                x1 += widthHalf;
                y1 -= innerRadiusBefore;
                x2 += widthHalf;
                y2 += innerRadiusAfter;
                canvas.MoveTo(x0, y0).CurveTo(x0 + innerRadiusFirst * CURV, y0, x1, y1 + innerRadiusBefore * CURV, x1, y1)
                .LineTo(x2, y2).CurveTo(x2, y2 - innerRadiusAfter * CURV, x3 + innerRadiusSecond * CURV, y3, x3, y3);
                break;
            }

            case Border.Side.BOTTOM: {
                innerRadiusBefore = Math.Max(0, horizontalRadius1 - borderWidthBefore);
                innerRadiusFirst  = Math.Max(0, verticalRadius1 - width);
                innerRadiusSecond = Math.Max(0, verticalRadius2 - width);
                innerRadiusAfter  = Math.Max(0, horizontalRadius2 - borderWidthAfter);
                x0        += borderWidthBefore / 2;
                y0        += innerRadiusFirst;
                x3        -= borderWidthAfter / 2;
                y3        += innerRadiusSecond;
                clipPoint1 = GetIntersectionPoint(new Point(x1 + borderWidthBefore, y1 - width), new Point(x1, y1), new Point
                                                      (x0, y0), new Point(x0 - 10, y0));
                clipPoint2 = GetIntersectionPoint(new Point(x2 - borderWidthAfter, y2 - width), new Point(x2, y2), new Point
                                                      (x3, y3), new Point(x3 + 10, y3));
                if (clipPoint1.x < clipPoint2.x)
                {
                    clipPoint = GetIntersectionPoint(new Point(x1 + borderWidthBefore, y1 - width), clipPoint1, clipPoint2, new
                                                     Point(x2 - borderWidthAfter, y2 - width));
                    canvas.MoveTo(x1 + borderWidthBefore, y1 - width).LineTo(clipPoint.x, clipPoint.y).LineTo(x2 - borderWidthAfter
                                                                                                              , y2 - width).LineTo(x1 + borderWidthBefore, y1 - width);
                }
                else
                {
                    canvas.MoveTo(x1 + borderWidthBefore, y1 - width).LineTo(clipPoint1.x, clipPoint1.y).LineTo(clipPoint2.x,
                                                                                                                clipPoint2.y).LineTo(x2 - borderWidthAfter, y2 - width).LineTo(x1 + borderWidthBefore, y1 - width);
                }
                canvas.Clip().EndPath();
                x1 -= innerRadiusBefore;
                y1 -= widthHalf;
                x2 += innerRadiusAfter;
                y2 -= widthHalf;
                canvas.MoveTo(x0, y0).CurveTo(x0, y0 - innerRadiusFirst * CURV, x1 + innerRadiusBefore * CURV, y1, x1, y1)
                .LineTo(x2, y2).CurveTo(x2 - innerRadiusAfter * CURV, y2, x3, y3 - innerRadiusSecond * CURV, x3, y3);
                break;
            }

            case Border.Side.LEFT: {
                innerRadiusBefore = Math.Max(0, verticalRadius1 - borderWidthBefore);
                innerRadiusFirst  = Math.Max(0, horizontalRadius1 - width);
                innerRadiusSecond = Math.Max(0, horizontalRadius2 - width);
                innerRadiusAfter  = Math.Max(0, verticalRadius2 - borderWidthAfter);
                x0        += innerRadiusFirst;
                y0        -= borderWidthBefore / 2;
                x3        += innerRadiusSecond;
                y3        += borderWidthAfter / 2;
                clipPoint1 = GetIntersectionPoint(new Point(x1 - width, y1 - borderWidthBefore), new Point(x1, y1), new Point
                                                      (x0, y0), new Point(x0, y0 + 10));
                clipPoint2 = GetIntersectionPoint(new Point(x2 - width, y2 + borderWidthAfter), new Point(x2, y2), new Point
                                                      (x3, y3), new Point(x3, y3 + 10));
                if (clipPoint1.y > clipPoint2.y)
                {
                    clipPoint = GetIntersectionPoint(new Point(x1 - width, y1 - borderWidthBefore), clipPoint1, clipPoint2, new
                                                     Point(x2 - width, y2 + borderWidthAfter));
                    canvas.MoveTo(x1 - width, y1 - borderWidthBefore).LineTo(clipPoint.x, clipPoint.y).LineTo(x2 - width, y2 +
                                                                                                              borderWidthAfter).LineTo(x1 - width, y1 - borderWidthBefore);
                }
                else
                {
                    canvas.MoveTo(x1 - width, y1 - borderWidthBefore).LineTo(clipPoint1.x, clipPoint1.y).LineTo(clipPoint2.x,
                                                                                                                clipPoint2.y).LineTo(x2 - width, y2 + borderWidthAfter).LineTo(x1 - width, y1 - borderWidthBefore);
                }
                canvas.Clip().EndPath();
                x1 -= widthHalf;
                y1 += innerRadiusBefore;
                x2 -= widthHalf;
                y2 -= innerRadiusAfter;
                canvas.MoveTo(x0, y0).CurveTo(x0 - innerRadiusFirst * CURV, y0, x1, y1 - innerRadiusBefore * CURV, x1, y1)
                .LineTo(x2, y2).CurveTo(x2, y2 + innerRadiusAfter * CURV, x3 - innerRadiusSecond * CURV, y3, x3, y3);
                break;
            }

            default: {
                break;
            }
            }
            canvas.Stroke().RestoreState();
        }
コード例 #14
0
ファイル: Border.cs プロジェクト: wangchengqun/itext7-dotnet
 /// <summary>Draws the border of a cell.</summary>
 /// <param name="canvas">PdfCanvas to be written to</param>
 /// <param name="x1">x coordinate of the beginning point of the element side, that should be bordered</param>
 /// <param name="y1">y coordinate of the beginning point of the element side, that should be bordered</param>
 /// <param name="x2">x coordinate of the ending point of the element side, that should be bordered</param>
 /// <param name="y2">y coordinate of the ending point of the element side, that should be bordered</param>
 /// <param name="defaultSide">
 /// the
 /// <see cref="Side"/>
 /// , that we will fallback to, if it cannot be determined by border coordinates
 /// </param>
 public abstract void DrawCellBorder(PdfCanvas canvas, float x1, float y1, float x2, float y2, Border.Side
                                     defaultSide);
コード例 #15
0
 public static void DrawCircle(PdfCanvas canvas, float centerX, float centerY, float radius)
 {
     canvas.Circle(centerX, centerY, radius).Fill();
 }
コード例 #16
0
ファイル: PdfUtils.cs プロジェクト: changhuixu/PdfExamples
        public static void PrintStarWarsCrawl(string dest)
        {
            var text = new List <string>
            {
                "            Episode V      ",
                "    THE EMPIRE STRIKES BACK  ",
                "It is a dark time for the",
                "Rebellion. Although the Death",
                "Star has been destroyed,",
                "Imperial troops have driven the",
                "Rebel forces from their hidden",
                "base and pursued them across",
                "the galaxy.",
                "Evading the dreaded Imperial",
                "Starfleet, a group of freedom",
                "fighters led by Luke Skywalker",
                "has established a new secret",
                "base on the remote ice world",
                "of Hoth..."
            };
            var maxStringWidth = text.Select(fragment => fragment.Length).Max();

            //Initialize PDF document
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            //Add new page
            PageSize  ps     = PageSize.A4;
            PdfPage   page   = pdf.AddNewPage(ps);
            PdfCanvas canvas = new PdfCanvas(page);

            //Set black background
            canvas.Rectangle(0, 0, ps.GetWidth(), ps.GetHeight()).SetColor(DeviceRgb.BLACK, true).Fill();
            //Replace the origin of the coordinate system to the top left corner
            canvas.ConcatMatrix(1, 0, 0, 1, 0, ps.GetHeight());
            Color yellowColor = new DeviceCmyk(0f, 0.0537f, 0.769f, 0.051f);
            float lineHeight  = 5;
            float yOffset     = -40;

            canvas.BeginText()
            .SetFontAndSize(PdfFontFactory.CreateFont(StandardFonts.COURIER_BOLD), 1)
            .SetColor(yellowColor, true);
            for (var j = 0; j < text.Count; j++)
            {
                var   line          = text[j];
                float xOffset       = ps.GetWidth() / 2 - 45 - 8 * j;
                float fontSizeCoeff = 6 + j;
                float lineSpacing   = (lineHeight + j) * j / 1.5f;
                var   stringWidth   = line.Length;
                for (var i = 0; i < stringWidth; i++)
                {
                    var angle       = (maxStringWidth / 2 - i) / 2f;
                    var charXOffset = (4 + (float)j / 2) * i;
                    canvas.SetTextMatrix(
                        fontSizeCoeff, 0,
                        angle, fontSizeCoeff / 1.5f,
                        xOffset + charXOffset, yOffset - lineSpacing
                        )
                    .ShowText(line[i].ToString());
                }
            }
            canvas.EndText();
            //Close document
            pdf.Close();
        }
コード例 #17
0
 public static void DrawPdfACheck(PdfCanvas canvas, float width, float height)
 {
     DrawPdfAAppearanceString(canvas, width, height, 0, 0, check);
 }
コード例 #18
0
        private static void DrawSetImageAndReturnTop(Rectangle rectangle, Cursor bottomCursor, string image, PdfCanvas canvas)
        {
            const float setImageHeight       = 7f;
            const float setImageWidthOffset  = 7f;
            const float setImageHeightOffset = 7f;
            var         setImageRectangle    = new Rectangle(
                rectangle.GetLeft() + setImageWidthOffset,
                bottomCursor.GetCurrent() + setImageHeightOffset,
                rectangle.GetWidth() - setImageWidthOffset,
                setImageHeight);

            if (!string.IsNullOrWhiteSpace(image))
            {
                DrawImage(setImageRectangle, canvas, Path.Combine(CurrentPath, "Dominion", $"{image}"));
            }
            bottomCursor.AdvanceCursor(setImageRectangle.GetHeight() + setImageHeightOffset);
        }
コード例 #19
0
        private void AddPageWithRotationAndScaling(PdfCanvas pdfCanvas, Rectangle documentPageSize, PdfXObject watermarkXObject, Rectangle watermarkPageSize, int rotation, bool fitToPage)
        {
            float scaleWidth;
            float scaleHeight;
            float scale;
            float backgroundHeight;
            float backgroundWidth;

            switch (rotation)
            {
            case 90:
                scaleWidth  = documentPageSize.GetWidth() / watermarkPageSize.GetHeight();
                scaleHeight = documentPageSize.GetHeight() / watermarkPageSize.GetWidth();
                scale       = scaleWidth < scaleHeight ? scaleWidth : scaleHeight;
                scale       = fitToPage ? scale : 1;

                backgroundHeight = scale * watermarkPageSize.GetHeight();
                backgroundWidth  = scale * watermarkPageSize.GetWidth();

                pdfCanvas.AddXObject(watermarkXObject, 0, -scale, scale, 0,
                                     (documentPageSize.GetWidth() - backgroundHeight) / 2,
                                     backgroundWidth + (documentPageSize.GetHeight() - backgroundWidth) / 2);
                break;

            case 180:
                scaleWidth  = documentPageSize.GetWidth() / watermarkPageSize.GetWidth();
                scaleHeight = documentPageSize.GetHeight() / watermarkPageSize.GetHeight();
                scale       = scaleWidth < scaleHeight ? scaleWidth : scaleHeight;
                scale       = fitToPage ? scale : 1;

                backgroundHeight = scale * watermarkPageSize.GetHeight();
                backgroundWidth  = scale * watermarkPageSize.GetWidth();

                pdfCanvas.AddXObject(watermarkXObject, -scale, 0, 0, -scale,
                                     backgroundWidth + (documentPageSize.GetWidth() - backgroundWidth) / 2,
                                     backgroundHeight + (documentPageSize.GetHeight() - backgroundHeight) / 2);
                break;

            case 270:
                scaleWidth  = documentPageSize.GetWidth() / watermarkPageSize.GetHeight();
                scaleHeight = documentPageSize.GetHeight() / watermarkPageSize.GetWidth();
                scale       = scaleWidth < scaleHeight ? scaleWidth : scaleHeight;
                scale       = fitToPage ? scale : 1;

                backgroundHeight = scale * watermarkPageSize.GetHeight();
                backgroundWidth  = scale * watermarkPageSize.GetWidth();

                pdfCanvas.AddXObject(watermarkXObject, 0, scale, -scale, 0,
                                     backgroundHeight + (documentPageSize.GetWidth() - backgroundHeight) / 2,
                                     (documentPageSize.GetHeight() - backgroundWidth) / 2);
                break;

            case 0:
            default:
                scaleWidth  = documentPageSize.GetWidth() / watermarkPageSize.GetWidth();
                scaleHeight = documentPageSize.GetHeight() / watermarkPageSize.GetHeight();
                scale       = scaleWidth < scaleHeight ? scaleWidth : scaleHeight;
                scale       = fitToPage ? scale : 1;

                backgroundHeight = scale * watermarkPageSize.GetHeight();
                backgroundWidth  = scale * watermarkPageSize.GetWidth();

                pdfCanvas.AddXObject(watermarkXObject, scale, 0, 0, scale, (documentPageSize.GetWidth() - backgroundWidth) / 2,
                                     (documentPageSize.GetHeight() - backgroundHeight) / 2);
                break;
            }
        }
コード例 #20
0
 /// <summary>
 /// Adds a
 /// <see cref="iText.Kernel.Pdf.Canvas.PdfCanvas"/>
 /// to the stack (by definition its top), for use in
 /// drawing operations.
 /// </summary>
 /// <param name="canvas">the new top of the stack</param>
 public virtual void PushCanvas(PdfCanvas canvas)
 {
     canvases.Push(canvas);
 }
コード例 #21
0
        /// <summary>Places the barcode in a <CODE>PdfCanvas</CODE>.</summary>
        /// <remarks>
        /// Places the barcode in a <CODE>PdfCanvas</CODE>. The
        /// barcode is always placed at coordinates (0, 0). Use the
        /// translation matrix to move it elsewhere.<para />
        /// The bars and text are written in the following colors:
        /// <br />
        /// <TABLE BORDER="1" SUMMARY="barcode properties">
        /// <TR>
        /// <TH><CODE>barColor</CODE></TH>
        /// <TH><CODE>textColor</CODE></TH>
        /// <TH>Result</TH>
        /// </TR>
        /// <TR>
        /// <TD><CODE>null</CODE></TD>
        /// <TD><CODE>null</CODE></TD>
        /// <TD>bars and text painted with current fill color</TD>
        /// </TR>
        /// <TR>
        /// <TD><CODE>barColor</CODE></TD>
        /// <TD><CODE>null</CODE></TD>
        /// <TD>bars and text painted with <CODE>barColor</CODE></TD>
        /// </TR>
        /// <TR>
        /// <TD><CODE>null</CODE></TD>
        /// <TD><CODE>textColor</CODE></TD>
        /// <TD>bars painted with current color<br />text painted with <CODE>textColor</CODE></TD>
        /// </TR>
        /// <TR>
        /// <TD><CODE>barColor</CODE></TD>
        /// <TD><CODE>textColor</CODE></TD>
        /// <TD>bars painted with <CODE>barColor</CODE><br />text painted with <CODE>textColor</CODE></TD>
        /// </TR>
        /// </TABLE>
        /// </remarks>
        /// <param name="canvas">the <CODE>PdfCanvas</CODE> where the barcode will be placed</param>
        /// <param name="barColor">the color of the bars. It can be <CODE>null</CODE></param>
        /// <param name="textColor">the color of the text. It can be <CODE>null</CODE></param>
        /// <returns>the dimensions the barcode occupies</returns>
        public override Rectangle PlaceBarcode(PdfCanvas canvas, Color barColor, Color textColor)
        {
            String fullCode = code;
            float  fontX    = 0;
            String bCode    = code;

            if (extended)
            {
                bCode = GetCode39Ex(code);
            }
            if (font != null)
            {
                if (generateChecksum && checksumText)
                {
                    fullCode += GetChecksum(bCode);
                }
                if (startStopText)
                {
                    fullCode = "*" + fullCode + "*";
                }
                fontX = font.GetWidth(fullCode = altText != null ? altText : fullCode, size);
            }
            if (generateChecksum)
            {
                bCode += GetChecksum(bCode);
            }
            int   len        = bCode.Length + 2;
            float fullWidth  = len * (6 * x + 3 * x * n) + (len - 1) * x;
            float barStartX  = 0;
            float textStartX = 0;

            switch (textAlignment)
            {
            case ALIGN_LEFT: {
                break;
            }

            case ALIGN_RIGHT: {
                if (fontX > fullWidth)
                {
                    barStartX = fontX - fullWidth;
                }
                else
                {
                    textStartX = fullWidth - fontX;
                }
                break;
            }

            default: {
                if (fontX > fullWidth)
                {
                    barStartX = (fontX - fullWidth) / 2;
                }
                else
                {
                    textStartX = (fullWidth - fontX) / 2;
                }
                break;
            }
            }
            float barStartY  = 0;
            float textStartY = 0;

            if (font != null)
            {
                if (baseline <= 0)
                {
                    textStartY = barHeight - baseline;
                }
                else
                {
                    textStartY = -GetDescender();
                    barStartY  = textStartY + baseline;
                }
            }
            byte[] bars  = GetBarsCode39(bCode);
            bool   print = true;

            if (barColor != null)
            {
                canvas.SetFillColor(barColor);
            }
            for (int k = 0; k < bars.Length; ++k)
            {
                float w = (bars[k] == 0 ? x : x * n);
                if (print)
                {
                    canvas.Rectangle(barStartX, barStartY, w - inkSpreading, barHeight);
                }
                print      = !print;
                barStartX += w;
            }
            canvas.Fill();
            if (font != null)
            {
                if (textColor != null)
                {
                    canvas.SetFillColor(textColor);
                }
                canvas.BeginText().SetFontAndSize(font, size).SetTextMatrix(textStartX, textStartY).ShowText(fullCode).EndText
                    ();
            }
            return(GetBarcodeSize());
        }
コード例 #22
0
 /// <summary>Operations to be performed after drawing the element.</summary>
 /// <remarks>
 /// Operations to be performed after drawing the element.
 /// This includes filling, stroking.
 /// </remarks>
 /// <param name="context">the svg draw context</param>
 internal virtual void PostDraw(SvgDrawContext context)
 {
     if (this.attributesAndStyles != null)
     {
         PdfCanvas currentCanvas = context.GetCurrentCanvas();
         // fill-rule
         if (partOfClipPath)
         {
             if (SvgConstants.Values.FILL_RULE_EVEN_ODD.EqualsIgnoreCase(this.GetAttribute(SvgConstants.Attributes.CLIP_RULE
                                                                                           )))
             {
                 currentCanvas.EoClip();
             }
             else
             {
                 currentCanvas.Clip();
             }
             currentCanvas.EndPath();
         }
         else
         {
             if (doFill && CanElementFill())
             {
                 String fillRuleRawValue = GetAttribute(SvgConstants.Attributes.FILL_RULE);
                 if (SvgConstants.Values.FILL_RULE_EVEN_ODD.EqualsIgnoreCase(fillRuleRawValue))
                 {
                     if (doStroke)
                     {
                         currentCanvas.EoFillStroke();
                     }
                     else
                     {
                         currentCanvas.EoFill();
                     }
                 }
                 else
                 {
                     if (doStroke)
                     {
                         currentCanvas.FillStroke();
                     }
                     else
                     {
                         currentCanvas.Fill();
                     }
                 }
             }
             else
             {
                 if (doStroke)
                 {
                     currentCanvas.Stroke();
                 }
                 else
                 {
                     if (!typeof(TextSvgBranchRenderer).IsInstanceOfType(this))
                     {
                         currentCanvas.EndPath();
                     }
                 }
             }
         }
         // Marker drawing
         if (this is IMarkerCapable)
         {
             // TODO (DEVSIX-3397) add processing of 'marker' property (shorthand for a joint using of all other properties)
             foreach (MarkerVertexType markerVertexType in MARKER_VERTEX_TYPES)
             {
                 if (attributesAndStyles.ContainsKey(markerVertexType.ToString()))
                 {
                     currentCanvas.SaveState();
                     ((IMarkerCapable)this).DrawMarker(context, markerVertexType);
                     currentCanvas.RestoreState();
                 }
             }
         }
     }
 }
コード例 #23
0
        /// <summary>Places the barcode in a <CODE>PdfCanvas</CODE>.</summary>
        /// <remarks>
        /// Places the barcode in a <CODE>PdfCanvas</CODE>. The
        /// barcode is always placed at coordinates (0, 0). Use the
        /// translation matrix to move it elsewhere.<para />
        /// The bars and text are written in the following colors:
        /// <br />
        /// <TABLE BORDER="1" SUMMARY="barcode properties">
        /// <TR>
        /// <TH><CODE>barColor</CODE></TH>
        /// <TH><CODE>textColor</CODE></TH>
        /// <TH>Result</TH>
        /// </TR>
        /// <TR>
        /// <TD><CODE>null</CODE></TD>
        /// <TD><CODE>null</CODE></TD>
        /// <TD>bars and text painted with current fill color</TD>
        /// </TR>
        /// <TR>
        /// <TD><CODE>barColor</CODE></TD>
        /// <TD><CODE>null</CODE></TD>
        /// <TD>bars and text painted with <CODE>barColor</CODE></TD>
        /// </TR>
        /// <TR>
        /// <TD><CODE>null</CODE></TD>
        /// <TD><CODE>textColor</CODE></TD>
        /// <TD>bars painted with current color<br />text painted with <CODE>textColor</CODE></TD>
        /// </TR>
        /// <TR>
        /// <TD><CODE>barColor</CODE></TD>
        /// <TD><CODE>textColor</CODE></TD>
        /// <TD>bars painted with <CODE>barColor</CODE><br />text painted with <CODE>textColor</CODE></TD>
        /// </TR>
        /// </TABLE>
        /// </remarks>
        /// <param name="canvas">the <CODE>PdfCanvas</CODE> where the barcode will be placed</param>
        /// <param name="barColor">the color of the bars. It can be <CODE>null</CODE></param>
        /// <param name="textColor">the color of the text. It can be <CODE>null</CODE></param>
        /// <returns>the dimensions the barcode occupies</returns>
        public override Rectangle PlaceBarcode(PdfCanvas canvas, Color barColor, Color textColor)
        {
            String fullCode = code;

            if (generateChecksum && checksumText)
            {
                fullCode = CalculateChecksum(code);
            }
            if (!startStopText)
            {
                fullCode = fullCode.JSubstring(1, fullCode.Length - 1);
            }
            float fontX = 0;

            if (font != null)
            {
                fontX = font.GetWidth(fullCode = altText != null ? altText : fullCode, size);
            }
            byte[] bars = GetBarsCodabar(generateChecksum ? CalculateChecksum(code) : code);
            int    wide = 0;

            for (int k = 0; k < bars.Length; ++k)
            {
                wide += bars[k];
            }
            int   narrow     = bars.Length - wide;
            float fullWidth  = x * (narrow + wide * n);
            float barStartX  = 0;
            float textStartX = 0;

            switch (textAlignment)
            {
            case ALIGN_LEFT: {
                break;
            }

            case ALIGN_RIGHT: {
                if (fontX > fullWidth)
                {
                    barStartX = fontX - fullWidth;
                }
                else
                {
                    textStartX = fullWidth - fontX;
                }
                break;
            }

            default: {
                if (fontX > fullWidth)
                {
                    barStartX = (fontX - fullWidth) / 2;
                }
                else
                {
                    textStartX = (fullWidth - fontX) / 2;
                }
                break;
            }
            }
            float barStartY  = 0;
            float textStartY = 0;

            if (font != null)
            {
                if (baseline <= 0)
                {
                    textStartY = barHeight - baseline;
                }
                else
                {
                    textStartY = -GetDescender();
                    barStartY  = textStartY + baseline;
                }
            }
            bool print = true;

            if (barColor != null)
            {
                canvas.SetFillColor(barColor);
            }
            for (int k = 0; k < bars.Length; ++k)
            {
                float w = (bars[k] == 0 ? x : x * n);
                if (print)
                {
                    canvas.Rectangle(barStartX, barStartY, w - inkSpreading, barHeight);
                }
                print      = !print;
                barStartX += w;
            }
            canvas.Fill();
            if (font != null)
            {
                if (textColor != null)
                {
                    canvas.SetFillColor(textColor);
                }
                canvas.BeginText();
                canvas.SetFontAndSize(font, size);
                canvas.SetTextMatrix(textStartX, textStartY);
                canvas.ShowText(fullCode);
                canvas.EndText();
            }
            return(GetBarcodeSize());
        }
コード例 #24
0
 /// <summary>Operations to perform before drawing an element.</summary>
 /// <remarks>
 /// Operations to perform before drawing an element.
 /// This includes setting stroke color and width, fill color.
 /// </remarks>
 /// <param name="context">the svg draw context</param>
 internal virtual void PreDraw(SvgDrawContext context)
 {
     if (this.attributesAndStyles != null)
     {
         PdfCanvas    currentCanvas        = context.GetCurrentCanvas();
         PdfExtGState opacityGraphicsState = new PdfExtGState();
         if (!partOfClipPath)
         {
             float generalOpacity = GetOpacity();
             {
                 // fill
                 String fillRawValue = GetAttributeOrDefault(SvgConstants.Attributes.FILL, "black");
                 this.doFill = !SvgConstants.Values.NONE.EqualsIgnoreCase(fillRawValue);
                 if (doFill && CanElementFill())
                 {
                     float            fillOpacity      = GetOpacityByAttributeName(SvgConstants.Attributes.FILL_OPACITY, generalOpacity);
                     Color            fillColor        = null;
                     TransparentColor transparentColor = GetColorFromAttributeValue(context, fillRawValue, 0, fillOpacity);
                     if (transparentColor != null)
                     {
                         fillColor   = transparentColor.GetColor();
                         fillOpacity = transparentColor.GetOpacity();
                     }
                     if (!CssUtils.CompareFloats(fillOpacity, 1f))
                     {
                         opacityGraphicsState.SetFillOpacity(fillOpacity);
                     }
                     // set default if no color has been parsed
                     if (fillColor == null)
                     {
                         fillColor = ColorConstants.BLACK;
                     }
                     currentCanvas.SetFillColor(fillColor);
                 }
             }
             {
                 // stroke
                 String strokeRawValue = GetAttributeOrDefault(SvgConstants.Attributes.STROKE, SvgConstants.Values.NONE);
                 if (!SvgConstants.Values.NONE.EqualsIgnoreCase(strokeRawValue))
                 {
                     String strokeWidthRawValue = GetAttribute(SvgConstants.Attributes.STROKE_WIDTH);
                     // 1 px = 0,75 pt
                     float strokeWidth = 0.75f;
                     if (strokeWidthRawValue != null)
                     {
                         strokeWidth = CssUtils.ParseAbsoluteLength(strokeWidthRawValue);
                     }
                     float            strokeOpacity    = GetOpacityByAttributeName(SvgConstants.Attributes.STROKE_OPACITY, generalOpacity);
                     Color            strokeColor      = null;
                     TransparentColor transparentColor = GetColorFromAttributeValue(context, strokeRawValue, strokeWidth / 2, strokeOpacity
                                                                                    );
                     if (transparentColor != null)
                     {
                         strokeColor   = transparentColor.GetColor();
                         strokeOpacity = transparentColor.GetOpacity();
                     }
                     if (!CssUtils.CompareFloats(strokeOpacity, 1f))
                     {
                         opacityGraphicsState.SetStrokeOpacity(strokeOpacity);
                     }
                     // as default value for stroke is 'none' we should not set
                     // it in case when value obtaining fails
                     if (strokeColor != null)
                     {
                         currentCanvas.SetStrokeColor(strokeColor);
                     }
                     currentCanvas.SetLineWidth(strokeWidth);
                     doStroke = true;
                 }
             }
             {
                 // opacity
                 if (!opacityGraphicsState.GetPdfObject().IsEmpty())
                 {
                     currentCanvas.SetExtGState(opacityGraphicsState);
                 }
             }
         }
     }
 }
コード例 #25
0
 /// <summary>
 /// Adds a
 /// <see cref="iText.Kernel.Pdf.Canvas.PdfCanvas"/>
 /// to the stack (by definition its top), for use in
 /// drawing operations.
 /// </summary>
 /// <param name="canvas">the new top of the stack</param>
 public virtual void PushCanvas(PdfCanvas canvas)
 {
     canvases.AddFirst(canvas);
 }
コード例 #26
0
 public static void DrawPdfADiamond(PdfCanvas canvas, float width, float height)
 {
     DrawPdfAAppearanceString(canvas, width, height, 0, 0, diamond);
 }
コード例 #27
0
        public virtual void ShowTextAlignedTest01()
        {
            String      outFileName = destinationFolder + "showTextAlignedTest01.pdf";
            String      cmpFileName = sourceFolder + "cmp_showTextAlignedTest01.pdf";
            PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName));
            Document    document    = new Document(pdfDocument);

            pdfDocument.AddNewPage();
            PdfCanvas canvas = new PdfCanvas(pdfDocument.GetLastPage());
            String    text   = "textapqgaPQGatext";
            float     width  = 200;
            float     x;
            float     y;

            y = 700;
            x = 115;
            DrawCross(canvas, x, y);
            document.ShowTextAligned(text, x, y, TextAlignment.LEFT, VerticalAlignment.BOTTOM, 0);
            document.ShowTextAligned(text, x, y, TextAlignment.LEFT, VerticalAlignment.BOTTOM, (float)(Math.PI / 6 * 1
                                                                                                       ));
            x = 300;
            DrawCross(canvas, x, y);
            document.ShowTextAligned(text, x, y, TextAlignment.LEFT, VerticalAlignment.MIDDLE, 0);
            document.ShowTextAligned(text, x, y, TextAlignment.LEFT, VerticalAlignment.MIDDLE, (float)(Math.PI / 6 * 3
                                                                                                       ));
            x = 485;
            DrawCross(canvas, x, y);
            document.ShowTextAligned(text, x, y, TextAlignment.LEFT, VerticalAlignment.TOP, 0);
            document.ShowTextAligned(text, x, y, TextAlignment.LEFT, VerticalAlignment.TOP, (float)(Math.PI / 6 * 5));
            y = 400;
            x = 115;
            DrawCross(canvas, x, y);
            document.ShowTextAligned(text, x, y, TextAlignment.CENTER, VerticalAlignment.BOTTOM, 0);
            document.ShowTextAligned(text, x, y, TextAlignment.CENTER, VerticalAlignment.BOTTOM, (float)(Math.PI / 6 *
                                                                                                         2));
            x = 300;
            DrawCross(canvas, x, y);
            document.ShowTextAligned(text, x, y, TextAlignment.CENTER, VerticalAlignment.MIDDLE, 0);
            document.ShowTextAligned(text, x, y, TextAlignment.CENTER, VerticalAlignment.MIDDLE, (float)(Math.PI / 6 *
                                                                                                         4));
            x = 485;
            DrawCross(canvas, x, y);
            document.ShowTextAligned(text, x, y, TextAlignment.CENTER, VerticalAlignment.TOP, 0);
            document.ShowTextAligned(text, x, y, TextAlignment.CENTER, VerticalAlignment.TOP, (float)(Math.PI / 6 * 8)
                                     );
            y = 100;
            x = 115;
            DrawCross(canvas, x, y);
            document.ShowTextAligned(text, x, y, TextAlignment.RIGHT, VerticalAlignment.BOTTOM, 0);
            document.ShowTextAligned(text, x, y, TextAlignment.RIGHT, VerticalAlignment.BOTTOM, (float)(Math.PI / 6 *
                                                                                                        9));
            x = 300;
            DrawCross(canvas, x, y);
            document.ShowTextAligned(text, x, y, TextAlignment.RIGHT, VerticalAlignment.MIDDLE, 0);
            document.ShowTextAligned(text, x, y, TextAlignment.RIGHT, VerticalAlignment.MIDDLE, (float)(Math.PI / 6 *
                                                                                                        7));
            x = 485;
            DrawCross(canvas, x, y);
            document.ShowTextAligned(text, x, y, TextAlignment.RIGHT, VerticalAlignment.TOP, 0);
            document.ShowTextAligned(text, x, y, TextAlignment.RIGHT, VerticalAlignment.TOP, (float)(Math.PI / 6 * 6));
            document.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
                                                                             , "diff"));
        }
コード例 #28
0
 public static void DrawPdfAStar(PdfCanvas canvas, float width, float height)
 {
     DrawPdfAAppearanceString(canvas, width, height, 0, 0, star);
 }
コード例 #29
0
 private void DrawCross(PdfCanvas canvas, float x, float y)
 {
     DrawLine(canvas, x - 50, y, x + 50, y);
     DrawLine(canvas, x, y - 50, x, y + 50);
 }
コード例 #30
0
ファイル: Border.cs プロジェクト: wangchengqun/itext7-dotnet
 /// <summary>
 /// All borders are supposed to be drawn in such way, that inner content of the element is on the right from the
 /// drawing direction.
 /// </summary>
 /// <remarks>
 /// All borders are supposed to be drawn in such way, that inner content of the element is on the right from the
 /// drawing direction. Borders are drawn in this order: top, right, bottom, left.
 /// <para />
 /// Given points specify the line which lies on the border of the content area,
 /// therefore the border itself should be drawn to the left from the drawing direction.
 /// <para />
 /// <c>borderWidthBefore</c> and <c>borderWidthAfter</c> parameters are used to
 /// define the widths of the borders that are before and after the current border, e.g. for
 /// the bottom border, <c>borderWidthBefore</c> specifies width of the right border and
 /// <c>borderWidthAfter</c> - width of the left border. Those width are used to handle areas
 /// of border joins.
 /// </remarks>
 /// <param name="canvas">PdfCanvas to be written to</param>
 /// <param name="x1">x coordinate of the beginning point of the element side, that should be bordered</param>
 /// <param name="y1">y coordinate of the beginning point of the element side, that should be bordered</param>
 /// <param name="x2">x coordinate of the ending point of the element side, that should be bordered</param>
 /// <param name="y2">y coordinate of the ending point of the element side, that should be bordered</param>
 /// <param name="defaultSide">
 /// the
 /// <see cref="Side"/>
 /// , that we will fallback to, if it cannot be determined by border coordinates
 /// </param>
 /// <param name="borderWidthBefore">defines width of the border that is before the current one</param>
 /// <param name="borderWidthAfter">defines width of the border that is after the current one</param>
 public abstract void Draw(PdfCanvas canvas, float x1, float y1, float x2, float y2, Border.Side defaultSide
                           , float borderWidthBefore, float borderWidthAfter);