Icepdf annotation let page disappear - java

I'm developing an application that use Icepdf to show a pdf and draw something upon it.
I have a square annotation floating over the page but i have a nasty issue.
If i change page and come back to the page where the annotation is visible, if i move the annotation by dragging it the content of the page disappear becoming completely blank. This is the portion of code i use to show the moving annotation on top of the pdf.
Probably I'm missing something or doing it in wrong way.
controller.setAnnotationPanel(null);
controller.setDocumentToolMode(DocumentViewModelImpl.DISPLAY_TOOL_SELECTION); controller.getDocumentViewController().setViewCursor(DocumentViewController.CURSOR_SELECT);
Rectangle bbox = new Rectangle((int) (getPageWidth(getLastPage()) - SIGN_BOX_WIDTH - SIGN_BOX_MARGIN), SIGN_BOX_MARGIN, SIGN_BOX_WIDTH, SIGN_BOX_HEIGHT);
int index = getLastPage() - 1;
Document document = controller.getDocument();
DocumentViewModel documentViewModel=controller.getDocumentViewController().getDocumentViewModel();
PageTree pageTree = document.getPageTree();
Page page = pageTree.getPage(index);
// create and init the page's annotation components. [b]
java.util.List<AbstractPageViewComponent> pageComponents=controller.getDocumentViewController().getDocumentViewModel().getPageComponents();
AbstractPageViewComponent pageViewComponent = pageComponents.get(getLastPage() - 1);
tsq = (SquareAnnotation) AnnotationFactory.buildAnnotation(controller.getDocument().getPageTree().getLibrary(),Annotation.SUBTYPE_SQUARE,bbox);
BorderStyle bs = new BorderStyle();
bs.setBorderStyle(BorderStyle.BORDER_STYLE_SOLID);
bs.setStrokeWidth(2.0f);
tsq.setColor(Color.red);
tsq.setBorderStyle(bs);
tsq.setBBox(bbox);
tsq.setRectangle(bbox);
AffineTransform at = controller.getDocument().getPageTree().getPage(getLastPage() - 1).getPageTransform(controller.getDocumentViewController().getDocumentViewModel().getPageBoundary(), controller.getUserRotation(), controller.getUserZoom());
tsq.resetAppearanceStream(at);
SquareAnnotationComponent annotationComponent= (SquareAnnotationComponent) AnnotationComponentFactory.buildAnnotationComponent(tsq,controller.getDocumentViewController(),pageViewComponent,controller.getDocumentViewController().getDocumentViewModel());
annoCB.setCurrentAnnotation(annotationComponent);
controller.getDocument().getPageTree().getPage(getLastPage() - 1).addAnnotation(tsq);
Please assist to solve.

I had to work around on this, no real clue hot to solve.

Related

adding sticky Notes to pdf using pdfbox

I am trying to add sticky notes to an existing pdf using pdfbox.
PDRectangle position = new PDRectangle();
System.out.println(textPosition.getXDirAdj());
position.setUpperRightX(textPosition.getX());
position.setUpperRightY(ph - textPosition.getY());
PDAnnotationTextMarkup polygonMarkup = new PDAnnotationTextMarkup(PDAnnotationMarkup.SUB_TYPE_POLYGON);
polygonMarkup.setContents("text");
polygonMarkup.setRectangle(position);
polygonMarkup.setLocked(true);
polygonMarkup.setReadOnly(true);
annotations.add(polygonMarkup);
page.setAnnotations(annotations);
this code is working fine, but it doesn't create a sticky Note which is my primary concern.
any leads are appreciated.
As Quoted by #mkl
According to the PDF specification, 'a text annotation represents a “sticky note” attached to a point in the PDF document.' Thus, neither the class PDAnnotationTextMarkup nor the subtype SUB_TYPE_POLYGON appears to match your requirements. Instead, you should use the PDAnnotationText class. As an aside, PDAnnotationTextMarkup is documented (JavaDocs) to be the abstract class that represents a text markup annotation. While it is not actually declared abstract, that characterization should make clear that it probably does not work without further ado.
so I used the below code and it worked like magic for me
PDRectangle position = new PDRectangle();
position.setUpperRightX(textPosition.getX());
position.setUpperRightY(ph - textPosition.getY());
position.setLowerLeftX(textPosition.getX()-4);
position.setLowerLeftY(ph - textPosition.getY());
PDGamma colourBlue = new PDGamma();
colourBlue.setB(1);
PDAnnotationText text = new PDAnnotationText();
text.setContents(commentNameWithComments.get(word));
text.setRectangle(position);
text.setOpen(true);
text.setConstantOpacity(50f);
assert annotations != null;
annotations.add(text);
page1.setAnnotations(annotations);
replaceText(word);
it might be useful for future devs :-)

