iTextsharp custom border - c#

i want know how can i append the custom border to the iTextSharp pdfdcell.
like
the problem is that i cant find a way to add border with 2 line.
i can only create a normal bottom border in the pdfdcell by
PdfPCell tmp = new PdfPCell(new Phrase(c));
tmp.HorizontalAlignment = Element.ALIGN_RIGHT;
tmp.Border = c.borderPosition;
tmp.BorderWidth = c.borderWidth;
pt.AddCell(tmp);
so the result is something like this
but i need to add one more line under the border.

Since I am using C# and iTextSharp and the comment is only showing the solution on Java.
I have implemented the similar thing to solve the issue.
The basic fact is that iTextSharp didn't support custom border, but it allows you to draw things on the PDF. Thus the objective is to draw a doubleline at the bottom of the cell.
hide existing border
find out the exact position of the cell
draw lines
the trick is that implementing the CellEvent on the cell, within the cellevent it gave us the exact position of the cell, thus we draw things easily.
below is the code which working in my C# project
public function void DrawACell_With_DOUBLELINE_BOTTOM_BORDER(Document doc, PdfWriter writer){
PdfPTable pt = new PdfPTable(new float[]{1});
Chunk c = new Chunk("A Cell with doubleline bottom border");
int padding = 3;
PdfPCell_DoubleLine cell = new PdfPCell_DoubleLine(PdfPTable pt,new Phrase(c), writer, padding);
pt.AddCell(cell);
doc.Add(pt);
}
public class PdfPCell_DoubleLine : PdfPCell
{
public PdfPCell_DoubleLine(Phrase phrase, PdfWriter writer, int padding) : base(phrase)
{
this.HorizontalAlignment = Element.ALIGN_RIGHT;
//1. hide existing border
this.Border = Rectangle.NO_BORDER;
//2. find out the exact position of the cell
this.CellEvent = new DLineCell(writer, padding);
}
public class DLineCell : IPdfPCellEvent
{
public PdfWriter writer { get; set; }
public int padding { get; set; }
public DLineCell(PdfWriter writer, int padding)
{
this.writer = writer;
this.padding = padding;
}
public void CellLayout(PdfPCell cell, iTextSharp.text.Rectangle rect, PdfContentByte[] canvases)
{
//draw line 1
PdfContentByte cb = writer.DirectContent;
cb.MoveTo(rect.GetLeft(0), rect.GetBottom(0) - padding);
cb.LineTo(rect.GetRight(0), rect.GetBottom(0) - padding);
//draw line 2
cb.MoveTo(rect.GetLeft(0), rect.GetBottom(0) - padding - 2);
cb.LineTo(rect.GetRight(0), rect.GetBottom(0) - padding - 2);
cb.Stroke();
}
}
}

Related

Setting margins, or cell spacing, between a grid of images in iTextSharp PdfPTable

