如何在 migradoc / pdfsharp 中获得项目符号列表

问题描述 投票:0回答:4

即使在阅读了此论坛帖子之后,如何使用 migradoc / pdfsharp 创建项目符号列表仍然很令人困惑。我基本上想显示这样的项目列表:

  • 闪避
  • 日产
  • 福特
  • 雪佛兰
c# pdfsharp migradoc
4个回答
22
投票

这是一个示例(在 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 方法将所有内容集中在一处。在真实的应用程序中,我会使用该方法(这是一个用户定义的方法,代码在在此线程中给出)。


10
投票

比上面的答案更简洁一点:

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仅创建一次,包括listinfo,并且可以在任何地方重复使用。


3
投票

使用 PDFsharp,您必须自己绘制子弹。

使用 MigraDoc,您可以添加一个段落并为此段落设置 paragraph.Format.ListInfo 以创建项目符号列表。

链接的线程显示了两个辅助例程: DefineList() 仅设置一个成员变量,因此下次将创建一个新列表。 每个条目都会调用 AddToList()。

只需调用 DefineList() 即可开始一个新的项目符号列表,然后为每个条目调用 AddToList()。 DefineList() 对于编号列表有很大的不同。

根据您的需要调整帮助例程。


0
投票

我发现这个辅助类和扩展方法使得管理单独的列表(编号列表和项目符号列表)变得更简单。只需在您想要新列表的部分使用扩展方法即可获取新的列表上下文。然后在列表上下文中调用

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;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.