使用itextsharp (c#)添加标题到PDF文件-只是新的章节页
本文关键字:-只 文件 PDF itextsharp 添加 标题 使用 | 更新日期: 2023-09-27 17:55:02
我的代码被阻塞了,因为我找不到这个主题的答案。所以我想做的是在新的章节页面上添加一个标题。我已经实现了这一点,但是当添加一个需要两个或更多页面的新表时,我无法将标题添加到我想要的页面。我有以下代码:
在我的main函数中:
document.NewPage();
writer.PageEvent = null;
addHeader(writer, document, chapter3, title3);
addFooter(writer, document, chapter3, title3);
table = createVariablesTable();
headerOffsetSpace(document);
addTableHeader(table);
addTableBody(table, firsPage);
document.Add(table);
document.NewPage();
writer.PageEvent = null;
addFooter(writer, document, chapter3, title3);
table = createVariablesTable();
addTableHeader(table);
firstPage = false;
addTableBody(table, firstPage);
document.Add(table);
地点:
private void addHeader(PdfWriter writer, Document document, String chapterText, String titleText)
{
Font ArialBold = new Font(Font.FontFamily.HELVETICA, 24, Font.BOLD);
Font ArialItalic = new Font(Font.FontFamily.HELVETICA, 24, Font.ITALIC);
Chunk headerChapterChunk = new Chunk(chapterText, ArialItalic);
Chunk headerTitleChunk = new Chunk(titleText, ArialBold);
Phrase headerText = new Phrase();
headerText.Add(headerChapterChunk);
headerText.Add(headerTitleChunk);
HeaderPageEvent headerEvent = new HeaderPageEvent() { header = headerText };
writer.PageEvent = headerEvent;
}
private void addFooter(PdfWriter writer, Document document, String chapterText, String titleText)
{
Font TimesNewRomanNormal = new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.NORMAL);
Font TimesNewRomanItalic = new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.ITALIC);
Chunk footerChapterChunk = new Chunk(chapterText, TimesNewRomanItalic);
Chunk footerTitleChunk = new Chunk(titleText, TimesNewRomanNormal);
Phrase footerText = new Phrase();
footerText.Add(footerChapterChunk);
footerText.Add(footerTitleChunk);
FooterPageEvent footerEvent = new FooterPageEvent() { footer = footerText };
writer.PageEvent = footerEvent;
}
和类PageEvent是:
class HeaderPageEvent : PdfPageEventHelper
{
float marginHorizontal = 42.6f;
float marginTop = 777;
float marginTopHeaderSeparator = 738.1f;
float indentHeaderSeparator = 1.3f;
float headerLeading = 27.6f;
public Phrase header { get; set; }
public override void OnEndPage(PdfWriter writer, Document document)
{
iTextSharp.text.Rectangle pageSize = document.PageSize;
PdfContentByte cb = writer.DirectContent;
ColumnText columnHeader = new ColumnText(cb);
columnHeader.SetSimpleColumn(header, marginHorizontal, marginTop + headerLeading, pageSize.Width - 2 * marginHorizontal, 0, headerLeading, Element.ALIGN_LEFT);
columnHeader.Go();
ColumnText columnHeaderSeparator = new ColumnText(cb);
Phrase headerSeparator = new Phrase(new Chunk(new iTextSharp.text.pdf.draw.LineSeparator(0.25f, 100.0f, BaseColor.BLACK, Element.ALIGN_LEFT, 0)));
columnHeaderSeparator.SetSimpleColumn(headerSeparator, marginHorizontal - indentHeaderSeparator, marginTopHeaderSeparator, pageSize.Width - marginHorizontal, 0, 0, Element.ALIGN_LEFT);
columnHeaderSeparator.Go();
}
}
class FooterPageEvent : PdfPageEventHelper
{
float marginHorizontal = 42.6f;
float indentFooterSeparator = 1.5f;
float marginBottomFooter = 37.9f;
float marginBottomFooterSeparator = 48.2f;
float pageNumberEvenOffset = 35.4f;
float pageNumberOddOffset = 28.4f;
public Phrase footer { get; set; }
Font TimesNewRomanNormal = new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.NORMAL);
public override void OnEndPage(PdfWriter writer, Document document)
{
iTextSharp.text.Rectangle pageSize = document.PageSize;
PdfContentByte cb = writer.DirectContent;
ColumnText columnFooterSeparator = new ColumnText(cb);
Phrase footerSeparator = new Phrase(new Chunk(new iTextSharp.text.pdf.draw.LineSeparator(0.25f, 100.0f, BaseColor.BLACK, Element.ALIGN_LEFT, 0)));
columnFooterSeparator.SetSimpleColumn(footerSeparator, marginHorizontal - indentFooterSeparator, 0, pageSize.Width - marginHorizontal + indentFooterSeparator, marginBottomFooterSeparator, 0, Element.ALIGN_LEFT);
columnFooterSeparator.Go();
ColumnText columnPageNumber = new ColumnText(cb);
int pageN = writer.PageNumber;
Phrase pageNumber = new Phrase(pageN.ToString(), TimesNewRomanNormal);
ColumnText columnFooter = new ColumnText(cb);
switch(pageN % 2)
{
case 0:
columnPageNumber.SetSimpleColumn(pageNumber, marginHorizontal, 0, pageSize.Width, marginBottomFooter, 0, Element.ALIGN_LEFT);
columnPageNumber.Go();
columnFooter.SetSimpleColumn(footer, marginHorizontal + pageNumberEvenOffset, 0, pageSize.Width, marginBottomFooter, 0, Element.ALIGN_LEFT);
columnFooter.Go();
break;
case 1:
columnPageNumber.SetSimpleColumn(pageNumber, 0, 0, pageSize.Width - marginHorizontal, marginBottomFooter, 0, Element.ALIGN_RIGHT);
columnPageNumber.Go();
columnFooter.SetSimpleColumn(footer, 0, 0, pageSize.Width - marginHorizontal - pageNumberOddOffset, marginBottomFooter, 0, Element.ALIGN_RIGHT);
columnFooter.Go();
break;
}
}
}
所以…像这样做我的代码,它工作正常。我将页眉和页脚事件添加到新的章节页面。然后,我将事件设置为null,并在下一页上再次添加footer事件,对于接下来的页面,文档将始终具有footer事件。这将解决我的问题,但是,有时"addTableBody"函数大于一个页面,所以文档自动添加一个新页面而不清理页面事件,所以它将是如此多的页面与标题一样多的页面表需要显示。有人能帮我吗?我只是想把Header事件添加到新的章节页。
Thanks in advance
为您的HeaderPageEvent
添加一个额外的参数:
class HeaderPageEvent : PdfPageEventHelper
{
boolean ignoreHeader = true;
float marginHorizontal = 42.6f;
float marginTop = 777;
// rest of your code...
}
像这样使用这个额外参数:
public override void OnEndPage(PdfWriter writer, Document document)
{
if (ignoreHeader) return;
ignoreHeader = true;
// rest of your code...
}
通过这样做,您告诉事件仅在ignoreHeader
的值为false
时显示标题,如果它为假,则立即设置为true
。
默认情况下,标题将永远不会显示,这是您想要的。您只希望在需要时显示标题。您可以通过将此方法添加到HeaderPageEvent
:
void ShowHeader() {
ignoreHeader = false;
}
您不再需要设置页面事件为null
。你现在可以触发showHeader()
方法,每次你想要一个标题出现。
HeaderPageEvent event = new HeaderPageEvent();
writer.PageEvent = event;
// Add stuff, no header will be added
event.ShowHeader();
document.NewPage();
// A header will be shown, but only on one page
document.Add(table);
如果您想让标题出现在第一页,您需要将ignoreHeader
的默认值更改为false