I've been trying to add annotations to an existing PDF using the iText API and the older Lowagie version. However, I need alternatives to the API since it does not seem to be able to do what is asked in our requirement.
The requirement is to put an Annotation into an existing PDF with the following details:
Type: plain text
Postion: x=0mm && y=0mm
Font: Arial
Text Colour: White
Text Content: "some text"
Using iText, I can put in an annotation but I need to approximate in pixels where in my A4 size page I should put it. The closest approximation is using
PdfReader reader = new PdfReader(headerFilePath.concat(xmlFileName));
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(headerFilePath.concat(xmlFileNameNew)));
PdfAnnotation annotation = PdfAnnotation.createText(stamper.getWriter(), new Rectangle(0, 842, 5, 842), "some text", "some text", true, null);
annotation.setColor(Color.WHITE);
stamper.addAnnotation(annotation, 1);
reader.close();
stamper.close();
This snippet places it at the top left corner but I'm not sure if it's 0mm,0mm. Also it is black and I cannot specify the font.
Any help on the matter is greatly appreciated. Thanks!
You may try rich text annotions as described here:
annotation.put(PdfName.RC, new PdfString( "<font size=\"whatever\">" +
some text+
"</font>" ) );
myStamper.setGenerateAppearances( false );
However I doubt that this is what you need. If you add an appearance then you'll get an rectangular icon in the upper left corner. If you then hover with you mouse over it you are able to read the annotation in form of a pop up. And even if you get a white font color of the annotation text you can't hide the rectangular icon of the annotation itself...
You want to have a white font color which indicates that you want to "transport" some hidden (white color on white background) information. Maybe in this case you may use the following mechanism ( which is normally used for adding watermarks etc.):
String text;
int pageNumber
PdfContentByte overContent = stamper.getOverContent(pageNumber);
overContent.beginText();
overContent.setFontAndSize(yourFont, yourFontSize);
//overContent.setGrayFill(...);
overContent.showTextAligned(PdfContentByte.ALIGN_CENTER, yourText + " Center", 150, 760, 0);
overContent.endText();
Update: To set an annotation to invisble add to your code:
annotation.setColor(Color.WHITE);
//set visibility to 0 (invisble). All values between 0...1 are possible
//so 0.5 means 50% opacity
annotation.put(PdfName.CA, new PdfNumber(0));
Related
I'm trying to test new lead creation in Salesforce Lightning (Sales app). When correct text is entered into the shown mandatory field(s), the color around text box element changes (from white to light yellow) as shown in images below. I've tried capturing that via getCssValue(String attribute) method of selenium by passing 'background-color' and 'color' attributes, but to no avail. Please tell how to get this color change in Selenium before & after text is entered in mandatory field (e.g Last Name). Your help would be highly appreciated. (Note: Please overlook the red underline. I used it to highlight the field via Snipping Tool)
You can get the element color(Background color of element) by:
element.getCssValue("background-color");
You can get the element text/caption color by:
element.getCssValue("color");
For example if you want to get the background and text color of "Sign in" button for LinkedIn, the code is as follows:
driver.get("https://www.linkedin.com/");
String buttonColor = driver.findElement(By.name("submit")).getCssValue("background-color");
String buttonTextColor = driver.findElement(By.name("submit")).getCssValue("color");
System.out.println("Button color: " + buttonColor);
System.out.println("Text color " + buttonTextColor);
do not forget to import
import org.openqa.selenium.support.Color;
The above code will return rgb value. If you want to convert to hex, you can go to
getCssValue (Color) in Hex format in Selenium WebDriver
I'm trying to create a button that has two lines of text in it. This is easy enough to do using the <br/> tags in <html> however the larger line is center aligned but the smaller line is floating left:
Below is my code for generating this button (I've wrapped the text so it doesn't look ugly). Is it possible to get the first line "View Config File" to appear centrally aligned, keeping the central alignment for the second line?
JButton viewConfigFile = new JButton("<html>View Config File" +
"<br/>Be careful of any changes made</html>");
Original suggestion was to use the center tag to wrap the text but Pete pointed out this tag is obsolete and deprecated in HTML 4 (so I don't recommend to use this).
JButton button = new JButton("<html><center>View Config File"
+ "<br/>Be careful of any changes made</center></html>");
Instead you can use the CSS text-align property:
JButton button = new JButton("<html><div text-align:center>View Config File" + "<br/>Be careful of any changes made</div></html>");
See: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/center
I have a script that creates a PDF file and writes contents to it. After the execution is complete I need to write the status (fail, success) to the PDF, but the status should be on the top of the page. So the solution I came up with is to use absolute positioned text. Below is my code for the same
PdfContentByte cb = writer.DirectContent;
BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb.SaveState();
cb.BeginText();
cb.MoveText(700, 30);
cb.SetFontAndSize(bf, 12);
cb.ShowText("My status");
cb.EndText();
cb.RestoreState();
But as the PDF creates multiple pages, this is added to the last page of the PDF. How can I add it to the 1st page??
Also is there a way to calculate the top coordinates of the page, ie the top-left coordinate?
iText was written with internet applications in mind. It was designed to flush content from memory as soon as possible: if a page is finished, that page is sent to the OutputStream and there is no way to return to that page.
That doesn't mean your requirement is impossible. PDF has a concept known as Form XObject. In iText, this concept is implemented under the name PdfTemplate. Such a PdfTemplate is a rectangular canvas with a fixed size that can be added to a page without being part of that page.
An example should clarify what that means. Please take a look at the WriteOnFirstPage example. In this example, we create a PdfTemplate like this:
PdfContentByte cb = writer.getDirectContent();
PdfTemplate message = cb.createTemplate(523, 50);
This message object refers to a Form XObject. It is a piece of content that is external to the page content.
We wrap the PdfTemplate inside an Image object. By doing so, we can add the Form XObject to the document just like any other object:
Image header = Image.getInstance(message);
document.add(header);
Now we can add as much data as we want:
for (int i = 0; i < 100; i++) {
document.add(new Paragraph("test"));
}
Adding 100 "test" lines will cause iText to create 3 pages. Once we're on page 3, we no longer have access to page 1, but we can still write content to the message object:
ColumnText ct = new ColumnText(message);
ct.setSimpleColumn(new Rectangle(0, 0, 523, 50));
ct.addElement(
new Paragraph(
String.format("There are %s pages in this document", writer.getPageNumber())));
ct.go();
If you check the resulting PDF write_on_first_page.pdf, you'll notice that the text we've added last is indeed on the first page.
I'm building a small conversation agent, where the text looks like follows:
I would like to set the System's text to always be red. The text is all placed in a JTextPane.
How can I accomplish this? I have tried doing the following:
agentTextPane.setForeground(Color.red); after the system's text is added, and then switching back to black, however that changes all the text in the JTextPane.
This is how the system's text is added:
//'output' is a stringBuilder
output.append("\nSystem: ").append(tempOutput).append("\n");
agentTextPane.setText(output.toString());
As shown here, you can define an attribute set representing a desired style. For example,
StyledDocument doc = (StyledDocument) jtp.getDocument();
SimpleAttributeSet system = new SimpleAttributeSet();
StyleConstants.setFontFamily(system, "Serif");
StyleConstants.setForeground(system, Color.red);
doc.insertString(doc.getLength(), "...", system);
The styles can be progressive, as shown here.
See Text Component Features for more examples.
You may want to use HTML tags to format your Strings in terms of colour. The following reference may be useful. setting JTextPane to content type HTML and using string builders
output.append("<font color=\"red\">");
output.append("\nSystem: ").append(tempOutput).append("\n");
output.append("</font>");
I am using PDFBox for generating a pdf from one of my user inputted forms. For drawing a line I am using PDPageContentStream.drawLine and for text PDPageContentStream.drawString. The text works perfect but while using drawLine, when I try to print the pdf, I get the error as shown in the attached Image. My code looks like this
PDPage page = new PDPage();
release.addPage(page);
contentStream = new PDPageContentStream(release,page);
int margin = 40;
vertHeight -= thisFontHeight * fontSize * 1.05f + 5;
contentStream.drawLine(margin,vertHeight + margin - 5, page.getMediaBox().getWidth() - margin, vertHeight + margin - 5)
Any help appreciated
Your code sample doesn't show it, but I suspect you're mixing lines and text. You must not draw lines between BT and ET.
begin Text
draw some text
end text
draw a line
begin text
draw some text
end text
draw some more lines
etc
If you drew a line between 1 and 3, for example, you'd get the above error (or one similar to it).
PS: If that's not it, we'll need a sample PDF to diagnose the issue.