我正在尝试使用 iTextSharp 制作 PDF 文档。
我不知道如何使用“currentPageNumber/documentsNumberOfPages”制作标题:
后续问题
目前,根据原始答案,我有这个:
public class StockComp : iTextSharp.text.pdf.PdfPageEventHelper
{
PdfTemplate templateNumPage;
public string Header
{
get;
set;
}
public override void OnOpenDocument(PdfWriter writer, Document document)
{
templateNumPage = writer.DirectContent.CreateTemplate(30, 250);
}
public override void OnEndPage(PdfWriter writer, Document document)
{
PdfPTable table = new PdfPTable(3);
try
{
table.SetWidths(new int[] { 10, 10, 2 });
table.TotalWidth = 800;
table.LockedWidth = true;
table.DefaultCell.FixedHeight = 20;
table.DefaultCell.Border = Rectangle.BOTTOM_BORDER;
table.AddCell(Header);
table.DefaultCell.HorizontalAlignment = Element.ALIGN_RIGHT;
table.AddCell(string.Format("Page {0} of", writer.PageNumber));
PdfPCell cell = new PdfPCell(Image.GetInstance(templateNumPage));
cell.Border = Rectangle.BOTTOM_BORDER;
table.AddCell(cell);
table.WriteSelectedRows(0, -1, 34, 803, writer.DirectContent);
}
catch (DocumentException de)
{
throw de;
}
}
public override void OnCloseDocument(PdfWriter writer, Document document)
{
ColumnText.ShowTextAligned(templateNumPage, Element.ALIGN_LEFT, new Phrase((writer.PageNumber - 1).ToString()), 2, 2, 0);
}
}
但是,我不明白为什么,它只是显示“templateNumPage”并且不显示包含“Header”和“Page {0} of”的第一个和第二个单元格......
我这样声明我的文件:
// On récupère le n ombre de lignes et de colonne de la GridView
int noOfColumns = gvReportingStockComp.Columns.Count;
int noOfRows = gvReportingStockComp.Rows.Count;
// On fixe les informations concernant les tailles de police du document PDF
float HeaderTextSize = 8;
float ReportNameSize = 10;
float ReportTextSize = 8;
float ApplicationNameSize = 7;
// --- On crée le document final --- \\
Document document = null;
// Le document final sera-t-il en mode paysage ou non
bool LandScape = true;
if (LandScape == true)
{
// Comme le document est en mode paysage, on execute une rotation sur ces dimensions
// Les 4 derniers paramètres de la fonction correspondent aux marges du document sur les bords
document = new Document(PageSize.A4.Rotate(), -50, -50, 50, 30);
}
else
{
// On spécifie les dimensions du document
// Les 4 derniers paramètres de la fonction correspondent aux marges du document sur les bords
document = new Document(PageSize.A4, 0, 0, 0, 0);
}
// --- On instancie le Writer et la classe d'evenement associée --- \\
// Le flux de sortie
PdfWriter pdfW = PdfWriter.GetInstance(document, Response.OutputStream);
// La classe évenement associée
StockComp evenement = new StockComp();
pdfW.PageEvent = evenement;
// --- Ouverture du document --- \\
document.Open();
// --- On ajoute les valeurs dans le document --- \\
// On créer un PdfTable qui contient le même nombre de colonne que la GridView
// On ajoute 3 car l'on souahite que la cellule composant ait une taille de 4
// et le autre cellule une taille de 1.
PdfPTable mainTable = new PdfPTable(noOfColumns+3);
// Notre document sera composé de au minimum 4 lignes :
// 1 : Nom appli + Date
// 2 : Titre du tableau
// 3 : Saut de ligne
// 4 : Headers du tableau
// X : Lignes du tableau
mainTable.HeaderRows = 4;
evenement.Header = "Trèves";
// On génère les noms de colonnes et on les insèrent
for (int i = 0; i < noOfColumns; i++)
{
PdfPCell cell = null;
Phrase ph = null;
ph = new Phrase(gvReportingStockComp.Columns[i].HeaderText, FontFactory.GetFont("Tahoma", HeaderTextSize, iTextSharp.text.Font.BOLD));
cell = new PdfPCell(ph);
cell.HorizontalAlignment = Element.ALIGN_CENTER;
if (i == 1)
{
cell.Colspan = 4;
}
mainTable.AddCell(cell);
}
document.Add(mainTable);
//pdfW.PageNumber*/
document.Close();
请帮我。谢谢 ;)