IText 7 Link Border Showing - itext7

Using Itext 7 for generating pdf and found Anchor tag is deprecated hence used Link.
Link projectNameLink = new Link("**Test**", PdfAction.createURI("https://www.google.com"));
projectNameLink.setFontColor(Color.BLUE)
.setBorder(Border.NO_BORDER);
Paragraph footerContent = new Paragraph().add(projectNameLink).setBorder(Border.NO_BORDER);
Added this to paragraph. The document in Acrobat Reader shows border around Test. Same thing I see in the below Url . Is it a bug ? Or am missing something. How to remove the border ?
Try to download the file
And open in Acrobat Reader on can see borders along the Link Text.

The default behaviour for iText is to add the border.
You can however apply some style to these actions.
PdfAnnotation la1 = new PdfLinkAnnotation(new Rectangle(0, 0, 0, 0))
.setHighlightMode(PdfAnnotation.HIGHLIGHT_INVERT)
.setAction(js)
.setBorderStyle(PdfAnnotation.STYLE_UNDERLINE); // this is what you need
Have a look at http://developers.itextpdf.com/content/itext-7-building-blocks/examples/chapter-6

I had the same problem just wanting to have no border at all, and I've found the solution:
PdfAnnotation la1 = new PdfLinkAnnotation(new Rectangle(0, 0, 0, 0))
.setHighlightMode(PdfAnnotation.HIGHLIGHT_NONE)
.setAction(js)
.setBorder(new PdfArray(new int[]{0,0,0}))
I found out the solution reading the javadoc: http://itextsupport.com/apidocs/itext7/latest/com/itextpdf/kernel/pdf/annot/PdfAnnotation.html#getBorder--

Related

iText 5 ColumnText to iText7 ColumnDocumentRenderer alignment issue

We are in the process of migrating from iText 5 to iText7. ColumnText is used in few places in our existing code. According to the following https://www.slideshare.net/iTextPDF/oops-i-broke-my-api?from_action=save
we tried to run a sample to test iText7 solution based on ColumnDocumentRenderer. Even though it is working fine, we are seeing a minor difference in the alignment. Apparently, iText5 code is aligning the text to the bottom, but iText7 is not doing that.
iText 5 code
Document document = new Document();
PdfWriter writer =
PdfWriter.getInstance(document, new FileOutputStream("D:\\Temp Files\\Test.pdf"));
document.open();
ColumnText ct = new ColumnText(writer.getDirectContent());
Font normal = new Font(FontFamily.TIMES_ROMAN, 12);
Paragraph p = new Paragraph("Text", normal);
ct.addElement(p);
int status = ColumnText.START_COLUMN;
Rectangle[] columns = {new Rectangle(36, 36, 290, 806)};
while(ColumnText.hasMoreText(status)) {
ct.setSimpleColumn(columns[0]);
status = ct.go();
}
document.close();
iText 7 code
PdfDocument pdf = new PdfDocument(new PdfWriter("D:\\Temp Files\\Test i7 ColumnText.pdf"));
Document document = new Document(pdf);
Rectangle[] columns = {new Rectangle(36, 36, 254, 770)};
document.setRenderer(new ColumnDocumentRenderer(document, columns));
BufferedReader br = new BufferedReader(new StringReader("Text"));
String line;
PdfFont normal = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN);
while ((line = br.readLine()) != null) {
document.add(new Paragraph(line).setFont(normal));
}
document.close();
There is no alignment or padding set in either case. Not sure if there is anything else that we need to set in iText7. We want to retain the placement of the text, which is very crucial while trying to generate dynamic PDF's. How to maintain the placement of the text between iText 5 and iText 7?
Note: Enclosed screenshot for reference.
iText5 Vs iText7
Left hand side pdf is created by 5 and right hand side is created by 7. The rectangular box was drawn to show how itext 5 is rendering Text aligned to the bottom, but itext 7 is placing the Text little bit above the base line. How to fix itext7 code to place Text similar itext 5?

Vaadin Image in a Grid not showing

