How do you have a bulletted list in migradoc / pdfsharp
Asked Answered
D

4

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
Ditchwater answered 26/10, 2010 at 15:37 Comment(0)
N
22

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

Nicoline answered 31/10, 2010 at 8:27 Comment(4)
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";Snack
I am little confused. Is each bullet list is considered as a Paragraph ?Dual
@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.Nicoline
Thanks Matt! That was exactly what I needed. Oh, and I loved you in "Friends."Liard
G
10

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.

Graphics answered 22/3, 2017 at 20:26 Comment(4)
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.Nicoline
@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).Graphics
if the paragraph does not fit on 1 line the next line is not indented properly. Is there a setting for this?Baggywrinkle
@Baggywrinkle style.ParagraphFormat.LeftIndent = "1cm"; style.ParagraphFormat.FirstLineIndent = "-0.5cm"; I solved it this way, not sure if there are better alternatives.Graphics
N
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.

Nicoline answered 27/10, 2010 at 7:39 Comment(1)
do you have any example code on this. . still can't get it to work . .Ditchwater
A
0

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;
    }
}
Albaugh answered 19/9, 2023 at 13:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.