私は助けが必要です。PDF ファイルの総ページ数を取得するために、過去 4 時間を費やしました。フッターに「Page X/Y」のようなものを入れたいです。誰かがこのコードをどうするか教えてもらえますか?
public class pdfPage : PdfPageEventHelper
{
public override void OnEndPage(PdfWriter writer, Document doc)
{
iTextSharp.text.Rectangle page = doc.PageSize;
//PdfPTable EndTable = new PdfPTable(2);
PdfPTable EndTable = new PdfPTable(2);
EndTable.DefaultCell.Padding = 2f;
EndTable.HorizontalAlignment = Element.ALIGN_JUSTIFIED;
iTextSharp.text.Font smallfont2 = FontFactory.GetFont(FontFactory.HELVETICA, "CP1250", 10);
PdfPCell stopka1 = new PdfPCell(new Phrase("Left column - not important", smallfont2));
stopka1.BorderWidthLeft = 0;
stopka1.BorderWidthBottom = 0;
stopka1.BorderWidthRight = 0;
stopka1.HorizontalAlignment = Element.ALIGN_LEFT;
stopka1.VerticalAlignment = Element.ALIGN_MIDDLE;
PdfPCell stopka2 = new PdfPCell(new Phrase("Page " + doc.PageNumber + "/", smallfont2));
stopka2.BorderWidthLeft = 0;
stopka2.BorderWidthBottom = 0;
stopka2.BorderWidthRight = 0;
stopka2.HorizontalAlignment = Element.ALIGN_RIGHT;
stopka2.VerticalAlignment = Element.ALIGN_MIDDLE;
EndTable.AddCell(stopka1);
EndTable.AddCell(stopka2);
EndTable.TotalWidth = page.Width - doc.LeftMargin - doc.RightMargin;
EndTable.WriteSelectedRows(0, -1, doc.LeftMargin, EndTable.TotalHeight + doc.BottomMargin - 45, writer.DirectContent);
}
}
編集
わかりました、私はそれを整理しました。作業中の PDF ファイルを閉じて、一時ファイルとしてコピーしました。次に、「OnEndPage」メソッドで、この一時ドキュメントのページ数を数えました。後で、この一時ファイルからすべてをコピーした新しいドキュメントを開き、pdfPage クラスのオブジェクトを作成して、それを writer2.PageEvent に接続しました。今では動作します:)