I'm trying to display a tiny little image (16x16 pixels) in a TreeGrid Column.
treeGrid.addComponentColumn(i -> new Image("file://c:/temp/reddot.png", "alt")).setHeader("Preview");
In my IDE, this file url is underlined, I can click it, it opens the image in my browser. So, the file seems ok and exists.
But somehow, it does not appear in the grid column and the "alt" text doesn't appear either.
Hm. Anyone any idea what's going wrong ? Unfortunatly I don't see any error messages...
thanks,
Thorsten
PS: I'm using Version 13.
You could try to put image under META-INF/resources. Then you could reference image directly by name like this : Image im=new Image("kissa.jpg","Random picture");
Otherwise, you could create a inside and then reference mentioning also folder. In the picture below I have this set-up
And this is the output :
The whole code:
TreeGrid<Person> grid = new TreeGrid<>(Person.class);
grid.setHierarchyColumn("name");
grid.addComponentColumn(e->{
if(e.getName().equals("daughter")) {
Image im=new Image("test/cat.jpg","Random picture");
im.setWidth("200px");
im.setHeight("150px");
return im;}
else {
Image im=new Image("kissa.jpg","Random picture");
im.setWidth("200px");
im.setHeight("150px");
return im;}}).setHeader("Cat");
Person dad = new Person("dad", null);
Person son = new Person("son", dad);
Person daughter = new Person("daughter", dad);
List<Person> all = Arrays.asList(dad, son, daughter);
all.forEach(p -> grid.getTreeData().addItem(p.getParent(), p));
add(grid);
The example of TreeGrid is copied from here: Using new features with the LTS version: case TreeGrid

iText Image and transparency

