无法在pdf中将文本设为粗体
Can't make text bold in pdf
我想将 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 格式呈现。
更新:问题在列表中。如果我将相同的应用到不在列表中的对象 - 它是粗体。
我只想走这条路,而不是为编写代码而烦恼:
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;
}
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);
此示例来自此答案 .
我能够产生的问题是因为我没有创建新的 ListItem
但立即将 Paragraph
添加到列表中。使用 List Item
一切正常。
我想将 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 格式呈现。
更新:问题在列表中。如果我将相同的应用到不在列表中的对象 - 它是粗体。
我只想走这条路,而不是为编写代码而烦恼:
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;
}
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);
此示例来自此答案 .
我能够产生的问题是因为我没有创建新的 ListItem
但立即将 Paragraph
添加到列表中。使用 List Item
一切正常。