How to set text field value position - java

I need to render the text field value correctly. I render the text field looks like the following:
final Rectangle rect = new Rectangle(100, page.getCropBox().getHeight() - (100 * pdf.getPageNumber(page)), 300, rectHeight);
field = PdfFormField.createText(pdf, rect, name);
field.setFontSize(14);
field.setValue(value);
field.setVisibility(VISIBLE);
form.addField(field, page);
and when I'm saving a pdf I have the result but when I'm trying to edit the following pdf using Acrobat Reader my text . How can I fix this issue?

Related

How to set border, margin and padding on a PDF document using itext 7

I am trying to set border, margin, and padding to a PDF document, is it possible to achieve using itext7
Margin works fine by setting below code
document.setLeftMargin(180);
But the border is not working, below code used to set the border
float width = 1.5f;
Color color = ColorConstants.BLUE;
Border border = new DottedBorder(color,width);
Document document = new Document(pdfDocument);
document.setBorder(border);
Unfortunately it's not possible to specify the document's background and borders just by setting some of the Document's properties. The good news is that iText7 provides us with an opportunity to override DocumentRenderer (renderers are classes responsible to render corresponding model objects, for example, ParagraphRenderer renders Paragraph and so on).
In the custom DocumentRenderer below updateCurrentArea was overridden to tackle the following two issues:
shrinking the area which will be used by DocumentRenderer to layout the Document's children
adding a background Div, whic will be resposible for border rendering (I've also shown how one could set background, if needed)
class CustomDocumentRenderer extends DocumentRenderer {
public CustomDocumentRenderer(Document document) {
super(document);
}
#Override
protected LayoutArea updateCurrentArea(LayoutResult overflowResult) {
LayoutArea area = super.updateCurrentArea(overflowResult); // margins are applied on this level
Rectangle newBBox = area.getBBox().clone();
// apply border
float[] borderWidths = {10, 10, 10, 10};
newBBox.applyMargins(borderWidths[0], borderWidths[1], borderWidths[2], borderWidths[3], false);
// this div will be added as a background
Div div = new Div()
.setWidth(newBBox.getWidth())
.setHeight(newBBox.getHeight())
.setBorder(new SolidBorder(10))
.setBackgroundColor(ColorConstants.GREEN);
addChild(new DivRenderer(div));
// apply padding
float[] paddingWidths = {20, 20, 20, 20};
newBBox.applyMargins(paddingWidths[0], paddingWidths[1], paddingWidths[2], paddingWidths[3], false);
return (currentArea = new RootLayoutArea(area.getPageNumber(), newBBox));
}
}
The last quetions is how to apply it on your document. It could be done as follows:
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outFileName));
Document doc = new Document(pdfDoc);
doc.setRenderer(new CustomDocumentRenderer(doc));
The resultant PDF:

how to get the content of a JTextPane with its styled format

I am trying to make a simple word processor that edits the text to make it bold, italic, underline, background color and foreground color. The problem is I want to set the contents/text of the JTextPane with all its edited attributes to a single object to save it to another class as a data field which have other data fields like date created and the name of the document given by the user.
I think the best approach its using html as content type for the Text Pane and string builders.
for example,
TextPane tp = new JTextPane();
tp.setContentType("text/html");
StringBuilder sb = new StringBuilder();
sb.append("<span style=\"color:red\">" + Hello red + "</span>");
sb.append("<span style=\"color:blue\">" + Hello blue + "</span>");
...
tp.setText(sb); // will print text with the style
works the same in the other way,
String txt = tp.getText();
System.print(txt); //wil show html code
You can reference http://www.java2s.com/Tutorials/Java/Swing_How_to/JTextPane/Style_JTextPane_with_HTML_and_CSS.htm

How to replace a string by an URL in PDFBOX?

I have a text PDF with a string "Click Here".
How can I replace "Click Here" by a URL using PDFBox?
Aka "Click Here" will be in blue and clickable after the replacement in the updated (or new) PDF.
This is from the AddAnnotations.java example from the source code download:
PDBorderStyleDictionary borderULine = new PDBorderStyleDictionary();
borderULine.setStyle(PDBorderStyleDictionary.STYLE_UNDERLINE);
borderULine.setWidth(1);
PDAnnotationLink txtLink = new PDAnnotationLink();
txtLink.setBorderStyle(borderULine);
// Set the rectangle containing the link
textWidth = font.getStringWidth("Click here") / 1000 * xscale;
position = new PDRectangle();
position.setLowerLeftX(...);
position.setLowerLeftY(...);
position.setUpperRightX(...);
position.setUpperRightY(...);
txtLink.setRectangle(position);
// add an action
PDActionURI action = new PDActionURI();
action.setURI("http://pdfbox.apache.org");
txtLink.setAction(action);
annotations.add(txtLink);
You'll need to know the coordinates of your "Click Here" and the x scaling factor and the font, obviously. In PDF, (0,0) is the bottom left. 1 Unit = 1/72 inch. It is easiest if you generate the file yourself. If it is an existing file, you'll have to analyse the file with PDFDebugger or do some trial and error.

