我正在使用itextsharp创建一个函数,该函数将标题表添加到PDF中,然后根据标题表调整边距,因此它不会被后面的文本覆盖。
然而,由于某种原因我无法理解,在第一页上,一行文字覆盖了标题,然后从第二页开始,边距是完美的。
我记得读过SetMargins调用从下一页开始生效;但是在第一页上更改边距,因为在打开文档之前进行了SetMargins调用。
那么为什么一行文本会覆盖第一页上的标题,而不是其他页面上的标题呢?毕竟,SetMargins调用只发生一次。
谁能告诉我为什么会这样?
以下是我用来创建pdf的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using it = iTextSharp.text;
using System.Diagnostics;
using System.Drawing;
using System.Data;
using Helper;
namespace Andrew
{
public class AndrewFunctions
{
public void createPDF()
{
//Create new document
Pdf doc = new Pdf(PageSize.LETTER, 15, 15, 15, 5);
//Set font and color
it.Font myfont = FontFactory.GetFont("Garamond");
myfont.SetColor(51, 51, 255);
//Create instance= of pdfwriter named test to write to file
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(@"C:\Users\Gabriela\Desktop\test.pdf", FileMode.Create));
try
{
//Create header table
PdfPTable header = new PdfPTable(1);
header.WidthPercentage = 80f;
header.TotalWidth = doc.Right - doc.Left;
header.AddCell(new PdfPCell(new Paragraph("THIS IS SOME HEADER TEXT")));
header.AddCell(new PdfPCell(new Paragraph("THIS IS SOME HEADER TEXT")));
header.AddCell(new PdfPCell(new Paragraph("THIS IS SOME HEADER TEXT")));
header.AddCell(new PdfPCell(new Paragraph("THIS IS SOME HEADER TEXT")));
//Add heading before opening doc to account for margin changes
doc.AddHeader(header, writer, doc);
doc.Open();
//Add filler content to pdf
for (int i = 0; i < 1200; i++)
{
doc.Add(new Phrase("This is filler text", myfont));
}
}
catch (it.DocumentException dex)
{
Console.WriteLine(dex.Message);
}
catch (IOException ioex)
{
Console.WriteLine(ioex.Message);
}
finally
{
//Close doc and writing stream
doc.Close();
writer.Close();
}
}
使用标题更容易理解差异,一些屏幕截图显示我的意思:
命名空间Helper:
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using it = iTextSharp.text;
using OfficeOpenXml;
using OfficeOpenXml.Drawing;
using System.Diagnostics;
using System.Drawing;
using System.Data;
using System.Web;
namespace Helper
{
public class Pdf : it.Document
{
public Pdf() : base() { }
public Pdf(it.Rectangle pageSize): base(pageSize) {}
public Pdf(it.Rectangle pageSize, float marginLeft, float marginRight, float marginTop, float marginBottom) : base(pageSize, marginLeft, marginRight, marginTop, marginBottom) { }
public void AddHeader(PdfPTable table, PdfWriter writer, Document document)
{
//Change margins to adapt to the Header
document.SetMargins(document.LeftMargin, document.RightMargin, document.TopMargin + table.TotalHeight, document.BottomMargin);
Header header = new Header(table);
writer.PageEvent = header;
}
public class Header : PdfPageEventHelper
{
//Table used for the header
public PdfPTable headerTable{ get; set; }
public Header(PdfPTable table)
{
this.headerTable = table;
}
// Place the header on each page
public override void OnEndPage(PdfWriter writer, Document document)
{
try
{
Rectangle page = document.PageSize;
float tableheight = document.TopMargin;
headerTable.WriteSelectedRows(0, -1, document.Left, page.Height - tableheight + headerTable.TotalHeight, writer.DirectContentUnder);
}
catch (DocumentException ex)
{
throw ex;
}
}
}
}
}