Itext 创建带有页眉和页脚的多页PDF

Itext 创建带有页眉和页脚的多页PDF,itext,Itext,大家好,提前谢谢你们的帮助。我需要开发一个应用程序,用iTextSharp生成多页PDF。每个页面都需要有页眉和页脚。此外,每个页面将有各种不同的对象,包括表、段落和类似于列表项的内容(可能是锥形列表)。我正试图找到一个教程来实现这一点。我困惑的核心是如何确定何时应该中断,以及如何在表格或段落太长而无法放在页面上时不覆盖页脚。任何帮助都将不胜感激 我很欣赏Bruno的反应,对HTML页面的样式设计很感兴趣,然后用iText进行渲染,但我真的很难在真正的页面上做到这一点。以下是我要打印的页面的一些

大家好,提前谢谢你们的帮助。我需要开发一个应用程序,用iTextSharp生成多页PDF。每个页面都需要有页眉和页脚。此外,每个页面将有各种不同的对象,包括表、段落和类似于列表项的内容(可能是锥形列表)。我正试图找到一个教程来实现这一点。我困惑的核心是如何确定何时应该中断,以及如何在表格或段落太长而无法放在页面上时不覆盖页脚。任何帮助都将不胜感激

我很欣赏Bruno的反应,对HTML页面的样式设计很感兴趣,然后用iText进行渲染,但我真的很难在真正的页面上做到这一点。以下是我要打印的页面的一些示例HTML:

<body>
<header class="onlyprint">
    <ul>
        <li>
            Dr. Who
        </li>
        <li>
            1234 Somewhere St.
        </li>
        <li>
            Anywhere, US  12345
        </li>
    </ul>