itextpdf : PdfPTable dont takes height

I migrated my publisher core from itext-2.... to itextpdf-5.5.6
In itext-2 I had an image into cell of a PdfPTable an my table's celle had same size that image.
Now, in itextpdf-5.5.6 image have small size like that cell.
For example I use
table.setTotalWidth(450);
and in core itextpdf it calculate height too :
//ITEXTPDF
calculateWidths();
calculateHeights();
calculateHeight call to getRowHeight
getRowHeight(k, true);
and it does not do the same that in itext-2 :
itext returns 400
itextpdf returns 23
I debbuged for itextpdf and found that in pdfLine.getMaxSize(..) there is the following code :
if (chunk.isImage()) {
Image img = chunk.getImage();
if (chunk.changeLeading()) {
float height = chunk.getImageHeight() + chunk.getImageOffsetY() + img.getSpacingBefore();
image_leading = Math.max(height, image_leading);
}
}
and in the case of itext i have no condition like that :
if (chunk.changeLeading()){......}
Is there an average for solve it ?
I found where was problem :
Paragraph para = new Paragraph(new Chunk((Image) element, 0, 0, Boolean.FALSE));
the last parameter of chunk is changeLeading and it just had to change as Boolean.TRUE
Thanks everybody for your responses

Define attributes of images generated by canvas2image e.g. alt tag

I am using the following code to convert html>canvas>image
image.src = canvas.toDataURL('jpeg',1.0);
$('.imagediv').html(image);
////This is just a snippet
My problem is that I want do define the other image attributes width,height,alt,class and a neat file name image.jpg. Like you can see, the image needs to be displayed in the browser on conversion.
What you want to do is really simple (and the answer is in the sample code that you added): just define the different attributes in the same way that you did the image source:
.src: to specify the picture source.
.width: to specify the element's width.
.height: to specify the element's height.
.alt: to specify an alternative text.
.title: to specify the title.
.className: to specify the class.
.id: to specify the element id.
etc...
Or if you want, you could use the setAttribute() method instead:
image.setAttribute("alt", "I am the alternative text");
Here is a simple demo of setting different attributes to an image generated using canvas:
// get the canvas for manipulation
var canvas = document.getElementById("myCanvas");
var context = canvas.getContext('2d');
// draw a square this is just a test
context.moveTo(5,5);
context.lineTo(395,5);
context.lineTo(395,195);
context.lineTo(5,195);
context.lineTo(5,5);
context.fillStyle = "#FF0000";
context.fillRect(5,5,390,190);
context.stroke();
// create the image and set the attributes
var image = new Image();
image.src = canvas.toDataURL('jpeg', 1.0);
image.alt = "A simple red rectangle with black border";
image.title = "Red rectangle with black border";
image.width = 400;
image.height = 200;
image.className = "myClass";
// place the image inside the div
document.getElementById('imagediv').appendChild( image );
.myClass {
box-shadow:2px 2px 8px red;
}
<canvas id="myCanvas" width="400" height="200" style="display:none;"></canvas>
<div id="imagediv"></div>
The only one that would be more complicated is the "neat file name". toDataURL method returns a data URI containing a representation of the image (in base64), and that's not a nice looking name. If you want to display a nice name, you will need to save the file and then point to it.
If what you want is a neat file name because the user will be able to download the picture using a link, what you can do is set the download attribute in the anchor and specify the name there.
Something like this:
// get the canvas for manipulation
var canvas = document.getElementById("myCanvas");
var context = canvas.getContext('2d');
// draw a square this is just a test
context.moveTo(5,5);
context.lineTo(395,5);
context.lineTo(395,195);
context.lineTo(5,195);
context.lineTo(5,5);
context.fillStyle = "#FF0000";
context.fillRect(5,5,390,190);
context.stroke();
// set the image as the href of the anchor
document.getElementById("myA").href = canvas.toDataURL('jpeg', 1.0);
<canvas id="myCanvas" width="400" height="200" style="display:none;"></canvas>
Download picture

Categories

Resources