I am trying to render a PDF with a grid of images, using iTextSharp (v5.5.10). The images will all have the same dimensions and should be evenly distributed on one page.
However, with the code mentioned below, I'm having difficulty in setting the appropriate margins, or spacing between the cells.
Visually this means that the expected result is this:
The yellow highlighted lines are the problem where I'm getting the following result instead:
Notice how there are no spaces between the images? This is based on my following code:
public void CreateGridOfImages(string outputFilePath)
{
// note: these constants are in millimeters (mm),
// which are converted using the ToPoints() helper later on
const float spacingBetweenCells = 7;
const float imageWidth = 80;
const float imageHeight = 80;
const string[] images = new [] { "a.jpg", "b.jpg", "c.jpg" };
using (var stream = new FileStream(outputFilePath, FileMode.CreateNew, FileAccess.Write, FileShare.None))
{
var document = new iTextSharp.text.Document(PageSize.B2, 0f, 0f, 0f, 0f);
var writer = PdfWriter.GetInstance(document, stream);
try
{
document.Open();
var table = new PdfPTable(5);
table.DefaultCell.Border = Rectangle.NO_BORDER;
foreach (var imagePath in images)
{
var img = iTextSharp.text.Image.GetInstance(imagePath);
img.ScaleToFit(ToPoints(imageWidth), ToPoints(imageHeight));
var cell = new PdfPCell();
// THIS IS THE PROBLEM... HOW TO SET IMAGE SPACING?
var cellMargin = ToPoints(spacingBetweenCells);
cell.AddElement(img);
table.AddCell(cell);
}
document.Add(table);
}
catch (Exception ex)
{
throw ex;
}
finally
{
document.Close();
}
}
}
private float ToPoints(float millimeters)
{
// converts millimeters to points
return iTextSharp.text.Utilities.MillimetersToPoints(millimeters);
}
Now this seems trivial. And it problably is, but I've tried several options and could none of them to work properly (or at all):
Adding Paragraph() objects with paddings between each
Adding Padding to PdfPCell does not seem to work for me
Looked into custom IPdfPCellEvent samples
Absolutely positioning the images alltogether (forgetting the PdfPTable)
My hunch is that the IPdfPCellEvent seems the right approach. But all the iText options and variations are simply overwhelming.
Summarized, does anyone know how can I properly set the margins/spacing between the cells?
I assume you want to have the second table in this image:
The only way to create white spacing between cells in an iText table is to set the relevant borders to the background colour and play with the padding of the cell. My pivotal code for creating the cells is:
for(int i = 0; i < nrCols* nrRows;i++) {
var img = Image.GetInstance(imagePath);
img.ScaleToFit(ToPoints(imageWidth), ToPoints(imageHeight));
//Create cell
var imageCell = new PdfPCell();
imageCell.Image = img;
imageCell.Border = Rectangle.BOX;
imageCell.BorderColor = useColor? BaseColor.YELLOW : BaseColor.WHITE;
//Play with this value to change the spacing
imageCell.Padding = ToPoints(spacingBetweenCells/2);
imageCell.HorizontalAlignment = Element.ALIGN_CENTER;
grid.AddCell(imageCell);
}
As to why adding padding to the PdfPCell didn't seem to worrk:
The reason why borders are still drawn in your example, despite
table.DefaultCell.Border = Rectangle.NO_BORDER;
Is because you never use the default cell because you create a custom one with var cell = new PdfPCell(); and pass the custom one to table.AddCell(cell);. If you'd have used table.addCell(img), the border would not have been there (although your padding would still not be your desired spacing, since it isn't set on the default cell).

Watermark on pdf not working properlly

I am creating pdf using itextsharp. Now I want to add watermark on each pages I tried method on
public override void OnStartPage(PdfWriter wr, iTextSharp.text.Document doc)
Now watermark is coming but if pdf page containing image then its hiding watermark
public class itsevent : PdfPageEventHelper
{
string watermarkText = string.Empty;
public itsevent(string watermark)
{
watermarkText = watermark;
}
public override void OnStartPage(PdfWriter wr, iTextSharp.text.Document doc)
{
float fontSize = 80;
float xPosition = 300;
float yPosition = 300;
float angle = 45;
PdfContentByte u = wr.DirectContentUnder;
PdfGState gs = new PdfGState();
u.SaveState();
u.SetGState(gs);
BaseFont baseFont =BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
u.BeginText();
u.SetColorFill(iTextSharp.text.pdf.CMYKColor.LIGHT_GRAY);
u.SetFontAndSize(baseFont, fontSize);
u.ShowTextAligned(PdfContentByte.ALIGN_CENTER, watermarkText, xPosition, yPosition, angle);
u.EndText();
under.RestoreState();
}
}
itsevent ev = new itsevent("watermark");
pdfW.PageEvent = ev;
doc.Open();
Which PdfContentByte to use
You use wr.DirectContentUnder which is for adding content under the normally added content:
PdfContentByte u = wr.DirectContentUnder;
Thus, obviously an image in the normal content will cover the watersign. Use wr.DirectContent instead.
Which page event to use
You are adding the watermark in OnStartPage; iText developers recommend against doing so. Instead do that in OnEndPage.
How to improve the looks
You might want to apply transparency by setting the fill opacity of PdfGState gs.
Try using GetOverContext() to get the image
https://simpledotnetsolutions.wordpress.com/2012/04/08/itextsharp-few-c-examples/

How to handle the case in which an iText\iTextSharp table is split into two pages?