</header>
<div id="divmain" style="width:  760px;">

    <ul id="ulPatientDetails">
        <li>
            <label>Patient Name:</label>
            <span>TESTCASE, TOM</span>
        </li>
        <li>
            <label>MRNumber:</label>
            <span>999999</span>
        </li>
        <li>
            <label>Patient DOB:</label>
            <span>99/99/9999</span>
        </li>
    </ul>
    <table id="tblhistorian">
        <caption>Historian</caption>
        <tr>
            <th>Time Entered</th>
            <th>Historian</th>
            <th>Historian Name</th>
            <th>Other Persons</th>
            <th>Language</th>
            <th>Other Language</th>
            <th>Translator</th>
            <th>Entered By</th>
        </tr>
        <tr>
            <td>
                99/99/9999
            </td>
            <td>
                Mother
            </td>
            <td></td>
            <td></td>
            <td>
                English
            </td>
            <td></td>
            <td></td>
            <td>
                Dr. Who
            </td>
        </tr>
    </table>



    <ul id="ulPatientAllergies">
        <li>
            <label>Allergies:</label>
            <span>BLAH BLAH BLAH BLAH</span>
        </li>
    </ul>
    <table id="tblTobaccoUse">
        <caption>Tobacco Use</caption>
        <tr>
            <th>Patient Uses Tobacco</th>
            <th>Family Uses Tobacco</th>
            <th>Entered By</th>
            <th>Time Entered</th>
        </tr>
        <tr>
            <td>N/A</td>
            <td>No</td>
            <td>Someone</td>
            <td>99/99/9999</td>
        </tr>
    </table>

    <table id="outsidemedications">
        <caption>Active Prescriptions</caption>
        <tr>
            <th>Date</th>
            <th>Prescribed By</th>
            <th>Prescription</th>
            <th>SIG</th>
            <th>Route</th>
            <th>Dispense</th>
            <th>Refills</th>
        </tr>
        <tr>
            <td>
                99/99/9999
            </td>
            <td>
                DR. WHO
            </td>
            <td>
                BLAH BLAH BLAH BLAH
            </td>
            <td>
                BLAH BLAH BLAH BLAH
            </td>
            <td>

            </td>
            <td>

            </td>
            <td>
                0
            </td>
        </tr>
        <tr>
            <td>
                99/99/9999
            </td>
            <td>
                DR. WHO
            </td>
            <td>
                BLAH BLAH BLAH BLAH
            </td>
            <td>
                BLAH BLAH BLAH BLAH
            </td>
            <td>
                BLAH BLAH BLAH BLAH
            </td>
            <td>

            </td>
            <td>

            </td>
        </tr>
    </table>




    <table id="outsidemedications">
        <caption>Other Medications</caption>
        <tr>
            <th>Medication</th>
            <th>SIG</th>
            <th>Source</th>
            <th>Category</th>
            <th>Comments</th>
            <th>Entered By</th>
            <th>Date Entered</th>
        </tr>
        <tr>
            <td colspan="7">No current medications.</td>
        </tr>
    </table>




    <table id="tblChiefComplaints">
        <caption>Chief Complaints</caption>
        <tr>
            <th>Category</th>
            <th>Description</th>
            <th>Duration</th>
        </tr>
        <tr>
            <td>General</td>
            <td>BLAH BLAH BLAH BLAH</td>
            <td>BLAH BLAH BLAH BLAH</td>
        </tr>
        <tr>
            <td>General</td>
            <td>BLAH BLAH BLAH BLAH</td>
            <td>BLAH BLAH BLAH BLAH</td>
        </tr>
    </table>

    <table id="tblReviewOfSystems">
        <caption>Review of Systems</caption>
        <tr>
            <th>Category</th>
            <th>Symptom</th>
            <th>Result</th>
        </tr>
        <tr>
            <td>General</td>
            <td>BLAH BLAH BLAH BLAH</td>
            <td>None</td>
        </tr>
        <tr>
            <td>Skin</td>
            <td>BLAH BLAH BLAH BLAH</td>
            <td>None</td>
        </tr>
        <tr>
            <td>Eyes</td>
            <td>BLAH BLAH BLAH BLAH</td>
            <td>None</td>
        </tr>
        <tr>
            <td>Ears</td>
            <td>BLAH BLAH BLAH BLAH</td>
            <td>None</td>
        </tr>
        <tr>
            <td>Nose</td>
            <td>BLAH BLAH BLAH BLAH</td>
            <td>None</td>
        </tr>
        <tr>
            <td>Mouth or Teeth</td>
            <td>BLAH BLAH BLAH BLAH</td>
            <td>None</td>
        </tr>
        <tr>
            <td>Face or Neck</td>
            <td>BLAH BLAH BLAH BLAH</td>
            <td>None</td>
        </tr>
        <tr>
            <td>Respiratory</td>
            <td>BLAH BLAH BLAH BLAH</td>
            <td>None</td>
        </tr>
        <tr>
            <td>Cardio</td>
            <td>BLAH BLAH BLAH BLAH</td>
            <td>None</td>
        </tr>
        <tr>
            <td>Abdomen</td>
            <td>BLAH BLAH BLAH BLAH </td>
            <td>None</td>
        </tr>
        <tr>
            <td>MSLymph</td>
            <td>BLAH BLAH BLAH BLAH</td>
            <td>None</td>
        </tr>
        <tr>
            <td>Neuro</td>
            <td>BLAH BLAH BLAH BLAH</td>
            <td>None</td>
        </tr>
    </table>

    <table id="tblPesentIllnesses">
        <caption>History of Present Illnesses</caption>
        <tr>
            <th>Complaint</th>
            <th>Entered By</th>
            <th>Time Entered</th>
        </tr>
        <tr>
            <td>BLAH BLAH BLAH BLAH</td>
            <td>Dr. Who</td>
            <td>99/99/9999</td>
        </tr>
        <tr>
            <td>BLAH BLAH BLAH BLAH</td>
            <td>Dr. Who</td>
            <td>99/99/9999</td>
        </tr>
        <tr>
            <td>BLAH BLAH BLAH BLAH</td>
            <td>Dr. Who</td>
            <td>99/99/9999</td>
        </tr>
    </table>

    <table id="tblFamilyIllnesses">
        <caption>Family History</caption>
        <tr>
            <th>Relationship</th>
            <th>Name</th>
            <th>DOB</th>
            <th>Age</th>
            <th>Illness</th>
        </tr>
    </table>

    <table id="tblWellChildHistory">
        <caption>Well Child History</caption>
        <tr>
            <th>Dietary History</th>
            <th>Physical Activity</th>
            <th>Sleeping Habits</th>
            <th>Bowel Habits</th>
            <th>Dental Visit In Past Year</th>
            <th>Exposure To Illnesses</th>
            <th>Alcohol Use</th>
            <th>Drug Use</th>
            <th>Travel History</th>
            <th>Sexual Activity</th>
            <th>Entered By</th>
            <th>Time Entered</th>
        </tr>
        <tr>
            <td colspan="3">No past medical history.</td>
        </tr>
    </table>

    <table id="tblSocialHistory">
        <caption>Social History</caption>
        <tr>
            <th>Primary Caregiver</th>
            <th>Home Status</th>
            <th>Child Attends</th>
            <th>School Name</th>
            <th>Grade</th>
            <th>School Performance</th>
            <th>Entered By</th>
            <th>Time Entered</th>
        </tr>
        <tr>
            <td colspan="12">No social history entered for this visit.</td>
        </tr>
    </table>


    <table id="tblDevelopmentalMilestones">
        <caption>Developmental Milestones</caption>
        <tr>
            <th>Category</th>
            <th>Question</th>
            <th>Answer</th>
            <th>Entered By</th>
            <th>TimeEntered</th>
        </tr>
        <tr>
            <td colspan="3">No developmental assessment entered.</td>
        </tr>
    </table>

    <table id="tblDevelopmentalComments">
        <caption>Developmental Comments</caption>
        <tr>
            <th>Comment</th>
            <th>Entered By</th>
            <th>TimeEntered</th>
        </tr>
        <tr>
            <td colspan="3">No developmental comments entered.</td>
        </tr>
    </table>

    <table id="tblHouseholdRiskFactorResponses">
        <caption>Household Risk Factors</caption>
        <tr>
            <th>Question</th>
            <th>Response</th>
            <th>Comment</th>
        </tr>
        <tr>
            <td colspan="3">No household risk factors entered for this visit.</td>
        </tr>
    </table>
    <table id="tblFamilyBarriersResponses">
        <caption>Family Barriers</caption>
        <tr>
            <th>Question</th>
            <th>Response</th>
            <th>Comment</th>
        </tr>
        <tr>
            <td colspan="3">No household risk factors entered for this visit.</td>
        </tr>
    </table>

    <table id="tblPHQAAssessments">
        <caption>PHQ-A Assessment</caption>
        <tr>
            <th>Question</th>
            <th>Response</th>
            <th>Score</th>
        </tr>
        <tr>
            <td colspan="3">No current assessments.</td>
        </tr>
    </table>


    <table id="tblPatientVitals">
        <caption>Vitals</caption>
        <tr>
            <th>Time Entered</th>
            <th>Temp.</th>
            <th>H.R.</th>
            <th>R.R.</th>
            <th>Weight</th>
            <th>Height</th>
            <th>BMI</th>
            <th>Head Circ.</th>
            <th>O2 Sat.</th>
            <th>BP</th>
            <th>Comments</th>
            <th>Entered By</th>
        </tr>
        <tr>
            <td>11/3/2017 2:51:00 PM</td>
            <td>98.6</td>
            <td>90</td>
            <td>25</td>
            <td>58.0000</td>
            <td>0.0000</td>
            <td></td>
            <td>0.0000</td>
            <td>0.0000</td>
            <td>0/0&nbsp;Sitting&nbsp;Sitting</td>
            <td></td>
            <td>Dr. Who</td>
        </tr>
    </table>


    <ul id="ulexamdetails">
        <li id="liphysicalexamheader">Physical Exam</li>
        <li><span class="categorylabel">General</span></li>
        <li><span class="spnormal">BLAH BLAH BLAH BLAH</span><span class="spexception">&nbsp;Except:&nbsp;BLAH BLAH BLAH BLAH</span></li>
        <li><span class="categorylabel">Skin</span></li>
        <li><span class="spnormal">BLAH BLAH BLAH BLAH</span></li>
        <li><span class="categorylabel">Head</span></li>
        <li><span class="spnormal">BLAH BLAH BLAH BLAH</span></li>
        <li><span class="categorylabel">Eyes</span></li>
        <li><span class="spnormal">BLAH BLAH BLAH BLAH</span></li>
        <li><span class="categorylabel">Ears</span></li>
        <li><span class="spnormal">BLAH BLAH BLAH BLAH</span></li>
        <li><span class="categorylabel">Nose</span></li>
        <li><span class="spnormal">BLAH BLAH BLAH BLAH</span></li>
        <li><span class="categorylabel">Mouth</span></li>
        <li><span class="spnormal">BLAH BLAH BLAH BLAH</span><span class="spexception">&nbsp;Except:&nbsp;BLAH BLAH BLAH BLAH</span></li>
        <li><span class="categorylabel">Face Or Neck</span></li>
        <li><span class="spnormal">BLAH BLAH BLAH BLAH</span></li>
        <li><span class="categorylabel">Respiratory</span></li>
        <li><span class="spnormal"></span></li>
        <li><span class="categorylabel">Cardio</span></li>
        <li>
            <span class="spnormal">BLAH BLAH BLAH BLAH
            </span>
        </li>
        <li><span class="categorylabel">Abdomen</span></li>
        <li>
            <span class="spnormal">BLAH BLAH BLAH BLAH
            </span>
        </li>
        <li><span class="categorylabel">MS/Lymph</span></li>
        <li><span class="spnormal">BLAH BLAH BLAH BLAH</span></li>
        <li><span class="categorylabel">Neuro</span></li>
        <li><span class="spnormal">BLAH BLAH BLAH BLAH</span></li>

    </ul>

    <table id="tblVisionResults">
        <caption>Vision Results</caption>
        <tr>
            <th>Left Eye</th>
            <th>Right Eye</th>
            <th>Entered By</th>
            <th>Time Entered</th>
        </tr>
        <tr>
            <td colspan="4">No vision results this visit.</td>
        </tr>
    </table>
    <table id="tblHearingTestResults">
        <caption>Hearing Results</caption>
        <tr>
            <th>Frequency</th>
            <th>Db Right</th>
            <th>Db Left</th>
            <!--
            <th>Entered By</th>
            <th>Time Entered</th>
                -->
        </tr>
        <tr>
            <td colspan="4">No hearing results this visit.</td>
        </tr>
    </table>
    <table id="tblInhouseLabs">
        <caption>In-house Labs</caption>
        <tr>
            <th>Lab</th>
            <th>Lab Value</th>
            <th>Comment</th>
            <th>Entered By</th>
            <th>Time Entered</th>
        </tr>
        <tr>
            <td colspan="4">No in-house labs results this visit.</td>
        </tr>
    </table>
    <table id="tblInhouseUrineResults">
        <caption>In-house Urine Results</caption>
        <tr>
            <th>Leukocytes</th>
            <th>Nitrite</th>
            <th>Urobili</th>
            <th>Protein</th>
            <th>PH</th>
            <th>Blood</th>
            <th>SG</th>
            <th>Ketone</th>
            <th>Bilirubin</th>
            <th>Glucose</th>
            <th>Entered By</th>
            <th>Time Entered</th>
        </tr>
        <tr>
            <td colspan="12">No urine results this visit.</td>
        </tr>
    </table>


    <table id="tblInOfficeCourse">
        <caption>Assessment</caption>
        <tr>
            <th>In-Office Course</th>
            <th>Entered By</th>
            <th>Time Entered</th>
        </tr>
        <tr>
            <td colspan="3">No in-office course.</td>
        </tr>
    </table>
    <fieldset id="fldassessment">
        <legend>Assessment</legend>
        <table id="tbldiagnosiscodes" name="tbldiagnosiscodes"></table>
        <table id="tblPatientAssessment">
            <caption>Assessment</caption>
            <tr>
                <th>Assessment</th>
                <th>Entered By</th>
                <th>Time Entered</th>
            </tr>
            <tr>
                <td>BLAH BLAH BLAH BLAH</td>
                <td>Dr. Who</td>
                <td>99/99/9999</td>

            </tr>
        </table>

        <table id="tblComplianced">
            <caption>Compliance</caption>
            <tr>
                <th>Compliance</th>
                <th>Compliance Code</th>
                <th>Entered By</th>
                <th>Time Entered By</th>
            </tr>
            <tr>
                <td colspan="4">No Compliance Entered This Visit.</td>
            </tr>
        </table>
    </fieldset>
    <fieldset id="fldplanofcare">
        <legend>Plan of Care</legend>
        <table id="tblvisitprescriptions" name="tblvisitprescriptions"></table>
        <table id="tblDiagnosisCodes">
            <caption>Diagnosis Codes</caption>
            <tr>
                <th>Code</th>
                <th>Description</th>
            </tr>
            <tr>
                <td colspan="2">No Diagnosis Codes Entered</td>
            </tr>
        </table>

        <table id="tblCPTCodes">
            <caption>CPT Codes</caption>
            <tr>
                <th>Code</th>
                <th>Description</th>
            </tr>
            <tr>
                <td>
                    99213
                </td>
                <td>
                    ESTAB. EXPANDED
                </td>
            </tr>
        </table>

        <table id="tblPrescriptionsThisVisit">
            <caption>Prescriptions This Visit</caption>
            <tr>
                <th>Date</th>
                <th>Prescribed By</th>
                <th>Prescription</th>
                <th>SIG</th>
                <th>Route</th>
                <th>Dispense</th>
                <th>Refills</th>
            </tr>
            <tr>
                <td>
                    99/99/9999
                </td>
                <td>
                    Dr. Who
                </td>
                <td>
                    BLAH BLAH BLAH BLAH
                </td>
                <td>
                    BLAH BLAH BLAH BLAH
                </td>
                <td>

                </td>
                <td>

                </td>
                <td>
                    0
                </td>
            </tr>
        </table>





        <table id="tblLabsOrdered">
            <caption>Labs Ordered This Visit</caption>
            <tr>
                <th>Date</th>
                <th>Test Ordered</th>
                <th>Ordered By</th>
                <th>Order Urgency</th>
            </tr>
            <tr>
                <td colspan="4">No Labs Ordered This Visit.</td>
            </tr>
        </table>


        <table id="outsidemedications">
            <caption>Radiology Orders This Visit</caption>
            <tr>
                <th>Date</th>
                <th>Test Ordered</th>
                <th>Ordered By</th>
                <th>Order Urgency</th>
            </tr>
            <tr>
                <td colspan="4">No Radiology Ordered This Visit.</td>
            </tr>
        </table>


        <table id="tblcptcodes" name="tblcptcodes"></table>
        <table id="tblReferrals">
            <caption>Referrals This Visit</caption>
            <tr>
                <th>Speciality</th>
                <th>Speciality Group</th>
                <th>Specialist</th>
            </tr>
            <tr>
                <td colspan="4">No Referrals This Visit.</td>
            </tr>
        </table>


        <div id="divplanofcare">
            <ul id="ulcareplanlist">
                <li>BLAH BLAH BLAH BLAH</li>
            </ul>
        </div>

    </fieldset>
