我设置了页眉(使用 iTextSharp 库)以及仅针对 A4 页面大小(根据其方向)设置的边距 - 因为我已将文档的页面大小设置为仅 A4 - 所以它仅适用于 A4 页面并显示在最佳。谁能告诉我,无论用户选择什么,如何使这个标题在所有页面大小中都保持一致?就像它应该在每个预定义的页面大小的顶部打印标题,就像它对 A4 所做的那样,因为当我选择 Pagesize.HALFLETTER 时,标题会在超出页面边界时消失。有什么帮助吗?
更新的问题!
我的代码是:
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
بِسْمِ اللّٰهِ الرَّحْمٰنِ الرَّحِيْمِ
这怎么能被定向,所以它应该对所有页面大小都是一样的?