I'm signing a document with token certificate:
var cp = new Org.BouncyCastle.X509.X509CertificateParser();
var chain = new[] { cp.ReadCertificate(cert.RawData) };
var externalSignature = new X509Certificate2Signature(cert, "SHA-1");
var pdfReader = new PdfReader(origem);
var signedPdf = new FileStream(destino, FileMode.Create);
var pdfStamper = PdfStamper.CreateSignature(pdfReader, signedPdf, '\0');
var sig = pdfStamper.SignatureAppearance;
sig.SetVisibleSignature(new Rectangle(50, 0, 500, 50), pdfReader.NumberOfPages, "Signature");
sig.SignatureRenderingMode = PdfSignatureAppearance.RenderingMode.DESCRIPTION;
sig.Layer2Text = "Assinado digitalmente por " + cert.SubjectName.Name;
sig.Layer2Font = new Font(Font.FontFamily.TIMES_ROMAN, 7);
MakeSignature.SignDetached(sig, externalSignature, chain, null, null, null, 0, CryptoStandard.CMS);
The signature text is rendered at bottom of the page. How can I change to a vertical mode, in the right part of document, outside the content margins?
thanks
First of all, to get some vertically oriented signature, the rectangle in which to visualize the signature should be somewhat more vertically oriented. Thus, in place of your
sig.SetVisibleSignature(new Rectangle(50, 0, 500, 50), pdfReader.NumberOfPages, "Signature");
you should use something like
sig.SetVisibleSignature(new Rectangle(50, 0, 50, 500), pdfReader.NumberOfPages, "Signature");
Now you clarified in comments that not only the visualization rectangle should have a vertical orientation but that the text also should be drawn vertically. iText by default creates visualizations with horizontal text. Thus, you have to use customized appearances.
As I am more at home with iText/Java, this example to customize a PdfSignatureAppearance appearance is in Java. It should be easy to transform to iTextSharp/C#, though.
appearance.setVisibleSignature(rectangle, PAGENUMBER, SIGNATURENAME);
// customize appearance layer 2 to display text vertically
PdfTemplate layer2 = appearance.getLayer(2);
layer2.transform(new AffineTransform(0, 1, -1, 0, rectangle.getWidth(), 0));
Font font = new Font();
font.setColor(BaseColor.WHITE);
font.setSize(10);
ColumnText ct2 = new ColumnText(layer2);
ct2.setRunDirection(PdfWriter.RUN_DIRECTION_NO_BIDI);
ct2.setSimpleColumn(new Phrase("Signed by me, myself and I", font), 0, 0, rectangle.getHeight(), rectangle.getWidth(), 15, Element.ALIGN_CENTER);
ct2.go();
This example draws "Signed by me, myself and I" vertically in the page area rectangle.
public bool drawVerticalText(string _text, Color _color, int _angle, int _size, int _left, int _top)
{
try
{
BaseColor bc = new BaseColor(_color.R, _color.G, _color.B, _color.A);
PdfContentByte cb = writer.DirectContent;
BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
//int width = baseFont.GetWidth(_text);
cb.BeginText();
cb.SetColorFill(CMYKColor.RED);
cb.SetFontAndSize(bf, _size);
cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, _text, _left, document.Top - _top, _angle);
cb.EndText();
document.Close();
return true;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
}
You can change the alpha value of the color, rotation angle (say, 45), text size and make a watermark to your document...
While above method uses DirectContent with absolute coordinates, the below method uses cell object with rotation property. Note that cell rotation can be multiple of 90, while with the 1st method you can have any angle...
public void drawVerticalText2()
{
PdfPTable table = new PdfPTable(4);
float[] widths = new float[] { 1.25f, 1.55f, 0.35f, 0.35f };
table.SetWidths(widths);
PdfPCell horizontalCell = new PdfPCell(new Phrase("I'm horizontal"));
horizontalCell.Border = BORDERS.BOX;
horizontalCell.HorizontalAlignment = 1;
table.AddCell(horizontalCell);
PdfPCell horizontalMirroredCell = new PdfPCell(new Phrase("I'm horizontal mirrored"));
horizontalMirroredCell.Border = BORDERS.BOX;
horizontalMirroredCell.HorizontalAlignment = 1;
horizontalMirroredCell.Rotation = 180;
table.AddCell(horizontalMirroredCell);
PdfPCell verticalCell = new PdfPCell(new Phrase("I'm vertical"));
verticalCell.Border = BORDERS.BOX;
verticalCell.HorizontalAlignment = 1;
verticalCell.Rotation = 90;
table.AddCell(verticalCell);
PdfPCell verticalMirroredCell = new PdfPCell(new Phrase("I'm vertical mirrored"));
verticalMirroredCell.Border = BORDERS.BOX;
verticalMirroredCell.HorizontalAlignment = 1;
verticalMirroredCell.Rotation = -90;
table.AddCell(verticalMirroredCell);
table.SpacingBefore = 20f;
table.SpacingAfter = 30f;
document.Add(table);
document.Close();
}
enjoy!
Related
I am using itextsharp library i want to have a checkbox checked or uncheck next to text.But I am unable to make it work.
Here is my class constructor.
public pdfCreator(string fileName)
{
//Create document
pdfDoc = new Document(PageSize.A4, 25, 25, 25, 15);
//Create a PDF file in specific path
PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, new FileStream(System.Web.HttpContext.Current.Server.MapPath(fileName+".pdf"), FileMode.Create));
pdfDoc.Open();
}
Here is my method.
public void chkBoxesCreator()
{
string FONT = "c:/windows/fonts/WINGDING.TTF";
string checkBox = "\u00fe";
string uncheckBox = "o";
BaseFont bf = BaseFont.CreateFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font f = new Font(bf, 12);
Paragraph p = new Paragraph(checkBox, f);
Paragraph p2 = new Paragraph(uncheckBox, f);
pdfDoc.Add(p);
pdfDoc.Add(p2);
}
I want text next to checkboxes that i but in blue. How can i get text next to my checkboxes. Thank you very much for any help.
You should use PdfAppearance to set the checkbox(or radioButton, button, etc. )
Here's a full example
public static void chkBoxesCreator()
{
String[] texts = { "one", "two", "three" };
using (var pdfDoc = new Document(PageSize.A4))
using (var output = new FileStream(fileLoc, FileMode.Create))
using (var writer = PdfWriter.GetInstance(pdfDoc, output))
{
{
pdfDoc.Open();
PdfContentByte cb = writer.DirectContent;
Rectangle _rect;
PdfFormField _Field1;
PdfAppearance[] checkBoxes = new PdfAppearance[2];
//set first checkBox style
checkBoxes[0] = cb.CreateAppearance(20, 20);
checkBoxes[0].Rectangle(1, 1, 18, 18);
checkBoxes[0].Stroke();
//set second checkBox style
checkBoxes[1] = cb.CreateAppearance(20, 20);
checkBoxes[1].Rectangle(1, 1, 18, 18);
checkBoxes[1].FillStroke();
RadioCheckField _checkbox1;
for (int i = 0; i < texts.Length; i++)
{
_rect = new Rectangle(180, 806 - i * 40, 200, 788 - i * 40); //can be any location
_checkbox1 = new RadioCheckField(writer, _rect, texts[i], "on");
_Field1 = _checkbox1.CheckField;
_Field1.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off", checkBoxes[0]);
_Field1.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, "On", checkBoxes[1]);
writer.AddAnnotation(_Field1);
ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT, new Phrase(texts[i], new Font(Font.FontFamily.HELVETICA, 18)), 210, 790 - i * 40, 0);
}
cb = writer.DirectContent;
pdfDoc.Close();
}
}
}
PS
The only problem i have that i couldn't use your font for some reason i was getting gibberish
Edit
You could even change you checkBox fill Color with(and you could make any more crazy thing like create a cross when the checkbox is checked)
checkBoxes[1].SetRGBColorFill(255, 128, 128); //change fill color
And the output is(with SetRGBColorFill)
I'm trying to align an image to the bottom-right of a cell.
I'm basically creating a table with two cells for each row. The cell contains text and an image, which I want to be aligned to the right bottom of the cells. Please refer to image
This is my code
PdfPTable table = new PdfPTable(2);
table.TotalWidth = 400f;
table.LockedWidth = true;
float[] widths = new float[] { 1.5f, 1.5f };
table.SetWidths(widths);
table.HorizontalAlignment = 0;
table.SpacingBefore = 50f;
table.SpacingAfter = 30f;
iTextSharp.text.Image logoImage = iTextSharp.text.Image.GetInstance(HttpContext.Current.Server.MapPath("~/Images/MyImage.png"));
logoImage.ScaleAbsolute(40, 40);
logoImage.Alignment = iTextSharp.text.Image.ALIGN_BOTTOM;
logoImage.Alignment = iTextSharp.text.Image.RIGHT_ALIGN;
foreach (EmployeeModel employee in employees)
{
PdfPCell cell = new PdfPCell();
cell.FixedHeight = 140f;
cell.PaddingLeft = 30f;
cell.PaddingRight = 10f;
cell.PaddingTop = 20f;
cell.PaddingBottom = 5f;
Paragraph p = new Paragraph(GetLabelCellText(Employee), NormalFont);
p.Alignment = Element.ALIGN_LEFT;
p.Alignment = Element.ALIGN_TOP;
cell.AddElement(p);
cell.AddElement(logoImage);
table.AddCell(cell);
}
How do I place the image at the bottom right of each cell (without affecting the position of the text of course).
The question is a bit ambiguous because you create a Table with two columns, but add cells without verifying the employees collection has an even number of elements, which will throw if not....
Assuming you really do want both the text and the image in a single cell, probably the simplest way to get the layout you want is to implement IPdfPCellEvent:
public class BottomRightImage : IPdfPCellEvent
{
public Image Image { get; set; }
public void CellLayout(
PdfPCell cell,
Rectangle position,
PdfContentByte[] canvases)
{
if (Image == null) throw new InvalidOperationException("image is null");
PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS];
Image.SetAbsolutePosition(
position.Right - Image.ScaledWidth - cell.PaddingRight,
position.Bottom + cell.PaddingBottom
);
canvas.AddImage(Image);
}
}
Then set the CellEvent property on the PdfPCell. Here's a simple working example:
using (var stream = new MemoryStream())
{
using (var document = new Document())
{
PdfWriter.GetInstance(document, stream);
document.Open();
var table = new PdfPTable(2)
{
HorizontalAlignment = Element.ALIGN_LEFT,
TotalWidth = 400f,
LockedWidth = true
};
var image = Image.GetInstance(imagePath);
image.ScaleAbsolute(40, 40);
var cellEvent = new BottomRightImage() { Image = image };
var testString =
#"first name: {0}
last name: {0}
ID no: {0}";
for (int i = 0; i < 2; ++i)
{
var cell = new PdfPCell()
{
FixedHeight = 140f,
PaddingLeft = 30f,
PaddingRight = 10f,
PaddingTop = 20f,
PaddingBottom = 5f
};
cell.CellEvent = cellEvent;
var p = new Paragraph(string.Format(testString, i))
{
Alignment = Element.ALIGN_TOP | Element.ALIGN_LEFT
};
cell.AddElement(p);
table.AddCell(cell);
}
document.Add(table);
}
File.WriteAllBytes(outputFile, stream.ToArray());
}
And PDF output:
An all-black square image is used to show the PdfPCell padding is taken into account.
I am adding footer text to existing BYTES in each page using the code shown below, which is visible in Chrome as well as in Foxit PhantomPDF, however, it's not visible in Adobe PDF reader.
In addition to the footer, I also add headers which are visible even with 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();
}
}
Issue:
Only the footer line that I draw is visible in Adobe.
Footer texts namely, footer line 1, footer line 2, Date as well as page number is not visible even when I try to print the document.
Screenshot:
Thanks in advance , I know that you're busy .. so I edited this based on the code you gave me ...
first I want you to see what I got when I tried you code ..
and the picture I used as a background is :
as you see I got several problems :
1- the image is not a background to the cells and I want it to be stretched .
2- I tried to put the text in different positions but I failed .
3 - also i got a missing cell that not showed.
the code I used is :
1- the ImageEvent class :
class ImageEvent : IPdfPCellEvent
{
protected Image img;
public ImageEvent(Image img) {
this.img = img;
}
void IPdfPCellEvent.CellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases)
{
img.ScaleToFit(position.Width, position.Height);
img.SetAbsolutePosition(position.Left + (position.Width - img.Width) / 2,
position.Bottom + (position.Height - img.ScaledHeight / 2));
PdfContentByte canvas = canvases[PdfPTable.BACKGROUNDCANVAS];
try {
canvas.AddImage(img);
} catch (DocumentException ex) {
// do nothing
}
}
}
2- The position class :
class PositionEvent : IPdfPCellEvent
{
protected Phrase content;
protected string pos;
public PositionEvent(Phrase content, string pos)
{
this.content = content;
this.pos = pos;
}
void IPdfPCellEvent.CellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases)
{
PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS];
float x = 0;
float y = 0;
int alignment = 0;
switch (pos)
{
case "TOP_LEFT":
x = position.GetLeft(3);
y = position.GetTop(content.Leading);
alignment = Element.ALIGN_LEFT;
break;
case "TOP_RIGHT":
x = position.GetRight(3);
y = position.GetTop(content.Leading);
alignment = Element.ALIGN_RIGHT;
break;
case "BOTTOM_LEFT":
x = position.GetLeft(3);
y = position.GetBottom(3);
alignment = Element.ALIGN_LEFT;
break;
case "BOTTOM_RIGHT":
x = position.GetRight(3);
y = position.GetBottom(3);
alignment = Element.ALIGN_RIGHT;
break;
case "CENTER_TOP":
x = position.GetRight(3) + position.GetLeft(3) / 2;
y = position.GetTop(3);
alignment = Element.ALIGN_RIGHT;
break;
case "CENTER_BOTTOM":
x = position.GetRight(3) + position.GetLeft(3) / 2;
y = position.GetBottom(3);
alignment = Element.ALIGN_RIGHT;
break;
case "CENTER_MIDDLE":
x = position.GetRight(3) + position.GetLeft(3) / 2;
y = x;
alignment = Element.ALIGN_RIGHT;
break;
}
ColumnText.ShowTextAligned(canvas, alignment, content, x, y, 0);
}
}
3- The method :
public void createPdf(string dest)
{
// 1. Create a Document which contains a table:
Document document = new Document();
PdfWriter.GetInstance(document, new FileStream(dest, FileMode.Create));
document.Open();
PdfPTable table = new PdfPTable(3);
table.WidthPercentage = 100f;
PdfPCell cell1 = new PdfPCell();
PdfPCell cell2 = new PdfPCell();
PdfPCell cell3 = new PdfPCell();
PdfPCell cell4 = new PdfPCell();
PdfPCell cell5 = new PdfPCell();
PdfPCell cell6 = new PdfPCell();
PdfPCell cell7 = new PdfPCell();
// 2. Inside that table, make each cell with specific height:
cell1.FixedHeight=50;
cell2.FixedHeight = 50;
cell3.FixedHeight = 50;
cell4.FixedHeight = 50;
cell5.FixedHeight = 50;
cell6.FixedHeight = 50;
cell7.FixedHeight = 50;
// 3. Each cell has the same background image
string path = string.Concat(this.openFileDialog_pic.FileName);
string imageFilePath = string.Concat(Environment.GetEnvironmentVariable("."), path);
iTextSharp.text.Image IMG = iTextSharp.text.Image.GetInstance(imageFilePath);
ImageEvent imgEvent = new ImageEvent(iTextSharp.text.Image.GetInstance(IMG));
cell1.CellEvent=imgEvent;
cell2.CellEvent = imgEvent;
cell3.CellEvent = imgEvent;
cell4.CellEvent = imgEvent;
cell5.CellEvent = imgEvent;
cell6.CellEvent = imgEvent;
cell7.CellEvent = imgEvent;
// 4. Add text in front of the image at specific position
cell1.CellEvent= new PositionEvent(new Phrase("Top left"), "TOP_LEFT");
cell2.CellEvent=new PositionEvent(new Phrase("Top right"), "TOP_RIGHT");
cell3.CellEvent=new PositionEvent(new Phrase("Bottom left"), "BOTTOM_LEFT");
cell4.CellEvent=new PositionEvent(new Phrase("Bottom right"), "BOTTOM_RIGHT");
cell5.CellEvent = new PositionEvent(new Phrase("Center Top"), "CENTER_TOP");
cell6.CellEvent = new PositionEvent(new Phrase("Center Bottom"), "CENTER_BOTTOM");
cell7.CellEvent = new PositionEvent(new Phrase("Center Middle"), "CENTER_MIDDLE");
// Wrap it all up!
table.AddCell(cell1);
table.AddCell(cell2);
table.AddCell(cell3);
table.AddCell(cell4);
table.AddCell(cell5);
table.AddCell(cell6);
table.AddCell(cell7);
document.Add(table);
document.Close();
}
In an additional comment, you clarify your requirements:
I want to create a document which contains one table. Inside that table, cells
I want to make each cell with specific height
each cell have the same background image
I want to put a text in front the image in the position I want inside the cell. For example: top left of the cell, bottom right of the cell
In other words: you want something like this: position_content_in_cell.pdf
There is more than one way to do this. I don't understand the code sample you use in your question. It uses nested tables and I don't understand why you'd need nested tables.
In the PositionContentInCell example, I used a method that allows you to really fine-tune the exact position of the text. I created an ImageEvent to scale and center the image:
class ImageEvent implements PdfPCellEvent {
protected Image img;
public ImageEvent(Image img) {
this.img = img;
}
public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
img.scaleToFit(position.getWidth(), position.getHeight());
img.setAbsolutePosition(position.getLeft() + (position.getWidth() - img.getScaledWidth()) / 2,
position.getBottom() + (position.getHeight() - img.getScaledHeight()) / 2);
PdfContentByte canvas = canvases[PdfPTable.BACKGROUNDCANVAS];
try {
canvas.addImage(img);
} catch (DocumentException ex) {
// do nothing
}
}
}
I created a PositionEvent to add the text inside the cell:
class PositionEvent implements PdfPCellEvent {
protected Phrase content;
protected POSITION pos;
public PositionEvent(Phrase content, POSITION pos) {
this.content = content;
this.pos = pos;
}
public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS];
float x = 0;
float y = 0;
int alignment = 0;
switch (pos) {
case TOP_LEFT:
x = position.getLeft(3);
y = position.getTop(content.getLeading());
alignment = Element.ALIGN_LEFT;
break;
case TOP_RIGHT:
x = position.getRight(3);
y = position.getTop(content.getLeading());
alignment = Element.ALIGN_RIGHT;
break;
case BOTTOM_LEFT:
x = position.getLeft(3);
y = position.getBottom(3);
alignment = Element.ALIGN_LEFT;
break;
case BOTTOM_RIGHT:
x = position.getRight(3);
y = position.getBottom(3);
alignment = Element.ALIGN_RIGHT;
break;
}
ColumnText.showTextAligned(canvas, alignment, content, x, y, 0);
}
}
This is how I use these events:
public void createPdf(String dest) throws IOException, DocumentException {
// 1. Create a Document which contains a table:
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
PdfPTable table = new PdfPTable(2);
PdfPCell cell1 = new PdfPCell();
PdfPCell cell2 = new PdfPCell();
PdfPCell cell3 = new PdfPCell();
PdfPCell cell4 = new PdfPCell();
// 2. Inside that table, make each cell with specific height:
cell1.setFixedHeight(50);
cell2.setFixedHeight(50);
cell3.setFixedHeight(50);
cell4.setFixedHeight(50);
// 3. Each cell has the same background image
ImageEvent imgEvent = new ImageEvent(Image.getInstance(IMG));
cell1.setCellEvent(imgEvent);
cell2.setCellEvent(imgEvent);
cell3.setCellEvent(imgEvent);
cell4.setCellEvent(imgEvent);
// 4. Add text in front of the image at specific position
cell1.setCellEvent(new PositionEvent(new Phrase("Top left"), POSITION.TOP_LEFT));
cell2.setCellEvent(new PositionEvent(new Phrase("Top right"), POSITION.TOP_RIGHT));
cell3.setCellEvent(new PositionEvent(new Phrase("Bottom left"), POSITION.BOTTOM_LEFT));
cell4.setCellEvent(new PositionEvent(new Phrase("Bottom right"), POSITION.BOTTOM_RIGHT));
// Wrap it all up!
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
table.addCell(cell4);
document.add(table);
document.close();
}
Normally, I would write this code in a more efficient way, but I order the code lines in a way so that they reflect your requirements 1, 2, 3 and 4 literally.
Update:
In the comments, you asked several additional questions. For instance: how to stretch the image:
You were able to answer most of these questions, for instance based on my hint to use ScaleAbsolute:
public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
img.scaleAbsolute(position.getWidth(), position.getHeight());
img.setAbsolutePosition(position.getLeft(), position.getBottom());
PdfContentByte canvas = canvases[PdfPTable.BACKGROUNDCANVAS];
try {
canvas.addImage(img);
} catch (DocumentException ex) {
// do nothing
}
}
You had one more question that required an extra example (it was hard to explain it in a comment box). I named that example PositionContentInCell2
Instead of using the POSITION enumeration, you asked if it was possible to pass x and y values. You could do that, but you probably won't always know the width and the height of the cells, so why not define percentages such as wPct and hPct, along with an alignment:
class PositionEvent implements PdfPCellEvent {
protected Phrase content;
protected float wPct;
protected float hPct;
protected int alignment;
public PositionEvent(Phrase content, float wPct, float hPct, int alignment) {
this.content = content;
this.wPct = wPct;
this.hPct = hPct;
this.alignment = alignment;
}
public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS];
float x = position.getLeft() + wPct * position.getWidth();
float y = position.getBottom() + hPct * (position.getHeight() - content.getLeading());
ColumnText.showTextAligned(canvas, alignment, content, x, y, 0);
}
}
Now you can add these events like this:
cell1.setCellEvent(new PositionEvent(new Phrase(14, "Top left"), 0, 1, Element.ALIGN_LEFT));
cell2.setCellEvent(new PositionEvent(new Phrase(14, "Top right"), 1, 1, Element.ALIGN_RIGHT));
cell3.setCellEvent(new PositionEvent(new Phrase(14, "Top center"), 0.5f, 1, Element.ALIGN_CENTER));
cell4.setCellEvent(new PositionEvent(new Phrase(14, "Bottom center"), 0.5f, 0, Element.ALIGN_CENTER));
cell5.setCellEvent(new PositionEvent(new Phrase(14, "Middle center"), 0.5f, 0.5f, Element.ALIGN_CENTER));
cell6.setCellEvent(new PositionEvent(new Phrase(14, "Middle center"), 0.5f, 0.5f, Element.ALIGN_CENTER));
cell7.setCellEvent(new PositionEvent(new Phrase(14, "Bottom left"), 0, 0, Element.ALIGN_LEFT));
cell8.setCellEvent(new PositionEvent(new Phrase(14, "Bottom right"), 1, 0, Element.ALIGN_RIGHT));
Of course, if you prefer passing x and y (after all: you do know the height, because you are defining a fixed height, the code can be made even simpler: you don't multiple the variables with position.getWidth and position.getHeight().
I am pretty new in iTextSharp and I have the following situation: I am creating a PDF that contains an header and a footer (for the header and footer creation I am using a class that extends PdfPageEventHelper and I have override the OnStartPage() and the OnEndPage() method, it work fine).
Now my problem is that I have to insert as Page X of Y into my footer. Where X**is the **current page number and Y is the total page number. The value of Y is not fixed (because the length of my PDF is not known in advance because it depends depends on the length of the content and can differ from PDF to PDF). How can I handle this situation? (inserting the correct Y value in each footer?)
Searching online I have find this tutorial (that is for Java iText but maybe it is not so different from iTextSharp version): http://itextpdf.com/examples/iia.php?id=104
In this example it create a PDF and its header contains something like Page X of Y.
I am trying to understand this example (and translate it for iTextSharp) but I have some doubts about how it work (and if it is a real solution for my problem).
From what I can understand it perform the following operations:
Into the class that extends PdfPageEventHelper it is declared a PdfTemplate object
PdfTemplate total;
I think that maybe it handles the total pages number that are into my PDF document, but reading the official documentation I have not many information about what exactly do this class: http://api.itextpdf.com/itext/com/itextpdf/text/pdf/PdfTemplate.html
I am trying to do something similar into my class that extends PdfPageEventHelper but I can't do it.
This is my not working code:
public class PdfHeaderFooter : PdfPageEventHelper
{
// The template with the total number of pages:
PdfTemplate total;
private static int numPagina = 1;
public Image CellImage;
private string folderImages;
private string _sourceId;
public PdfHeaderFooter(string _folderImages, string sourceId)
{
folderImages = _folderImages;
_sourceId = sourceId;
}
/* Creates the PdfTemplate that will hold the total number of pages.
* Write on top of document
* */
public override void OnOpenDocument(PdfWriter writer, Document document)
{
base.OnOpenDocument(writer, document);
// (Nobili) Page Number:
total = writer.DirectContent.CreateTemplate(30, 16);
//PdfPTable tabFot = new PdfPTable(new float[] { 1F });
PdfPTable tabFot = new PdfPTable(2);
tabFot.WidthPercentage = 98;
tabFot.SpacingAfter = 10F;
PdfPCell cell;
//tabFot.TotalWidth = 300F;
cell = new PdfPCell(new Phrase("Header"));
tabFot.AddCell(cell);
tabFot.WriteSelectedRows(0, -1, 150, 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;
String text = "Page " + pageN + " of ";
//PdfPTable tabFoot = new PdfPTable(new float[] { 1F });
PdfPTable tabFoot = new PdfPTable(3);
tabFoot.TotalWidth = document.Right - document.Left;
tabFoot.DefaultCell.Border = PdfPCell.NO_BORDER;
tabFoot.DefaultCell.CellEvent = new RoundedBorder();
PdfPTable innerTable = new PdfPTable(2);
innerTable.SetWidths(new int[] { 247, 246 });
innerTable.TotalWidth = document.Right - document.Left;
innerTable.DefaultCell.Border = PdfPCell.NO_BORDER;
PdfPCell innerCellLeft = new PdfPCell(new Phrase("Early Warning - Bollettino")) { Border = PdfPCell.NO_BORDER, Padding = 5, MinimumHeight = 20, HorizontalAlignment = Element.ALIGN_LEFT };
//PdfPCell innerCellRight = new PdfPCell(new Phrase("Pag. " + numPagina + "/5")) { Border = PdfPCell.NO_BORDER, Padding = 5, MinimumHeight = 20, HorizontalAlignment = Element.ALIGN_RIGHT };
PdfPCell innerCellCenter = new PdfPCell(new Phrase(text)) { Border = PdfPCell.NO_BORDER, Padding = 5, MinimumHeight = 20, HorizontalAlignment = Element.ALIGN_RIGHT };
PdfPCell innerCellRight = new PdfPCell(Image.GetInstance(total)) { Border = PdfPCell.NO_BORDER, Padding = 5, MinimumHeight = 20, HorizontalAlignment = Element.ALIGN_RIGHT };
innerTable.AddCell(innerCellLeft);
innerTable.AddCell(innerCellRight);
tabFoot.AddCell(innerTable);
tabFoot.WriteSelectedRows(0, -1, document.Left, document.Bottom, writer.DirectContent);
numPagina++;
}
// 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("CELL 2:")) { 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 close of document
public override void OnCloseDocument(PdfWriter writer, Document document)
{
base.OnCloseDocument(writer, document);
}
}
}
I want put in the footer something like the Page X of Y as shown in the tutorial
I tryied to do the following thing:
1) I declared the PdfTemplate total; object as field of my class and I initialize it into my OnOpenDocument() method
total = writer.DirectContent.CreateTemplate(30, 16);
2) Into my OnEndPage() method I put:
String text = "Page " + pageN + " of ";
and I created the following table to show the Page X of Y
PdfPCell innerCellLeft = new PdfPCell(new Phrase("Early Warning - Bollettino")) { Border = PdfPCell.NO_BORDER, Padding = 5, MinimumHeight = 20, HorizontalAlignment = Element.ALIGN_LEFT };
PdfPCell innerCellCenter = new PdfPCell(new Phrase(text)) { Border = PdfPCell.NO_BORDER, Padding = 5, MinimumHeight = 20, HorizontalAlignment = Element.ALIGN_RIGHT };
PdfPCell innerCellRight = new PdfPCell(Image.GetInstance(total)) { Border = PdfPCell.NO_BORDER, Padding = 5, MinimumHeight = 20, HorizontalAlignment = Element.ALIGN_RIGHT };
But it don't work and throw me an exception
What could be the problem in my code?
You are copy/pasting code without reading the documentation that comes with the examples. Moreover you are completely ignoring the C# version of the examples from my book. I have paid good money to a C# developer to port these examples. It feels as if that money was thrown away. You can find the example you tried to port yourself here: http://tinyurl.com/itextsharpIIA2C05
Error #1: you are adding content in the OnStartPage() method. That is forbidden! This is documented on many places. See for instance this answer copied from page 150 of the official documentation:
FAQ Why is it not advised to add content in the onStartPage() method? You'll remember from section 5.2.4 that iText ignores newPage() calls when the current page is empty. This method is executed — or ignored — when you call it explicitly from your code, but it's also invoked implicitly from within iText on multiple occasions. It's important that it's ignored for empty pages; otherwise you'd end up with plenty of unwanted new pages that are unintentionally left blank. If you add content in an onStartPage() method, there's always a risk of having unwanted pages. Consider it more safe to reserve the onEndPage() method for adding content.
Error #2: you are adding content in the OnOpenDocument() method. Why would that make sense?
Error #3: you create the total object because you want to create a place holder that can be added to each page. Once you know the total number of pages, you want to fill out that number on that place holder. However: I don't see you doing this anywhere. The appropriate place to do this, is obviously in the OnCloseDocument() even. This event is triggered right before the document is closed, so at that moment, the total number of pages is known. This is, as Sherlock Holmes would say, elementary, my dear!