</div>
<!--End of body-->
<footer class="onlyprint">
    <ul>
        <li>Tom Testcase</li>
        <li>99/99/9999</li>
    </ul>
</footer>
</body>

我不确定如何实现CSS以确保页面内容不会覆盖页脚。此外,由于页面上的许多元素都是可能会扩展的表,您是否需要预先了解这些元素中有多少数据,以及在何处使用分页符?有人能帮我开始吗?谢谢。

使用CSS设置文档的页眉、页脚和分页符的示例:

页面设置

标题

页脚

分页符

在元素上使用前分页符、后分页符和内部分页符来设置适当的分页符

补充阅读

布鲁诺·洛瓦吉更新

上述答案是正确的,但不完整。它解释了如何在HTML中定义页眉和页脚,但没有解释如何使用这些页眉和页脚创建PDF。幸运的是,这只需要一行代码

看。在中,有一个部分名为使用@Page规则添加第X页(共Y页),该部分展示了@Page规则如何与iText 7的pdfHTML加载项结合使用

您只需编写以下代码行:

HtmlConverter.ConvertToPdf(src, dest);

其中src是HTML文件的源文件,dest是PDF文件的目标文件。

首先:我们不再谈论iTextSharp。iTextSharp已重命名为.NET的iText。当前版本是iText 7

