I'm trying to offset the position of an image within a table-cell in ITextSharp. Below is some pseudo-code outlining some of my attempts, none of which seem to affect the positioning of the image. I'd specifically like to align the middle of the image with the left border of the cell, but I can't even seem to figure out how to move the image at all.
doc.Open();
var table = new PdfPTable(1);
var cell = new PdfPCell();
var image = Image.GetInstance(); //etc
image.SetAbsolutePosition(-10, 0); //no effect
image.Left -= 10; //no effect
image.IndentationRight = 10; // no effect
cell.AddElement(image);
table.Rows.Add(new PdfPRow(new PdfPCell[] { cell }));
doc.Add(table);
When adding an image to a cell, using an absolute position or changing the properties of the image won't have an effect. If I interpret your question correctly, you want to define a padding for the cell so that there's 10pt of space to the left. Just use the appropriate padding method on the cell object (in iText, that would be cell.setPaddingLeft(10);).
Related
I have a datagridview control where I want to put a 3px line at the bottom of each header cell to look something like
I have put code in CellPainting even for the datagridview like:
if (e.RowIndex < 0) // headers
{
Rectangle newRect = new Rectangle(e.CellBounds.X, e.CellBounds.Y - 1 + e.CellBounds.Height, e.CellBounds.Width, 2);
using (Brush gridBrush = new SolidBrush(Color.Red))
{
e.Graphics.FillRectangle(gridBrush, newRect);
}e.Handled = true;
}
The red line appears correctly (I will add the 3px later). However, the header text now is missing.
I am assuming that setting the e.Handled = true; tells to not continue to draw the original header text. If I set it to false, then the red line disappears. There is no base.CellPainting type concept for this control (apparently).
I know I can draw the text myself, but then I have to worry about alignment, font...
Is there now way to tell the system to do both the line AND draw original header text?
I am willing to try other approaches if necessary.
There is no base.CellPainting type concept for this control
Indeed; the DGV has more options than just calling the base event.
Instead you can let it draw the parts separately and in the order you want:
if (e.RowIndex < 0) // headers
{
e.PaintBackground(e.CellBounds, true); // draw the default background
Rectangle newRect =
new Rectangle(e.CellBounds.X, e.CellBounds.Bottom - 2, e.CellBounds.Width, 2);
e.Graphics.FillRectangle(Brushes.Red, newRect); // now draw the red line
e.PaintContent(e.CellBounds); // finally draw the text in the default way
e.Handled = true; // done
}
If you disable dgv.EnableHeadersVisualStyles you can also set many other properties to be used when drawing the column headers..
For even finer-tuned options you may want to look into MSDN.
I have an image (attached).
I want the image to be stretched out to cover the yellow area.
I am using c#, magick.net
What will be the best approach?
I propose the following approach:
read input image saving original size
remove the transparent* area using trim
stretch the (now smaller) image to the original size
*If the yellow area in your sample image is actually transparent you can leave fuzz = 0 in the following code, otherwise you'll have to adjust the value to be sure to remove all the unwanted area.
string srcImageFullPath = "c:\input.png";
int fuzz = 0;
string destImageFullPath = "c:\output.png";
// Read image from file
using (MagickImage image = new MagickImage(srcImageFullPath))
{
//save height/width of the original image
int height = image.Page.Height;
int width = image.Page.Width;
//set fuzz percentage
image.ColorFuzz = new ImageMagick.Percentage(fuzz);
//trim borders
image.Trim();
//resize image to original size
MagickGeometry size = new MagickGeometry(width, height);
size.IgnoreAspectRatio = true;
image.Resize(size);
// Save the result
image.Write(destImageFullPath);
}
In the following picture you can see the original image on the left, and the image after resizing on the right:
Notes
Trim removes any border that has the same color of the pixels in the corner of you image (see here for the details)
Since the yellow border in your sample image is not made of a single color you can use Fuzz to remove "similar" colors (more info here). As said before if your border is transparent just leave fuzz = 0
I'm using iTextSharp to create a PDF using an HTML page as an example. I am trying to put a border around text to highlight certain items. Using methods from these posts (Draw Rect at curr position, and Add Text over image ) I have used OnGenericTag events to create the rectangle for each Chunk that matches certain criteria. The event fires correctly and the rectangles are drawn on the correct elements. However the problem occurs when I add the background for the tablecell, it appears on top of the Chunk rectangles. Is there a way to make the rectangle drawn from the OnGenericTag event drawn above the table cell background?
OnGenericTags Method:
public class GenericTags : PdfPageEventHelper
{
public override void OnGenericTag(PdfWriter writer, Document pdfDocument, iTextSharp.text.Rectangle rect, String text)
{
if ("ellipse".Equals(text)) //I know it's not needed.
ellipse(writer.DirectContent, rect);
}
public void ellipse(PdfContentByte content, iTextSharp.text.Rectangle rect)
{
content.SaveState();
content.SetRGBColorStroke(0x00, 0x00, 0xFF);
content.SetLineWidth(2);
content.Rectangle(rect.Left - 2f, rect.Bottom - 3f, rect.Width, rect.Height + 3f);
content.Stroke();
content.RestoreState();
}
}
Each PdfPCell is defined as:
PdfPCell med = new PdfPCell(ph[4])
{
HorizontalAlignment = Element.ALIGN_CENTER,
VerticalAlignment = Element.ALIGN_CENTER,
BackgroundColor = hm_yellow //this draws above rect from onGenericTag()
};
The Chunks are created and assign via a foreach loop:
ph[j] = new Phrase();
foreach (var x in p.Risks)
{
c[i] = new Chunk("R" + x.APP_RISK_ID.ToString() + ", ");
if (x.RISK_STATUS_CD == "57")
{
//c[i].SetBackground(hm_top_tier); // still shows behind table cell background
c[i].SetGenericTag("ellipse");
}
ph[j].Add(c[i]);
}
This is the HTML page I'm trying to turn into a PDF. It shows the table cell background colors and the values that need to have a rectangle around them to highlight them.
This is the result when rendering a PDF. The highlighted R#'s match, however, when I apply a background color to the PdfPCell it is drawn over the rectangle from the Chunk
Draw the table using the layer writer.DirectContentUnder to make sure that the rectangle stays on top (writer.DirectContent).
I need to have a table with multiple columns where I have different coloured circles in different cells with a number in the middle of the circle.
Similar to the mockup below but with everything centralized and equal.
I have tried the following:
PdfContentByte canvas = writer.DirectContent;
PdfTemplate template = canvas.CreateTemplate(40, 40);
template.SetLineWidth(1f);
template.Circle(15f, 15f, 15);
template.Stroke();
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(template);
img.Alignment = iTextSharp.text.Image.UNDERLYING | iTextSharp.text.Image.ALIGN_CENTER;
Phrase imgPhrase = new Paragraph(new Chunk(img, 1f, 1f));
PdfPCell meAnswerCell = new PdfPCell();
meAnswerCell.Colspan = 1;
meAnswerCell.BorderWidthBottom = 0;
meAnswerCell.HorizontalAlignment = Element.ALIGN_CENTER;
string meAnswerText = "1;
Phrase phrase = new Phrase(meAnswerText, questionFont);
Paragraph para = new Paragraph();
para.Add(imgPhrase);
para.Add(phrase);
para.Alignment = Element.ALIGN_CENTER;
meAnswerCell.AddElement(para);
answersTable.AddCell(meAnswerCell);
but I end up with something like this. (I haven't tried setting the colour yet). I cannot get the image and the text to sit in the same place.
I have also tried following this post:
iTextSharp - Text overlapping image
which explains how to put an event on the cell to set the background image of the cell but my circle appears half way down the page.
Has anyone go an example of this working?
There are many different ways to achieve what you want.
Actually, I just voted to close a question https://stackoverflow.com/questions/28066639/how-to-get-text-on-image-in-itext-using-java because it was a duplicate of How to add text to an image?
In your case, you may also benefit from the documentation. There are some examples in The Best iText Questions on StackOverflow that explain how to use cell events, but your requirement is very similar to what was done in a couple of examples from the book iText in Action - Second Edition.
You could use cell events to draw the background of a cell, as shown in the Calendar examples: calendar.pdf, but your requirement can also be met using a generic tag event: movie_years.pdf
Do you see how the word IMDB nicely fits inside an ellipse? You could fit a number inside a circle in the exact same way.
With this code, one draws an ellipse (changing it into a circle is fairly easy):
class GenericTags : PdfPageEventHelper {
public override void OnGenericTag(
PdfWriter writer, Document pdfDocument, Rectangle rect, String text) {
PdfContentByte content = writer.DirectContentUnder, rect);
content.SaveState();
content.SetRGBColorFill(0x00, 0x00, 0xFF);
content.Ellipse(
rect.Left - 3f, rect.Bottom - 5f,
rect.Right + 3f, rect.Top + 3f
);
content.Fill();
content.RestoreState();
}
}
With this snippet, you introduce this event:
GenericTags gevent = new GenericTags();
writer.PageEvent = gevent;
With this snippet, you mark a Chunk with the generic tag:
chunk.SetGenericTag("circle");
I want to be able to resize an image to the dimension of 159x159 points, using iTextSharp 4.2.0, but the resulting image need to have exactly the dimensions specified.
I've tried this:
Image image = Image.GetInstance(imagePath);
image.ScaleAbsolute(159f, 159f);
But the image is not a square. It keeps the aspect ratio.
Example:
I have this image:
And the result image should look loke this:
Thanks.
The problem you describe is typically what happens when you try and add an Image directly to a PdfPTable by calling AddCell(), which always scales the image to fit the PdfPCell. So if you're adding the image to the Document like this:
Image img = Image.GetInstance(imagePath);
img.ScaleAbsolute(159f, 159f);
PdfPTable table = new PdfPTable(1);
table.AddCell(img);
document.Add(table);
your ScaleAbsolute() call is ignored. To get the scaling you want:
PdfPTable table = new PdfPTable(1);
table.AddCell(new PdfPCell(img));
document.Add(table);
PdfPCell has property to fit image in cell so just set it to true.
iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance("/test.png");
PdfPCell logocell = new PdfPCell(logo,true); // **PdfPCell(Image,Boolean Fit)**