ドキュメントのページサイズをA4のみに設定しているため、A4ページサイズのみに設定された余白とともに(iTextSharpライブラリを使用して)ページヘッダーを設定しました(その向きに応じて)。したがって、A4ページでのみ機能し、上。ユーザーが選択したすべてのページサイズでこのヘッダーを一貫させる方法を教えてもらえますか? Pagesize.HALFLETTERを選択すると、ヘッダーがページ境界から出ると消えてしまうため、A4の場合と同様に、事前定義されたすべてのページサイズの上部にヘッダーを印刷する必要があります。ヘルプはありますか?
更新された質問!
私のコードは次のとおりです。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.exceptions;
using System.Windows.Forms;
using System.IO;
namespace PDFGeneration
{
public class Events : iTextSharp.text.pdf.PdfPageEventHelper
{
private String header;
private iTextSharp.text.pdf.PdfTemplate total;
private PdfContentByte cb;
private PdfTemplate template;
protected Font footer
{
get
{
//BaseFont bfArialUniCode = BaseFont.CreateFont(@"fonts\\adobe-arabic-regular-1.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
BaseColor grey = new BaseColor(128, 128, 128);
Font font = FontFactory.GetFont("Arial", 10.0f, Font.NORMAL, grey);
//Font font = new Font(bfArialUniCode, 10.0f, Font.NORMAL, grey);
return font;
}
}
protected Font watermark
{
get
{
BaseFont bfArialUniCode = BaseFont.CreateFont(@"fonts\\adobe-arabic-regular-1.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
BaseColor grey = new BaseColor(128, 128, 128);
Font font = new Font(bfArialUniCode, 14.0f, Font.BOLDITALIC, grey);
return font;
}
}
public override void OnStartPage(iTextSharp.text.pdf.PdfWriter writer, iTextSharp.text.Document document)
{
PdfPTable headerTbl = new PdfPTable(1);
headerTbl.TotalWidth = document.PageSize.Width;
//Center the table on the page
headerTbl.HorizontalAlignment = Element.ALIGN_JUSTIFIED;
Paragraph para = new Paragraph(String.Format("Urdu Word Processor Date : {0:dd/MMM/yyyy} Page Number : {1}", DateTime.Now, writer.PageNumber), footer);
PdfPCell cell = new PdfPCell(para);
cell.Border = 0;
cell.PaddingLeft = 120.0f;
//cell.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
//add cell to table
headerTbl.AddCell(cell);
headerTbl.WriteSelectedRows(0, -1, 0, (document.BottomMargin + 790), writer.DirectContent);
}
public override void OnEndPage(iTextSharp.text.pdf.PdfWriter writer, iTextSharp.text.Document document)
{
//PdfContentByte cb = writer.DirectContent;
//PdfPTable tablefooter = new PdfPTable(1);
////tablefooter.HeaderRows = 3;
//tablefooter.HorizontalAlignment = 1;
//float[] widthfooter = new float[] { 1f };
//tablefooter.SetWidths(widthfooter);
//tablefooter.SpacingBefore = 100f;
//PdfPCell headerfooter = new PdfPCell(new Phrase(String.Format("Urdu Word Processor Date : {0:dd/MMM/yyyy} Page Number : {1}", DateTime.Now, writer.PageNumber), new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12, iTextSharp.text.Font.NORMAL, BaseColor.BLACK)));
////Phrase headerfooter = new Phrase(new Phrase("Urdu Word Processor " + "Date: " + DateTime.Now.ToString("dd-MMM-yyyy-ss") + " " + "Page: " + writer.PageNumber, new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12, iTextSharp.text.Font.NORMAL, BaseColor.BLACK)));
//headerfooter.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
//headerfooter.Border = iTextSharp.text.Rectangle.TOP_BORDER;
//tablefooter.AddCell(headerfooter);
////document.Add(tablefooter);
//ColumnText ct = new ColumnText(cb);
//ct.SetSimpleColumn(new Phrase(new Chunk("بِسْمِ اللّٰهِ الرَّحْمٰنِ الرَّحِيْمِ ", FontFactory.GetFont(FontFactory.HELVETICA, 18, Font.NORMAL))),
// 46, 190, 530, 36, 25, Element.ALIGN_LEFT | Element.ALIGN_TOP);
//ct.Go();
// water mark
//ColumnText.ShowTextAligned(writer.DirectContentUnder,Element.ALIGN_CENTER,
// new Phrase("بِسْمِ اللّٰهِ الرَّحْمٰنِ الرَّحِيْمِ", watermark),
// 297.5f, 421, ((writer.PageNumber % 2) == 1) ? 45 : -45); // have made a loop so to have multiple orientations.
ColumnText col = new ColumnText(writer.DirectContentUnder);
col.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
col.SetSimpleColumn(220, 810, 569, 36);
col.AddElement(new Paragraph("بِسْمِ اللّٰهِ الرَّحْمٰنِ الرَّحِيْمِ",watermark));
col.Go();
}
//public void setHeader(String header)
//{
// this.header = header;
//}
//public void onOpenDocument(iTextSharp.text.pdf.PdfWriter writer, iTextSharp.text.Document document)
//{
// total = writer.DirectContent.CreateTemplate(30,16);
//}
//public void onEndPage(iTextSharp.text.pdf.PdfWriter writer, iTextSharp.text.Document document)
//{
// iTextSharp.text.pdf.PdfPTable table = new PdfPTable(1);
// try
// {
// //table.SetWidths(new int[]{24, 24, 2});
// table.SetTotalWidth(new float[] {527.0f,527.0f,527.0f});
// //table.LockedWidth = true;
// table.DefaultCell.FixedHeight = 20;
// table.DefaultCell.Border = iTextSharp.text.Rectangle.ALIGN_BOTTOM;
// table.AddCell(header);
// table.DefaultCell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_RIGHT;
// table.AddCell( String.Format("Page:{0} of", writer.PageNumber));
// // MessageBox.Show(String.Format("Page:{0} of", writer.PageNumber));
// iTextSharp.text.pdf.PdfPCell cell = new iTextSharp.text.pdf.PdfPCell(iTextSharp.text.Image.GetInstance(total));
// cell.Border = iTextSharp.text.Rectangle.ALIGN_BOTTOM;
// table.AddCell(cell);
// table.WriteSelectedRows(0, -1,
// 34, 803, writer.DirectContent);
// }
// catch(Exception de)
// {
// // MessageBox.Show(de.Message.ToString());
// }
//}
//public void onCloseDocument(iTextSharp.text.pdf.PdfWriter writer, iTextSharp.text.Document document)
//{
// String pageNumber = (writer.PageNumber).ToString();
// pageNumber += " of " + (writer.PageNumber).ToString();
// iTextSharp.text.pdf.ColumnText.ShowTextAligned(writer.DirectContent, iTextSharp.text.Element.ALIGN_RIGHT, new iTextSharp.text.Phrase(pageNumber), 580, 10, -90);
//}
//override the OnPageEnd event handler to add our footer
//public override void OnOpenDocument(PdfWriter writer, Document document)
//{
// cb = writer.DirectContent;
// template = cb.CreateTemplate(50, 50);
//}
//public override void OnEndPage(PdfWriter writer, Document document)
//{
// base.OnEndPage(writer, document);
// int pageN = writer.PageNumber;
// String text = "Page " + pageN.ToString() + " of ";
// //float len = this.RunDateFont.BaseFont.GetWidthPoint(text, this.RunDateFont.Size);
// iTextSharp.text.Rectangle pageSize = document.PageSize;
// cb.SetRGBColorFill(100, 100, 100);
// cb.BeginText();
// //cb.SetFontAndSize(this.RunDateFont.BaseFont, this.RunDateFont.Size);
// cb.SetTextMatrix(document.LeftMargin, pageSize.GetBottom(document.BottomMargin));
// cb.ShowText(text);
// cb.EndText();
// cb.AddTemplate(template, document.LeftMargin + 10.0f, pageSize.GetBottom(document.BottomMargin));
//}
//public override void OnCloseDocument(PdfWriter writer, Document document)
//{
// base.OnCloseDocument(writer, document);
// BaseFont bfArialUniCode = BaseFont.CreateFont(@"fonts\\adobe-arabic-regular-1.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
// template.BeginText();
// template.SetFontAndSize(bfArialUniCode, 14.0f);
// template.SetTextMatrix(0, 0);
// template.ShowText("" + (writer.PageNumber - 1));
// template.EndText();
//}
}// end of class
}
このコードは、この出力を A4 ページ サイズの上部にのみ印刷します。これは、テキストを揃えるためにこのコード内に値がハードコードされているためです。
ウルドゥー語ワープロ 日付: 17-02-2013 ページ番号: 1
بِسْمِ اللّٰهِ الرَّحْمٰنِ الرَّحِيْمِ
すべてのページサイズで同じになるように、これをどのように方向付けることができますか?