添加页眉和页脚的说明如下:

首先,您需要创建IEventHandler接口的实现,例如:

protected internal class MyEventHandler : IEventHandler {
    public virtual void HandleEvent(Event @event) {
        PdfDocumentEvent docEvent = (PdfDocumentEvent)@event;
        PdfDocument pdfDoc = docEvent.GetDocument();
        PdfPage page = docEvent.GetPage();
        int pageNumber = pdfDoc.GetPageNumber(page);
        Rectangle pageSize = page.GetPageSize();
        PdfCanvas pdfCanvas = new PdfCanvas(page.NewContentStreamBefore(), page.GetResources(), pdfDoc);
        //Set background
        Color limeColor = new DeviceCmyk(0.208f, 0, 0.584f, 0);
        Color blueColor = new DeviceCmyk(0.445f, 0.0546f, 0, 0.0667f);
        pdfCanvas.SaveState()
                    .SetFillColor(pageNumber % 2 == 1 ? limeColor : blueColor)
                    .Rectangle(pageSize.GetLeft(), pageSize.GetBottom(), pageSize.GetWidth(), pageSize.GetHeight())
                    .Fill()
                    .RestoreState();
        //Add header and footer
        pdfCanvas.BeginText()
                    .SetFontAndSize(C03E03_UFO.helvetica, 9)
                    .MoveText(pageSize.GetWidth() / 2 - 60, pageSize.GetTop() - 20)
                    .ShowText("THE TRUTH IS OUT THERE")
                    .MoveText(60, -pageSize.GetTop() + 30)
                    .ShowText(pageNumber.ToString())
                    .EndText();
        //Add watermark
        iText.Layout.Canvas canvas = new iText.Layout.Canvas(pdfCanvas, pdfDoc, page.GetPageSize());
        canvas.SetProperty(Property.FONT_COLOR, Color.WHITE);
        canvas.SetProperty(Property.FONT_SIZE, 60);
        canvas.SetProperty(Property.FONT, C03E03_UFO.helveticaBold);
        canvas.ShowTextAligned(new Paragraph("CONFIDENTIAL"), 298, 421, pdfDoc.GetPageNumber(page), TextAlignment.
            CENTER, VerticalAlignment.MIDDLE, 45);
        pdfCanvas.Release();
    }
}
在上面的事件处理程序中,我们设置背景,添加页眉和页脚,添加水印。这超出了你的需要,但请随意适应

