I'm having a weird problem with images in iTextSharp library.
I'm adding the image to the PdfPCell and for some reason it gets scaled up.
How do i keep it to original size?
I though that the images would be same when printed but the difference on the pic is the same on the printed version. Having to manually scale the image with ScaleXXX to get it to right seems a bit illogical and does not give a good result.
So how do I put the image in its original size inside a PdfPCell of a table without having to scale it?
Here's my code:
private PdfPTable CreateTestPDF()
{
PdfPTable table = new PdfPTable(1);
table.WidthPercentage = 100;
Phrase phrase = new Phrase("MY TITLE", _font24Bold);
table.AddCell(phrase);
PdfPTable nestedTable = new PdfPTable(5);
table.WidthPercentage = 100;
Phrase cellText = new Phrase("cell 1", _font9BoldBlack);
nestedTable.AddCell(cellText);
cellText = new Phrase("cell 2", _font9BoldBlack);
nestedTable.AddCell(cellText);
cellText = new Phrase("cell 3", _font9BoldBlack);
nestedTable.AddCell(cellText);
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(#"d:\MyPic.jpg");
image.Alignment = iTextSharp.text.Image.ALIGN_CENTER;
PdfPCell cell = new PdfPCell(image);
cell.HorizontalAlignment = PdfPCell.ALIGN_MIDDLE;
nestedTable.AddCell(cell);
cellText = new Phrase("cell 5", _font9BoldBlack);
nestedTable.AddCell(cellText);
nestedTable.AddCell("");
string articleInfo = "Test Text";
cellText = new Phrase(articleInfo, _font8Black);
nestedTable.AddCell(cellText);
nestedTable.AddCell("");
nestedTable.AddCell("");
nestedTable.AddCell("");
table.AddCell(nestedTable);
SetBorderSizeForAllCells(table, iTextSharp.text.Rectangle.NO_BORDER);
return table;
}
static BaseColor _textColor = new BaseColor(154, 154, 154);
iTextSharp.text.Font _font8 = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 8, iTextSharp.text.Font.NORMAL, _textColor);
iTextSharp.text.Font _font8Black = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 8, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);
iTextSharp.text.Font _font9 = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 9, iTextSharp.text.Font.NORMAL, _textColor);
iTextSharp.text.Font _font9BoldBlack = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 9, iTextSharp.text.Font.BOLD, BaseColor.BLACK);
iTextSharp.text.Font _font10 = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 10, iTextSharp.text.Font.NORMAL, _textColor);
iTextSharp.text.Font _font10Black = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 10, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);
iTextSharp.text.Font _font10BoldBlack = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 10, iTextSharp.text.Font.BOLD, BaseColor.BLACK);
iTextSharp.text.Font _font24Bold = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 24, iTextSharp.text.Font.BOLD, _textColor);
I'm using iTextSharp v4.1.2 and I get the following behavior:
Using this code, adding the image directly to the table via the AddCell method, the image is scaled up to fit the cell:
nestedTable.AddCell(image);
Using this code, adding the image to a cell, then adding the cell to the table, the image is displayed at its original size:
PdfPCell cell = new PdfPCell(image);
cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
nestedTable.AddCell(cell);
Have you added the image directly to the pdf document (outside the table) just to compare/double-check the image sizes?
document.add(image);
I assume that you want the image centered in the cell with some space around it. As a last resort, you can change your image. Make it a png with a transparent background, and just make sure that there is some transparent 'margin' around all the edges of your image.
EDIT
I just downloaded the v5.0.2 and I get the same results as mentioned above. I've tried it with images that are both smaller and larger than the size of the cell, and the behavior is the same; the first method scales the image, the second method does not.
EDIT
Well, apparently I have been wrong for years about the whole DPI thing when it comes to images. I can't seem to see that it makes any difference at all what the DPI of the image is.
I created a 600x400px image at three different resolutions, 72dpi, 96 dpi, and 110 dpi. Then I added each these images to a new document that was exactly 600x400.
Dim pSize As Rectangle = New Rectangle(600, 1000)
Dim document As Document = New Document(pSize, 0, 0, 0, 0)
For each of the three image files, when added to the document with
document.add(image)
they fit the document perfectly, with no differences for the different DPI settings.
#Stewbob's answer does work, but it's only incidently related to the methods of the table.
The thing with iTextSharp is that it will behave differently depending on which constructor you use. This will (annoyingly) scale up the image to fill the cell:
PdfPCell c = new PdfPCell();
c.Add(image);
c.setHorizontalAlignment(Element.ALIGN_CENTER); // this will be ignored
But this will leave the image at the size you set it (and allow for alignment):
PdfPCell c = new PdfPCell(image);
c.setHorizontalAlignment(Element.ALIGN_CENTER);
I don't know exactly why this is, it's got something to do with the cell being in 'text mode' if you add the image in the constructor versus 'composite mode' if you add it later (in which case each object is supposed to look after it's own alignment).
Some more info (in Java, but still applies) http://tutorials.jenkov.com/java-itext/table.html#cell-modes
So if you have to mantain the size of the image in the PdfPCell you can loock at this code :
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imageFilePath);
// Save the image width
float width = image.Width;
PdfPCell cell = new PdfPCell();
cell.AddElement(image);
// Now find the Image element in the cell and resize it
foreach (IElement element in cell.CompositeElements)
{
// The inserted image is stored in a PdfPTable, so when you find
// the table element just set the table width with the image width, and lock it.
PdfPTable tblImg = element as PdfPTable;
if (tblImg != null)
{
tblImg.TotalWidth = width;
tblImg.LockedWidth = true;
}
}
The function has a property to fit image. Only add a true
cell.AddElement(image,true);
For those asking for the overload, use this :
var imageCell = new PdfPCell(image, true);
instead of :
cell.AddElement(image,true);
Related
Is it possible to make the border length of a specific table cell fit the contents of that cell?
I did
PdfPCell cell = new PdfPCell(new Phrase(total.ToString(),myFont));
cell.Border=PdfPCell.BOTTOM_BORDER | PdfPCell.TOP_BORDER;
cell.HorizontalAlignment=Element.ALIGN_LEFT;
tabl.AddCell(cell);
But it produces the below result (the top & bottom border exceeds the cell data length/space)
Can someone help?
iText 7
In iText 7, this is very straightforward, because it's possible out of the box to set borders to any element. In this use case, instead of using table or cell borders, it's easier to set a top and bottom border to the piece of text itself.
Table table = new Table(4);
table.SetWidth(UnitValue.CreatePercentValue(100));
// no border on the table
table.SetBorder(null);
// no border on the cell
Cell cell = new Cell().SetBorder(null);
Text t = new Text("410.40");
// top and bottom border on the Text instance
t.SetBorderTop(new SolidBorder(1.5f));
t.SetBorderBottom(new SolidBorder(1.5f));
Paragraph p = new Paragraph().Add(t);
cell.Add(p);
table.AddCell(cell);
// some test cells
table.AddCell(new Cell().Add(new Paragraph("Column 2")).SetBorder(null));
table.AddCell(new Cell().Add(new Paragraph("C 3")).SetBorder(null));
table.AddCell(new Cell().Add(new Paragraph("C 4")).SetBorder(null));
doc.Add(table);
iText 5 / iTextSharp
A bit more grunt work is involved to get the same effect. A possible approach is to use a page event listener and a Chunk with a "generic tag" to trigger a page event upon rendering. That callback will expose the rendering rectangle of the Chunk, which allows those coordinates to be used to draw the top and bottom line at the correct location.
writer.PageEvent = new BorderEvent();
PdfPTable table = new PdfPTable(4);
table.WidthPercentage = 100;
PdfPCell cell = new PdfPCell();
// Use a Chunk with a "generic tag", which triggers a callback when it's rendered
Chunk c = new Chunk("410.40");
c.SetGenericTag("borders");
cell.AddElement(c);
// no border on the cell
cell.Border = 0;
table.AddCell(cell);
// some test cells
cell = new PdfPCell();
cell.AddElement(new Paragraph("Column 2"));
cell.Border = 0;
table.AddCell(cell);
cell = new PdfPCell();
cell.AddElement(new Paragraph("C 3"));
cell.Border = 0;
table.AddCell(cell);
cell = new PdfPCell();
cell.AddElement(new Paragraph("C 4"));
cell.Border = 0;
table.AddCell(cell);
doc.Add(table);
The callback for the generic tag:
class BorderEvent : PdfPageEventHelper
{
public override void OnGenericTag(PdfWriter writer, Document document,
Rectangle rect, String text)
{
PdfContentByte canvas = writer.DirectContent;
// draw the top border, based on the rendering rectangle
canvas.SetLineWidth(1.5);
canvas.MoveTo(rect.Left, rect.Top);
canvas.LineTo(rect.Right, rect.Top);
// draw the bottom border, based on the rendering rectangle
// the bottom coordinate is the base line of the text,
// so some calculation, probably including the font size, is
// needed to lower it a bit
// I've used a quick and dirty 3 points here
canvas.Stroke();
canvas.MoveTo(rect.Left, rect.Bottom - 3);
canvas.LineTo(rect.Right, rect.Bottom - 3);
canvas.Stroke();
}
}
I'm trying to write some text to a background image and save it as PDF, I'm using iTextSharp and the following Code:
var image = System.Drawing.Image.FromFile("resources\\bg2.png");
Document document = new Document();
var pageSize = new iTextSharp.text.Rectangle((1122*75)/100, (793 * 75) / 100);
document.SetPageSize(pageSize);
iTextSharp.text.Image bg = iTextSharp.text.Image.GetInstance(image,System.Drawing.Imaging.ImageFormat.Png);
bg.Alignment = iTextSharp.text.Image.UNDERLYING;
var writter = PdfWriter.GetInstance(document, new FileStream("file.pdf", FileMode.OpenOrCreate));
document.Open();
document.Open();
bg.SetAbsolutePosition(0, 0);
document.NewPage();
document.Add(bg);
BaseFont Tajawal = BaseFont.CreateFont(#"Resources\Tajawal-Medium 500.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Paragraph paragraphTable1 = new Paragraph();
paragraphTable1.SpacingAfter = 15;
PdfContentByte cb = writter.DirectContent;
PdfPTable table = new PdfPTable(1);
table.DefaultCell.NoWrap = false;
table.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
table.TotalWidth = (image.Width - (image.Width / 3));
table.AddH1(#"شهادة", new iTextSharp.text.Font(Tajawal));
table.WriteSelectedRows(0, -1, 60, 420, cb);
document.Close();
and the Method for AddH1 is as following:
public static void AddH1(this PdfPTable table, string Text, iTextSharp.text.Font tajawal)
{
tajawal.Size = 26f;
tajawal.SetStyle(iTextSharp.text.Font.BOLD);
Phrase ph = new Phrase(Text, tajawal);
PdfPCell text = new PdfPCell(ph);
text.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
text.Padding = 6;
text.Border = 0;
text.NoWrap = false;
text.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
table.AddCell(text);
}
My problem is if I use Tajawal font some of the characters are missing, like the last letter, if i use Tahoma font it works as expected, I'm thinking it might be a problem with encoding, any one faced this before ?
if i use the same font on word, or HTML it works perfectly, one thing to notice here is that Arabic letters can be presented in a separated way or joined together, what i figured out is all characters will not be displayed if typed in separated way, it would only work if its joined.
If i open Character Map i found out the following:
Characters starting from U+0621 to U+0649 are separated form (Does not work)
Characters starting from U+FE82 to U+FEFC are joined form (Works Perfectly)
I am creating a PdfTable from iTextSharp but the pdf containing this table does not allow editing in its cells.
How can I make sure that the cell contents are editable in resulting pdf?
iTextSharp.text.pdf.PdfPTable table = new iTextSharp.text.pdf.PdfPTable(columnsToAdd);
table.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
Font headerFont = new Font(Font.HELVETICA, 8f, Font.BOLD, Color.BLACK);
Font bodyFont = new Font(Font.HELVETICA, 6f, Font.NORMAL, Color.BLACK);
PdfPCell cEmpId = new PdfPCell(new Phrase("Emp ID", headerFont));
PdfPCell cEmpAge = new PdfPCell(new Phrase("Emp Age", headerFont));
table.AddCell(cEmpId);
table.AddCell(cEmpAge);
foreach(Child child in childData)
{
PdfPCell cellEmpID = new PdfPCell {FixedHeight = 10f};
cellEmpID.Phrase = (new Phrase(child.Emp_ID.ToString(CultureInfo.InvariantCulture),
bodyFont));
PdfPCell cellEmpAge = new PdfPCell {Phrase = (new Phrase(child.Emp_Age, bodyFont))};
table.AddCell(cellEmpID);
table.AddCell(cellEmpAge);
}
EDIT 1:
Based on Chris's answer, I am going to try out the following code, but not sure at this time.
//WITHIN THE LOOP IN MY CODE ABOVE ADD THESE LINES FOR EVERY PDF CELL
//Create our textfield, the rectangle that we're passing in is ignored and doesn't matter
var tfEmpID = new TextField(writer, new iTextSharp.text.Rectangle(0, 0), child.Emp_ID);
//Set the cell event to our custom IPdfPCellEvent implementation
cellEmpID.CellEvent = new ChildFieldEvent(root, tfEmpID.GetTextField(), 1);
//THEN OUTSIDE THE LOOP
//IMPORTANT! Add the root annotation to the writer which also adds all of the child annotations
writer.AddAnnotation(root);
I am creating a single pdf page with 6 images in a table in separate cells, even though I am setting the images height and width on the server side exactly the same with ScaleToFit the images sizes are not the same on the pdf page.
Is there anyway to get all the images the exact same size?
PdfPTable table = new PdfPTable(3);
table.HorizontalAlignment = Element.ALIGN_CENTER;
table.WidthPercentage = 100;
table.TotalWidth = 698.5f;
table.LockedWidth = true;
table.SetWidths(new float [] {1,1,1});
iTextSharp.text.Image img1 = iTextSharp.text.Image.GetInstance("C:\\Users\\DaNet\\Downloads\\image.jpg");
img1.Alignment = iTextSharp.text.Image.ALIGN_CENTER;
img1.ScaleToFit(120f, 155.25f);
iTextSharp.text.pdf.PdfPCell imgCell1 = new iTextSharp.text.pdf.PdfPCell(img1);
imgCell1.HorizontalAlignment = Element.ALIGN_CENTER;
imgCell1.BackgroundColor = new BaseColor(255, 255, 255);
imgCell1.Border = iTextSharp.text.Rectangle.NO_BORDER;
table.AddCell(imgCell1);
Two things.
First, see this post about wrapping the Image in a Chunk. Basically:
iTextSharp.text.pdf.PdfPCell imgCell1 = new iTextSharp.text.pdf.PdfPCell();
imgCell1.AddElement(new Chunk(img1, 0, 0));
Second, if you want the exact same size then you want to use ScaleAbsolute instead of ScaleToFit. The latter keeps the aspect ratio of the image so a 100x200 image scaled to fit 50x50 would come out as 25x50.
img1.ScaleAbsolute(120f, 155.25f);
I am having trouble trying to define an iTextSharp Font object that I can re-use throughout a class.
My first attempt was to use the FontFactory.GetFont method in 2 private methods that create PdfPCell objects:
FontFactory.GetFont("helvetica", 8)
The problem with this is that only one of the methods prints the font correctly, the other method would not print the inner text of the cell.
Next I tried to create a static field in the class representing the font:
private static Font _standardCellFont;
public static PdfPCell CreateTableHeaderCell(string cellText, int colspan = 1)
{
var innerCellText = new Phrase(cellText, PdfSupport._standardCellFont);
innerCellText.Font.Color = BaseColor.WHITE;
return new PdfPCell(innerCellText)
{
Colspan = colspan,
PaddingLeft = 5,
PaddingRight = 5,
FixedHeight = 10,
BackgroundColor = BaseColor.BLACK,
VerticalAlignment = Element.ALIGN_CENTER
};
}
public static PdfPCell CreateTableCell(string cellText, int colspan = 1, int rowspan = 1, bool bold = false, float minHeight = 0)
{
var cellInnerText =
bold ?
new Phrase(cellText, FontFactory.GetFont("helvetica", 8, Font.BOLD)) :
new Phrase(cellText, PdfSupport._standardCellFont);
return new PdfPCell(cellInnerText)
{
Border = Rectangle.NO_BORDER,
Colspan = colspan,
Rowspan = rowspan,
PaddingTop = 1,
PaddingBottom = 1,
PaddingLeft = 5,
PaddingRight = 5,
MinimumHeight = minHeight,
VerticalAlignment = Element.ALIGN_CENTER
};
}
The result of this is that neither of the methods print the cell text.
I don't think I really understand what is happening when I create a font object, my goal is to be able to define a few common fonts and basic properties (Size, Bold) that I can then re-use throughout my class, however from my usage it appears that a font object cannot be persisted in this way.
How strange. They can, and SHOULD, be shared. Are the characters you're trying to draw all in WInAnsiEncoding (mostly the first 256 characters of Unicode).
Ah! I suspect you may be running into an ALIGNMENT issue.
Valid settings for VerticalAlignment:
Element.ALIGN_TOP
Element.ALIGN_MIDDLE
Element.ALIGN_BOTTOM
Element.ALIGN_CENTER is strictly for horizontal alignment. I'm not sure that would make your text vanish, but it's certainly worth looking into.
PS: I believe you can even reuse Font and BaseFont objects across documents, though I haven't tried it myself.