不能在pdf中使文字加粗

问题描述 投票:0回答:2

我想以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);

还尝试添加段落一些字体...试图将段落改为短语...

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


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

c# pdf fonts itext
2个回答
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
投票
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一切正常。

© www.soinside.com 2019 - 2024. All rights reserved.