GetCalculatedBaseFont() публичный Метод

public GetCalculatedBaseFont ( bool specialEncoding ) : BaseFont
specialEncoding bool
Результат iTextSharp.text.pdf.BaseFont
Пример #1
0
        private void DrawText(PdfWriter writer,
                              string text,
                              int fontSize,
                              BaseColor color,
                              double x,
                              double y,
                              bool vertical)
        {
            AffineTransform textTransform = AffineTransform.GetTranslateInstance(x, y);

            if (vertical)
            {
                textTransform.Rotate(Math.PI / 2);
            }
            //writer.DirectContent.Transform(rotate90);

            PdfContentByte cb = writer.DirectContent;

            cb.BeginText();
            ////BaseFont textFont = BaseFont.CreateFont("Arial", BaseFont.CP1252, true);
            cb.SetColorStroke(color);
            cb.SetColorFill(color);
            cb.SetFontAndSize(SearchTextFont.GetCalculatedBaseFont(false), fontSize);
            cb.SetTextMatrix(textTransform); //(xPos, yPos)

            cb.ShowText(text);
            cb.EndText();
        }
Пример #2
0
        public void ApplyForText(PdfContentByte cb, IDictionary<String, String> css, Chunk chunk)
        {
            SetStrokeAndFillColor(cb, css);
            SetStrokeAndFill(cb, css);

            try
            {
                Font font = new Font(Font.FontFamily.COURIER, 6, Font.NORMAL, BaseColor.BLACK);

                Font font2 = chunk.Font;

                BaseFont bf2 = font2.BaseFont;
                //BaseFont bf = ;

                if (bf2 == null)
                {
                    cb.SetFontAndSize(font.GetCalculatedBaseFont(false), font2.Size);
                }
                else
                {
                    cb.SetFontAndSize(bf2, font2.Size);
                }
            } catch { }

        }
Пример #3
0
 /**
  * Gets the width of the Chunk in points.
  * @return a width in points
  */
 virtual public float GetWidthPoint()
 {
     if (GetImage() != null)
     {
         return(GetImage().ScaledWidth);
     }
     return(font.GetCalculatedBaseFont(true).GetWidthPoint(Content, font.CalculatedSize) * HorizontalScaling);
 }
Пример #4
0
 /**
 * Adds a <CODE>Font</CODE> to be searched for valid characters.
 * @param font the <CODE>Font</CODE>
 */    
 public void AddFont(Font font) {
     if (font.BaseFont != null) {
         fonts.Add(font);
         return;
     }
     BaseFont bf = font.GetCalculatedBaseFont(true);
     Font f2 = new Font(bf, font.Size, font.CalculatedStyle, font.Color);
     fonts.Add(f2);
 }
Пример #5
0
        private void WritepPDF()
        {
            Document document = new Document(PageSize.A4, 50, 50, 50, 50);

            var PDFfont = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 24);

            var PDFfontBase = PDFfont.GetCalculatedBaseFont(false);

            string UserInforamtion = $"User: {mainWindow.loggedUser.Login}\nPhone Number: {PhoneBox.Text}\n\n\n";

            string BasketInformation =
                $"{(mainWindow.basket.laptops.Count != 0 ? $"Laptops: {mainWindow.basket.laptops.Select(item => item.LaptopName).Aggregate((x, y) => x + ", " + y)}" : string.Empty)}\n" +
                $"{(mainWindow.basket.computres.Count != 0 ? $"Computers: {mainWindow.basket.computres.Select(item => item.ComputerName).Aggregate((x, y) => x + ", " + y)}" : string.Empty)}\n" +
                $"{(mainWindow.basket.phones.Count != 0 ? $"Phones: {mainWindow.basket.phones.Select(item => item.PhoneName).Aggregate((x, y) => x + ", " + y)}" : string.Empty)}\n";

            using (var writer = PdfWriter.GetInstance(document, new FileStream("Check.pdf", FileMode.Create)))
            {
                document.Open();

                writer.DirectContent.SetColorStroke(new BaseColor(255, 0, 0));

                writer.DirectContent.SetLineWidth(2.0f);

                writer.DirectContent.MoveTo(document.Left - 25, document.Top);
                writer.DirectContent.LineTo(document.Right - 25, document.Top);
                writer.DirectContent.Stroke();

                writer.DirectContent.MoveTo(document.Left - 25, document.Top);
                writer.DirectContent.LineTo(document.Left - 25, document.Bottom + 100);
                writer.DirectContent.Stroke();

                writer.DirectContent.MoveTo(document.Right - 25, document.Top);
                writer.DirectContent.LineTo(document.Right - 25, document.Bottom + 100);
                writer.DirectContent.Stroke();

                writer.DirectContent.MoveTo(document.Left - 25, document.Bottom + 100);
                writer.DirectContent.LineTo(document.Right - 25, document.Bottom + 100);
                writer.DirectContent.Stroke();

                writer.DirectContent.BeginText();

                writer.DirectContent.SetFontAndSize(PDFfontBase, 24f);

                document.Add(new iTextSharp.text.Paragraph("Check"));

                document.Add(new iTextSharp.text.Paragraph(UserInforamtion + BasketInformation));

                document.Add(new iTextSharp.text.Paragraph($"\n{DateTime.Now}"));

                writer.DirectContent.EndText();

                document.Close();

                writer.Close();
            }
        }