Is it possible to add 'Open a web link action' action to acroform field programmatically using iText?

In Adobe Acrobat there is a possibility to add 'Open a web link action' to acroform. Is it possible to do so with iText usind already existing acroform?
I was unable to find any mention about it at iText docs and therefore tried to create new acrofield programmatically and add this action to it, but without success. Here's my code:
PdfReader pdfReader = new PdfReader(templateStream);
PdfStamper stamper = new PdfStamper(pdfReader, new FileOutputStream("delivery.pdf"));
stamper.setFormFlattening(true);
stamper.getAcroFields().setField("package", packages);
stamper.getAcroFields().setField("purchase_id", purchaseId);
stamper.getAcroFields().setField("activation_code", activationCode);
if (partner != "") {
PdfFormField field = PdfFormField.createTextField(stamper.getWriter(), false,
false, 100);
field.setFieldName("partner");
PdfAction action = new PdfAction(partner);
field.setAction(action);
field.setColor(new BaseColor(0,0,255));
PdfAppearance appearance = stamper.getUnderContent(1).
createAppearance(200, 20);
appearance.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED), 12f);
appearance.setColorFill(BaseColor.BLUE);
field.setAppearance(PdfAnnotation.APPEARANCE_DOWN, appearance);
field.setDefaultAppearanceString(appearance);
stamper.getAcroFields().setField("partner", "Click here to show partner's web site");
}
The resulting PDF document is shown without partner field. Please point me to some docs or to mistake at my code.
You are trying to add interactivity to a form. However, you are also throwing away all interactivity by using this line:
stamper.setFormFlattening(true);
You also claim that you are adding an extra field. As far as I can see, that claim is false. You create a field name field (and you create it the hard way; I would expect you to use the TextField class instead). However, I don't see you adding that field anywhere. I miss the following line:
stamper.addAnnotation(field, 1);
Note that this line doesn't make sense:
stamper.getAcroFields().setField("partner", "Click here to show partner's web site");
Why would you create a field first (and possibly add a caption) and then change it immediately afterwards? Why not create the field correctly from the start?
Finally, it seems that you want to create a button that can be clicked by people. Then why are you creating a text field? Wouldn't it make more sense to create a push button?
This is an example of a question to which a machine would respond: Too many errors... Maybe you should consider reading the documentation before trying to fix your code.

Programatically getting a screenshot of a Java applet

