15

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
leora
  • 188,729
  • 360
  • 878
  • 1,366

3 Answers3

21

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).

  • 3
    Nice, thank you. I would add this detail to the style to make it aligned right : style.ParagraphFormat.LeftIndent = "1cm"; style.ParagraphFormat.FirstLineIndent = "-0.5cm"; – Spiky Dec 05 '13 at 01:03
  • 1
    I am little confused. Is each bullet list is considered as a Paragraph ? – Sabareesh Kkanan Apr 03 '14 at 21:23
  • 3
    @Sabareesh Kkanan: Each bulleted item is a paragraph, the bullet list is a sequence of paragraphs. This allows advanced formatting (e.g. first line indent) if an entry spans more than a single line. – I liked the old Stack Overflow Apr 07 '14 at 07:44
  • Thanks Matt! That was exactly what I needed. Oh, and I loved you in "Friends." – Ryan Jul 11 '16 at 03:54
9

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.

Robin van der Knaap
  • 4,060
  • 2
  • 33
  • 48
  • Reusing the `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. – I liked the old Stack Overflow Mar 22 '17 at 20:56
  • @User241.007 I didn't realize this won't work for numbered lists or RTF files. For PDF files, however, this approach works fine, also with multiple bullet lists. I'll leave this answer be, because for pdf's at least, this approach saves a lot of code (you have to define listinfo just once). – Robin van der Knaap Apr 04 '17 at 08:37
  • if the paragraph does not fit on 1 line the next line is not indented properly. Is there a setting for this? – MIKE Jan 30 '18 at 19:00
  • @MIKE style.ParagraphFormat.LeftIndent = "1cm"; style.ParagraphFormat.FirstLineIndent = "-0.5cm"; I solved it this way, not sure if there are better alternatives. – Robin van der Knaap Feb 07 '18 at 13:32
3

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.