I have the following function, which I am using to add a rotated date on the right side of each page of a PDF containing multiple engineering drawings, using iText:
Action<string, string> InsertDateStamp =
(newFile, sourcePdfPath) =>
{
string dateStamp = DateTime.Now.ToString("dd-MMM-yyyy");
using (var pdfReader = new iText.Kernel.Pdf.PdfReader(sourcePdfPath))
using (var pdfWriter = new iText.Kernel.Pdf.PdfWriter(newFile))
using (var pdfDoc = new iText.Kernel.Pdf.PdfDocument(pdfReader, pdfWriter))
{
var numPages = pdfDoc.GetNumberOfPages();
for (int pageNum = 1; pageNum <= numPages; pageNum++)
{
var page = pdfDoc.GetPage(pageNum);
var pdfCanvas = new PdfCanvas(page.NewContentStreamBefore(), page.GetResources(), pdfDoc);
var canvas = new iText.Layout.Canvas(pdfCanvas, page.GetPageSize());
canvas.SetFontColor(iText.Kernel.Colors.ColorConstants.BLACK);
canvas.SetFontSize(14);
canvas.SetFont(iText.Kernel.Font.PdfFontFactory.CreateFont(iText.IO.Font.Constants.StandardFonts.TIMES_ROMAN));
canvas.ShowTextAligned(new iText.Layout.Element.Paragraph(dateStamp), 785, 300, pageNum, iText.Layout.Properties.TextAlignment.CENTER, iText.Layout.Properties.VerticalAlignment.MIDDLE, 1.5708f);
pdfCanvas.Release();
}
pdfDoc.Close();
pdfReader.Close();
pdfWriter.Close();
}
};
This code works most of the time, but for some reason on some pages the added text is not visible. It is THERE, as I can select it and copy and paste it into notepad, but the text is just not visible on the page.
For example:
This only occurs on some pages, and I cannot figure out why, or what is wrong with the code in question...
Any help would be greatly appreciated.
Thanks!
Finally, after much messing around, managed to get it working:
var over = new PdfCanvas(page);
over.SetFillColorRgb(0, 0, 0);
var p = new iText.Layout.Element.Paragraph(dateStamp).SetFont(iText.Kernel.Font.PdfFontFactory.CreateFont(iText.IO.Font.Constants.StandardFonts.TIMES_ROMAN)).SetFontSize(14);
new iText.Layout.Canvas(over, page.GetPageSize()).ShowTextAligned(p, 785, 300, pageNum, iText.Layout.Properties.TextAlignment.CENTER, iText.Layout.Properties.VerticalAlignment.MIDDLE, 1.5708f);
It makes no sense to me, but hey, it works....