Пример #6
0
        public PdfMakerByIText(ILogService log)
        {
            _log = log;

            TextColor          = BaseColor.BLACK;
            TransparentColor   = new BaseColor(255, 255, 255, 255);
            SearchTextFont     = FontFactory.GetFont(FontFactory.HELVETICA);
            SearchTextBaseFont = SearchTextFont.GetCalculatedBaseFont(false);
            //SearchTextFont.Color = new BaseColor(255, 255, 255, 255); //Transparent
        }
Пример #7
0
        private float[] GetHeaderWidths(iTextSharp.text.Font font, string[] headers)
        {
            var total   = 0;
            var columns = headers.Length;
            var widths  = new int[columns];

            for (var i = 0; i < columns; ++i)
            {
                var w = font.GetCalculatedBaseFont(true).GetWidth(headers[i]);
                total    += w;
                widths[i] = w;
            }
            var   result        = new float[columns];
            float totPercentage = 0;

            for (var i = 0; i < columns; ++i)
            {
                result[i]      = (float)widths[i] / total * 100;
                totPercentage += result[i];
            }
            return(result);
        }
 // ===========================================================================
 public void Write(Stream stream)
 {
     string RESOURCE = Utility.ResourcePosters;
     // step 1
     using (Document document = new Document())
     {
         // step 2
         PdfWriter writer = PdfWriter.GetInstance(document, stream);
         // step 3
         document.Open();
         // step 4
         Chunk c;
         String foobar = "Foobar Film Festival";
         // Measuring a String in Helvetica
         Font helvetica = new Font(Font.FontFamily.HELVETICA, 12);
         BaseFont bf_helv = helvetica.GetCalculatedBaseFont(false);
         float width_helv = bf_helv.GetWidthPoint(foobar, 12);
         c = new Chunk(foobar + ": " + width_helv, helvetica);
         document.Add(new Paragraph(c));
         document.Add(new Paragraph(string.Format(
           "Chunk width: {0}", c.GetWidthPoint()
         )));
         // Measuring a String in Times
         BaseFont bf_times = BaseFont.CreateFont(
           "c:/windows/fonts/times.ttf",
           BaseFont.WINANSI, BaseFont.EMBEDDED
         );
         Font times = new Font(bf_times, 12);
         float width_times = bf_times.GetWidthPoint(foobar, 12);
         c = new Chunk(foobar + ": " + width_times, times);
         document.Add(new Paragraph(c));
         document.Add(new Paragraph(String.Format(
           "Chunk width: {0}", c.GetWidthPoint()
         )));
         document.Add(Chunk.NEWLINE);
         // Ascent and descent of the String
         document.Add(new Paragraph(
           "Ascent Helvetica: " + bf_helv.GetAscentPoint(foobar, 12)
         ));
         document.Add(new Paragraph(
           "Ascent Times: " + bf_times.GetAscentPoint(foobar, 12)
         ));
         document.Add(new Paragraph(
           "Descent Helvetica: " + bf_helv.GetDescentPoint(foobar, 12)
         ));
         document.Add(new Paragraph(
           "Descent Times: " + bf_times.GetDescentPoint(foobar, 12)
         ));
         document.Add(Chunk.NEWLINE);
         // Kerned text
         width_helv = bf_helv.GetWidthPointKerned(foobar, 12);
         c = new Chunk(foobar + ": " + width_helv, helvetica);
         document.Add(new Paragraph(c));
         // Drawing lines to see where the text is added
         PdfContentByte canvas = writer.DirectContent;
         canvas.SaveState();
         canvas.SetLineWidth(0.05f);
         canvas.MoveTo(400, 806);
         canvas.LineTo(400, 626);
         canvas.MoveTo(508.7f, 806);
         canvas.LineTo(508.7f, 626);
         canvas.MoveTo(280, 788);
         canvas.LineTo(520, 788);
         canvas.MoveTo(280, 752);
         canvas.LineTo(520, 752);
         canvas.MoveTo(280, 716);
         canvas.LineTo(520, 716);
         canvas.MoveTo(280, 680);
         canvas.LineTo(520, 680);
         canvas.MoveTo(280, 644);
         canvas.LineTo(520, 644);
         canvas.Stroke();
         canvas.RestoreState();
         // Adding text with PdfContentByte.ShowTextAligned()
         canvas.BeginText();
         canvas.SetFontAndSize(bf_helv, 12);
         canvas.ShowTextAligned(Element.ALIGN_LEFT, foobar, 400, 788, 0);
         canvas.ShowTextAligned(Element.ALIGN_RIGHT, foobar, 400, 752, 0);
         canvas.ShowTextAligned(Element.ALIGN_CENTER, foobar, 400, 716, 0);
         canvas.ShowTextAligned(Element.ALIGN_CENTER, foobar, 400, 680, 30);
         canvas.ShowTextAlignedKerned(Element.ALIGN_LEFT, foobar, 400, 644, 0);
         canvas.EndText();
         // More lines to see where the text is added
         canvas.SaveState();
         canvas.SetLineWidth(0.05f);
         canvas.MoveTo(200, 590);
         canvas.LineTo(200, 410);
         canvas.MoveTo(400, 590);
         canvas.LineTo(400, 410);
         canvas.MoveTo(80, 572);
         canvas.LineTo(520, 572);
         canvas.MoveTo(80, 536);
         canvas.LineTo(520, 536);
         canvas.MoveTo(80, 500);
         canvas.LineTo(520, 500);
         canvas.MoveTo(80, 464);
         canvas.LineTo(520, 464);
         canvas.MoveTo(80, 428);
         canvas.LineTo(520, 428);
         canvas.Stroke();
         canvas.RestoreState();
         // Adding text with ColumnText.ShowTextAligned()
         Phrase phrase = new Phrase(foobar, times);
         ColumnText.ShowTextAligned(canvas, Element.ALIGN_LEFT, phrase, 200, 572, 0);
         ColumnText.ShowTextAligned(canvas, Element.ALIGN_RIGHT, phrase, 200, 536, 0);
         ColumnText.ShowTextAligned(canvas, Element.ALIGN_CENTER, phrase, 200, 500, 0);
         ColumnText.ShowTextAligned(canvas, Element.ALIGN_CENTER, phrase, 200, 464, 30);
         ColumnText.ShowTextAligned(canvas, Element.ALIGN_CENTER, phrase, 200, 428, -30);
         // Chunk attributes
         c = new Chunk(foobar, times);
         c.SetHorizontalScaling(0.5f);
         phrase = new Phrase(c);
         ColumnText.ShowTextAligned(canvas, Element.ALIGN_LEFT, phrase, 400, 572, 0);
         c = new Chunk(foobar, times);
         c.SetSkew(15, 15);
         phrase = new Phrase(c);
         ColumnText.ShowTextAligned(canvas, Element.ALIGN_LEFT, phrase, 400, 536, 0);
         c = new Chunk(foobar, times);
         c.SetSkew(0, 25);
         phrase = new Phrase(c);
         ColumnText.ShowTextAligned(canvas, Element.ALIGN_LEFT, phrase, 400, 500, 0);
         c = new Chunk(foobar, times);
         c.SetTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_STROKE, 0.1f, BaseColor.RED);
         phrase = new Phrase(c);
         ColumnText.ShowTextAligned(canvas, Element.ALIGN_LEFT, phrase, 400, 464, 0);
         c = new Chunk(foobar, times);
         c.SetTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE, 1, null);
         phrase = new Phrase(c);
         ColumnText.ShowTextAligned(canvas, Element.ALIGN_LEFT, phrase, 400, 428, -0);
     }
 }
