public virtual void CreatePdf(String dest)
        {
            //Initialize PDF document
            PdfDocument pdf       = new PdfDocument(new PdfWriter(dest));
            PdfPage     page      = pdf.AddNewPage();
            PdfCanvas   pdfCanvas = new PdfCanvas(page);
            Rectangle   rectangle = new Rectangle(36, 500, 100, 250);

            pdfCanvas.Rectangle(rectangle);
            pdfCanvas.Stroke();
            iText.Layout.Canvas canvas   = new iText.Layout.Canvas(pdfCanvas, rectangle);
            MyCanvasRenderer    renderer = new MyCanvasRenderer(canvas);

            canvas.SetRenderer(renderer);
            PdfFont   font   = PdfFontFactory.CreateFont(StandardFonts.TIMES_ROMAN);
            PdfFont   bold   = PdfFontFactory.CreateFont(StandardFonts.TIMES_BOLD);
            Text      title  = new Text("The Strange Case of Dr. Jekyll and Mr. Hyde").SetFont(bold);
            Text      author = new Text("Robert Louis Stevenson").SetFont(font);
            Paragraph p      = new Paragraph().Add(title).Add(" by ").Add(author);

            while (!renderer.IsFull())
            {
                canvas.Add(p);
            }
            //Close document
            pdf.Close();
        }
Beispiel #2
0
            public virtual void HandleEvent(Event @event)
            {
                PdfDocumentEvent docEvent = (PdfDocumentEvent)@event;
                PdfDocument      pdfDoc   = docEvent.GetDocument();
                PdfPage          page     = docEvent.GetPage();
                int       pageNumber      = pdfDoc.GetPageNumber(page);
                Rectangle pageSize        = page.GetPageSize();
                PdfCanvas pdfCanvas       = new PdfCanvas(page.NewContentStreamBefore(), page.GetResources(), pdfDoc);
                //Set background
                Color limeColor = new DeviceCmyk(0.208f, 0, 0.584f, 0);
                Color blueColor = new DeviceCmyk(0.445f, 0.0546f, 0, 0.0667f);

                pdfCanvas.SaveState()
                .SetFillColor(pageNumber % 2 == 1 ? limeColor : blueColor)
                .Rectangle(pageSize.GetLeft(), pageSize.GetBottom(), pageSize.GetWidth(), pageSize.GetHeight())
                .Fill()
                .RestoreState();
                //Add header and footer
                pdfCanvas.BeginText()
                .SetFontAndSize(Helvetica, 9)
                .MoveText(pageSize.GetWidth() / 2 - 60, pageSize.GetTop() - 20)
                .ShowText("THE TRUTH IS OUT THERE")
                .MoveText(60, -pageSize.GetTop() + 30)
                .ShowText(pageNumber.ToString())
                .EndText();
                //Add watermark
                Canvas canvas = new iText.Layout.Canvas(pdfCanvas, pdfDoc, page.GetPageSize());

                canvas.SetProperty(Property.FONT_COLOR, DeviceRgb.WHITE);
                canvas.SetProperty(Property.FONT_SIZE, 60);
                canvas.SetProperty(Property.FONT, HelveticaBold);
                //canvas.ShowTextAligned(new Paragraph("CONFIDENTIAL"), 298.0f, 421.0f, pageNumber,
                //    TextAlignment.CENTER, VerticalAlignment.MIDDLE, 45.0f);      // not working
                pdfCanvas.Release();
            }
