can anyone please help me with the java code to use push button as an image field using itext 5. I have tried the below code but does not work
com.itextpdf.text.pdf.PushbuttonField button = new com.itextpdf.text.pdf.PushbuttonField(writer, new com.itextpdf.text.Rectangle(90, 500, 140, 800), "submit");
button.setText("POST");
button.setBackgroundColor(new com.itextpdf.text.BaseColor(255, 255, 255));
button.setVisibility(com.itextpdf.text.pdf.PushbuttonField.VISIBLE_BUT_DOES_NOT_PRINT);
com.itextpdf.text.pdf.PdfFormField submit = button.getField();
submit.setAction(com.itextpdf.text.pdf.PdfAction.createSubmitForm("/book/fdf", null, 0));
writer.addAnnotation(submit);
com.itextpdf.text.pdf.TextField file = new com.itextpdf.text.pdf.TextField(writer, new com.itextpdf.text.Rectangle(90, 500, 140, 800), "image");
file.setOptions(com.itextpdf.text.pdf.PushbuttonField.FILE_SELECTION);
com.itextpdf.text.pdf.PdfFormField upload = file.getTextField();
upload.setAdditionalActions(com.itextpdf.text.pdf.PdfName.U, com.itextpdf.text.pdf.PdfAction.javaScript("this.getField('image').browseForFileToSubmit();"+ "this.getField('submit').setFocus();", writer));
writer.addAnnotation(upload);
try this one:
<div class="col-md-2">
<label for="pre">
Your Image
</label>
<a id="pre" onclick="$('#imagetr').trigger('click'); ">
<img id="preview" class="col img img-fluid" src="images/defaultloadimage.jpg" title="your default place holder" />
</a>
<input type="file" name="image" id="imagetr" style="display:none;" />
<script>
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#preview').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
$("#imagetr").change(function () {
readURL(this);
});
</script>
Related
How can I display an image I uploaded immediately in HTML using button like a profile picture
Thank you
<div>
<img id="myImg" class="img img-circle" src="#" alt="your image"
height=200 width=100>
</div>
<input type='file' />
$(function () {
$(":file").change(function () {
if (this.files && this.files[0]) {
var reader = new FileReader();
reader.onload = imageIsLoaded;
reader.readAsDataURL(this.files[0]);
}
});
});
function imageIsLoaded(e) {
$('#myImg').attr('src', e.target.result);
$('#yourImage').attr('src', e.target.result);
};
So you essentially want an image preview without a page reload? Here is some code that will do just that.
HTML:
<img id="myImg" alt="your image" class="img img-circle" style="visibility: hidden; display: none" height="200" width="100">
<input type="file" onchange="showimagepreview(this)" accept=".png,.jpg,.jpeg,.gif" />
JavaScript:
function showimagepreview(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
document.getElementById("myImg").setAttribute("src", e.target.result);
document.getElementById("myImg").style = "visibility: visible; display: inline";
}
reader.readAsDataURL(input.files[0]);
}
}
Your code already successfully displays the image preview. As far as I understand your question you intend to display the full image on button click.
You will need an image for that
<img id="yourImage">
on file upload you will need to store the src somewhere, for the sake of simplicity that will be a global variable here:
var src = "";
and then:
function imageIsLoaded(e) {
$('#myImg').attr('src', e.target.result);
src = e.target.result;
};
and the button click:
$("#yourbutton").click(function() {
$("#yourImage").att("src", src);
});
So basically you want to use an image as a button.Here is the solution you are looking for
<button>
<img src="http://www.w3.org/html/logo/downloads/HTML5_Logo_32.png" id="dagger" />
</button>
$(function(){
$("#dagger").click(function(){
alert("click");
});
});
Trying to upload an image file with preview using Jquery. It's working and I got the preview, but when I click on the upload button I saw an error message
Uncaught RangeError: Maximum call stack size exceeded
in the console.
Here is the code:
$(document).ready(function() {
var readURL = function(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function(e) {
$('.profile-image').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
$(".file-upload").on('change', function() {
readURL(this);
});
$(".upload-button").on('click', function() {
$(".file-upload").click();
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<img class="profile-image" src="demo.png" alt="Profile Image">
<a class="profile-img-edit upload-button">
<i class="fas fa-camera"></i> Select Image
<input class="file-upload" type="file" name="file" size="40" accept=".png, .jpg, .jpeg, .gif">
</a>
</div>
The issue is because the click event on the .file-upload element bubbles up to .upload-button and is caught by the click handler, which triggers a click handler on .file-upload which bubbles up to .upload-button which triggers a click... and so on.
To fix this, stop the event from bubbling up from the .file-upload by adding a click event handler to it and calling stopPropagation() on the event, like this:
$(document).ready(function() {
var readURL = function(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function(e) {
$('.profile-image').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
$(".file-upload").on('change click', function(e) {
e.stopPropagation();
readURL(this);
});
$(".upload-button").on('click', function() {
$(".file-upload").click();
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<img class="profile-image" src="demo.png" alt="Profile Image">
<a class="profile-img-edit upload-button">
<i class="fas fa-camera"></i> Select Image
<input class="file-upload" type="file" name="file" size="40" accept=".png, .jpg, .jpeg, .gif">
</a>
</div>
Having a hard time fixing my two upload file.My Problem is when i upload a photo in the first form file, the preview displays same photo both in the two form for file and ngo_logo and the same as when i upload photo in the second form file for ngo_logo, it displays the same photo both in the form.The first one is always overridden.Anyone could help me out here is much appreciated.
<div class="col-md-6">
<div class="form-group">
<label class="control-label">Proof of Accreditation<em>(Upload File)</em>:</label>
{!! Form::file('file',['class'=>'form‐control','id'=>'file' ]) !!}
</div>
<div class="form-group">
<img id="fil" src="#" name="file" class="img-responsive well" alt="Image" />
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label class="control-label">Organizational Logo:</label>
{!! Form::file('ngo_logo',['class'=>'form‐control','id'=>'ngo_logo' ]) !!}
</div>
<div class="form-group">
<img id="logo" src="#" name="ngo_logo" class="img-responsive well" alt=" Your Image" />
</div>
</div>
/Javascript/
<script type="text/javascript">
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#fil').attr('src', e.target.result);
//$('#logo').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#logo').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}/**end of function readURL(input)**/
$("#file").change(function(){
readURL(this);
});
$("#ngo_logo").change(function(){
readURL(this);
});
</script>
You don't need to read file twice, just need to target correct element and set src.
You can use DOM relationship to target the desired element and set the SRC. there are various methods to traverse DOM with current element context i.e. event.target
function readURL(event) {
var input = event.target;
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$(event.target)
.closest('form-group') //Target parent form control div
.next() //Target sibling of parent form control
.find('img') //Target Ima
.attr('src', e.target.result); //Set image
}
reader.readAsDataURL(input.files[0]);
}
}
$("#file, #ngo_logo").change(readURL);
References
.closest()
.find()
.next()
As an alternative, you can associate the element using data-* prefixed attributes.
{!! Form::file('file',['class'=>'form‐control','id'=>'file', data-target=>'#fil' ]) !!}
{!! Form::file('ngo_logo',['class'=>'form‐control','id'=>'ngo_logo', data-target=>'#logo' ]) !!}
and in the event handler read it using $.fn.data() or Element.dataset property.
function readURL(event) {
var input = event.target;
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$(event.target).data('target').attr('src', e.target.result);
//$(event.target.dataset.target).attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
{!! Form::file('file',['class'=>'form‐control','id'=>'file' ]) !!}
Replace id=>file with id=>fil because your image id is fil.
<img id="fil" src="#" name="file" class="img-responsive well" alt="Image" />
Ok, i have this function right here:
function realImgDimension(img) {
var i = new Image();
i.src = img.src;
return {
naturalWidth: i.width,
naturalHeight: i.height
};
}
var myImage = document.getElementById('blah');
myImage.addEventListener('load', function() {
var realSize = realImgDimension(this);
console.log('My width is: ', realSize.naturalWidth);
console.log('My height is: ', realSize.naturalHeight);
});
when I upload image that is for example 1530x2500 it prints that width is 2500 and height 1530. Why is this happening? If I have an image 2500x1300 it prints it corrctly. So something happens with the images that their width is smaller than their height, or images that are "tall"
Any ideas?
this is what i call onchange
function readURL(files) {
if (files && files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#blah')
.attr('src', e.target.result)
.width($(window).width() * 0.3)
};
reader.readAsDataURL(files[0]);
}
}
and this is my form
<div class="form-group">
{{ Form::label('image', 'Slider Image') }}<br>
<img id="blah" alt="your image" style="background-color:black; margin-top:10px; border-radius:5px; image-orientation: from-image;"/><br><br>
<div style="position:relative;">
<a class='btn btn-primary' href='javascript:;'>
Choose File...
<input type="file" style='position:absolute;z-index:2;top:0;left:0;filter: alpha(opacity=0);-ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";opacity:0;background-color:transparent;color:transparent;' name="image" id="image" size="40" accept="image/gif, image/jpeg, image/png" onchange="readURL(this.files);">
</a>
<span class='label label-info' id="upload-file-info"></span>
</div>
</div>
You should use/set images to dimensions with a power of two (i.e. 2500, 1600). You should also check orientations whether printing a portrait on a landscape and vice versa. Maybe this documentation may help you: WebGL and the power of two image size
I am using javascript code.this code show the image which image you select before uploading.
Javascript
<script>
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#blah').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
$("#imgInp").change(function(){
readURL(this);
});
</script>
Html
<form id="imageform" method="post" enctype="multipart/form-data" >
<input type="file" name="photoimg" id="imgInp" >
<img id="blah" src="#" alt="your image" />
<input type="submit" Value="Upload">
</form>
This piece of code work perfectly.SO i need normaly when image not select than image dispaly:none; and if image select than show image.i want to handle this problem with client side scripting.
Modify the markup so that the image is hidden by default:
<img id="blah" src="#" alt="your image" style="display:none" />
And then call the jQuery show method on the image when you have read the file like so:
$('#blah').attr('src', e.target.result);
$('#blah').show();
Fiddle is here
Try
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#blah').prop('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
jQuery(function () {
$("#imgInp").change(function () {
readURL(this);
});
})
Demo: Fiddle