我是创建pdfs的新手,并成功地使用了一个但没有页脚,所以我一直在寻找并基于this
这是我的代码:
string nome = "MapaAnual" + DateTime.Now.Year.ToString() + "_" + DateTime.Now.Month.ToString() + "" + DateTime.Now.Day.ToString() + ".pdf";
string outputFile = "C:\\Users\\sies4578\\Documents\\Visual Studio 2012\\Projects\\ULSM_Equipamentos\\REL\\" + nome.ToString();
try
{
//Create a standard .Net FileStream for the file, setting various flags
//FileStream fs = new FileStream(outputFile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
System.IO.FileStream fs = new System.IO.FileStream(outputFile + DateTime.Now.ToString("ddMMyyHHmmss") + ".pdf", System.IO.FileMode.OpenOrCreate);
//Create a new PDF document setting the size to A4
Document doc = new Document(PageSize.A4.Rotate());
//Bind the PDF document to the FileStream using an iTextSharp PdfWriter
PdfWriter w = PdfWriter.GetInstance(doc, fs);
w.PageEvent = new MyPageEventHandler();
//Open the document for writing
doc.Open();
for (int i = 0; i < listaServicos.Count; i++)
{
doc.NewPage(); //where it goes to the catch giving that error
#region Calibracoes
//Code that doesn't matter
#endregion
}
doc.Close();
Process.Start(outputFile);
return "PDF criado com sucesso";
}
catch (Exception ex)
{
Console.WriteLine("An error ocurred, the PDF-document could not be created.");
return ex.Message;
}
我怀疑它是否相关,但这里要问的是MyPageEventHandler的代码:
public class MyPageEventHandler : iTextSharp.text.pdf.PdfPageEventHelper
{
// This is the contentbyte object of the writer
PdfContentByte cb;
// we will put the final number of pages in a template
PdfTemplate template;
// this is the BaseFont we are going to use for the header / footer
BaseFont bf = null;
// This keeps track of the creation time
DateTime PrintTime = DateTime.Now;
protected iTextSharp.text.Font footer
{
get
{
// create a basecolor to use for the footer iTextSharp.text.Font, if needed.
BaseColor grey = new BaseColor(128, 128, 128);
iTextSharp.text.Font font = iTextSharp.text.FontFactory.GetFont("Arial", 9, iTextSharp.text.Font.NORMAL, grey);
return font;
}
}
public override void OnStartPage(PdfWriter writer, Document doc)
{
//Inicio Cabeçalho Logo
//Inicio Cabeçalho Logo
PdfPTable cabecalho = new PdfPTable(3);
cabecalho.DefaultCell.Border = 0;
cabecalho.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
cabecalho.TotalWidth = 700;
float[] width1 = { 150.0F, 550.0F, 550.0F };
cabecalho.SetWidths(width1);
cabecalho.LockedWidth = true;
iTextSharp.text.Image im = iTextSharp.text.Image.GetInstance("C:\\ULSMatosinhos\\Software\\ULS-Matosinhos\\Images\\logopro.png");
cabecalho.AddCell(im);
iTextSharp.text.Font cabeca = new iTextSharp.text.Font(iTextSharp.text.FontFactory.GetFont("arial", 14, iTextSharp.text.Font.BOLDITALIC, new BaseColor(23, 181, 150)));
PdfPCell linha = new PdfPCell(new Phrase("PLANO DE CALIBRAÇÕES - " + anoCalibracao.ToString(), cabeca));
linha.Border = 0;
linha.HorizontalAlignment = 1;
linha.VerticalAlignment = Element.ALIGN_MIDDLE;
cabecalho.AddCell(linha);
iTextSharp.text.Font cd = new iTextSharp.text.Font(iTextSharp.text.FontFactory.GetFont("arial", 8, iTextSharp.text.Font.NORMAL, BaseColor.BLACK));
PdfPCell codigoDoc = new PdfPCell(new Phrase(" 591_00_SIE_1191", cd));
codigoDoc.Border = 0;
codigoDoc.HorizontalAlignment = 1;
codigoDoc.VerticalAlignment = Element.ALIGN_MIDDLE;
cabecalho.AddCell(codigoDoc);
doc.Add(cabecalho);
}
public override void OnEndPage(PdfWriter writer, Document document)
{
base.OnEndPage(writer, document);
int pageN = writer.PageNumber;
String text = "Page " + pageN + " of ";
float len = bf.GetWidthPoint(text, 8);
iTextSharp.text.Rectangle pageSize = document.PageSize;
cb.SetRGBColorFill(100, 100, 100);
cb.BeginText();
cb.SetFontAndSize(bf, 8);
cb.SetTextMatrix(pageSize.GetLeft(40), pageSize.GetBottom(30));
cb.ShowText(text);
cb.EndText();
cb.AddTemplate(template, pageSize.GetLeft(40) + len, pageSize.GetBottom(30));
cb.BeginText();
cb.SetFontAndSize(bf, 8);
cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHT,
"Printed On " + PrintTime.ToString(),
pageSize.GetRight(40),
pageSize.GetBottom(30), 0);
cb.EndText();
}
public override void OnCloseDocument(PdfWriter writer, Document document)
{
base.OnCloseDocument(writer, document);
template.BeginText();
template.SetFontAndSize(bf, 8);
template.SetTextMatrix(0, 0);
template.ShowText("" + (writer.PageNumber - 1));
template.EndText();
}
}