adding sticky Notes to pdf using pdfbox - java

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 :-)

Related

JasperReports export to Excel uses only last set background color

Im pretty pretty new to Dynamic-Jasper, but due to work i had to add a new feature to our already implemented solution.
My Problem
The Goal is to add a Column to a report that consists only out of a background-color based on some Information. I managed to do that, but while testing I stumbled upon a Problem. While all my Columns in the html and pdf view had the right color, the Excel one only colored the fields in the last Color.
While debugging i noticed, that the same colored Fields had the same templateId, but while all Views run through mostly the same Code the Excel one showed different behavior and had the same ID in all fields.
My Code where I manipulate the template
for(JRPrintElement elemt : jasperPrint.getPages().get(0).getElements()) {
if(elemt instanceof JRTemplatePrintText) {
JRTemplatePrintText text = (JRTemplatePrintText) elemt;
(...)
if (text.getFullText().startsWith("COLOR_IDENTIFIER")) {
String marker = text.getFullText().substring(text.getFullText().indexOf('#') + 1);
text.setText("ID = " + ((JRTemplatePrintText) elemt).getTemplate().getId());
int rgb = TypeConverter.string2int(Integer.parseInt(marker, 16) + "", 0);
((JRTemplatePrintText) elemt).getTemplate().setBackcolor(new Color(rgb));
}
}
}
The html view
The Excel view
Temporary Conclusion
The same styles uses the same Objects in the background and the JR-Excel export messes something up by assigning the same Object to all the Fields that I manipulated there. If anyone knows of a misstake by me or potential Solutions to change something different to result the same thing please let me know.
Something different I tried earlier, was trying to set the field in an evaluate Method that was called by Jasper. In that method we assign the textvalue of each field. It contained a map with JRFillFields, but unfortunatelly the Map-Implementation denied access to them and just retuned the Value of those. The map was provided by dj and couldn't be switched with a different one.
Edit
We are using JasperReports 6.7.1
I found a Solution, where I replaced each template with a new one that was supposed to look exactly alike. That way every Field has its own ID guaranteed and its not up to chance, how JasperReports handles its Data internaly.
JRTemplateElement custom =
new JRTemplateText(((JRTemplatePrintText) elemt).getTemplate().getOrigin(),
((JRTemplatePrintText) elemt).getTemplate().getDefaultStyleProvider());
custom.setBackcolor(new Color(rgb));
custom.setStyle(((JRTemplatePrintText) elemt).getTemplate().getStyle());
((JRTemplatePrintText) elemt).setTemplate(custom);

Icepdf annotation let page disappear

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.

Itext : set zoom level of external hyper link in pdf

I am using following code to set external hyperlink using itext library in Java.
Chunk chunk = new Chunk("Click to Open File");
PdfAction action = new PdfAction("externalfile.pdf");
action.put(PdfName.NEWWINDOW, PdfBoolean.PDFTRUE);
action.put(PdfName.ZOOM, PdfName.FIT);
chunk.setAction(action);
I want to set zoom level of external hyper link: when I click on hyper link file should be open and FIT page.
I tried using action.put(PdfName.ZOOM, PdfName.FIT); but it's not working.
Please don't ever create PDF object manually without consulting ISO-32000-1.
You want to create a GoToR action. Such an action is expressed as a PDF dictionary that can contain the following keys:
There is no key named Zoom in that table, hence your code is wrong.
You need the D key and as you want to link to a page and define a zoom factor, you need to define a destination:
In other words, the destination needs to be a PdfArray! PdfName.FIT isn't sufficient!
(All screen shots are taken from the copy of ISO-32000-1 that is provided by Adobe on its web site.)
Update:
If you want to add a link to a remote page, you can also follow the example on page 197-198 of iText in Action - Second Edition: see the LinkActions example that uses the gotoRemotePage() method.
Internally, this method looks like this:
public static PdfAction gotoRemotePage(String filename, String dest, boolean isName, boolean newWindow) {
PdfAction action = new PdfAction();
action.put(PdfName.F, new PdfString(filename));
action.put(PdfName.S, PdfName.GOTOR);
if (isName)
action.put(PdfName.D, new PdfName(dest));
else
action.put(PdfName.D, new PdfString(dest, PdfObject.TEXT_UNICODE));
if (newWindow)
action.put(PdfName.NEWWINDOW, PdfBoolean.PDFTRUE);
return action;
}
Note that this assumes that you have a named destination in the target file.
I think that you'd rather want to use the constructor that takes a page number:
public PdfAction(String filename, int page) {
put(PdfName.S, PdfName.GOTOR);
put(PdfName.F, new PdfString(filename));
put(PdfName.D, new PdfLiteral("[" + (page - 1) + " /FitH 10000]"));
}
Of course, this doesn't use PdfName.FIT. If you really want to define the destination yourself, you need a line that looks like this:
put(PdfName.D, new PdfLiteral("[" + (page - 1) + " /Fit]"));

Remove page reference from annotation

I added a textfield with several kids similar as described here. Did that to use the autofill functionality of PDF...
Now my question is how am I able to remove the page reference from the parent element? The data field should not contain a parent reference since it is not related to any page. The widgets should contain those (which I added there however I can't remove the parent /P page reference)
I tried
PdfFormField parent = PdfFormField.createTextField(stamper.getWriter(), false, false, 0);
parent.setFieldName(fieldName);
for (int page = 1; page <= pages; page++) {
TextField textField = new TextField(stamper.getWriter(), new Rectangle(560, 600, 590, 800), null);
PdfFormField pff = textField.getTextField();
parent.addKid(pff);
// add widget to each page
pff.setPlaceInPage(page);
//iText workarounds
field.put(PdfName.P, stamper.getWriter().getPageReference(page));
field.remove(PdfName.FF);
field.remove(PdfName.FT);
}
//in addAnnotation() the page reference is written
stamper.addAnnotation(parent, 1);
//does not work
parent.remove(PdfName.P);
however it didn't work since I guess the page reference is already written. Is there a way to remove it afterwards?
Fact 1:
The PdfFormField class extends the PdfAnnotation class, because most of the times, it is possible to merge a field dictionary with an annotation dictionary.
In your case, you have a PdfFormField that is used as a hierarchical element and although that element is also an instance of PdfAnnotation, it isn't. You can check this by using the isAnnotation() method. It will return false.
Fact 2:
When you add an annotation to an existing PDF, you have to use PdfStamper's addAnnotation() method that not only accepts an annotation object, but also a page number. If you didn't add a page number, PdfStamper wouldn't know where to add the annotation.
When you add a real annotation, PdfStamper will add a /P key that refers to the page where the annotations is visualized.
Fact 3:
There is no other way than using addAnnotation() with an annotation and a page number when working with PdfStamper. When you add a PdfFormField object that is not a real annotation, it won't be treated differently, hence a /P entry will be added, although that doesn't really make sense (as you rightly point out in your question).
Fact 4:
The /P entry is optional. For the widget annotations to be displayed correctly, it is sufficient that they appear in the /Annots of a page.
Conclusion:
iText shouldn't add a /P entry in case you add a PdfFormField that is not a real annotation. Hence I have committed the following change: revision 6756
void addAnnotation(PdfAnnotation annot, int page) {
- annot.setPage(page);
+ if (annot.isAnnotation())
+ annot.setPage(page);
addAnnotation(annot, reader.getPageN(page));
}
I have tested this with the AddFieldAndKids example and it seems to work: the /P entry is no longer added.
This solves your problem in a more robust way. You shouldn't try to remove something that shouldn't have been added in the first place.

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.

Categories