Beispiel #3
0
        protected override void DrawTicks(PdfCanvas canvas, PdfPage page)
        {
            base.DrawTicks(canvas, page);

            try
            {
                float lastTickValue = float.Parse(_data.Columns[0].Label);

                iText.Layout.Canvas layoutCanvas = new iText.Layout.Canvas(canvas, _pdf, new iText.Kernel.Geom.Rectangle(_canvasWidth, _canvasHeight));
                layoutCanvas.SetFont(_font);
                layoutCanvas.SetFontSize(_valueFontSize);
                layoutCanvas.SetFontColor(_valueFontColour);

                canvas.SetLineWidth(_tickLineWidth).SetStrokeColor(_tickColour);

                float x = _originX;
                foreach (Column column in _data.Columns)
                {
                    float y           = _originY;
                    float columnValue = float.Parse(column.Label);

                    if (columnValue >= (lastTickValue + _lineXTicksInterval))
                    {
                        lastTickValue = columnValue;

                        canvas.MoveTo(x, _originY);
                        canvas.LineTo(x, _originY + _axisHeight * _yScale);

                        if (_columnLabelAngle == 0.0f)
                        {
                            layoutCanvas.ShowTextAligned(column.Label, x, _originY - _columnLabelMargin, TextAlignment.CENTER, VerticalAlignment.TOP, (float)Math.PI / 180.0f * _columnLabelAngle);
                        }
                        else if (_columnLabelAngle == 90.0f)
                        {
                            layoutCanvas.ShowTextAligned(column.Label, x, _originY - _columnLabelMargin, TextAlignment.RIGHT, VerticalAlignment.MIDDLE, (float)Math.PI / 180.0f * _columnLabelAngle);
                        }
                        else
                        {
                            layoutCanvas.ShowTextAligned(column.Label, x, _originY - _columnLabelMargin, TextAlignment.RIGHT, VerticalAlignment.TOP, (float)Math.PI / 180.0f * _columnLabelAngle);
                        }
                    }


                    x += _linePointSpace;
                }
                canvas.Stroke();
            }
            catch
            {
                // not numeric column values, bail
                return;
            }
        }
            public virtual void HandleEvent(Event @event)
            {
                PdfDocumentEvent docEvent  = (PdfDocumentEvent)@event;
                PdfDocument      pdf       = docEvent.GetDocument();
                PdfPage          page      = docEvent.GetPage();
                Rectangle        pageSize  = page.GetPageSize();
                PdfCanvas        pdfCanvas = new PdfCanvas(page.NewContentStreamBefore(), page.GetResources(), pdf);

                pdfCanvas.SaveState().SetExtGState(this.gState);
                iText.Layout.Canvas canvas = new iText.Layout.Canvas(pdfCanvas, page.GetPageSize());
                canvas.Add(this.img.ScaleAbsolute(pageSize.GetWidth(), pageSize.GetHeight()));
                pdfCanvas.RestoreState();
                pdfCanvas.Release();
            }
