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

时间:2011-10-18 12:39:03

标签: c# xml pdf itextsharp itext

我有兴趣使用iTextSharp在XML文档中创建表格。我买不起这本书,我在网上找不到任何例子。我还需要能够进行单元格扩展并避免在表格中间分页。我更愿意将表格发送到下一页。

这是我以后的例子:http://www.codeproject.com/Articles/66948/Rendering-PDF-views-in-ASP-MVC-using-iTextSharp.aspx

有谁知道如何完成这项工作或者可以指导我学习一些教程?

由于

1 个答案:

答案 0 :(得分:0)

如果您使用的是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>