Пример #9
0
        public static string Create(IEnumerable <string> headers,
                                    IEnumerable <IEnumerable <string> > items,
                                    string filePath,
                                    string fileName,
                                    string title = null)
        {
            Contract.Assert(headers != null);
            Contract.Assert(items != null);

            var headerList = headers as IList <string> ?? headers.ToList();
            var table      = new PdfPTable(headerList.Count())
            {
                RunDirection = PdfWriter.RUN_DIRECTION_RTL, WidthPercentage = 100, LockedWidth = false
            };
            var columnMaxWidths = new int[headerList.Count()];
            var _path           = string.Empty;

            for (var i = 0; i < columnMaxWidths.Length; i++)
            {
                columnMaxWidths[i] = 0;
            }
            var fontPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "tahoma.ttf");
            var baseFont = BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);


            if (string.IsNullOrEmpty(title))
            {
                var tblTitle = new PdfPCell(new Phrase(title))
                {
                    PaddingTop    = 10,
                    PaddingBottom = 10,
                    Colspan       = headerList.Count()
                };

                table.AddCell(tblTitle);
            }

            int index = 0;

            foreach (var hd in headerList)
            {
                var font = new Font(baseFont, 7f, Font.BOLD, new BaseColor(Color.White));
                var cell = new PdfPCell(new Phrase(hd, font))
                {
                    BackgroundColor     = new BaseColor(25, 121, 255, 255),
                    PaddingTop          = 5,
                    PaddingRight        = 3,
                    PaddingBottom       = 5,
                    PaddingLeft         = 2,
                    HorizontalAlignment = 1
                };
                table.AddCell(cell);
                var textWidth = font.GetCalculatedBaseFont(true).GetWidth(hd);
                columnMaxWidths[index] = columnMaxWidths[index] > textWidth ? columnMaxWidths[index] : textWidth;
                index++;
            }

            int rowNumber = 1;

            foreach (var row in items)
            {
                var bgColor = rowNumber % 2 == 0
                    ? new BaseColor(Color.FromArgb(255, 214, 215, 216))
                    : new BaseColor(Color.White);

                var index2 = 0;
                foreach (var cell in row)
                {
                    var font    = new Font(baseFont, 6f, Font.NORMAL, new BaseColor(Color.Black));
                    var newCell = new PdfPCell(new Phrase(cell, font))
                    {
                        BackgroundColor     = bgColor,
                        Padding             = 2,
                        HorizontalAlignment = 1,
                        RunDirection        = PdfWriter.RUN_DIRECTION_RTL
                    };
                    table.AddCell(newCell);
                    var textWidth = string.IsNullOrEmpty(cell) ? 6 : font.GetCalculatedBaseFont(true).GetWidth(cell);
                    columnMaxWidths[index2] = columnMaxWidths[index2] > textWidth ? columnMaxWidths[index2] : textWidth;
                    index2++;
                }
                rowNumber++;
            }

            using (var memStream = new MemoryStream())
            {
                var doc = new Document(PageSize.A4);
                doc.AddHeader("default", "Default header");
                doc.AddCreator("mdkh.ir");
                doc.AddCreationDate();
                doc.AddTitle("Default Title");
                doc.AddAuthor("mdkh");
                PdfWriter.GetInstance(doc, memStream);
                doc.SetPageSize(PageSize.A4.Rotate());
                var tableWidth     = doc.PageSize.Height - (doc.RightMargin + doc.LeftMargin);
                var total          = columnMaxWidths.Sum();
                var relativeWidths = new float[columnMaxWidths.Count()];
                for (int i = 0; i < relativeWidths.Length; i++)
                {
                    var widthShare = (columnMaxWidths[i]) / (total * 1.0f);
                    //because of rtl direction
                    relativeWidths[relativeWidths.Length - 1 - i] = Convert.ToSingle((widthShare > .35f ? .3f : widthShare) * tableWidth);
                }
                table.SetWidths(relativeWidths);

                doc.Open();
                doc.Add(table);
                var ms2 = new MemoryStream();
                memStream.CopyTo(ms2);
                doc.Close();
                var fileContent = memStream.ToArray();
                var pgNumber    = new PageNumber()
                {
                    TextTemplate  = "{{current}}/{{total}}",
                    PrintLocation = PageSection.Bottom,
                    Alignment     = Alignment.Center,
                    FontOption    = new FontOption(baseFont),
                    Direction     = Direction.LeftToRight
                };
                AddPageNumbers(fileContent, ms2, pgNumber);

                var content = ms2.ToArray();
                _path = Path.Combine(filePath, (fileName.ToLowerInvariant().EndsWith(".pdf") ? fileName : fileName + ".pdf"));
                using (var fs = File.Create(_path))
                {
                    fs.Write(content, 0, (int)content.Length);
                }
            }

            return(_path);
        }