Beispiel #5
0
 /// <summary>Places retrieved text to canvas to a separate layer.</summary>
 /// <param name="imageSize">
 /// size of the image according to the selected
 /// <see cref="ScaleMode"/>
 /// </param>
 /// <param name="pageText">text that was found on this image (or on this page)</param>
 /// <param name="pdfCanvas">canvas to place the text</param>
 /// <param name="multiplier">coefficient to adjust text placing on canvas</param>
 /// <param name="pageMediaBox">page parameters</param>
 private void AddTextToCanvas(Rectangle imageSize, IList <TextInfo> pageText, PdfCanvas pdfCanvas, float multiplier
                              , Rectangle pageMediaBox)
 {
     if (pageText != null && pageText.Count > 0)
     {
         Point imageCoordinates = PdfCreatorUtil.CalculateImageCoordinates(ocrPdfCreatorProperties.GetPageSize(), imageSize
                                                                           );
         foreach (TextInfo item in pageText)
         {
             String        line         = item.GetText();
             IList <float> coordinates  = item.GetBbox();
             float         left         = coordinates[0] * multiplier;
             float         right        = (coordinates[2] + 1) * multiplier - 1;
             float         top          = coordinates[1] * multiplier;
             float         bottom       = (coordinates[3] + 1) * multiplier - 1;
             float         bboxWidthPt  = PdfCreatorUtil.GetPoints(right - left);
             float         bboxHeightPt = PdfCreatorUtil.GetPoints(bottom - top);
             FontProvider  fontProvider = GetOcrPdfCreatorProperties().GetFontProvider();
             String        fontFamily   = GetOcrPdfCreatorProperties().GetDefaultFontFamily();
             if (!String.IsNullOrEmpty(line) && bboxHeightPt > 0 && bboxWidthPt > 0)
             {
                 Document document = new Document(pdfCanvas.GetDocument());
                 document.SetFontProvider(fontProvider);
                 // Scale the text width to fit the OCR bbox
                 float fontSize             = PdfCreatorUtil.CalculateFontSize(document, line, fontFamily, bboxHeightPt, bboxWidthPt);
                 float lineWidth            = PdfCreatorUtil.GetRealLineWidth(document, line, fontFamily, fontSize);
                 float deltaX               = PdfCreatorUtil.GetPoints(left);
                 float deltaY               = imageSize.GetHeight() - PdfCreatorUtil.GetPoints(bottom);
                 iText.Layout.Canvas canvas = new iText.Layout.Canvas(pdfCanvas, pageMediaBox);
                 canvas.SetFontProvider(fontProvider);
                 Text      text      = new Text(line).SetHorizontalScaling(bboxWidthPt / lineWidth);
                 Paragraph paragraph = new Paragraph(text).SetMargin(0).SetMultipliedLeading(1.2f);
                 paragraph.SetFontFamily(fontFamily).SetFontSize(fontSize);
                 paragraph.SetWidth(bboxWidthPt * 1.5f);
                 if (ocrPdfCreatorProperties.GetTextColor() != null)
                 {
                     paragraph.SetFontColor(ocrPdfCreatorProperties.GetTextColor());
                 }
                 else
                 {
                     paragraph.SetTextRenderingMode(PdfCanvasConstants.TextRenderingMode.INVISIBLE);
                 }
                 canvas.ShowTextAligned(paragraph, deltaX + (float)imageCoordinates.x, deltaY + (float)imageCoordinates.y,
                                        TextAlignment.LEFT);
                 canvas.Close();
             }
         }
     }
 }
        private static void SingleLimitedCanvasSizeCase(Document document, Paragraph paraOnCanvas, String description
                                                        , float canvasHeight, int pageNum)
        {
            PdfDocument pdfDocument = document.GetPdfDocument();

            document.Add(new Paragraph(description).SetBorder(new SolidBorder(ColorConstants.RED, 1)));
            PdfCanvas pdfCanvas     = new PdfCanvas(pdfDocument.GetPage(pageNum));
            Rectangle effectiveArea = document.GetPageEffectiveArea(pdfDocument.GetDefaultPageSize());
            Rectangle rectangle     = new Rectangle(36, 550, effectiveArea.GetWidth(), canvasHeight + LINES_SPACE_EPS);

            pdfCanvas.SaveState().SetFillColor(ColorConstants.MAGENTA).Rectangle(rectangle).Fill().RestoreState();
            iText.Layout.Canvas canvas = new iText.Layout.Canvas(pdfCanvas, pdfDocument, rectangle);
            canvas.Add(paraOnCanvas);
            canvas.Close();
        }
Beispiel #7
0
            public virtual void HandleEvent(Event @event)
            {
                PdfDocumentEvent docEvent = (PdfDocumentEvent)@event;
                PdfDocument      pdf      = docEvent.GetDocument();
                PdfPage          page     = docEvent.GetPage();
                int       pageNumber      = pdf.GetPageNumber(page);
                Rectangle pageSize        = page.GetPageSize();
                PdfCanvas pdfCanvas       = new PdfCanvas(page.GetLastContentStream(), page.GetResources(), pdf);

                iText.Layout.Canvas canvas = new iText.Layout.Canvas(pdfCanvas, pdf, pageSize);
                Paragraph           p      = new Paragraph().Add("Page ").Add(pageNumber.ToString()).Add(" of");

                canvas.ShowTextAligned(p, this.x, this.y, TextAlignment.RIGHT);
                pdfCanvas.AddXObject(this.placeholder, this.x + this.space, this.y - this.descent);
                canvas.Close();
            }
Beispiel #8
0
            public virtual void HandleEvent(Event @event)
            {
                PdfDocumentEvent docEvent = (PdfDocumentEvent)@event;
                PdfDocument      pdf      = docEvent.GetDocument();
                PdfPage          page     = docEvent.GetPage();

                if (pdf.GetPageNumber(page) == 1)
                {
                    return;
                }
                Rectangle pageSize  = page.GetPageSize();
                PdfCanvas pdfCanvas = new PdfCanvas(page.GetLastContentStream(), page.GetResources(), pdf);

                iText.Layout.Canvas canvas = new iText.Layout.Canvas(pdfCanvas, pageSize);
                canvas.ShowTextAligned(this.header, pageSize.GetWidth() / 2, pageSize.GetTop() - 30, TextAlignment.CENTER);
            }
