不能在pdf中使文字加粗

时间:2015-08-11 15:03:14

标签: c# pdf fonts itext

我想以pdf粗体加总。我接下来试过了:

var phraseTotal = new Paragraph();
phraseTotal.Font.SetStyle(Font.BOLD);
phraseTotal.Add(new Chunk(string.Format("Total earned for period {0} - {1} : ", invoice.FromDate.ToShortDateString(), invoice.ToDate.ToShortDateString())));
phraseTotal.Add(new Chunk("£" + Math.Round(invoice.TotalAmount / 100.0, 2, MidpointRounding.AwayFromZero)));

list.Add(phraseTotal);

还试图将Paragraph添加到某些字体中...试图将Paragraph更改为Phrase ...

但总是这个总和是不可见的或不以pdf呈现。

更新:问题出在列表中。如果我将相同的内容应用于列表中没有的对象 - 它是粗体。

2 个答案:

答案 0 :(得分:1)

我会走这条路,而不是为编写代码而烦恼:

private MemoryStream createPDF(string html)
{
    MemoryStream msOutput = new MemoryStream();
    TextReader reader = new StringReader(html);

    // step 1: creation of a document-object
    Document document = new Document(PageSize.A4, 30, 30, 30, 30);            

    // step 2:
    // we create a writer that listens to the document
    // and directs a XML-stream to a file
    PdfWriter writer = PdfWriter.GetInstance(document, msOutput);

    // step 3: we create a worker parse the document
    HTMLWorker worker = new HTMLWorker(document);

    // step 4: we open document and start the worker on the document
    document.Open();
    worker.StartDocument();

    // step 5: parse the html into the document
    worker.Parse(reader);

    // step 6: close the document and the worker
    worker.EndDocument();
    worker.Close();
    document.Close();

    return msOutput;
}

答案 1 :(得分:1)

Chunk c1 = new Chunk("Concentrate:", new Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 11, Font.BOLD, BaseColor.BLUE)));
Chunk c2 = new Chunk("In order to remember something you need to learn it. Learning is possible only if you pay enough attention to it. You will retain information for a longer period of time only if you concentrate properly at the time of learning.", new Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 11, Font.NORMAL, BaseColor.BLUE)));

Pharagraph p2 = new Pharagraph();
p2.Add(c1);
p2.Add(c2);

ListItem firstRecommend = new ListItem(p2);

此示例来自此回答https://stackoverflow.com/a/9227686/3917754

我能够生成的问题导致我没有创建新的ListItem但是立即将Paragraph添加到列表中。使用List Item一切正常。