migradoc / pdfsharp 中如何拥有项目符号列表

发布于 2024-09-29 03:27:11 字数 224 浏览 0 评论 0原文

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

  • 道奇
  • 日产
  • 福特
  • 雪佛兰

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

神回复 2024-10-06 03:27:11

下面是一个示例(在 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 方法将其全部放在一处。在实际的应用程序中,我会使用该方法(它是用户定义的方法,给出的代码 在此线程中)。

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

甜警司 2024-10-06 03:27:11

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

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,并且可以在任何地方重复使用。

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.

天涯沦落人 2024-10-06 03:27:11

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

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

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

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

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

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.

深爱不及久伴 2024-10-06 03:27:11

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

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文