Exemplo n.º 1
0
        private void DrawOverlayText(PdfCanvas canvas, String overlayText, Rectangle annotRect, PdfBoolean repeat,
                                     PdfString defaultAppearance, int justification)
        {
            IDictionary <String, IList> parsedDA;

            try {
                parsedDA = ParseDAParam(defaultAppearance);
            }
            catch (NullReferenceException) {
                throw new PdfException(PdfException.DefaultAppearanceNotFound);
            }
            PdfFont       font;
            float         fontSize       = 12;
            IList         fontArgs       = parsedDA.Get("Tf");
            PdfDictionary formDictionary = pdfDocument.GetCatalog().GetPdfObject().GetAsDictionary(PdfName.AcroForm);

            if (fontArgs != null && formDictionary != null)
            {
                font     = GetFontFromAcroForm((PdfName)fontArgs[0]);
                fontSize = ((PdfNumber)fontArgs[1]).FloatValue();
            }
            else
            {
                font = PdfFontFactory.CreateFont();
            }
            if (pdfDocument.IsTagged())
            {
                canvas.OpenTag(new CanvasArtifact());
            }
            iText.Layout.Canvas modelCanvas   = new iText.Layout.Canvas(canvas, pdfDocument, annotRect, false);
            Paragraph           p             = new Paragraph(overlayText).SetFont(font).SetFontSize(fontSize).SetMargin(0);
            TextAlignment?      textAlignment = TextAlignment.LEFT;

            switch (justification)
            {
            case 1: {
                textAlignment = TextAlignment.CENTER;
                break;
            }

            case 2: {
                textAlignment = TextAlignment.RIGHT;
                break;
            }

            default: {
                break;
            }
            }
            p.SetTextAlignment(textAlignment);
            IList strokeColorArgs;

            parsedDA.TryGetValue("StrokeColor", out strokeColorArgs);
            if (strokeColorArgs != null)
            {
                p.SetStrokeColor(GetColor(strokeColorArgs));
            }
            IList fillColorArgs;

            parsedDA.TryGetValue("FillColor", out fillColorArgs);
            if (fillColorArgs != null)
            {
                p.SetFontColor(GetColor(fillColorArgs));
            }
            modelCanvas.Add(p);
            if (repeat != null && repeat.GetValue())
            {
                bool?isFull = modelCanvas.GetRenderer().GetPropertyAsBoolean(Property.FULL);
                while (isFull == null || (bool)!isFull)
                {
                    p.Add(overlayText);
                    LayoutArea previousArea = modelCanvas.GetRenderer().GetCurrentArea().Clone();
                    modelCanvas.Relayout();
                    if (modelCanvas.GetRenderer().GetCurrentArea().Equals(previousArea))
                    {
                        // Avoid infinite loop. This might be caused by the fact that the font does not support the text we want to show
                        break;
                    }
                    isFull = modelCanvas.GetRenderer().GetPropertyAsBoolean(Property.FULL);
                }
            }
            modelCanvas.GetRenderer().Flush();
            if (pdfDocument.IsTagged())
            {
                canvas.CloseTag();
            }
        }