如何使用 XML 和 iTextSharp 在 PDF 文档中创建表格?

发布于 2024-12-10 10:29:22 字数 393 浏览 0 评论 0原文

我有兴趣使用 iTextSharp 在带有 XML 的 PDF 文档中创建表格。我买不起这本书,而且在网上也找不到任何例子。我还需要能够进行单元格跨越并避免表格中间出现分页符。我宁愿将表格发送到下一页。

这是我一直在关注的示例: http://www.codeproject.com/Articles/66948/Rendering-PDF-views-in-ASP-MVC-using-iTextSharp.aspx

有谁知道如何要完成此任务或者可以指导我一些教程吗?

谢谢

I am interested in creating tables in a PDF document with XML using iTextSharp. I cannot afford the book, and i cant find any examples on the web. I also need to be able to make cellspanning and avoid page breaks in the middle of a table. I would rather prefer the table being sent to the next page.

This is the example ive been follwing: http://www.codeproject.com/Articles/66948/Rendering-PDF-views-in-ASP-MVC-using-iTextSharp.aspx

Does anyone know how to accomplish this or can direct me to some tutorials?

Thanks

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

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

发布评论

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

评论(1

二智少女猫性小仙女 2024-12-17 10:29:22

如果您使用的是 iTextSharp 4.x,那么您可以继续使用 ITextHandler 来解析特定于 iText 的 XML。据我所知,此功能已从 5.x 版本中删除,因此不幸的是您将很难找到对其的支持。下面是 iText XML 的 DTD,希望这会有所帮助。

我不能说为什么它被删除,但我可以猜测它与以下事实有关:如果您可以编写 XML,那么您还不如编写常规代码。此外,构建和维护 PDF 库,同时维护库的元语言 (XML) 也很麻烦。因此,了解这一点后,我个人建议跳过 XML,只使用本机 iTextSharp 库。

<!--
    This DTD can be used to validate the output of XmlWriter.
    XmlWriter is part of the iText library by lowagie.com

    For further information, see: http://www.lowagie.com/iText/

    Copyright 2001 by Bruno Lowagie
    All Rights Reserved.
-->

<!ENTITY % chunk.content "#PCDATA | newline | newpage | entity | ignore">
<!ENTITY % phrase.content "chunk | anchor | phrase | list | table | annotation">

<!ENTITY % color.attributes
"red      CDATA   #IMPLIED
 green    CDATA   #IMPLIED
 blue     CDATA   #IMPLIED"
>
<!ENTITY % font.attributes
"font     CDATA   #IMPLIED
 size     CDATA   #IMPLIED
 style    CDATA   #IMPLIED
 color    CDATA   #IMPLIED
 %color.attributes;"
>
<!ENTITY % phrase.attributes
"leading  CDATA   #IMPLIED"
>
<!ENTITY % paragraph.attributes
"align    CDATA   #IMPLIED"
>
<!ENTITY % indentation.attributes
"indentationleft    CDATA   #IMPLIED
 indentationright   CDATA   #IMPLIED"
>
<!ENTITY % section.attributes
"depth              CDATA   #IMPLIED
 numberdepth        CDATA   #IMPLIED
 indent             CDATA   #IMPLIED"
>
<!ENTITY % rectangle.attributes
"bgred              CDATA   #IMPLIED
 bggreen            CDATA   #IMPLIED
 bgblue             CDATA   #IMPLIED
 width              CDATA   #IMPLIED
 bordercolor        CDATA   #IMPLIED
 backgroundcolor    CDATA   #IMPLIED
 left               CDATA   #IMPLIED
 right              CDATA   #IMPLIED
 top                CDATA   #IMPLIED
 bottom             CDATA   #IMPLIED
 borderwidth        CDATA   #IMPLIED
 grayfill           CDATA   #IMPLIED"
>

<!ELEMENT itext (%chunk.content; | %phrase.content; | chapter | paragraph)*>
<!ATTLIST itext
    title       CDATA   #IMPLIED
    subject     CDATA   #IMPLIED
    keywords    CDATA   #IMPLIED
    author      CDATA   #IMPLIED
>

<!ELEMENT symbol EMPTY>
<!ATTLIST symbol
    id      CDATA #REQUIRED
>

<!ELEMENT chunk (%chunk.content;)*> 
<!ATTLIST chunk
    %font.attributes;
    subsupscript        CDATA   #IMPLIED
    localgoto           CDATA   #IMPLIED
    localdestination    CDATA   #IMPLIED
    generictag          CDATA   #IMPLIED
>

<!ELEMENT phrase (%chunk.content; | %phrase.content;)*> 
<!ATTLIST phrase
    %font.attributes;
    %phrase.attributes;
>

<!ELEMENT anchor (%chunk.content; | %phrase.content;)*> 
<!ATTLIST anchor
%font.attributes;
    %phrase.attributes;
    name       CDATA   #IMPLIED
    reference  CDATA   #IMPLIED
>

<!ELEMENT paragraph (%chunk.content; | %phrase.content; | image)*> 
<!ATTLIST paragraph
    %font.attributes;
    %phrase.attributes;
    %indentation.attributes;
    %paragraph.attributes;
>

<!ELEMENT list (listitem | ignore)*> 
<!ATTLIST list
    %font.attributes;
    %indentation.attributes;
    numbered       CDATA   #IMPLIED
    symbolindent   CDATA   #IMPLIED
    first          CDATA   #IMPLIED
    listsymbol     CDATA   #IMPLIED
>

<!ELEMENT listitem (%chunk.content; | %phrase.content; | image)*> 
<!ATTLIST listitem
    %font.attributes;
    %phrase.attributes;
    %indentation.attributes;
    %paragraph.attributes;
>

<!ELEMENT chapter (title?, sectioncontent) >
<!ATTLIST chapter
    %section.attributes;
    %indentation.attributes;
>

<!ELEMENT section (title?, sectioncontent) >
<!ATTLIST section
    %section.attributes;
    %indentation.attributes;
>

<!ELEMENT title (%chunk.content; | phrase | chunk | annotation)*> 
<!ATTLIST title
    %font.attributes;
    %phrase.attributes;
    %indentation.attributes;
    %paragraph.attributes;
>

<!ELEMENT sectioncontent (%chunk.content; | %phrase.content; | section | paragraph | image)*>

<!ELEMENT table (cell*)>
<!ATTLIST table
    %color.attributes;
    %paragraph.attributes;
    %rectangle.attributes;
    columns        CDATA   #IMPLIED
    lastHeaderRow  CDATA   #IMPLIED
    cellpadding    CDATA   #IMPLIED
    cellspacing    CDATA   #IMPLIED
    widths         CDATA   #IMPLIED
>

<!ELEMENT row (cell*)>
<!ELEMENT cell (%chunk.content; | %phrase.content; | paragraph | image)*>
<!ATTLIST cell
    %color.attributes;
    %phrase.attributes;
    %indentation.attributes;
    %rectangle.attributes;
    colspan    CDATA   #IMPLIED
    rowspan    CDATA   #IMPLIED
    header     CDATA   #IMPLIED
    nowrap     CDATA   #IMPLIED
>

<!ELEMENT image EMPTY>
<!ATTLIST image
    url             CDATA   #REQUIRED
    align           CDATA   #IMPLIED
    underlying      CDATA   #IMPLIED
    textwrap        CDATA   #IMPLIED
    alt             CDATA   #IMPLIED
    absolutex       CDATA   #IMPLIED
    absolutey       CDATA   #IMPLIED
    plainwidth      CDATA   #IMPLIED
    plainheight     CDATA   #IMPLIED
    rotation        CDATA   #IMPLIED
>

<!ELEMENT annotation EMPTY>
<!ATTLIST annotation
    title       CDATA   #IMPLIED
    content     CDATA   #IMPLIED
>

<!ELEMENT newpage EMPTY>
<!ELEMENT newline EMPTY>

If you're using iTextSharp 4.x then you can continue using ITextHandler to parse iText-specific XML. To the best of my knowledge this feature has been removed from the 5.x versions so unfortunately you're going to have a hard time find support for it. Below is the DTD for the iText XML, hopefully this will help.

I can't say why it was removed but I can guess that it had something to do with the fact that if you can write XML you might as well just write regular code. Also, building and maintaining a PDF library while also maintaining a meta-language (XML) for your library is a lot of hassle. So know this, I would personally recommend skipping the XML and just working with the native iTextSharp library.

<!--
    This DTD can be used to validate the output of XmlWriter.
    XmlWriter is part of the iText library by lowagie.com

    For further information, see: http://www.lowagie.com/iText/

    Copyright 2001 by Bruno Lowagie
    All Rights Reserved.
-->

<!ENTITY % chunk.content "#PCDATA | newline | newpage | entity | ignore">
<!ENTITY % phrase.content "chunk | anchor | phrase | list | table | annotation">

<!ENTITY % color.attributes
"red      CDATA   #IMPLIED
 green    CDATA   #IMPLIED
 blue     CDATA   #IMPLIED"
>
<!ENTITY % font.attributes
"font     CDATA   #IMPLIED
 size     CDATA   #IMPLIED
 style    CDATA   #IMPLIED
 color    CDATA   #IMPLIED
 %color.attributes;"
>
<!ENTITY % phrase.attributes
"leading  CDATA   #IMPLIED"
>
<!ENTITY % paragraph.attributes
"align    CDATA   #IMPLIED"
>
<!ENTITY % indentation.attributes
"indentationleft    CDATA   #IMPLIED
 indentationright   CDATA   #IMPLIED"
>
<!ENTITY % section.attributes
"depth              CDATA   #IMPLIED
 numberdepth        CDATA   #IMPLIED
 indent             CDATA   #IMPLIED"
>
<!ENTITY % rectangle.attributes
"bgred              CDATA   #IMPLIED
 bggreen            CDATA   #IMPLIED
 bgblue             CDATA   #IMPLIED
 width              CDATA   #IMPLIED
 bordercolor        CDATA   #IMPLIED
 backgroundcolor    CDATA   #IMPLIED
 left               CDATA   #IMPLIED
 right              CDATA   #IMPLIED
 top                CDATA   #IMPLIED
 bottom             CDATA   #IMPLIED
 borderwidth        CDATA   #IMPLIED
 grayfill           CDATA   #IMPLIED"
>

<!ELEMENT itext (%chunk.content; | %phrase.content; | chapter | paragraph)*>
<!ATTLIST itext
    title       CDATA   #IMPLIED
    subject     CDATA   #IMPLIED
    keywords    CDATA   #IMPLIED
    author      CDATA   #IMPLIED
>

<!ELEMENT symbol EMPTY>
<!ATTLIST symbol
    id      CDATA #REQUIRED
>

<!ELEMENT chunk (%chunk.content;)*> 
<!ATTLIST chunk
    %font.attributes;
    subsupscript        CDATA   #IMPLIED
    localgoto           CDATA   #IMPLIED
    localdestination    CDATA   #IMPLIED
    generictag          CDATA   #IMPLIED
>

<!ELEMENT phrase (%chunk.content; | %phrase.content;)*> 
<!ATTLIST phrase
    %font.attributes;
    %phrase.attributes;
>

<!ELEMENT anchor (%chunk.content; | %phrase.content;)*> 
<!ATTLIST anchor
%font.attributes;
    %phrase.attributes;
    name       CDATA   #IMPLIED
    reference  CDATA   #IMPLIED
>

<!ELEMENT paragraph (%chunk.content; | %phrase.content; | image)*> 
<!ATTLIST paragraph
    %font.attributes;
    %phrase.attributes;
    %indentation.attributes;
    %paragraph.attributes;
>

<!ELEMENT list (listitem | ignore)*> 
<!ATTLIST list
    %font.attributes;
    %indentation.attributes;
    numbered       CDATA   #IMPLIED
    symbolindent   CDATA   #IMPLIED
    first          CDATA   #IMPLIED
    listsymbol     CDATA   #IMPLIED
>

<!ELEMENT listitem (%chunk.content; | %phrase.content; | image)*> 
<!ATTLIST listitem
    %font.attributes;
    %phrase.attributes;
    %indentation.attributes;
    %paragraph.attributes;
>

<!ELEMENT chapter (title?, sectioncontent) >
<!ATTLIST chapter
    %section.attributes;
    %indentation.attributes;
>

<!ELEMENT section (title?, sectioncontent) >
<!ATTLIST section
    %section.attributes;
    %indentation.attributes;
>

<!ELEMENT title (%chunk.content; | phrase | chunk | annotation)*> 
<!ATTLIST title
    %font.attributes;
    %phrase.attributes;
    %indentation.attributes;
    %paragraph.attributes;
>

<!ELEMENT sectioncontent (%chunk.content; | %phrase.content; | section | paragraph | image)*>

<!ELEMENT table (cell*)>
<!ATTLIST table
    %color.attributes;
    %paragraph.attributes;
    %rectangle.attributes;
    columns        CDATA   #IMPLIED
    lastHeaderRow  CDATA   #IMPLIED
    cellpadding    CDATA   #IMPLIED
    cellspacing    CDATA   #IMPLIED
    widths         CDATA   #IMPLIED
>

<!ELEMENT row (cell*)>
<!ELEMENT cell (%chunk.content; | %phrase.content; | paragraph | image)*>
<!ATTLIST cell
    %color.attributes;
    %phrase.attributes;
    %indentation.attributes;
    %rectangle.attributes;
    colspan    CDATA   #IMPLIED
    rowspan    CDATA   #IMPLIED
    header     CDATA   #IMPLIED
    nowrap     CDATA   #IMPLIED
>

<!ELEMENT image EMPTY>
<!ATTLIST image
    url             CDATA   #REQUIRED
    align           CDATA   #IMPLIED
    underlying      CDATA   #IMPLIED
    textwrap        CDATA   #IMPLIED
    alt             CDATA   #IMPLIED
    absolutex       CDATA   #IMPLIED
    absolutey       CDATA   #IMPLIED
    plainwidth      CDATA   #IMPLIED
    plainheight     CDATA   #IMPLIED
    rotation        CDATA   #IMPLIED
>

<!ELEMENT annotation EMPTY>
<!ATTLIST annotation
    title       CDATA   #IMPLIED
    content     CDATA   #IMPLIED
>

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