I have tried to devise a way to get a screenshot of a Java applet running in a browser, but I can't seem to get it working. I managed successfully to use cutycapt to get screenshots fine from "normal" websites, but I soon found out that qtwebkit which it seems to rely on for the rendering does not support java. I also tried IEcapt thinking that it would somehow inherit the Java rendering capabilities of IE on the system, but it does not work. Flash also does not seem to be working in IEcapt, and it has no flags for enabling plugins, so I am assuming the functionality is not there either.
Does anyone have any thoughts on how you could render something like an /index.jsp to an image from a Windows or Linux command line?
Selenium webdriver might be useful here:
http://docs.seleniumhq.org/projects/webdriver/
It is used primarily for test automation but it might be helpful.
It could be used, for example, like this:
import org.openqa.selenium.*;
WebDriver driver = new FirefoxDriver(); // create a Firefox webdriver instance
driver.get("http://www.google.com/"); // navigate to page
File screenshotFile = ((Screenshot)driver).getScreenshotAs(file); // capture screenshot
// and save to a file
// here you can trigger any necessary actions on the website:
Webelement element = driver.findElement(By.name("q")).sendKeys("xxxxx");
element.click();
new WebDriverWait(driver, 10)).until(ExpectedConditions.titleContains("xxxxx"));
// and capture a new screenshot once the content has changed
File xxxScreenshotFile = ((Screenshot)driver).getScreenshotAs(file);
Have you tried using java.awt.Robot?
Rectangle rect = yourGragphicsConfiguration.getBounds();
BufferedImage image = new Robot().createScreenCapture(rect);
If you know the position of your applet you might be able to get it with
BufferedImage dest = image.getSubimage(appletX, appletY, appletHeight, appletWidth);
You can take a screenshot of Swing/AWT component.
This can be done in 2 ways. In both cases the component must be visible.
Without the robot use:
BufferedImage image = new BufferedImage(component.getWidth(),
component.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics g = image.getGraphics();
component.paint(g);
With the robot use:
In this case the screenshot of area in which this component is situated will be made. That is, if the component overlaps another application window then the screenshot will contain an area of this another window.
Point point = new Point(0, 0);
SwingUtilities.convertPointToScreen(point, component);
Rectangle region = component.getBounds();
region.x = point.x;
region.y = point.y;
BufferedImage image= new Robot().createScreenCapture(region);
This information is taken from the article: Frequently Asked Questions during Java applet development

iText error while trying to set an image field

I am using iText 5.3.4 and trying to set an image field, the below code is what Bruno suggested that I use however I am getting an error of "The type of the expression must be an array type but it resolved to List".
I see no documentation on how to write the code the way that the api is asking for.
Does anyone have an idea on how this should be written????
Or how to send an image to an image field?
Rectangle rect = form.getFieldPositions(fieldName)[0].position;
int page = form.getFieldPositions(fieldName)[0].page;
image1.scaleAbsolute(rect.getWidth(), rect.getHeight());
img.setAbsolutePosition(rect.getLeft(), rect.getBottom());
stamper.getOverContent(page).addImage(img);
The return from getFieldPositions is of type List<AcroFields.FieldPosition>, so the complaint is valid. You should use .get(0) rather than [0] to reference the first item in the collection.
A simple refactoring might be:
List<AcroFields.FieldPosition> positions = form.getFieldPositions(fieldName);
Rectangle rect = positions.get(0).position;
int page = positions.get(0).page;
image1.scaleAbsolute(rect.getWidth(), rect.getHeight());
img.setAbsolutePosition(rect.getLeft(), rect.getBottom());
stamper.getOverContent(page).addImage(img);

Blackberry: BrowserField2 API scrolling issue on OS 5.0

I have strange issue with browseField2 API on device 9700 (BB OS 5.0).
Browser field is displays blank white space exactly of screen height after rendering HTML content of the browser field. I have added browser field on VerticalFieldManager and this manager also has other controls on it. I also tried with directly adding browse field on screen in combination of other controls on screen, but it doesn’t worked. My goal is to add browser field on screen which has others controls added on it. Also scrolling between controls should be proper.
Here is code snippet which I am using –
BrowserFieldConfig myBrowserFieldConfig = new BrowserFieldConfig(); myBrowserFieldConfig.setProperty(BrowserFieldConfig.NAVIGATION_MODE,BrowserFieldConfig.NAVIGATION_MODE_CARET);
myBrowserFieldConfig.setProperty(BrowserFieldConfig.JAVASCRIPT_ENABLED,Boolean.TRUE);
browseField = new BrowserField(myBrowserFieldConfig);
LOMABrowseField2Listener listener = new LOMABrowseField2Listener();
ProtocolController protocolController = new ProtocolController(browseField);
myBrowserFieldConfig.setProperty(BrowserFieldConfig.CONTROLLER, protocolController);
String myProtocol = "http";
protocolController.setNavigationRequestHandler(myProtocol, listener);
browseField.displayContent(htmlSpecificContent,"");
Please help me out.
Thanks in Advance,
I have solved this issue. This was not the issue with browser field instead the screen which has the browser field was vertically scrollable. The fix is the screen should be vertically non scrollable.

Categories