Bold in iTextSharp

1

I'm working with itextsharp and I've been looking for and testing the subject of bold for several weeks.

I've tried with this code, for example:

Font verdanaBold = FontFactory.GetFont("Verdana", 7f, Font.BOLD);

He tells me that a using is missing, but I have a post:

using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.fonts;

After with:

documento.Add(new Paragraph("TITULO 1",Text.BOLD);

And it did not work either.

I put a part of the code:

//CARACTERISTICAS CLIENTE
var MyFont1 = FontFactory.GetFont("Times New Roman", 18);

//CARACTERISTICAS EMPRESA
var FontColour = new BaseColor(255, 0, 0);
var MyFont = FontFactory.GetFont("Times New Roman", 11, FontColour, Font.BOLD);

A parafo:

Paragraph p2 = new Paragraph(" " + tb_direccion.Text + " ", MyFont1);
p2.Alignment = Element.ALIGN_CENTER;
doc.Add(p2);

Any suggestions to add bold?

    
asked by S.Carrillo 10.09.2018 в 11:13
source

3 answers

0

You can rehearse in the following way:

var MyFont = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 11, Font.BOLD);
documento.Add(new Paragraph("TITULO 1", MyFont));
    
answered by 10.09.2018 в 18:23
0

I solved it with the following code:

private static Font SegoeBold
{
    get
    {
        try
        {
            var fontName = "Segoe Bold";
            if (!FontFactory.IsRegistered(fontName))
            {
                var fontPath = Environment.GetEnvironmentVariable("SystemRoot") + "\Fonts\segoeui.ttf";
                FontFactory.Register(fontPath);
            }
            return FontFactory.GetFont(fontName, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 9f, Font.BOLD, BaseColor.BLACK);
        }
        catch
        {
            return null;
        }
    }
}

What it does is directly search the typography file. You can even download typographies from the internet and, without installing them, do something very similar:

private static BaseFont _sourceSans = BaseFont.CreateFont(System.Web.HttpContext.Current.Server.MapPath("~/Content/fonts/SourceSansPro-Regular.otf"), BaseFont.CP1252, BaseFont.EMBEDDED);

private static Font StandardFont = new Font(_sourceSans, 9, Font.NORMAL, BaseColor.BLACK);

To place the texts, remember the following hierarchy:

  • Chunk
  • Phrase
  • Paragraph
  • A Paragraph is composed of Phrase s which in turn are composed of Chunk s. An example:

    Phrase folio = new Phrase(new Chunk("Folio: ", SemiboldFont));
    folio.Add(new Chunk("12345", StandardFont));
    

    As you can see, first I created a Phrase object containing a Chunk with a "semibold" typeface, and then at the same Phrase I added another Chunk but with the "standard" typography, so you can create paragraphs with different fonts. With this, you only need to put it in the document:

    doc.Add(folio);
    
        
    answered by 10.09.2018 в 22:50
    0

    I solved it using this:

            var MyFontBold = FontFactory.GetFont(FontFactory.TIMES_BOLD, 11);
    

    And then to use them:

         PdfPCell titulomaquina = new PdfPCell(new Phrase("" + cb_maquina.Text + "", MyFontBold));
            titulomaquina.Colspan = 3;
            titulomaquina.Border = 0;
            titulomaquina.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
            table.AddCell(titulomaquina);
    
        
    answered by 12.09.2018 в 09:36