Beispiel #9
0
        protected override void DrawLabels(PdfCanvas canvas, PdfPage page)
        {
            iText.Layout.Canvas layoutCanvas = new iText.Layout.Canvas(canvas, _pdf, new iText.Kernel.Geom.Rectangle(_canvasWidth, _canvasHeight));
            layoutCanvas.SetFont(_font);
            layoutCanvas.SetFontSize(_valueFontSize);
            layoutCanvas.SetFontColor(_valueFontColour);
            float x;

            if (_data.ValueLabel != null)
            {
                x = _originX - _valueLabelMargin;
                layoutCanvas.ShowTextAligned(_data.ValueLabel, x, _originY + _axisHeight * _yScale / 2, TextAlignment.CENTER, VerticalAlignment.MIDDLE, 0.5f * (float)Math.PI);
            }

            if (_data.Legends.Count == 1)
            {
                layoutCanvas.ShowTextAligned(_data.Legends[0].Label, _originX + _axisWidth / 2, _originY - _valueLabelMargin, TextAlignment.CENTER, VerticalAlignment.MIDDLE, 0);
            }
        }
Beispiel #10
0
 public virtual void WriteTotal(PdfDocument pdf)
 {
     iText.Layout.Canvas canvas = new iText.Layout.Canvas(this.placeholder, pdf);
     canvas.ShowTextAligned(pdf.GetNumberOfPages().ToString(), 0, this.descent, TextAlignment.LEFT);
 }
Beispiel #11
0
        protected override void DrawTicks(PdfCanvas canvas, PdfPage page)
        {
            float  i;
            String text;
            float  textWidth;
            float  textHeight;

            double yOffset = 0;

            if (_tickLineWidth == 0.0f)
            {
                return;
            }

            if (_yAxisMin < 0)
            {
                yOffset = _yAxisMin * -1.0 * _yScale;
            }

            canvas.SetLineWidth(_tickLineWidth).SetStrokeColor(_tickColour);
            // DrawTicks
            for (i = (_yAxisMin + _valueAxisInterval); i <= _valueAxisMax; i += _valueAxisInterval)
            {
                text = i.ToString();
                if (_data.FormatPercent)
                {
                    text = text + "%";
                }
                textWidth  = _font.GetWidth(text, _tickFontSize) + _tickLabelMargin;
                textHeight = _font.GetAscent(text, _tickFontSize) + _font.GetDescent(text, _tickFontSize);

                canvas.MoveTo(_originX, _originY + i * _yScale + yOffset);
                canvas.LineTo(_originX + _axisWidth, _originY + i * _yScale + yOffset);

                canvas.BeginText()
                .SetFontAndSize(_font, _tickFontSize)
                .MoveText(_originX - textWidth, _originY + i * _yScale - textHeight / 2 + yOffset)
                .SetColor(_tickFontColour, true)
                .ShowText(text)
                .EndText();
            }
            canvas.Stroke();

            if (_lineXTicks)
            {
                float lastTickValue = float.Parse(_data.Columns[0].Label);

                iText.Layout.Canvas layoutCanvas = new iText.Layout.Canvas(canvas, _pdf, new iText.Kernel.Geom.Rectangle(_canvasWidth, _canvasHeight));
                layoutCanvas.SetFont(_font);
                layoutCanvas.SetFontSize(_valueFontSize);
                layoutCanvas.SetFontColor(_valueFontColour);

                canvas.SetLineWidth(_tickLineWidth).SetStrokeColor(_tickColour);

                float x      = _originX;
                int   xIndex = 0;
                for (xIndex = 0; xIndex < _pointCount; xIndex += (int)_lineXTicksInterval)
                {
                    float y = _originY;

                    canvas.MoveTo(_originX + xIndex * _linePointSpace, _originY);
                    canvas.LineTo(_originX + xIndex * _linePointSpace, _originY + _axisHeight * _yScale);
                }
                canvas.Stroke();
            }
        }