作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我可以使用 iText7 和 C# ASP.NET 创建一个 pdf 文件
此 pdf 文件共包含 9 页。
我试图在我的代码中添加总页数和一条虚线
但是我有两个问题
任何帮助将不胜感激......谢谢。
下面是我的代码
private void mtpdfnew()
{
DataTable dt = new DataTable();
filename = @"C:\inetpub\wwwroot\public\pdf\" +
DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf";
PdfWriter writer = new PdfWriter(filename);
PdfDocument pdf = new PdfDocument(writer);
Document document = new Document(pdf);
using (MySqlConnection myConnectionString =
new MySqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString))
{
using (MySqlCommand cmd =
new MySqlCommand("SP", myConnectionString))
{
cmd.Connection.Open();
cmd.CommandType = CommandType.StoredProcedure;
using (MySqlDataAdapter da =
new MySqlDataAdapter(cmd))
{
da.Fill(dt);
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
contents = new Paragraph(dt.Rows[i]["contents"].ToString())
.SetTextAlignment(TextAlignment.JUSTIFIED)
.SetFontSize(12);
if (dt.Rows[i]["contents"].ToString().StartsWith("Set") && i != 0)
{
document.Add(new AreaBreak(AreaBreakType.NEXT_PAGE));
document.Add(new LineSeparator(new DottedLine(1, 2)).SetMarginTop(4));
}
List<IElement> lst = HtmlConverter.ConvertToElements(dt.Rows[i]["contents"].ToString()).ToList();
for (int j = 0; j < lst.Count; j++)
{
IBlockElement element = (IBlockElement)lst[j];
if (dt.Rows[i]["contents"].ToString().StartsWith("Set"))
{
contents.SetFontSize(12)
.SetBold()
.SetFontColor(ColorConstants.BLUE);
}
else if (dt.Rows[i]["contents"].ToString().StartsWith("- "))
{
contents.SetFontSize(10)
.SetBold()
.SetFontColor(ColorConstants.BLACK);
}
else
{
contents.SetTextAlignment(TextAlignment.JUSTIFIED_ALL)
.SetFontSize(10)
.SetFontColor(ColorConstants.BLACK);
}
element.SetProperty(Property.LEADING, new Leading(Leading.MULTIPLIED, -1f));
document.Add(element);
}
}
Footer footerHandler = new Footer();
pdf.AddEventHandler(PdfDocumentEvent.END_PAGE, footerHandler);
footerHandler.WriteTotal(pdf);
dest = filename.ToString();
document.Close();
}
Response.Clear();
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + dest);
Response.TransmitFile(dest);
Response.End();
}
}
}
}
private class TableFooterEventHandler : IEventHandler
{
private Table table;
public TableFooterEventHandler(Table table)
{
this.table = table;
}
public void HandleEvent(Event currentEvent)
{
PdfDocumentEvent docEvent = (PdfDocumentEvent)currentEvent;
PdfDocument pdfDoc = docEvent.GetDocument();
PdfPage page = docEvent.GetPage();
int pageNumber = pdfDoc.GetPageNumber(page);
Rectangle pageSize = page.GetPageSize();
PdfCanvas canvas = new PdfCanvas(page.NewContentStreamBefore(), page.GetResources(), pdfDoc);
new Canvas(canvas, new Rectangle(36, 20, page.GetPageSize().GetWidth() - 72, 50))
.Add(table)
.Close();
}
}
protected class Footer : IEventHandler
{
protected PdfFormXObject placeholder;
protected float side = 20;
protected float x = 300;
protected float y = 25;
protected float space = 4.5f;
protected float descent = 3;
public Footer()
{
placeholder = new PdfFormXObject(new Rectangle(0, 0, side, side));
}
public virtual void HandleEvent(Event @event)
{
PdfDocumentEvent docEvent = (PdfDocumentEvent)@event;
PdfDocument pdf = docEvent.GetDocument();
PdfPage page = docEvent.GetPage();
int pageNumber = pdf.GetPageNumber(page);
Rectangle pageSize = page.GetPageSize();
PdfCanvas pdfCanvas = new PdfCanvas(page);
Canvas canvas = new Canvas(pdfCanvas, pageSize);
canvas.SetFontSize(10);
Paragraph p = new Paragraph()
.Add("Page ")
.Add(pageNumber.ToString())
.Add(" of");
canvas.ShowTextAligned(p, x, y, TextAlignment.RIGHT);
canvas.Close();
pdfCanvas.AddXObjectAt(placeholder, x + space, y - descent);
pdfCanvas.Release();
}
public void WriteTotal(PdfDocument pdfDoc)
{
Canvas canvas = new Canvas(placeholder, pdfDoc);
canvas.SetFontSize(10);
canvas.ShowTextAligned(pdfDoc.GetNumberOfPages().ToString(),
0, descent, TextAlignment.LEFT);
canvas.Close();
}
}
private class TableHeaderEventHandler : IEventHandler
{
private Table table;
private float tableHeight;
private Document doc;
public TableHeaderEventHandler(Document doc)
{
this.doc = doc;
InitTable();
TableRenderer renderer = (TableRenderer)table.CreateRendererSubTree();
renderer.SetParent(new DocumentRenderer(doc));
LayoutResult result = renderer.Layout(new LayoutContext(new LayoutArea(0, PageSize.A4)));
tableHeight = result.GetOccupiedArea().GetBBox().GetHeight();
}
public void HandleEvent(Event currentEvent)
{
PdfDocumentEvent docEvent = (PdfDocumentEvent)currentEvent;
PdfDocument pdfDoc = docEvent.GetDocument();
PdfPage page = docEvent.GetPage();
int pageNum = docEvent.GetDocument().GetPageNumber(page);
PdfCanvas canvas = new PdfCanvas(page.NewContentStreamBefore(), page.GetResources(), pdfDoc);
PageSize pageSize = pdfDoc.GetDefaultPageSize();
float coordX = pageSize.GetX() + doc.GetLeftMargin();
float coordY = pageSize.GetTop() - doc.GetTopMargin();
float width = pageSize.GetWidth() - doc.GetRightMargin() - doc.GetLeftMargin();
float height = GetTableHeight();
Rectangle rect = new Rectangle(coordX, coordY, width, height);
new Canvas(canvas, rect)
.Add(table)
.Close();
}
public float GetTableHeight()
{
return tableHeight;
}
private void InitTable()
{
}
}
最佳答案
您在关闭文档之前将页脚绘制事件处理程序添加到文档:
Footer footerHandler = new Footer();
pdf.AddEventHandler(PdfDocumentEvent.END_PAGE, footerHandler);
footerHandler.WriteTotal(pdf);
dest = filename.ToString();
document.Close();
此时几乎所有页面都已完成,它们的 END_PAGE
事件已被触发和处理,因此您的页脚处理程序将不会接收它们。只有最后一页和可能是上一页还有待完成,因此将为它们调用页脚处理程序。
因此,要让您的页脚处理程序接收所有页面的 END_PAGE
事件,请在创建 pdf 文档后尽早创建并注册它:
PdfWriter writer = new PdfWriter(filename);
PdfDocument pdf = new PdfDocument(writer);
Footer footerHandler = new Footer();
pdf.AddEventHandler(PdfDocumentEvent.END_PAGE, footerHandler);
Document document = new Document(pdf);
写总数,当然还是要放在最后:
footerHandler.WriteTotal(pdf);
dest = filename.ToString();
document.Close();
关于c# - 使用 iText7 和 ASP.NET C# 在 PDF 文件的页脚中添加页码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66712009/
我是一名优秀的程序员,十分优秀!