我使用下面显示的代码在每个页面的现有BYTES中添加页脚文本,这在Chrome和Foxit PhantomPDF中都可见,但是,它在Adobe PDF阅读器中不可见。
除了页脚之外,我还添加了即使使用Adobe也可以看到的标题。
private byte[] AddPageHeaderFooter(byte[] pdf, float marginLeft, float marginRight, float marginTop,
float marginBottom, DataSet prospectDetails, ref int startingPageNumber, string logoPath, bool isLandscape = false)
{
var dataRow = prospectDetails.Tables[0].Rows[0];
var prospectDate = Convert.ToDateTime(dataRow[0]);
using (var stream = new MemoryStream())
{
stream.Write(pdf, 0, pdf.Length);
var reader = new PdfReader(pdf);
var document = new Document(reader.GetPageSizeWithRotation(1), marginLeft, marginRight, marginTop,
marginBottom);
var writer = PdfWriter.GetInstance(document, stream);
document.Open();
var contentByte = writer.DirectContent;
var pageIndex = startingPageNumber;
for (var page = 1; page <= reader.NumberOfPages; page++)
{
document.NewPage();
pageIndex++;
var importedPage = writer.GetImportedPage(reader, page);
if (isLandscape)
contentByte.AddTemplate(importedPage, 0, -1f, 1f, 0, 0,
reader.GetPageSizeWithRotation(page).Height);
else
contentByte.AddTemplate(importedPage, 0, 0);
contentByte.BeginText();
var baseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
// Code to add header
// Footer
// Not visible parts **START**
contentByte.SetFontAndSize(baseFont, 7);
var prospectDateString = string.Format("{0:ddd, MMM d, yyyy}", prospectDate);
contentByte.ShowTextAligned(PdfContentByte.ALIGN_CENTER,
"FOOTER LINE 1",
isLandscape ? 398 : 305f, 50, 0);
contentByte.ShowTextAligned(PdfContentByte.ALIGN_CENTER,
"FOOTER LINE 2", isLandscape ? 398 : 305f, 42, 0);
contentByte.ShowTextAligned(PdfContentByte.ALIGN_LEFT,
prospectDateString.Substring(5, prospectDateString.Length - 5), document.Left, marginBottom, 0);
contentByte.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "- " + pageIndex + " -",
isLandscape ? 398 : 305f, marginBottom, 0);
contentByte.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, string.Format("{0:T}", prospectDate),
document.Right, marginBottom, 0);
contentByte.EndText();
contentByte.SaveState();
// Not visible parts **END**
// Footer line
// Not visible parts **START**
contentByte.SetColorStroke(new PdfSpotColor("black", new BaseColor(0, 0, 0)), 100);
contentByte.SetLineWidth(0.25f);
contentByte.Rectangle(marginLeft, 58, isLandscape ? 732 : 552, 0.25f);
contentByte.FillStroke();
// Not visible parts **END**
contentByte.RestoreState();
}
startingPageNumber = pageIndex;
document.Close();
return stream.ToArray();
}
}
问题: