即使阅读了这篇论坛帖子后,使用migradoc / pdfsharp创建带项目符号的列表仍然很难理解。我基本上想像这样显示一组项目:
- Dodge
- Nissan
- Ford
- Chevy
这是一个示例(在HelloWorld示例中添加了几行代码):
// Add some text to the paragraph
paragraph.AddFormattedText("Hello, World!", TextFormat.Italic);
// Add Bulletlist begin
Style style = document.AddStyle("MyBulletList", "Normal");
style.ParagraphFormat.LeftIndent = "0.5cm";
string[] items = "Dodge|Nissan|Ford|Chevy".Split('|');
for (int idx = 0; idx < items.Length; ++idx)
{
ListInfo listinfo = new ListInfo();
listinfo.ContinuePreviousList = idx > 0;
listinfo.ListType = ListType.BulletList1;
paragraph = section.AddParagraph(items[idx]);
paragraph.Style = "MyBulletList";
paragraph.Format.ListInfo = listinfo;
}
// Add Bulletlist end
return document;
我没使用AddToList方法将所有内容放在一个地方。在真实的应用程序中,我会使用这个方法(它是一个用户定义的方法,在这个线程中提供了代码)。
var document = new Document();
var style = document.AddStyle("BulletList", "Normal");
style.ParagraphFormat.LeftIndent = "0.5cm";
style.ParagraphFormat.ListInfo = new ListInfo
{
ContinuePreviousList = true,
ListType = ListType.BulletList1
};
var section = document.AddSection();
section.AddParagraph("Bullet 1", "BulletList");
section.AddParagraph("Bullet 2", "BulletList");
样式只需创建一次,包括列表信息,并可在任何地方重复使用。
ListInfo
对于编号列表是无效的。对于带有多个项目符号列表的RTF文件生成,我也不会重复使用它。 - I liked the old Stack Overflow使用PDFsharp需要自己绘制项目符号。
使用MigraDoc,您可以添加一个段落,并为该段落设置paragraph.Format.ListInfo以创建项目符号列表。
链接的线程显示了两个帮助程序:
DefineList()仅设置一个成员变量,以便下次创建新列表。
AddToList()对于每个条目都会调用。
只需调用DefineList()开始新的项目符号列表,然后为每个条目调用AddToList()。 DefineList()对于编号列表有很大的作用。
根据您的需要调整帮助程序。
AddListItem
方法,以获得一个新的段落来添加你的内容。using MigraDoc.DocumentObjectModel;
public static class ListHelper
{
/// <summary>
/// Start a new list in the current section.
/// </summary>
/// <param name="section"></param>
/// <param name="listType"></param>
/// <returns></returns>
public static ListContext NewItemList(this Section section, ListType listType)
{
return new ListContext(section, listType);
}
}
public class ListContext
{
private readonly Section section;
private readonly ListType listType;
private bool isFirstItem;
public ListContext(Section section, ListType listType)
{
this.section = section;
this.listType = listType;
this.isFirstItem = true;
}
/// <summary>
/// Returns a new paragraph to add the content for the list item.
/// </summary>
/// <returns></returns>
public Paragraph AddListItem()
{
var par = section.AddParagraph();
par.Format.LeftIndent = "0.5cm";
var listInfo = new ListInfo()
{
ListType = listType,
ContinuePreviousList = !isFirstItem
};
isFirstItem = false;
par.Format.ListInfo = listInfo;
return par;
}
}