I have the following problem using iTextSharp.
I am putting some tables into my document. The problem is that if a table content not fit on a page and go into another page I have that it overwrite the second page header, so I have the following situation:
As you can see I am inserting a table at the end of a page and this is split into two pages and the second page header is overwrite by the table content.
I want to avoid this situation but I don't know how have I to do.
I am thinking that maybe I can do one of the following things:
Maybe I can check if an element completely enter into a page. If not create a new page and put it into this new page (but this can be a problem if a single table need more then one page, in the case if I have a very big table)
I allow that a table is split across 2 pages but in this case I left some margin spacing in the upper of the second page so the header is correctly shown.
Or what can I do to solve this situation?
Tnx
EDIT:
I have add the header in the following way:
I have implement create a class named PdfHeaderFooter that extends the PdfPageEventHelper class and that implements its methods.
At this time its class contains the following methods:
// write on start of each page
public override void OnStartPage(PdfWriter writer, Document document)
{
base.OnStartPage(writer, document);
PdfPTable tabHead = new PdfPTable(3);
tabHead.SetWidths(new int[] { 165, 205, 125 });
//tabHead.TotalWidth = 460F;
tabHead.TotalWidth = document.Right - document.Left; // TotalWidth = 495
tabHead.WidthPercentage = 98;
PdfPCell cell1 = new PdfPCell(iTextSharp.text.Image.GetInstance(folderImages + "logoEarlyWarning.png"), true) { Border = PdfPCell.BOTTOM_BORDER };
tabHead.AddCell(cell1);
//tabHead.AddCell(new PdfPCell(new Phrase("CELL 1:")) { Border = PdfPCell.BOTTOM_BORDER, Padding = 5, MinimumHeight = 50, PaddingTop = 15, });
tabHead.AddCell(new PdfPCell(new Phrase("")) { Border = PdfPCell.BOTTOM_BORDER, Padding = 5, MinimumHeight = 50, PaddingTop = 15 });
if(_sourceId == "NVD")
{
iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance(folderImages + "nvdLogo.png");
logo.ScalePercent(48f);
//PdfPCell cell3 = new PdfPCell(iTextSharp.text.Image.GetInstance(folderImages + "nvdLogo.png"), true) { Border = PdfPCell.BOTTOM_BORDER, PaddingBottom = 25 };
PdfPCell cell3 = new PdfPCell(logo) { Border = PdfPCell.BOTTOM_BORDER, PaddingLeft = 50 };
tabHead.AddCell(cell3);
}
else if(_sourceId == "DeepSight")
{
PdfPCell cell3 = new PdfPCell(iTextSharp.text.Image.GetInstance(folderImages + "DSLogo.jpg"), true) { Border = PdfPCell.BOTTOM_BORDER };
tabHead.AddCell(cell3);
}
//tabHead.AddCell(new PdfPCell(new Phrase("CELL 3:")) { Border = PdfPCell.BOTTOM_BORDER, Padding = 5, MinimumHeight = 50, PaddingTop = 15 });
tabHead.WriteSelectedRows(0, -1, document.Left, document.Top, writer.DirectContent);
}
// write on end of each page
public override void OnEndPage(PdfWriter writer, Document document)
{
base.OnEndPage(writer, document);
int pageN = writer.PageNumber; // numero della pagina corrente OK
String text = "Page " + pageN + " of ";
float len = bf.GetWidthPoint(text, 8);
Rectangle pageSize = document.PageSize;
cb.SetRGBColorFill(100, 100, 100);
cb.BeginText();
cb.SetFontAndSize(bf, 8);
cb.SetTextMatrix(pageSize.GetLeft(40), pageSize.GetBottom(30));
cb.ShowText(text);
cb.EndText();
cb.AddTemplate(template, pageSize.GetLeft(40) + len, pageSize.GetBottom(30));
cb.BeginText();
cb.SetFontAndSize(bf, 8);
cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHT,
"Printed On " + PrintTime,
pageSize.GetRight(40),
pageSize.GetBottom(30), 0);
cb.EndText();
}
So ad you can see, the OnStartPage() method add the header at the beginning of each pages and the OnEndPage() add the footer at the end of each pages.
So from what I understand from your response I have to do the followings steps:
Move the header insertion from OnStartPage() to OnEndPage()
Use an absolute position for place it in the upper part of the pages.
In the document creation use the header height to set the top margin.
Is it right?
EDIT 2:
I tried to do as you say to me and now I have the following situations:
Into my PdfHeaderFooter that extends PdfPageEventHelper I have deleted the OnStartPage() method
I have moved the header table creation into the OnEndPage() method that now it this one:
// write on end of each page
public override void OnEndPage(PdfWriter writer, Document document)
{
base.OnEndPage(writer, document);
// HEADER:
PdfPTable tabHead = new PdfPTable(3);
tabHead.SetWidths(new int[] { 165, 205, 125 });
//tabHead.TotalWidth = 460F;
tabHead.TotalWidth = document.Right - document.Left; // TotalWidth = 495
tabHead.WidthPercentage = 98;
PdfPCell cell1 = new PdfPCell(iTextSharp.text.Image.GetInstance(folderImages + "logoEarlyWarning.png"), true) { Border = PdfPCell.BOTTOM_BORDER };
tabHead.AddCell(cell1);
//tabHead.AddCell(new PdfPCell(new Phrase("CELL 1:")) { Border = PdfPCell.BOTTOM_BORDER, Padding = 5, MinimumHeight = 50, PaddingTop = 15, });
tabHead.AddCell(new PdfPCell(new Phrase("")) { Border = PdfPCell.BOTTOM_BORDER, Padding = 5, MinimumHeight = 50, PaddingTop = 15 });
if (_sourceId == "NVD")
{
iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance(folderImages + "nvdLogo.png");
logo.ScalePercent(48f);
//PdfPCell cell3 = new PdfPCell(iTextSharp.text.Image.GetInstance(folderImages + "nvdLogo.png"), true) { Border = PdfPCell.BOTTOM_BORDER, PaddingBottom = 25 };
PdfPCell cell3 = new PdfPCell(logo) { Border = PdfPCell.BOTTOM_BORDER, PaddingLeft = 50 };
tabHead.AddCell(cell3);
}
else if (_sourceId == "DeepSight")
{
PdfPCell cell3 = new PdfPCell(iTextSharp.text.Image.GetInstance(folderImages + "DSLogo.jpg"), true) { Border = PdfPCell.BOTTOM_BORDER };
tabHead.AddCell(cell3);
}
//tabHead.AddCell(new PdfPCell(new Phrase("CELL 3:")) { Border = PdfPCell.BOTTOM_BORDER, Padding = 5, MinimumHeight = 50, PaddingTop = 15 });
tabHead.WriteSelectedRows(0, -1, document.Left, document.Top, writer.DirectContent);
float headerHeight = tabHead.CalculateHeights();
// FOOTER:
int pageN = writer.PageNumber; // numero della pagina corrente OK
String text = "Page " + pageN + " of ";
float len = bf.GetWidthPoint(text, 8);
Rectangle pageSize = document.PageSize;
cb.SetRGBColorFill(100, 100, 100);
cb.BeginText();
cb.SetFontAndSize(bf, 8);
cb.SetTextMatrix(pageSize.GetLeft(40), pageSize.GetBottom(30));
cb.ShowText(text);
cb.EndText();
cb.AddTemplate(template, pageSize.GetLeft(40) + len, pageSize.GetBottom(30));
cb.BeginText();
cb.SetFontAndSize(bf, 8);
cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHT,
"Printed On " + PrintTime,
pageSize.GetRight(40),
pageSize.GetBottom(30), 0);
cb.EndText();
}
As you can see now the OnEndPage() method contains the header and footer creation.
It seems to me that tabHead (my header table) use the absolute positioning because I have:
tabHead.WriteSelectedRows(0, -1, document.Left, document.Top, writer.DirectContent);
In the other class where I create the document I have this line:
document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 50, 50, 30, 65);
so the pages are A4 and have 30px as upper margin.
But I still have the same problem.
If I change the 30 value with 80 it simply moves down the header leaving a white top but does not solve the problem.
What am I missing? What is wrong?
I assume that you are adding page headers correctly. That is: you have implemented the onEndPage() method in a page event (not the onStartPage() method) and you're adding the header at an absolute position. As you are adding the header at an absolute position, you know exactly how much space it takes.
When you create a document object, you define a page size. If you don't, the page size will be A4 (595 x 842 user units). You also define margins. If you don't, the margins will be half an inch (36 user units) on either side.
When a table splits, the page size and its margins are respected: iText won't put any part of the table in that area.
Hence the solution is simple: as you know the space needed by the header, all you have to do is to define the margin in a way that the header fits into the margin.
Update after the question was updated:
1.
This is wrong:
// write on start of each page
public override void OnStartPage(PdfWriter writer, Document document)
{
...
tabHead.WriteSelectedRows(0, -1, document.Left, document.Top, writer.DirectContent);
}
You should never add any content in the OnStartPage() method. Move the code that writes the header to the OnEndPage() method.
2.
You are already using an absolute position to add tabHead: you are adding it at the coordinate x = document.left; y = document.Top.
You define a width percentage for a file that is added at an absolute position. That doesn't make any sense, remove the following line:
tabHead.WidthPercentage = 98;
However, you are wasting resources. For instance: you create the logo in the page event: iTextSharp.text.Image.GetInstance(folderImages + "nvdLogo.png"). This means that you are adding the image bytes as many times as you have pages, resulting in redundant info in the PDF file (you have a bloated file).
You can optimize the process by creating the images outside the onEndPage() method. You can even create the table outside that method. For instance: create a member variable tabHead in the event class and create the table either in the constructor of your event implementation or in the OnOpenDocument() method (that method only gets called once). Reuse the table (and the images) in the onEndPage() method.
Now the image bytes will be present in the PDF file only once (you'll gain plenty of KBytes) and you'll only have to create the table once (less CPU-time wasted).
3.
An even better solution is to create the tabHead object outside the page event and before you open the document. As you define a total width, you can ask the table for its height:
float h = tabHead.TotalHeight;
Now you can use h to define the top margin:
document.SetMargins(36f, 36f, h, 36f);
Note that it is important to set the margins before you open your document. You'll also have to adapt the coordinate at which you add the table. For instance:
tabHead.WriteSelectedRows(0, -1, document.Left, document.Top + h, writer.DirectContent);
Your comment regarding the position of the header revealed a serious lack of insight.

itextsharp pdf page border?

I am using itext sharp for creating reports in PDF format.
I want page borders. I tried some ways. I am not successful.
How can I get a page border for top, bottom, left, right using iText for .NET?
I added one image 1. I want borders like described in the image.
You can try this code for adding the image for the header manually.
//Step 1: Add the Image file
strImgPath is refer the directory Info..
Image imgLogo = Image.GetInstance(strImgPath.ToString()+"\\abcdur compe.Jpg");
imgLogo.Alignment = Image.ALIGN_CENTER;
imgLogo.ScalePercent(50f);
// Step 2:
Add this ImgLogo to the PdfPTable by use of this
PdfPCell pdfcellImage = new PdfPCell(imgLogo, true);
pdfcellImage.FixedHeight = 40f;
pdfcellImage.HorizontalAlignment = Element.ALIGN_CENTER;
pdfcellImage.VerticalAlignment = Element.ALIGN_CENTER;
pdfcellImage.Border = Rectangle.NO_BORDER;
pdfcellImage.Border = Rectangle.NO_BORDER;
pdftblImage.AddCell(pdfcellImage);
// Step 3:
Create Chunck to add Text for address or others
fntBoldComHd is a Base Font Library Object
Chunk chnCompany = new Chunk("Your CompanyName\nAddress", fntBoldComHd);
//Step 4:
Create Phrase For add the Chunks and PdfPTables
Phrase phHeader = new Phrase();
phHeader.Add(pdftblImage);
phHeader.Add(chnCompany);
// Step 5:
Assign the Phrase to PDF Header
HeaderFooter header = new HeaderFooter(phHeader, false);
header.Border = Rectangle.NO_BORDER;
header.Alignment = Element.ALIGN_CENTER;
docPDF.Header = header;
I am using one hacky way of workaround but it will create the border.Use this method.
private void CreateBorder(PdfContentByte cb, PdfWriter writer, Document doc)
{
iTextSharp.text.Rectangle r = doc.PageSize;
float left = r.Left + 30;
float right = r.Right - 30;
float top = r.Top - 30;
float bottom = r.Bottom + 30;
float width = right - left;
float height = top - bottom;
PdfPTable tab = new PdfPTable(1);
tab.TotalWidth = width;
tab.LockedWidth = true;
PdfPCell t = new PdfPCell(new Phrase(String.Empty));
t.BackgroundColor = new BaseColor(250, 235, 215);
t.FixedHeight = height;
t.BorderWidth = 3;
tab.AddCell(t);
Paragraph pa = new Paragraph();
pa.Add(tab);
float h = tab.TotalHeight;
PdfTemplate temp = cb.CreateTemplate(tab.TotalWidth, h);
tab.WriteSelectedRows(0, -1, 0.0F, h, temp);
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(temp);
img.SetAbsolutePosition(30, 30);
cb.AddImage(img);
}
If u want one more section for header create table width two rows.I hope this helps.
Are you referring to the document margins? If yes, use the Document object's ctor to specify them:
public Document(Rectangle pageSize, float marginLeft, float marginRight, float marginTop, float marginBottom);

iTextSharp PageNumber but not on first and last pages

I am trying to show the page number of my PDF pages, but I don`t want to show it on first and last pages, because they are covers.
I use this code:
public class PDFPage : iTextSharp.text.pdf.PdfPageEventHelper
{
//I create a font object to use within my footer
protected iTextSharp.text.Font footer
{
get
{
// create a basecolor to use for the footer font, if needed.
iTextSharp.text.Color grey = new iTextSharp.text.Color(40, 40, 40);
Font font = FontFactory.GetFont("Arial", 16, iTextSharp.text.Font.BOLD, grey);
return font;
}
}
//override the OnPageEnd event handler to add our footer
public override void OnEndPage(PdfWriter writer, Document doc)
{
if (doc.PageNumber > 1)
{
//I use a PdfPtable with 2 columns to position my footer where I want it
PdfPTable footerTbl = new PdfPTable(2);
//set the width of the table to be the same as the document
footerTbl.TotalWidth = doc.PageSize.Width;
//Center the table on the page
footerTbl.HorizontalAlignment = Element.ALIGN_CENTER;
//Create a paragraph that contains the footer text
Paragraph para = new Paragraph(" ", footer);
//add a carriage return
para.Add(Environment.NewLine);
para.Add(" ");
//create a cell instance to hold the text
PdfPCell cell = new PdfPCell(para);
//set cell border to 0
cell.Border = 0;
//add some padding to bring away from the edge
cell.PaddingLeft = 10;
//add cell to table
footerTbl.AddCell(cell);
//create new instance of Paragraph for 2nd cell text
para = new Paragraph(" " + doc.PageNumber, footer);
//create new instance of cell to hold the text
cell = new PdfPCell(para);
//align the text to the right of the cell
cell.HorizontalAlignment = Element.ALIGN_LEFT;
//set border to 0
cell.Border = 0;
// add some padding to take away from the edge of the page
cell.PaddingRight = 10;
//add the cell to the table
footerTbl.AddCell(cell);
//write the rows out to the PDF output stream.
footerTbl.WriteSelectedRows(0, -1, 0, (doc.BottomMargin + 25), writer.DirectContent);
}
}
}
Thank you!
(writer.PageNumber - 1) is the last page. so check that too.
public class PageEventHelper : PdfPageEventHelper
{
PdfContentByte cb;
PdfTemplate template;
private int TotalNumber = 0;
//for remeber lastpage
public void SetNumber(int num)
{
TotalNumber = num;
}
public override void OnOpenDocument(PdfWriter writer, Document document)
{
cb = writer.DirectContent;
template = cb.CreateTemplate(50, 50);
}
public override void OnEndPage(PdfWriter writer, Document document)
{
base.OnEndPage(writer, document);
cb = writer.DirectContent;
template = cb.CreateTemplate(50, 50);
BaseFont font = BaseFont.CreateFont();
int pageN = writer.PageNumber;
String text = pageN.ToString();
float len = font.GetWidthPoint(text, 9);
iTextSharp.text.Rectangle pageSize = document.PageSize;
// cb.SetRGBColorFill(100, 100, 100);
;
cb.BeginText();
cb.SetFontAndSize(font, 9);
cb.SetTextMatrix(document.LeftMargin, pageSize.GetBottom(document.BottomMargin));
//cb.ShowText(text);
if (pageN > 1 && TotalNumber==0)
{
cb.ShowTextAligned(Element.ALIGN_CENTER, (pageN - 6).ToString() , 300f, 10f, 0);
}
cb.EndText();
cb.AddTemplate(template, document.LeftMargin + len, pageSize.GetBottom(document.BottomMargin));
}
}
And how to use the code ?
doc.Open();
//PdfWriter writer = PdfWriter.GetInstance(doc, stream);
PageEventHelper pageEventHelper = new PageEventHelper();
writer.PageEvent = pageEventHelper;
And before doc.closed()
Use the function SetNumber()
pageEventHelper.SetNumber(writer.PageNumber);
doc.Close();
you can use other number instead of the writer.PageNumber but not 0.
Hope this help you!

Categories