How to add multiple image into EditText using spannable or any other way? - image

I am creating a note taking app, I have done text section and its working properly. Also database working properly. But I want to add image in the EditText. When user add an image, it will place at the EditText cursor position and this task will happen for multiple image. And save the EditText entities (text and image) in SQL Lite database.
Please help someone to do this job. Thank you.
I have tried this job in onActivityResult , but image are not showing
if(requestCode==1 && resultCode==RESULT_OK && data!=null) {
Uri imageUri= data.getData();
try {
InputStream inputStream= getContentResolver().openInputStream(imageUri);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
final Drawable drawable = new BitmapDrawable(getResources(), bitmap);
final ImageSpan imageSpan = new ImageSpan(drawable,ImageSpan.ALIGN_BOTTOM);
Spannable span = new SpannableStringBuilder(editText.getText().toString()+"\n");
span.setSpan(imageSpan, 0, 0, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
editText.append(span, 0, ("\n").length());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

You are replacing zero characters when you do the following:
span.setSpan(imageSpan, 0, 0, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
You will need to replace at least one character. Since you are inserting an ImageSpan, you will need to add at least one character to replace.
ImageSpan imageSpan = new ImageSpan(drawable, ImageSpan.ALIGN_BOTTOM);
Editable editable = editText.getText();
// Insert a single space to replace.
editable = editable.insert(0, " ");
editable.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
editText.setText(editable);

Related

Mixing page-level elements and column text

Trying to tap into iText7 potential, that is in there, but hidden from the developer. I want my text in columns to flow around images that will be placed on page-level (fixed position). Either I'm doing something wrong, or there is a bug in the code. Here is what I have so far:
open Document for writing
set document renderer to custom ColumnDocumentRenderer:
Class<RootRenderer> cls = RootRenderer.class;
final Field f = cls.getDeclaredField("floatRendererAreas");
f.setAccessible(true);
ColumnDocumentRenderer cdr = new ColumnDocumentRenderer(document, new Rectangle[]{
new Rectangle(36f, 36f, 243.5f, 770f),
new Rectangle(315.5f, 36f, 243.5f, 770f)
}) {
#Override
protected LayoutArea updateCurrentArea(LayoutResult overflowResult) {
LayoutArea lr = super.updateCurrentArea(overflowResult);
try {
List<Rectangle> list = (List<Rectangle>) f.get(this);
list.add(new Rectangle(130f, 230f, 350f, 200f));
} catch (Exception ex) {
throw new RuntimeException(ex);
}
return lr;
}
};
document.setRenderer(cdr);
This custom renderer adds the rectangle in which I will later place an image to floatRendererAreas field, so that the renderers will know which area to avoid.
add my content:
Div div = new Div();
div.setBackgroundColor(ColorConstants.BLUE);
div.setMargins(0f, 0f, 0f, 0f);
div.setPaddings(0f, 0f, 0f, 0f);
div.setFixedPosition(130f, 230f, 350f);
div.setHeight(200f);
document.add(div);
for (int i = 0; i < 6; i++) {
Paragraph p = new Paragraph();
p.add("Lorem ipsum...");
document.add(p);
}
unfortunately I get this:
edit: the layouting code is aware of the forbidden area, it just detects it too late, so up to one line is rendered in a wrong way. With y set to 260:
Probably it looks that the top of the text line is not affected, whereas it should check whether bottom of the text line is not affected as well.
What am I doing wrong? Is there another method to do it? Why is this not part of public/protected API? It could be extremely useful when creating documents.

How to add multiple Textfields in single or multiple pages in a Loop

I am Using Itext 5 maven and I want to add multiple textfields in multiple pdf pages. like page 1 need 3 fields, page 2 need 4 fields etc.
I have write the below code
public byte[] setupDocument(EditPdfDTO editPdfDTOList, MultipartFile attachment)
{
WritePDF obj = new WritePDF();
Document document = null;
PdfWriter writer = null;
PdfImportedPage page = null;
PdfReader reader = null;
try
{
// Create output PDF
document = new Document(PageSize.A4);
document.setMargins(0, 0, 0, 0);
writer = PdfWriter.getInstance(document,
new FileOutputStream("D:/test.pdf"));
document.open();
PdfContentByte cb = writer.getDirectContent();
// Load existing PDF
reader = new PdfReader(attachment.getBytes());
int totalPages = reader.getNumberOfPages();
for (int i = 0; i < totalPages; i++)
{
page = writer.getImportedPage(reader, i + 1);
document.newPage();
cb.addTemplate(page, 0, 0);
for (int j = 0; j < editPdfDTOList.getPdf().size(); j++)
{
if (i + 1 == editPdfDTOList.getPdf().get(j).getPageNo())
{
BaseFont baseFont = null;
try
{
baseFont = BaseFont.createFont();
}
catch (DocumentException | IOException e1)
{
e1.printStackTrace();
}
int a, b;
a = editPdfDTOList.getPdf().get(j).getxCoordinate();
b = editPdfDTOList.getPdf().get(j).getyCoordinate();
String str = editPdfDTOList.getPdf().get(j).getTextContent();
Rectangle linkLocation =
new Rectangle(a, b + baseFont.getDescentPoint(str, 10),
a + 10 + baseFont.getWidthPoint(str, 10),
b + baseFont.getAscentPoint(str, 10) + 10);
TextField field =
new TextField(writer, linkLocation, "user1" + j+UUID.randomUUID());
field.setFontSize(10);
field.setOptions(TextField.MULTILINE | TextField.READ_ONLY);
field.setTextColor(BaseColor.RED);
field.setText(str);
field.setBorderWidth(1);
cb = writer.getDirectContent();
try
{
cb.addAnnotation(field.getTextField(),false);
}
catch (IOException | DocumentException e)
{
e.printStackTrace();
}
}
}
}
}
catch (DocumentException | IOException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
document.close();
}
return null;
}
this code is able to add only one Textfield on every expected but not to add 2 or many textfields in a single page.
there is no issue of multiple try--catch block.
The appropriate classes to use
First of, you say you "want to add multiple textfields in multiple pdf pages". When implementing tasks like this, i.e. tasks that take a single document and want to somehow manipulate it while keeping it structurally more or less as before, one should usually work with a PdfReader/PdfStamper couple. This allows you to concentrate on the manipulation and provides a copy of the original PDF with all its properties to work on.
Adding multiple fields to a page of an existing PDF
Adding multiple fields to a single existing page is trivial, e.g.:
PdfReader pdfReader = new PdfReader(resource);
PdfStamper pdfStamper = new PdfStamper(pdfReader, output);
TextField field1 = new TextField(pdfStamper.getWriter(),
new Rectangle(100, 800, 200, 820), "Field1");
field1.setBorderColor(BaseColor.CYAN);
field1.setBorderStyle(PdfBorderDictionary.STYLE_DASHED);
field1.setBorderWidth(BaseField.BORDER_WIDTH_MEDIUM);
field1.setText("Field 1");
pdfStamper.addAnnotation(field1.getTextField(), 1);
TextField field2 = new TextField(pdfStamper.getWriter(),
new Rectangle(300, 800, 400, 820), "Field2");
field2.setBorderColor(BaseColor.RED);
field2.setBorderStyle(PdfBorderDictionary.STYLE_INSET);
field2.setBorderWidth(BaseField.BORDER_WIDTH_THIN);
field2.setText("Field 2");
pdfStamper.addAnnotation(field2.getTextField(), 1);
pdfStamper.close();
(AddField test testAddMultipleFields)
Applied to my example document
the code generates
Thus, there is no conceptual problem adding multiple text fields to the same document page, it works in a very natural manner.
In your case I would switch to using a PdfReader/PdfStamper couple. If some issue still remain, I would inspect your data. Probably they simply contain only a single field dataset per page. Or two textfields have the same coordinates and, therefore, look like one. Or some text fields have off-screen coordinates. Or... Or... Or...
The original answer
Originally the code in the question looked differently. This original answer focused on issues of that code.
You claim your code
is able to add only one Textfield on every expected but not to add 2 or many textfields in a single page
I doubt that because
you have two distinct objects writing to the same file "D:/TemplateFilePDf/" + attachment.getOriginalFilename() concurrently, the PdfWriter writer and the PdfStamper stamper. If you get something sensible as a result of your code, then only by pure luck; and
additionally stamper is instantiated for a null instance of PdfReader. This actually will cause a NullPointerException in the constructor which will keep your textfield adding code from being executed at all.
Thus, either the code you shared is considerably different from the code you run or your test runs actually all throw that NullPointerException and you probably find the outputs of a former, less broken version of your code which happens to have added only a single text field.
After fixing those two issues, some questions still remain (e.g. what is the intention of that cb.fill()? That instruction is only allowed directly after a path definition, the path whose inner area to fill, but I don't see you defining any path).
Furthermore, you access your editPdfDTOList for a lot of relevant values but we don't know those values. Thus, we cannot run your code to try and reproduce the issue. Probably you create only a single textfield because that object contains only values for a single textfield...

Label is not show in print

I have used below code to print the Panel of windows form.
private void button1_Click(object sender, EventArgs e)
{
System.Drawing.Printing.PrintDocument doc = new System.Drawing.Printing.PrintDocument();
doc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(Doc_PrintPage);
doc.Print();
}
private void Doc_PrintPage(object sender, PrintPageEventArgs e)
{
Panel grd = new Panel();
Bitmap bmp = new Bitmap(panel2.Width, panel2.Height, panel2.CreateGraphics());
panel2.DrawToBitmap(bmp, new Rectangle(0, 0, panel2.Width, panel2.Height));
RectangleF bounds = e.PageSettings.PrintableArea;
float factor = ((float)bmp.Height / (float)bmp.Width);
e.Graphics.DrawImage(bmp, bounds.Left, bounds.Top, bounds.Width, factor * bounds.Width);
bmp.Save("test12.jpg");
}
Now from above code, when i click on button the print function will be call but it excluded label in it. i am attaching image for your reference. first image is my UI design. , when i use print functionality it removes the label value as you can see in other image. i have used rectagleshap control which are in Pink color and i am displaying label on it. I think the label may be send back but when i used front back then also it is not appear.
Can you just try this one here i was using this for capture the whole screen which ever is active window its like screencapture or screenshot.
private void Doc_PrintPage(object sender, PrintPageEventArgs e)
{
Bitmap bitmap = new Bitmap(panel2.Width, panel2.Height);
Graphics graphics = Graphics.FromImage(bitmap as Image);
graphics.InterpolationMode = InterpolationMode.Default;
graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);
bitmap.Save(pathDownload + filename + ".jpeg", ImageFormat.Jpeg);
bmp.Save("test12.jpg");
}
In my case label was shown back to the rectangle, so i added one more label and set it as bring front. thanks for the help.

Mouse Click Events on JTable

I want to get the index of selected row when user double clicks on a row.
Here is my code:
tab.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
int selectedRow = tab.getSelectedRow();
try {
String file = rows[selectedRow][2];
String path = "C:\\Users\\raj kumar\\Gallery\\" + file;
JLabel fileLable = new JLabel();
fileLable.setBounds(500, 600, 300, 300);
fileLable.setIcon(new ImageIcon(path));
pan.add(fileLable);
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
});
But the tab.getSelectedRow() returns -1 even though I double clicked the row in the table.
You want to know on which row your mouse points, but you ask which row is selected. So the simple solution is, instead of
int selectedRow = tab.getSelectedRow();
you can use
int row = tab.rowAtPoint(e.getPoint());
to get the wanted row. The Event e has every necessary information you need. The e.getPoint() returns the exact Point your cursor is currently located. And the rowAtPoint() should be self explaining.
This also makes sure that you only work with one row at a time, if this is important to you. I don't know how getSelectedRow() works if multiple rows are selected.

PDFBox image size issues

I'm new to working with PdfBox and I'm having a small issue when displaying images. I'm able to import the image, which is sized at 800*900 pixels, and looks fine when viewed in an existing pdf at 100%. However when the resulting PDF is generated using the below code, the image becomes blurry, and the image extends beyond the boundaries of the A4 page.
Is there a different way of sizing/saving images so that they display correctly in pdfbox?
public class PDFtest {
/**
* #param args the command line arguments
*/
public static void main(String[] args) throws IOException, COSVisitorException {
// TODO code application logic here
// Create a document and add a page to it
PDDocument document = new PDDocument();
PDPage page = new PDPage(PDPage.PAGE_SIZE_A4);
document.addPage(page);
// Create a new font object selecting one of the PDF base fonts
PDFont font = PDType1Font.HELVETICA_BOLD;
InputStream in = new FileInputStream(new File("img.jpg"));
PDJpeg img = new PDJpeg(document, in);
// Start a new content stream which will "hold" the to be created content
PDPageContentStream contentStream = new PDPageContentStream(document, page);
// Define a text content stream using the selected font, moving the cursor and drawing the text "Hello World"
contentStream.drawImage(img, 10, 700);
contentStream.beginText();
contentStream.setFont(font, 12);
contentStream.moveTextPositionByAmount(10, 650);
contentStream.drawString("Hello World");
contentStream.endText();
// Make sure that the content stream is closed:
contentStream.close();
// Save the results and ensure that the document is properly closed:
document.save("Hello World.pdf");
document.close();
}
I'd like to point out that as of 2.0 the contentStream.drawXObject function call in Victor's answer is deprecated. If you want to specify a width and height you should use contentStream.drawImage(image, x, y, width, height)
I had the same problem asked in this question, but the given answer is not right.
After some research I found a solution.
Instead of using the function drawImage use the function drawXObject
contentStream.drawXObject( img, 10, 700, 100, 100 );
Where the last two numbers specify the size of the image to be drawn.
For similar situation, for me, with PDF 2.0.11 and a tiff file of dimensions - 1600 x 2100 the following code perfectly fit the image in A4 (portrait) size. Not sure if PDFRectangle is okay with you.
I got this example straight from PDFBOX - Example
The only thing I tweaked/introduced is:
PDRectangle.A4.getWidth(), PDRectangle.A4.getHeight()
Here is the full sample:
public static void main(String[] args) throws IOException
{
// if (args.length != 2)
// {
// System.err.println("usage: " + ImageToPDF.class.getName() + " <image> <output-file>");
// System.exit(1);
// }
String imagePath = "C:/FAX/sample.tiff";
String pdfPath = "C:/FAX/sample.pdf";
if (!pdfPath.endsWith(".pdf"))
{
System.err.println("Last argument must be the destination .pdf file");
System.exit(1);
}
try (PDDocument doc = new PDDocument())
{
PDPage page = new PDPage();
doc.addPage(page);
// createFromFile is the easiest way with an image file
// if you already have the image in a BufferedImage,
// call LosslessFactory.createFromImage() instead
PDImageXObject pdImage = PDImageXObject.createFromFile(imagePath, doc);
// draw the image at full size at (x=20, y=20)
try (PDPageContentStream contents = new PDPageContentStream(doc, page))
{
// draw the image at full size at (x=20, y=20)
contents.drawImage(pdImage, 0, 0, PDRectangle.A4.getWidth(), PDRectangle.A4.getHeight());
// to draw the image at half size at (x=20, y=20) use
// contents.drawImage(pdImage, 20, 20, pdImage.getWidth() / 2, pdImage.getHeight() / 2);
}
doc.save(pdfPath);
System.out.println("Tiff converted to PDF succussfully..!");
}
}
Hope it helps.
If your intention is an A4 sized pic on a PDF, then i guess you find the actual size of typical A4 in pixels.
Also you should be aware of the extension of the picture that you want to view like jpg, gif, or bmp ...
from what I saw in your code, the dimensions of the picture are 10 X 700 which I believe is pretty small size.
contentStream.drawImage(img, 10, 700);
And the extension of the picture is : jpg
InputStream in = new FileInputStream(new File("img.jpg"));
check those and return for more info.
that's all.
good luck'''
As per the new API 2.0.x, one can use the PDRectangle to fetch Pdf page width and height. One can use PDPageContentStream to draw the image in accordance with PDF page.
For reference:
try (PDPageContentStream contents = new PDPageContentStream(pdDocument, pdPage)) {
final PDRectangle mediaBox = pdPage.getMediaBox();
final PDImageXObject pdImage = PDImageXObject.createFromFile(image, pdDocument);
contents.drawImage(pdImage, 0, 0, mediaBox.getWidth(), mediaBox.getHeight());
}

Resources