问题
I am using Pdfbox https://github.com/TomRoush/PdfBox-Android
to add signature(image) on pdf i want to add signature using drag and drop i have implemented some code using touch listener please see and help me i am not getting exact x and y coordinates to add it in pdfbox code from touch listener and i also try drag and drop codehttps://www.tutorialspoint.com/android/android_drag_and_drop.htm
but it won't help. when i test the app and drop the signature(image) on pdf the result pdf won't get the signature on right position. please help your little brother and guide me if anything wrong with the code
Here is the code of Pdfbox
private void createPdfTwo(File file,float x,float y) {
try {
PDDocument doc = PDDocument.load(file);
PDPage page = doc.getPages().get(0);
// StoredPath is the path where it gonna be saved
PDImageXObject pdImage = PDImageXObject.createFromFile(StoredPath,doc);
PDPageContentStream contents = new PDPageContentStream(doc, page,true,false);
// here is the x,y which i am getting from touch listener
contents.drawImage(pdImage, x, y,150,150);
contents.close();
doc.save(storedPath);
doc.close();
pdfView.fromFile(new File(storedPath)).load();
imageView.setVisibility(View.GONE);
} catch (IOException e) {
e.printStackTrace();
}
}
here is the touch listener code
@SuppressLint("ClickableViewAccessibility")
private View.OnTouchListener onTouchListener()
{
return (view, event) -> {
final int x = (int) event.getRawX();
final int y = (int) event.getRawY();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams)
view.getLayoutParams();
xDelta = x - lParams.leftMargin;
yDelta = y - lParams.topMargin;
break;
case MotionEvent.ACTION_UP:
// here is the code where magic should happen
RelativeLayout.LayoutParams qParams = (RelativeLayout.LayoutParams)
view.getLayoutParams();
float xD = x - qParams.leftMargin;
float yD = y - qParams.topMargin;
// this method will calling the create pdf (file is defined global)
createPdfTwo(file,xD,yD);
Toast.makeText(MainActivity.this,
"Signature Drop", Toast.LENGTH_SHORT)
.show();
break;
case MotionEvent.ACTION_MOVE:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view
.getLayoutParams();
layoutParams.leftMargin = x - xDelta;
layoutParams.topMargin = y - yDelta;
layoutParams.rightMargin = 0;
layoutParams.bottomMargin = 0;
view.setLayoutParams(layoutParams);
break;
}
mainLayout.invalidate();
return true;
};
}
}
来源:https://stackoverflow.com/questions/58039018/add-signature-or-image-on-pdf-using-drag-and-drop-android-get-x-and-y-coordinat