I am building a PDF at runtime in itext7.pdfHTML, using a template file. I want to add a footer to every page of the resulting PDF, which has two pages, but for some reason the footer only appears on the second page.
I'm trying to build on this example from the iText website. The example deals with page numbers, but since I'm just adding static text to my document, the principle is the same. Here's my code:
string footer = "This is the footer".
string body = "<span>This is raw HTML</span>";
//create a temporary PDF with the raw HTML, made from my template and given data
private void createPDF()
{
destination = System.IO.Path.Combine(HttpContext.Current.Server.MapPath("~/pdf_repo"), "tempFile.pdf");
ConverterProperties properties = new ConverterProperties();
properties.SetBaseUri(HttpContext.Current.Server.MapPath("~/templates/"));
HtmlConverter.ConvertToPdf(body, new FileStream(destination, FileMode.Create), properties);
addFooter(id);
}
//modify the PDF file created above by adding the footer
private void addFooter(string id)
{
string newFile = System.IO.Path.Combine(HttpContext.Current.Server.MapPath("pdf_repo", "finalFile.pdf");
PdfDocument pdfDoc = new PdfDocument(new PdfReader(destination), new PdfWriter(newFile));
Document doc = new Document(pdfDoc);
Paragraph foot = new Paragraph(footer);
foot.SetFontSize(8);
float x = 300; //559
float y = 0; //806
int numberOfPages = pdfDoc.GetNumberOfPages();
for (int i = 1; i <= numberOfPages; i++)
{
doc.ShowTextAligned(foot, x, y, TextAlignment.CENTER, VerticalAlignment.BOTTOM);
}
doc.Close();
//delete temporary PDF
File.Delete(destination);
}
I've tried setting i to 0 in the addFooter() "for" loop, but that doesn't solve the issue. How can I get the footer to appear on every page?
Yeah, you weren't specifying which page to add the footer to, so it only added it to the bottom of the entire document. Try this:
Note, the only change was: doc.ShowTextAligned(foot, x, y, i, TextAlignment.CENTER, VerticalAlignment.BOTTOM);
string footer = "This is the footer".
string body = "<span>This is raw HTML</span>";
//create a temporary PDF with the raw HTML, made from my template and given data
private void createPDF()
{
destination = System.IO.Path.Combine(HttpContext.Current.Server.MapPath("~/pdf_repo"), "tempFile.pdf");
ConverterProperties properties = new ConverterProperties();
properties.SetBaseUri(HttpContext.Current.Server.MapPath("~/templates/"));
HtmlConverter.ConvertToPdf(body, new FileStream(destination, FileMode.Create), properties);
addFooter(id);
}
//modify the PDF file created above by adding the footer
private void addFooter(string id)
{
string newFile = System.IO.Path.Combine(HttpContext.Current.Server.MapPath("pdf_repo", "finalFile.pdf");
PdfDocument pdfDoc = new PdfDocument(new PdfReader(destination), new PdfWriter(newFile));
Document doc = new Document(pdfDoc);
Paragraph foot = new Paragraph(footer);
foot.SetFontSize(8);
float x = 300; //559
float y = 0; //806
int numberOfPages = pdfDoc.GetNumberOfPages();
for (int i = 1; i <= numberOfPages; i++)
{
doc.ShowTextAligned(foot, x, y, i, TextAlignment.CENTER, VerticalAlignment.BOTTOM);
}
doc.Close();
//delete temporary PDF
File.Delete(destination);
}
Related
We are creating a PDF, it contains a large table, with headers (bold), sub-headers (non bold), rows and cells.
One of the requirements is to add an already existing PDF (can be multiple pages) inside the cell (under the sub-header). And this is where we are struggling. What we have tried so far:
Converting the PDF to a FormXObject, and adding that into the cell.
FontProvider fontProvider = new FontProvider();
ConverterProperties props = new ConverterProperties();
props.SetFontProvider(fontProvider);
var historyId = text.Replace("=Spectec_template", "");
var pdfTemplate = templateToPdfClient.GetPdf(historyId, "HI").Result;
PdfDocument srcDoc = new PdfDocument(new PdfReader(pdfTemplate.FileStream));
for (int i = 1; i < srcDoc.GetNumberOfPages() + 1; i++)
{
Cell newCell = new Cell(1, columnInfo.ColumnSpan).SetFont(PdfFontFactory.CreateFont(_fontName));
PdfPage origPage = srcDoc.GetPage(i);
Rectangle rect = origPage.GetPageSize();
PdfFormXObject pageCopy = origPage.CopyAsFormXObject(pdf);
Image image = new Image(pageCopy);
image.SetBorder(Border.NO_BORDER);
image.SetMaxWidth(UnitValue.CreatePercentValue(100));
image.SetMaxHeight(UnitValue.CreatePercentValue(50));
Div div = new Div();
div.Add(image.SetMaxWidth(UnitValue.CreatePercentValue(100)));
newCell.Add(div);
newCell = ApplyLayoutsToCell(newCell, cellStyles);
table.AddCell(newCell);
}
But the end result is that we only see 1 page and it's completely overlapping at the end of the page.
When we set image.SetAutoScale(true); the PDF is visible but it's extremely small.
When adding the Image to a Paragraph instead of a DIV the PDF pages display next to each other.
Any ideas, suggestions?
Thank you
Thanks to #mkl and #KJ I figured this out.
I ended up adding each PDF page in a new Cell, and setting the scaling of image to 0.6f.
for (int i = 1; i < srcDoc.GetNumberOfPages() + 1; i++)
{
newCell = new Cell(1, columnInfo.ColumnSpan).SetFont(PdfFontFactory.CreateFont(_fontName));
PdfPage origPage = srcDoc.GetPage(i);
PdfFormXObject pageCopy = origPage.CopyAsFormXObject(pdf);
Image image = new Image(pageCopy);
image.SetBorder(Border.NO_BORDER);
image.Scale(0.6f, 0.6f);
newCell.Add(image);
newCell = ApplyLayoutsToCell(newCell, cellStyles);
table.AddCell(newCell);
}
PDF Page Example
The PDF is composed by serveral paragraphs added to the document, and there is a long table which need to set started from top of one page(not the first page) which means a paragraph will be splitted to two parts.
If the table is short(length not exceed the page height), it can be done gracefully by handling Page Event of start page, just added the table to the canvas with the fixed position and size, and set the document's top margin.
PdfDocumentEvent docEvent = (PdfDocumentEvent)currentEvent;
PdfDocument pdfDoc = docEvent.GetDocument();
PdfPage page = docEvent.GetPage();
var currentPageNumber = pdfDoc.GetPageNumber(page);
var addingTablesOfPage = addingTables.Where(t => t.PageNumber == currentPageNumber && t.Prepared).ToList();
if (addingTablesOfPage != null && addingTablesOfPage.Count > 0)
{
PdfCanvas canvas = new PdfCanvas(page.NewContentStreamBefore(), page.GetResources(), pdfDoc);
PageSize pageSize = pdfDoc.GetDefaultPageSize();
var pageHeight = pageSize.GetHeight() - Constants.DocumentMargins[0] - Constants.DocumentMargins[2];
var pageWidth = pageSize.GetWidth() - Constants.DocumentMargins[1] - Constants.DocumentMargins[3];
var totalHeight = 0f;
var alignRight = false;
// add tables to the top of page
foreach (var table in addingTablesOfPage.OrderBy(t => t.Type).ToList())
{
var tableWidth = 0f;
float coordX = 0;
tableWidth = pageWidth;
coordX = pageSize.GetX() + doc.GetLeftMargin();
totalHeight += table.TableHeight;
float coordY = pageSize.GetTop() - Constants.DocumentMargins[0] - totalHeight;
Rectangle rect = new Rectangle(coordX, coordY, tableWidth, table.TableHeight);
var tableCanvas = new Canvas(canvas, rect);
tableCanvas.Add(table.Table);
tableCanvas.Close();
}
float topMargin = Constants.DocumentMargins[0] + totalHeight;
doc.SetTopMargin(topMargin);
}
else
{
doc.SetTopMargin(Constants.DocumentMargins[0]);
}
But here the table is too long which will be splitted to multi pages. As far as I known, Canvas class is primarily aimed at cases when you need to add elements to a specific predefined area on a page / XObject and it is not aimed at overflowing your content to next areas. So how can I achieve the behavior?
Thank you!
The description of what you are trying to achieve is quite vague (not clear if it's paragraph wrapping around table, or if it's a table in between paragraph; not clear if the table is bound to any text in paragraph or not etc etc), but my answer should guide you to the right direction.
I will show you how to add the paragraph part that fits till the end of the current page to the document, then add your table spanning across more than one page, and then add the leftover part of your paragraph.
First off, we need a custom renderer for our paragraph that will only render the part that currently fits in the page and will remember the rest to be added later.
private static class CustomParagraphRenderer extends ParagraphRenderer {
public CustomParagraphRenderer leftover;
private CustomParagraphRenderer toDraw;
public CustomParagraphRenderer(Paragraph modelElement) {
super(modelElement);
}
#Override
public LayoutResult layout(LayoutContext layoutContext) {
LayoutResult result = super.layout(layoutContext);
if (result.getStatus() == LayoutResult.PARTIAL) {
// Expected result here is that paragraph splits across pages
leftover = (CustomParagraphRenderer) result.getOverflowRenderer();
toDraw = (CustomParagraphRenderer) result.getSplitRenderer();
return new LayoutResult(LayoutResult.FULL, result.getSplitRenderer().getOccupiedArea(), null, null);
}
return result;
}
#Override
public void draw(DrawContext drawContext) {
if (toDraw != null) {
toDraw.draw(drawContext);
} else {
super.draw(drawContext);
}
}
#Override
public IRenderer getNextRenderer() {
return new CustomParagraphRenderer((Paragraph) modelElement);
}
}
Now, we are adding some placeholder rectangle to the document just to occupy some space for the testing purposes, then we add our paragraph with our custom renderer (note that the expectation is that the paragraph will be rendered as much as possible on the current page, and the leftover part will be remembered), then we add our table spanning across multiple pages, and finally, we add our leftover paragraph part.
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName));
Document document = new Document(pdfDocument);
document.setFontSize(20);
Div emptyArea = new Div().setHeight(400).setBackgroundColor(ColorConstants.RED);
document.add(emptyArea);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 500; i++) {
sb.append(i).append(" ");
}
Paragraph p = new Paragraph(sb.toString());
p.setNextRenderer(new CustomParagraphRenderer(p).setParent(document.getRenderer()));
CustomParagraphRenderer paragraphRenderer = (CustomParagraphRenderer) p.createRendererSubTree();
// Add first part of the paragraph
document.getRenderer().addChild(paragraphRenderer);
Table table = new Table(3);
for (int i = 0; i < 25; i++) {
for (int j = 0; j < 3; j++) {
table.addCell(new Cell().add(new Paragraph("Cell (" + i + ", " + j + ")")));
}
}
// Add big table
document.add(table);
// Add leftover paragraph part
document.getRenderer().addChild(paragraphRenderer.leftover.setParent(document.getRenderer()));
document.close();
Result looks as follows. Note that I deliberately added consequent numbers as paragraph content so that it's clear that it's a continuous paragraph that is interrupted with a table:
I am currently trying to iterate over an existing PDF and stamp each page with some footer text using the OnPageEnd event as detailed in the iText documentation, Chapter 5: Table, cell, and page events.
When I assign my new custom event class to the PdfCopy instance, I receive this exception:
"Operation is not valid due to the current state of the object" at
iTextSharp.text.pdf.PdfCopy.set_PageEvent(IPdfPageEvent value)
Below is the code I have written to preform the operation:
PdfReader pdf = new PdfReader(file.Value);
int pages = pdf.NumberOfPages;
pdf.SelectPages(string.Format("0 - {0}", pages));
using (MemoryStream stream = new MemoryStream())
{
Document doc = new Document();
PdfCopy copy = new PdfCopy(doc, stream) { PageEvent = new PdfFooterStamp() };
doc.Open();
for (int x = 0, y = pages; x < y; x++)
{
copy.AddPage(copy.GetImportedPage(pdf, x + 1));
}
doc.Close();
copy.Flush();
copy.Close();
collection[file.Key] = stream.ToArray();
}
And this is my custom event class definition:
public class PdfFooterStamp : PdfPageEventHelper
{
public override void OnEndPage(PdfWriter writer, Document document)
{
Rectangle rect = writer.PageSize;
ColumnText.ShowTextAligned(writer.DirectContent,
Element.ALIGN_CENTER, new Phrase("PERSONALISED DOCUMENT"),
(rect.Left + rect.Right) / 2, rect.Bottom - 18, 0);
base.OnEndPage(writer, document);
}
}
Is there anyone that might have an idea as to what might be going wrong?
Following #BrunoLowagie's advice, I went with the former option to create a PageStamp from an imported page and alter it's content as I iterated over the collection of imported PDF.
You can keep on using PdfCopy and use PageStamp to add the text to
each page that is added. Or you can create the PDF in two passes:
first create the concatenated PDF in memory with PdfCopy; then add the
footer with PdfStamper in a second pass.
The reason my previous attmepts had not worked was due to the fact that,
PdfPageEventHelper and PdfCopy are mutually exclusive. You can't
define a page event when using PdfCopy - #BrunoLowagie
The following code is an example of the preferred solution and tests have proven it to work as intended.
Document doc = new Document();
PdfCopy copy = new PdfCopy(doc, stream);
doc.Open();
for (int x = 0, y = pages; x < y; x++)
{
PdfImportedPage import = copy.GetImportedPage(pdf, x + 1);
PageStamp stamp = copy.CreatePageStamp(import);
Rectangle rect = stamp.GetUnderContent().PdfWriter.PageSize;
ColumnText.ShowTextAligned(stamp.GetUnderContent(),
Element.ALIGN_CENTER, new Phrase(User.Identity.Name, font),
(rect.Bottom + rect.Top) / 2, rect.Bottom + 8, 0);
stamp.AlterContents();
copy.AddPage(import);
}
My requirement is to print the details in landscape,not the page rotation.i need content printed in landscape.
Document doc = new Document(iTextSharp.text.PageSize.A4, (float)MarginLeft, (float)MarginRight, (float)MarginTop, (float)MarginBottom);
//210 mm width * 297 mm height
PaperWidthAvailable = iTextSharp.text.Utilities.MillimetersToPoints(210f) - ((float)MarginLeft + (float)MarginRight);
PaperHeightAvailable = iTextSharp.text.Utilities.MillimetersToPoints(297f) - ((float)MarginTop + (float)MarginBottom);
wdtOFcell = (float)BarcodeWidth + (float)BarcodeSpaceHorizontal;
colNo = (int)Math.Floor(PaperWidthAvailable / wdtOFcell);
TableWidth = wdtOFcell * colNo;
htOFcell = (float)BarcodeHeight + (float)BarcodeSpaceVertical;
PdfWriter writer = PdfWriter.GetInstance(doc, memStream);
doc.Open();
int noOfColumns = colNo;
// int additionalRow = imageBarcodeLists.Count % noOfColumns;
int i = 1;
PdfPTable table = new PdfPTable(noOfColumns);
table.DefaultCell.Border = iTextSharp.text.Rectangle.NO_BORDER;
table.HorizontalAlignment = 0;
table.TotalWidth = TableWidth;
table.LockedWidth = true;
float[] widths = new float[colNo];
for (int j = 0; j < colNo; j++)
{
widths[j] = wdtOFcell;
}
table.SetWidths(widths);
iTextSharp.text.Image itextBarcodeImage = null;
foreach (System.Drawing.Image barcodeImage in imageBarcodeLists)
{
var imageCompressor = new ImageCompressionUtility();
System.Drawing.Image barcodeImages = imageCompressor.TrimImageWhiteSpacesFromImage(barcodeImage);
itextBarcodeImage = iTextSharp.text.Image.GetInstance(barcodeImages, BaseColor.BLUE);
itextBarcodeImage.ScaleAbsolute((float)BarcodeWidth, (float)BarcodeHeight);
PdfPCell cells = new PdfPCell(itextBarcodeImage);
cells.Border = iTextSharp.text.Rectangle.NO_BORDER;
cells.PaddingTop = 0f;
cells.PaddingRight = 0f;
cells.PaddingBottom = 0f;
cells.PaddingLeft = 0f;
cells.UseAscender = true;
cells.FixedHeight = htOFcell;
cells.BackgroundColor = BaseColor.WHITE;
cells.Border = iTextSharp.text.Rectangle.NO_BORDER;
table.AddCell(cells);
i++;
}
doc.Add(table);
doc.Close();
The constant iTextSharp.text.PageSize.A4 is actually a Rectangle that is created like this:
public static readonly Rectangle A4 = new Rectangle(595,842);
If you want to rotate the page, you can use the Rotate() method as explained in my answer to this question: How to print custom page size as portrait in itextsharp
However, based on your comment "it rotates only the paper not the content written", you may be looking for a page size that is created like this:
Rectangle myA4 = new Rectangle(842,595);
Document doc = new Document(myA4);
If that doesn't work, please take a look at my answer to the question iText - Rotate page content while creating PDF
In that answer, I introduce a page rotation using a page event:
public class MyPdfPageEvent : iTextSharp.text.pdf.PdfPageEventHelper
{
public override void OnEndPage(PdfWriter writer, Document document)
{
writer.AddPageDictEntry(PdfName.ROTATE, PdfPage.SEASCAPE);
}
}
If none of the above has the effect you desire, you should improve your question (or it will be closed as "unclear what is asked").
I am using the below code to add watermark to my pdf.
private void Merge(List<string> src, string dest)
{
iTextKernel.PdfWriter writer = new iTextKernel.PdfWriter(dest);
iTextKernel.PdfDocument pdfDocument1 = new iTextKernel.PdfDocument(new iTextKernel.PdfReader(src[0]), writer);
pdfDocument1.AddEventHandler(PdfDocumentEvent.END_PAGE, new WatermarkingEventHandler());
for (int i = 1, max = src.Count; i < max; i++)
{
iTextKernel.PdfDocument pdfDocument2 = new iTextKernel.PdfDocument(new iTextKernel.PdfReader(src[i]));
var pagesCount = pdfDocument2.GetNumberOfPages();
pdfDocument2.CopyPagesTo(1, pagesCount, pdfDocument1);
pdfDocument2.Close();
}
pdfDocument1.Close();
protected class WatermarkingEventHandler : IEventHandler {
public void HandleEvent(Event e) {
PdfDocumentEvent docEvent = (PdfDocumentEvent) e;
iTextKernel.PdfDocument pdfDoc = docEvent.GetDocument();
iTextKernel.PdfPage page = docEvent.GetPage();
iText.Kernel.Font.PdfFont font = null;
try {
font = PdfFontFactory.CreateFont(FontConstants.HELVETICA_BOLD);
} catch (IOException ex) {
//_log.Error(ex);
}
PdfCanvas canvas = new PdfCanvas(page.NewContentStreamBefore(), page.GetResources(), pdfDoc);
new Canvas(canvas, pdfDoc, page.GetPageSize())
.SetFontColor(iText.Kernel.Colors.DeviceGray.LIGHT_GRAY)
.SetFontSize(60)
.SetFont(font)
.ShowTextAligned(new Paragraph("FOR YOUR RECORDS ONLY: DO NOT SUBMIT"), 298, 421, pdfDoc.GetPageNumber(page),
TextAlignment.CENTER, VerticalAlignment.MIDDLE, 45);
}
But am getting the watermark only in the last page that too hidden under the contents. Could you please modify this code so that i could get the watermark on all the pages and shown over the contents.
Please take a look at the iText 7 for C# jump-start tutorial, more specifically Chapter 5: Manipulating an existing PDF document. Scroll to the part where it says: "Adding a header, footer, and watermark" and look at the example:
PdfDocument pdfDoc = new PdfDocument(new PdfReader(src), new PdfWriter(dest));
Document document = new Document(pdfDoc);
Rectangle pageSize;
PdfCanvas canvas;
int n = pdfDoc.GetNumberOfPages();
for (int i = 1; i <= n; i++) {
PdfPage page = pdfDoc.GetPage(i);
pageSize = page.GetPageSize();
canvas = new PdfCanvas(page);
//Draw header text
}
pdfDoc.close();
As you can see, we only need one PdfDocument instance, but instead of passing only a PdfWriter, we also pass a PdfReader instance. We will read the file with path src and we will write to a file with path dest.
You want to add content to each page. This means that you have to loop over each page (from 1 to n). Get the PdfPage object for each page i and replace the line //Draw header text with whatever it is you want to do.
In your case, you add an image underneath the existing content. That is the normal thing to do, but you say that the watermark is covered by the existing content. That happens for instance when the actual content consists of images (e.g. scanned pages). If you add a watermark under the pages of a PDF that consists of scanned pages, you will never see the watermark.
In that case, you have to add the content on top of the existing content, but it is best to make the watermark transparent:
Paragraph p = new Paragraph("FOR YOUR RECORDS ONLY: DO NOT SUBMIT").SetFontSize(60);
canvas.SaveState();
PdfExtGState gs1 = new PdfExtGState().SetFillOpacity(0.2f);
canvas.SetExtGState(gs1);
document.ShowTextAligned(p, pageSize.GetWidth() / 2, pageSize.GetHeight() / 2, pdfDoc.GetPageNumber(page), TextAlignment.CENTER, VerticalAlignment.MIDDLE, 45);
canvas.RestoreState();
Note that in the tutorial, we are using pageSize.GetWidth() / 2 and pageSize.GetHeight() / 2 as coordinates, which means that we assume that the lower-left corner of the page has the coordinate (0, 0). That may not be the case. You may have to add the x-offset and the y-offset to that value.