C# 如何使用iTextSharp 5.0.4编写页脚
我试图使用ITextSharp library 5.0.4编写页脚,但无法打印页码,因为OnEndPage和OnClosePage没有被调用 这是一个简单的控制台应用程序,我在其中打印表格以生成两个页面,并希望调用OnEndPage或OnClosePage,以便在执行document.close()方法后执行我的页脚代码C# 如何使用iTextSharp 5.0.4编写页脚,c#,itextsharp,C#,Itextsharp,我试图使用ITextSharp library 5.0.4编写页脚,但无法打印页码,因为OnEndPage和OnClosePage没有被调用 这是一个简单的控制台应用程序,我在其中打印表格以生成两个页面,并希望调用OnEndPage或OnClosePage,以便在执行document.close()方法后执行我的页脚代码 请告诉我这有什么问题?您需要创建一个类,该类扩展了PdfPageEventHelper,并覆盖了启动页和onedpage。使用此帮助器类处理PdfWriter中的PageEve
请告诉我这有什么问题?您需要创建一个类,该类扩展了
PdfPageEventHelper
,并覆盖了启动页和onedpage
。使用此帮助器类处理PdfWriter
中的PageEvent
马苏德·马扎尔的博客文章有一个很好的例子
如果您正在执行此操作,但仍然存在问题,请发布一些代码。您需要创建一个类,该类扩展了
PdfPageEventHelper
,并覆盖启动页和onedpage
。使用此帮助器类处理PdfWriter
中的PageEvent
马苏德·马扎尔的博客文章有一个很好的例子
如果您正在这样做,但仍然有问题,请发布一些代码。我发现了如何创建页脚,并带有页码和图像,而不会出现任何复杂情况。我正在使用itextsharp 5.1.2。这很容易。 步骤1:在APP_代码中创建一个名为。。。在我的例子“pdfPage.cs” 步骤2:将此代码复制并粘贴到:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.IO;
using System.Xml;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
using System.Collections;
using System.Net;
namespace myApp.ns.pages
{
public class pdfPage : iTextSharp.text.pdf.PdfPageEventHelper
{
public override void OnEndPage(PdfWriter writer, Document doc)
{
BaseColor grey = new BaseColor(128, 128, 128);
Font font = FontFactory.GetFont("Arial", 9, Font.NORMAL, grey);
//tbl footer
PdfPTable footerTbl = new PdfPTable(1);
footerTbl.TotalWidth = doc.PageSize.Width;
//img footer
iTextSharp.text.Image foot= iTextSharp.text.Image.GetInstance(HttpContext.Current.Server.MapPath("footer.jpg"));
foot.ScalePercent(45);
footerTbl.HorizontalAlignment = Element.ALIGN_CENTER;
PdfPCell cell = new PdfPCell(foot);
cell.HorizontalAlignment = Element.ALIGN_CENTER ;
cell.Border = 0;
footerTbl.AddCell(cell);
//page number
Chunk myFooter = new Chunk("Page " + (doc.PageNumber), FontFactory.GetFont(FontFactory.HELVETICA_OBLIQUE, 8, grey));
PdfPCell footer = new PdfPCell(new Phrase(myFooter));
footer.Border = Rectangle.NO_BORDER;
footer.HorizontalAlignment = Element.ALIGN_CENTER;
footerTbl.AddCell(footer);
//this is for the position of the footer ... im my case is "+80"
footerTbl.WriteSelectedRows(0,-1, 0, (doc.BottomMargin + 80), writer.DirectContent);
}
}
}
并保存它
最后一步:在开始时写入创建pdf的.cs文件“usingmyapp.ns.pages;”
就这些好吧,我找到了如何创建页脚,页数和图像都不复杂。我正在使用itextsharp 5.1.2。这很容易。 步骤1:在APP_代码中创建一个名为。。。在我的例子“pdfPage.cs” 步骤2:将此代码复制并粘贴到:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.IO;
using System.Xml;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
using System.Collections;
using System.Net;
namespace myApp.ns.pages
{
public class pdfPage : iTextSharp.text.pdf.PdfPageEventHelper
{
public override void OnEndPage(PdfWriter writer, Document doc)
{
BaseColor grey = new BaseColor(128, 128, 128);
Font font = FontFactory.GetFont("Arial", 9, Font.NORMAL, grey);
//tbl footer
PdfPTable footerTbl = new PdfPTable(1);
footerTbl.TotalWidth = doc.PageSize.Width;
//img footer
iTextSharp.text.Image foot= iTextSharp.text.Image.GetInstance(HttpContext.Current.Server.MapPath("footer.jpg"));
foot.ScalePercent(45);
footerTbl.HorizontalAlignment = Element.ALIGN_CENTER;
PdfPCell cell = new PdfPCell(foot);
cell.HorizontalAlignment = Element.ALIGN_CENTER ;
cell.Border = 0;
footerTbl.AddCell(cell);
//page number
Chunk myFooter = new Chunk("Page " + (doc.PageNumber), FontFactory.GetFont(FontFactory.HELVETICA_OBLIQUE, 8, grey));
PdfPCell footer = new PdfPCell(new Phrase(myFooter));
footer.Border = Rectangle.NO_BORDER;
footer.HorizontalAlignment = Element.ALIGN_CENTER;
footerTbl.AddCell(footer);
//this is for the position of the footer ... im my case is "+80"
footerTbl.WriteSelectedRows(0,-1, 0, (doc.BottomMargin + 80), writer.DirectContent);
}
}
}
并保存它
最后一步:在开始时写入创建pdf的.cs文件“usingmyapp.ns.pages;”
就这些我们能看看你的代码吗?我们能看看你的代码吗?谢谢你的帖子。我已经参考了那个博客,唯一剩下的就是分配writer.pageeventswriter.PageEvent=PageEventHandler;谢谢你的帖子。我已经参考了那个博客,唯一剩下的就是分配writer.pageeventswriter.PageEvent=PageEventHandler;