I am not sure how to add this text to the canvas in iText7. In the old version I use this BaseFont.CreateFont
and overContent
. In iText7 I see this PdfCanvas
control and this PdfCanvas.BeginText
mehtod but I am getting a error related to no overoad.
PdfPage pdfPage = pdfDocument.GetPage(i);
Rectangle pageSizeWithRotation = pdfPage.GetPageSizeWithRotation();
PdfCanvas canvas = new PdfCanvas(pdfPage);
Text pdfText = new Text(disclaimerText)
.SetFontColor(ColorConstants.BLACK)
.SetFont(PdfFontFactory.CreateFont(StandardFonts.HELVETICA, "Cp1250"))
.SetFontSize(7F);
canvas.BeginText(pdfText);
Old Version I have something like this
PdfContentByte overContent = pdfStamper.GetOverContent(i);
overContent.BeginText();
BaseFont baseFont = BaseFont.CreateFont("Helvetica", "Cp1250", false);
overContent.SetFontAndSize(baseFont, 7F);
overContent.SetRGBColorFill(0, 0, 0);
float n2 = 15F;
float n3 = pageSizeWithRotation.Height - 10F;
overContent.ShowTextAligned(0, disclaimerText, n2, n3, 0F);
overContent.EndText();
You can refer to the following code to understand how the BeginText and EndText work together with PdfCanvas.
//Get the page from the pdf
PdfPage page = pdfDoc.GetPage(i);
Rectangle pageSize = page.GetPageSizeWithRotation();
int pageNumber = pdfDoc.GetPageNumber(page);
PdfCanvas pdfCanvas = new PdfCanvas(page.NewContentStreamBefore(), page.GetResources(), pdfDoc);
//Set background
Color limeColor = new DeviceCmyk(0.208 f, 0, 0.584 f, 0);
Color blueColor = new DeviceCmyk(0.445 f, 0.0546 f, 0, 0.0667 f);
pdfCanvas.SaveState()
.SetFillColor(pageNumber % 2 == 1 ? limeColor : blueColor)
.Rectangle(pageSize.GetLeft(), pageSize.GetBottom(),
pageSize.GetWidth(), pageSize.GetHeight())
.Fill()
.RestoreState();
//Add header and footer
pdfCanvas.BeginText()
.SetFontAndSize(PdfFontFactory.CreateFont(StandardFonts.HELVETICA), 9)
.MoveText(pageSize.GetWidth() / 2 - 60, pageSize.GetTop() - 20)
.ShowText("THE TRUTH IS OUT THERE")
.MoveText(60, -pageSize.GetTop() + 30)
.ShowText(pageNumber.ToString())
.EndText();
//Add watermark
iText.Layout.Canvas canvas = new iText.Layout.Canvas(pdfCanvas, pdfDoc, page.GetPageSize());
canvas.SetProperty(Property.FONT_COLOR, Color.WHITE);
canvas.SetProperty(Property.FONT_SIZE, 60);
canvas.SetProperty(Property.FONT, PdfFontFactory.CreateFont(StandardFonts.HELVETICA_BOLD));
canvas.ShowTextAligned(new Paragraph("CONFIDENTIAL"), 298, 421, pdfDoc.GetPageNumber(page), TextAlignment.CENTER, VerticalAlignment.MIDDLE, 45);
pdfCanvas.Release();
This example is also available here in the knowledge base of iText 7 at https://kb.itextpdf.com/home/it7kb/examples/itext-7-jump-start-tutorial-chapter-3 example "c03e03_ufo"