拥有此实现后,可以将其声明为特定PdfDocument实例的事件处理程序:

PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
pdf.AddEventHandler(PdfDocumentEvent.END_PAGE, new MyEventHandler(this));

有关完整示例,请参见。

查看分页媒体,它将帮助您处理分页符、页眉和页脚:您是否查看了itextpdf.com网站上的示例?这是一个完整的教程,我看到您在更新答案的同时删除了答案。我很高兴看到你取消了它,因为这确实是一个很好的简单的方法来创建PDF的页眉和页脚几乎没有C代码需要,只是HTML和CSS。请原谅我的无知。你是在建议我最初用HTML设置页面,然后将HTML转换为PDF吗?@BrunoLowagie是的,我认为我走错了方向,打算重新编写我的答案,尽管你的编辑再次确认了这一点-谢谢@kmcnet yep-本质上,使用HTML作为内容,CSS作为页面样式,iTextSharp转换为PDF。下面是一个很好的教程,可以帮助您理解所需的CSS:@kmcnet您有两个答案和两个有效选项。这取决于你决定什么是你的项目的最佳选择。它可以是Matt's,您可以在那里创建HTML并转换为PDF;它可以是我的,你可以在那里编写你的PDF文件,并对页眉和页脚使用事件处理程序。我们不能为你做那个决定,因为我们不知道你的项目。布鲁诺。谢谢你的帮助和有趣的信息。您的许多教程似乎都与格式良好、样式新颖的HTML页面齐头并进。我试图模仿你在用CSS定义样式中描述的页面规则,我成功地获得了显示在每个页面上的页眉和页脚,但是每个页面的主体都覆盖了页眉和页脚。我编辑了第一篇文章以显示当前的HTML。我不确定我错在哪里。显然,我不能继续转换为PDF。想象一下,一台打印机可以打印在信纸上。文档和打印机都不知道该纸张上已打印的内容,文档内容可能与纸张上预打印的内容重叠。为了避免这种情况,您必须以不发生重叠的方式定义文档的页边距。Bruno。我很欣赏并理解这个类比,但在实践中似乎无法做到这一点。我已经在@page上使用了边距,但似乎没有任何效果。有什么见解吗?
HtmlConverter.ConvertToPdf(src, dest);
protected internal class MyEventHandler : IEventHandler {
    public virtual void HandleEvent(Event @event) {
        PdfDocumentEvent docEvent = (PdfDocumentEvent)@event;
        PdfDocument pdfDoc = docEvent.GetDocument();
        PdfPage page = docEvent.GetPage();
        int pageNumber = pdfDoc.GetPageNumber(page);
        Rectangle pageSize = page.GetPageSize();
        PdfCanvas pdfCanvas = new PdfCanvas(page.NewContentStreamBefore(), page.GetResources(), pdfDoc);
        //Set background
        Color limeColor = new DeviceCmyk(0.208f, 0, 0.584f, 0);
        Color blueColor = new DeviceCmyk(0.445f, 0.0546f, 0, 0.0667f);
        pdfCanvas.SaveState()
                    .SetFillColor(pageNumber % 2 == 1 ? limeColor : blueColor)
                    .Rectangle(pageSize.GetLeft(), pageSize.GetBottom(), pageSize.GetWidth(), pageSize.GetHeight())
                    .Fill()
                    .RestoreState();
        //Add header and footer
        pdfCanvas.BeginText()
                    .SetFontAndSize(C03E03_UFO.helvetica, 9)
                    .MoveText(pageSize.GetWidth() / 2 - 60, pageSize.GetTop() - 20)
                    .ShowText("THE TRUTH IS OUT THERE")
                    .MoveText(60, -pageSize.GetTop() + 30)
                    .ShowText(pageNumber.ToString())
                    .EndText();
        //Add watermark
        iText.Layout.Canvas canvas = new iText.Layout.Canvas(pdfCanvas, pdfDoc, page.GetPageSize());
        canvas.SetProperty(Property.FONT_COLOR, Color.WHITE);
        canvas.SetProperty(Property.FONT_SIZE, 60);
        canvas.SetProperty(Property.FONT, C03E03_UFO.helveticaBold);
        canvas.ShowTextAligned(new Paragraph("CONFIDENTIAL"), 298, 421, pdfDoc.GetPageNumber(page), TextAlignment.
            CENTER, VerticalAlignment.MIDDLE, 45);
        pdfCanvas.Release();
    }
}
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
pdf.AddEventHandler(PdfDocumentEvent.END_PAGE, new MyEventHandler(this));