I'm trying to add a PNG image to an existing pdf, but the transparency is converted to black color.
PdfReader reader = new PdfReader(pdfPath);
File f = new File(pdfPath);
String result = f.getParent() + File.separator + UUID.randomUUID().toString() + ".pdf";
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(result));
Image image = Image.getInstance(ImageIO.read(new File(imagePath)), null);
PdfImage stream = new PdfImage(image, null, null);
PdfIndirectObject ref = stamper.getWriter().addToBody(stream);
image.setDirectReference(ref.getIndirectReference());
image.setAbsolutePosition(30, 300);
PdfContentByte canvas = stamper.getOverContent(1);
canvas.addImage(image);
stamper.close();
reader.close();
How can I keep transparency?
First this: I am violating the policy at iText Software by answering this question. You are using an old version of iText, and the policy dictates that voluntary support on iText 5 or earlier has stopped. You should either use iText 7, or you should get a support contract if you still want support for an old iText version.
However, I am curious. I want to know where you found this clunky code (or why you decided to write this code):
Image image = Image.getInstance(ImageIO.read(new File(imagePath)), null);
PdfImage stream = new PdfImage(image, null, null);
PdfIndirectObject ref = stamper.getWriter().addToBody(stream);
image.setDirectReference(ref.getIndirectReference());
image.setAbsolutePosition(30, 300);
PdfContentByte canvas = stamper.getOverContent(1);
canvas.addImage(image);
You don't need ImageIO and you don't need to create a PdfImage, nor do you need to add that image to the body of a PDF file. The code you are using is code specialists would use for a very particular purpose. If you know that particular purpose, please explain.
If adding an image at an absolute position is all you want to do (that's a general purpose, not a particular purpose), your code should be as simple as this:
Image image = Image.getInstance(imagePath);
image.setAbsolutePosition(30, 300);
PdfContentByte canvas = stamper.getOverContent(1);
canvas.addImage(image);
In this case, you don't have to worry about the image mask; iText will take care of that for you.
Please also explain why you're using an outdated version of iText instead of iText 7. If you want your application to be future-proof, you should upgrade to iText 7 now (to avoid wasting time later).

I want to display numbers on the system tray notification Icons on windows

I am trying to create a notification Icon based application in which I want to display some numbers ranging from 1-999.
I looked at this video which is similar to what I want to do but here the system tray icon just displays the icon and it shows a pop up rather than the system tray icon showing the number or any text.
Excluding the popup item, all I want to do is to read a number (input from somewhere) and display that number in the notification icon section.
I am open to trying any technology (QT, .net) for doing this. Basically, I am looking for some examples.
While parts of your question are vague, this is very possible, I'd even dare-say quite simple. Since you mentioned you're open to trying any technology, C# would probably simplify things for you.
Generate a new 16 x 16 Bitmap and draw the number to it using the Graphics class.
Convert the Image instance to an Icon instance, after disposing of your Graphics object.
Set the Icon property of your NotifyIcon to the icon you've just created.
These are the basic steps. You'll likely need to do some research if you aren't familiar with the classes used.
Thanks for replying to my question. Here is what I came up with. Not sure if this is what you were talking about.
Bitmap bmp = new Bitmap(WindowsFormsApplication2.Properties.Resources._16by16BitmapIcon);
RectangleF rectf = new RectangleF(2, 2, 16, 16);
Graphics g = Graphics.FromImage(bmp);
g.DrawString("99", new Font("Tahoma", 7), Brushes.Blue, rectf);
pictureBox1.Image = bmp;
pictureBox1.Height = bmp.Height;
pictureBox1.Width = bmp.Width;
g.Dispose();
var thumb = (Bitmap)bmp.GetThumbnailImage(64, 64, null, IntPtr.Zero);
thumb.MakeTransparent();
notifyIcon1.Icon = Icon.FromHandle(thumb.GetHicon());
Now my next question could this be done in a better way? This is my first C Sharp app so any suggestions are welcome!
public void ShowText(string text, Font font, Color col)
{
Brush brush = new SolidBrush(col);
// Create a bitmap and draw text on it
Bitmap bitmap = new Bitmap(16, 16);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.DrawString(text, font, brush, 0, 0);
// Convert the bitmap with text to an Icon
Icon icon = Icon.FromHandle(bitmap.GetHicon());
m_notifyIcon.Icon = icon;
}

iText - Adding external image using Chunk

I am new to iText and faced with a real interesting case about adding external images to a paragraph. Here is the thing:
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("out2.pdf"));
document.open();
Paragraph p = new Paragraph();
Image img = Image.getInstance("blablabla.jpg");
img.setAlignment(Image.LEFT| Image.TEXTWRAP);
// Notice the image added to the Paragraph through a Chunk
p.add(new Chunk(img2, 0, 0, true));
document.add(p);
Paragraph p2 = new Paragraph("Hello Worlddd!");
document.add(p2);
gives me the picture and "Hello Worlddd!" string below. However,
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("out2.pdf"));
document.open();
Paragraph p = new Paragraph();
Image img = Image.getInstance("blablabla.jpg");
img.setAlignment(Image.LEFT| Image.TEXTWRAP);
// Notice the image added directly to the Paragraph
p.add(img);
document.add(p);
Paragraph p2 = new Paragraph("Hello Worlddd!");
document.add(p2);
gives me the picture and string "Hello worlddd!" located on the right hand side of the picture and one line above it.
What is the logic behind that difference?
The behaviour you described is because in the second code snippet the Paragraph doesn't adjust its leading, but adjust its width. If in the second snippet you add the line
p.add("Hello world 1")
just before
p.add(img)
you'll see the string "Hello world 1" on the left and a little bit above the string "Hello Worlddd!". If you output the leading of p (System.out.println(p.getLeading()) you can see it's a low number (typically 16) and not the height of the image.
In the first example you use the chunk constructor with 4 arguments
new Chunk(img, 0, 0, true)
with the last (true) saying to adjust the leading, so it print as you expected.
If you add an image directly, its alignment properties (set with
setAlignment()) are taken into account. So the image is on the left (Image.LEFT) and the text is wrapped around (Image.TEXTWRAP).
If you wrap the image in a Chunk it is handled as if it were a chunk of
text. So the alignment properties, specific to images, are lost. This results in the text being below the image.
If you try Image.RIGHT, this becomes more apparent. Nothing changes in the first example: the image is still on the left. In the second example, the image is aligned to the right and the text is wrapped left of it.

Resources