Ejemplo n.º 1
0
        /// <summary>
        /// Adds a link
        /// </summary>
        /// <param name="link">Link to add</param>
        /// <param name="text">Optional - Text for the link</param>
        /// <param name="size">Optional - Font size</param>
        public void AddLink(string link, string text = null, int size = 10)
        {
            if (string.IsNullOrEmpty(text))
            {
                text = link;
            }
            PdfFont font              = PdfFonts.Select(false, false);
            int     availableWidthPt  = AvailableWidth();
            int     availableHeightPt = AvailableHeight();
            float   lineHeightPt;

            float[] linesWidthPt;
            var     lines = font.BreakText(text, size, availableWidthPt, out linesWidthPt, out lineHeightPt);

            if (availableHeightPt < lineHeightPt * lines.Count)
            {
                this.AddPage();
            }
            int linesCount = lines.Count;
            int x1         = CurrentX();
            int x2         = x1 + (int)linesWidthPt[0];
            int y1         = CurrentY();
            int y2         = (int)(y1 - linesCount * lineHeightPt);

            _writer.WriteLines(lines, 0, linesCount, x1, y1, font, size, 0x0000F0, lineHeightPt);
            _writer.WriteLink(link, x1, y2, x2, y1);
            _contentHeightPt += (int)(linesCount * lineHeightPt) + 4;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Saves as a file.
        /// </summary>
        /// <param name="filePath"></param>
        public void SaveAs(string filePath)
        {
            EnsureNotDisposed();
            PdfFont font = PdfFonts.Select(false, false);
            int     x    = -_pageNumberRightPt;
            int     y    = _pageNumberBottomPt;

            _writer.WritePagesNumbers(font, 9, x, y, 0);
            _writer.Close();
            try {
                if (filePath.IndexOf('{') != -1)
                {
                    filePath = string.Format(filePath, DateTime.UtcNow);
                }
                filePath = IOExt.ExpandPath(filePath);
                filePath = Path.GetFullPath(filePath);
                string folder = Path.GetDirectoryName(filePath);
                if (!Directory.Exists(folder))
                {
                    Directory.CreateDirectory(folder);
                }
                if (File.Exists(filePath))
                {
                    File.Delete(filePath);
                }

                using (var fs = new FileStream(filePath, FileMode.CreateNew, FileAccess.Write)) {
                    fs.Write(_writer.Buffer, 0, _writer.Length);
                }
            } catch (Exception ex) {
                throw new SeleniumException(ex);
            } finally {
                this.Dispose();
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Writes one or more lines of text
        /// </summary>
        /// <param name="text">Text</param>
        /// <param name="size">Font size</param>
        /// <param name="bold">Font bold</param>
        /// <param name="italic">Font italic</param>
        /// <param name="color">Font color</param>
        /// <param name="center">Center the text or not</param>
        /// <param name="indent">Indentation in em</param>
        /// <param name="spacing">Spacing in em</param>
        /// <param name="spacebefore">Space before in em</param>
        /// <param name="spaceAfter">Space after in em</param>
        private void WriteText(string text, short size, bool bold, bool italic
                               , string color, bool center, int indent, int spacing, int spacebefore, int spaceAfter)
        {
            EnsureNotDisposed();
            PdfFont font             = PdfFonts.Select(bold, italic);
            int     rgb              = ParseColorToRGB(color);
            int     availableWidthPt = AvailableWidth();
            float   lineHeightPt;

            float[] linesWidthPt;
            var     lines = font.BreakText(text, size, availableWidthPt, out linesWidthPt, out lineHeightPt);

            lineHeightPt += spacing;
            if (_contentHeightPt != 0)
            {
                _contentHeightPt += spacebefore;
            }

            int x = (int)(CurrentX() + indent);

            for (int iLine = 0, lineCount = lines.Count; iLine < lineCount;)
            {
                int maxLineCount   = (int)(AvailableHeight() / lineHeightPt);
                int chunkLineCount = Math.Min(lineCount - iLine, maxLineCount);
                if (chunkLineCount < 1)
                {
                    this.AddPage();
                    continue;
                }
                int y = CurrentY();
                if (center)
                {
                    int xx = x + (availableWidthPt / 2);
                    _writer.WriteLinesCenter(lines, iLine, chunkLineCount
                                             , xx, y, font, size, rgb, linesWidthPt, lineHeightPt);
                }
                else
                {
                    _writer.WriteLines(lines, iLine, chunkLineCount
                                       , x, y, font, size, rgb, lineHeightPt);
                }
                iLine            += chunkLineCount;
                _contentHeightPt += (int)(chunkLineCount * lineHeightPt);
            }
            _contentHeightPt += spaceAfter;
        }