如何从html生成带有页眉和页脚的pdf

时间:2014-01-02 09:40:15

标签: c# html xslt pdf xsl-fo

尝试为HTML中的每个页面生成pdf以及页眉和页脚,但最后在最后一页的第一页和页脚开始时得到了标题。

如何为所有页面添加, 找到我使用的以下c#代码,

string outputHTML;// here it contains HTML as string and im passing this
IPechkin sc = Factory.Create(new GlobalConfig());
byte[] buf = sc.Convert(new ObjectConfig(), outputHTML);
try
{
  string fn;
  fn = @"D:\NewFolder\Test.pdf";
  FileStream fs = new FileStream(fn, FileMode.Create);
  fs.Write(buf, 0, buf.Length);
  fs.Close();
}
catch
{
  //
}
MessageBox.Show("PDF Generated.");

这里我使用“IPechkin”DLL来创建pdf。

请找到XSL-FO文件,这个文件工作正常,并且可以获得在线pdf生成中所有页面的页眉和页脚,但在C#应用程序中执行时无法获取每个页面。

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <xsl:template match='/'>
        <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
            <fo:layout-master-set>
                <fo:simple-page-master master-name="my-page"
                                   page-height="29.7cm"
                      page-width="21cm"
                      margin-top="1cm"
                      margin-bottom="0.1cm"
                      margin-left="0.8cm"
                      margin-right="1.0cm" >
                    <fo:region-body region-name="xsl-region-body" margin-top="2.5cm" margin-bottom="2.5cm"/>
                    <fo:region-before region-name="xsl-region-before" extent="2.0cm"/>
                    <fo:region-after region-name="xsl-region-after" extent="2.0cm"/>
                </fo:simple-page-master>
            </fo:layout-master-set>
            <fo:page-sequence master-reference="my-page">
                <fo:static-content flow-name="xsl-region-before">
                    <fo:block>
                        Page header
                    </fo:block>
                </fo:static-content>
                <fo:static-content flow-name="xsl-region-after">
                    <fo:block>footer</fo:block>
                </fo:static-content>
                <fo:flow flow-name="xsl-region-body">
                    <fo:block break-after="xsl-region-before">
                        Body
                    </fo:block>
                    <table align="right" border="1" bordercolor="#ccc" cellpadding="5" cellspacing="0" style="border-collapse:collapse">
                        <caption>
                            <strong>Mission crew</strong>
                        </caption>
                        <thead>
                            <tr>
                                <th scope="col">Position</th>
                                <th scope="col">Astronaut</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td>First Name</td>
                                <td>
                                    <xsl:value-of disable-output-escaping="yes" select="string(firstName)" />
                                </td>
                            </tr>
                            <tr>
                                <td>Treatment ID</td>
                                <td>
                                    <xsl:value-of disable-output-escaping="yes" select="string(treatmentID)" />
                                </td>
                            </tr>
                            <tr>
                                <td>Key Name</td>
                                <td>
                                    <xsl:value-of disable-output-escaping="yes" select="string(keyName)" />
                                </td>
                            </tr>
                        </tbody>
                    </table>

                    <p>
                        Launched by a <strong>Saturn V</strong> rocket from <a href="http://en.wikipedia.org/wiki/Kennedy_Space_Center">Kennedy Space Center</a> in Merritt Island, Florida on July 16, Apollo 11 was the fifth manned mission of <a href="http://en.wikipedia.org/wiki/NASA">NASA</a>&#39;s Apollo program. The Apollo spacecraft had three parts:
                    </p>

                    <ol>
                        <li>
                            <strong>Command Module</strong> with a cabin for the three astronauts which was the only part which landed back on Earth
                        </li>
                        <li>
                            <strong>Service Module</strong> which supported the Command Module with propulsion, electrical power, oxygen and water
                        </li>
                        <li>
                            <strong>Lunar Module</strong> for landing on the Moon.
                        </li>
                    </ol>

                    <p>
                        After being sent to the Moon by the Saturn V&#39;s upper stage, the astronauts separated the spacecraft from it and travelled for three days until they entered into lunar orbit. Armstrong and Aldrin then moved into the Lunar Module and landed in the <a href="http://en.wikipedia.org/wiki/Mare_Tranquillitatis">Sea of Tranquility</a>. They stayed a total of about 21 and a half hours on the lunar surface. After lifting off in the upper part of the Lunar Module and rejoining Collins in the Command Module, they returned to Earth and landed in the <a href="http://en.wikipedia.org/wiki/Pacific_Ocean">Pacific Ocean</a> on July 24.
                    </p>

                    <hr />
                    <p>
                        <small>
                            Source: <a href="http://en.wikipedia.org/wiki/Apollo_11">Wikipedia.org</a>
                        </small>
                    </p>
                </fo:flow>
            </fo:page-sequence>
        </fo:root>
    </xsl:template>
    <xsl:template name="replace-returns">
        <xsl:param name="text"/>
        <xsl:choose>
            <xsl:when test="contains($text, '&#xa;')">
                <xsl:value-of select="substring-before($text, '&#xa;')"/>
                <xsl:value-of select="'&lt;br /&gt;'" disable-output-escaping="yes"/>
                <xsl:call-template name="replace-returns">
                    <xsl:with-param name="text" select="substring-after($text, '&#xa;')"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$text"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

但无法为每个页面获取页眉和页脚。

0 个答案:

没有答案
相关问题