示例#1
0
        public void Verify_GraphicsStateStack_CanBeCreated()
        {
            var pdfFilePath = TestUtils.GetOutputFileName();
            var stream      = new FileStream(pdfFilePath, FileMode.Create);

            // step 1
            var document = new Document(new Rectangle(200, 120));

            // step 2
            PdfWriter writer = PdfWriter.GetInstance(document, stream);

            // step 3
            document.AddAuthor(TestUtils.Author);
            document.Open();
            // step 4
            PdfContentByte canvas = writer.DirectContent;

            // state 1:
            canvas.SetRgbColorFill(0xFF, 0x45, 0x00);
            // fill a rectangle in state 1
            canvas.Rectangle(10, 10, 60, 60);
            canvas.Fill();
            canvas.SaveState();
            // state 2;
            canvas.SetLineWidth(3);
            canvas.SetRgbColorFill(0x8B, 0x00, 0x00);
            // fill and stroke a rectangle in state 2
            canvas.Rectangle(40, 20, 60, 60);
            canvas.FillStroke();
            canvas.SaveState();
            // state 3:
            canvas.ConcatCtm(1, 0, 0.1f, 1, 0, 0);
            canvas.SetRgbColorStroke(0xFF, 0x45, 0x00);
            canvas.SetRgbColorFill(0xFF, 0xD7, 0x00);
            // fill and stroke a rectangle in state 3
            canvas.Rectangle(70, 30, 60, 60);
            canvas.FillStroke();
            canvas.RestoreState();
            // stroke a rectangle in state 2
            canvas.Rectangle(100, 40, 60, 60);
            canvas.Stroke();
            canvas.RestoreState();
            // fill and stroke a rectangle in state 1
            canvas.Rectangle(130, 50, 60, 60);
            canvas.FillStroke();

            document.Close();
            stream.Dispose();

            TestUtils.VerifyPdfFileIsReadable(pdfFilePath);
        }
        /// <summary>
        /// Places the barcode in a  PdfContentByte . The
        /// barcode is always placed at coodinates (0, 0). Use the
        /// translation matrix to move it elsewhere.
        /// The bars and text are written in the following colors:
        ///
        ///
        ///    barColor
        ///    textColor
        ///   Result
        ///
        ///
        ///    null
        ///    null
        ///   bars and text painted with current fill color
        ///
        ///
        ///    barColor
        ///    null
        ///   bars and text painted with  barColor
        ///
        ///
        ///    null
        ///    textColor
        ///   bars painted with current color text painted with  textColor
        ///
        ///
        ///    barColor
        ///    textColor
        ///   bars painted with  barColor  text painted with  textColor
        ///
        ///
        /// </summary>
        /// <param name="cb">the  PdfContentByte  where the barcode will be placed</param>
        /// <param name="barColor">the color of the bars. It can be  null </param>
        /// <param name="textColor">the color of the text. It can be  null </param>
        /// <returns>the dimensions the barcode occupies</returns>
        public override Rectangle PlaceBarcode(PdfContentByte cb, BaseColor barColor, BaseColor textColor)
        {
            if (Supp.Font != null)
            {
                Supp.BarHeight = Ean.BarHeight + Supp.Baseline - Supp.Font.GetFontDescriptor(BaseFont.CAPHEIGHT, Supp.Size);
            }
            else
            {
                Supp.BarHeight = Ean.BarHeight;
            }
            Rectangle eanR = Ean.BarcodeSize;

            cb.SaveState();
            Ean.PlaceBarcode(cb, barColor, textColor);
            cb.RestoreState();
            cb.SaveState();
            cb.ConcatCtm(1, 0, 0, 1, eanR.Width + n, eanR.Height - Ean.BarHeight);
            Supp.PlaceBarcode(cb, barColor, textColor);
            cb.RestoreState();
            return(BarcodeSize);
        }
        public void OutputText(int x, int y, int flag, int x1, int y1, int x2, int y2, string text)
        {
            var   font      = _state.CurrentFont;
            var   refX      = _state.TransformX(x);
            var   refY      = _state.TransformY(y);
            var   angle     = _state.TransformAngle(font.Angle);
            var   sin       = (float)Math.Sin(angle);
            var   cos       = (float)Math.Cos(angle);
            var   fontSize  = font.GetFontSize(_state);
            var   bf        = font.Font;
            var   align     = _state.TextAlign;
            var   textWidth = bf.GetWidthPoint(text, fontSize);
            float tx        = 0;
            float ty        = 0;
            var   descender = bf.GetFontDescriptor(BaseFont.DESCENT, fontSize);
            var   ury       = bf.GetFontDescriptor(BaseFont.BBOXURY, fontSize);

            Cb.SaveState();
            Cb.ConcatCtm(cos, sin, -sin, cos, refX, refY);
            if ((align & MetaState.TaCenter) == MetaState.TaCenter)
            {
                tx = -textWidth / 2;
            }
            else if ((align & MetaState.TaRight) == MetaState.TaRight)
            {
                tx = -textWidth;
            }

            if ((align & MetaState.TaBaseline) == MetaState.TaBaseline)
            {
                ty = 0;
            }
            else if ((align & MetaState.TaBottom) == MetaState.TaBottom)
            {
                ty = -descender;
            }
            else
            {
                ty = -ury;
            }

            BaseColor textColor;

            if (_state.BackgroundMode == MetaState.Opaque)
            {
                textColor = _state.CurrentBackgroundColor;
                Cb.SetColorFill(textColor);
                Cb.Rectangle(tx, ty + descender, textWidth, ury - descender);
                Cb.Fill();
            }
            textColor = _state.CurrentTextColor;
            Cb.SetColorFill(textColor);
            Cb.BeginText();
            Cb.SetFontAndSize(bf, fontSize);
            Cb.SetTextMatrix(tx, ty);
            Cb.ShowText(text);
            Cb.EndText();
            if (font.IsUnderline())
            {
                Cb.Rectangle(tx, ty - fontSize / 4, textWidth, fontSize / 15);
                Cb.Fill();
            }
            if (font.IsStrikeout())
            {
                Cb.Rectangle(tx, ty + fontSize / 3, textWidth, fontSize / 15);
                Cb.Fill();
            }
            Cb.RestoreState();
        }