even after reading this forum post, its still quite confusing how to create a bulletted list using migradoc / pdfsharp. I basically want to display a list of items like this:
- Dodge
- Nissan
- Ford
- Chevy
even after reading this forum post, its still quite confusing how to create a bulletted list using migradoc / pdfsharp. I basically want to display a list of items like this:
Here's a sample (a few lines added to the HelloWorld sample):
// 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;
I didn't use the AddToList method to have it all in one place. In a real application I'd use that method (it's a user-defined method, code given in this thread).
A little bit more concise than the above answer:
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");
Style is only created once, including listinfo, and can be re-used everywhere.
ListInfo
does not work for numbered lists. I wouldn't reuse it for bullet lists either, at least not when generating RTF files with multiple bullet lists. –
Nicoline With PDFsharp you must draw the bullets yourself.
With MigraDoc you add a paragraph and set paragraph.Format.ListInfo for this paragraph to create a bullet list.
The linked thread shows two helper routines: DefineList() only sets a member variable so next time a new list will be created. AddToList() is called for each entry.
Simply call DefineList() to start a new bullet list, then call AddToList() for every entry. DefineList() makes a big difference for numbered lists.
Adapt the helper routines for your needs.
I found that this helper class and extension method made it simpler to manage seperate lists (both numbered and bullet). Simply use the extension method on the section you want a new list on to get a new list context. Then call AddListItem
on the list context to get a new paragraph to add your item content.
When you need to start a new list, simply call the extension method again to get a new list context.
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;
}
}
© 2022 - 2024 — McMap. All rights reserved.