How to save text from TextView in word file? - android

I am designing a speech-to-text android application where I display the converted text in a textview. I want to save the converted text in the form of a word file/pdf on the user's device. Can somebody tell me how to do it. Thanks.

To create a .pdf file
// create a new document
PdfDocument document = new PdfDocument();
// crate a page description
PageInfo pageInfo = new PageInfo.Builder(new Rect(0, 0, 100, 100), 1).create();
// start a page
Page page = document.startPage(pageInfo);
// draw something on the page
View content = getContentView();
content.draw(page.getCanvas());
// finish the page
document.finishPage(page);
. . .
// add more pages
. . .
// write the document content
document.writeTo(getOutputStream());
// close the document
document.close();
for more detail please have a look on below link:
https://developer.android.com/reference/android/graphics/pdf/PdfDocument.html
I hope, it will work.

Related

Adding image from gallery and saving to pdf in adroid studio

I made an app in android studio (Java) on which is possible to fill some brief data in text boxes. Also app has two buttons, one for loading image from gallery and one for saving complete data to pdf.
I can successfully save all text data, but have problem with loaded image. Image is sucessfully loaded to app, but i dont know hot to save it to pdf. Image is loaded as an ImageView object.
Just to mention for pdf part i use itext.
Pls, help with hints or code for saving ImageVIew object to pdf file.
i had the same problem, but i can fixed.
first i changed the way i used to pick the image and replaced it with this code:
Intent getImage = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(getImage, GALLERY_REQUEST_CODE);
then, in the class onActivityResult i transformed the uri to a bitmap using this:
if(requestCode==GALLERY_REQUEST_CODE && resultCode== RESULT_OK && data!=null){
Uri imageData = data.getData();
String[] filePath = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(imageData,filePath,null,null,null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePath[0]);
String myPath = cursor.getString(columnIndex);
cursor.close();
Bitmap bitmap = BitmapFactory.decodeFile(myPath);
then passed the bitmap to the pdf
pdf.addFoto(bitmap);
and finally, in the pdf template i used this to put the image in a table:
public void addFoto (Bitmap u) {
try{
PdfPTable tabla = new PdfPTable(1);
tabla.setWidthPercentage(60);
tabla.setSpacingBefore(10);
tabla.setSpacingAfter(10);
Bitmap bmp = u;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
Image image = Image.getInstance(stream.toByteArray());
PdfPCell imageCell = new PdfPCell();
imageCell.addElement(image);
tabla.addCell(imageCell);
document.add(tabla);
Look into PdfDocument
Example from the documentation
// create a new document
PdfDocument document = new PdfDocument();
// crate a page description
PageInfo pageInfo = new PageInfo.Builder(new Rect(0, 0, 100, 100), 1).create();
// start a page
Page page = document.startPage(pageInfo);
// draw something on the page
View content = getContentView();
content.draw(page.getCanvas());
// finish the page
document.finishPage(page);
. . .
// add more pages
. . .
// write the document content
document.writeTo(getOutputStream());
// close the document
document.close();

Place holder (First Page) of PDF files in android

I want to show first page of multi page PDF file on imageview and open PDF file on imageView click.
I'm calling an api and getting PDF files urls as response. Users can upload PDF files and view uploaded files.
(Whatsapp also create placeholder of the pdf, just like that)
Thanks
If you are looking to support API > 21 then you can use PdfRenderer like this.
// create a new renderer
PdfRenderer renderer = new PdfRenderer(getSeekableFileDescriptor());
// let us just render all pages
final int pageCount = renderer.getPageCount();
for (int i = 0; i < pageCount; i++) {
Page page = renderer.openPage(i);
// say we render for showing on the screen
page.render(mBitmap, null, null, Page.RENDER_MODE_FOR_DISPLAY);
// do stuff with the bitmap
// close the page
page.close();
}
// close the renderer
renderer.close();
For more info have a look at this: https://developer.android.com/reference/android/graphics/pdf/PdfRenderer

Load image from url in ListView Xamarin Forms Visual Studio

I know there are several questions about this.
I have the following code:
Image image = new Image();
UriImageSource imgSource = new UriImageSource();
imgSource.SetBinding( UriImageSource.UriProperty, "ThumbnailUri" );
imgSource.Parent = image;
...
The problem is I don't know how to integrate imgSource to image object ?
Means how to add as child the imgSource to image ?
I set imgSource.Parent = image; but image is not visible in list view UI.
I have a model which contains a property like:
Uri ThumbnailUri {get;set;}
And I want to display that image in list view.
Image has a Source property
image.Source = imgSource;

Android Webview to PDF using PdfDocument

When attempting to create a PDF using PdfDocument from a Webview, my content is always blank. Trying a very simple example (writting a report writter) and yet the string does not show up in the PDF. The PDF is getting create, however the text is not showing up.
WebView webview;
String summary = "<html><body><h1>A very simple line of <b>text</b> </h1></body></html>";
webview.setBackgroundColor(Color.RED);
webview.loadDataWithBaseURL(null, summary,
"text/HTML", "UTF-8", null);
PdfDocument document = new PdfDocument();
int pageNumber = 1;
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(webview.getWidth(),
webview.getHeight(), pageNumber).create();
PdfDocument.Page page = document.startPage(pageInfo);
webview.draw(page.getCanvas());
document.finishPage(page);
I also attempted to place this inside a custom webview and process in public void onPageFinished(WebView view, String url), but still no string shown in PDF.
The PDF is RED.

Creating a PDF File in PDFBOX (from existing pdf file)

I have a 300 page PDF file. I want to create a new PDF file from my chosen pages the existing pdf file.
I have created a blank PDF File (using PDFBox ) like this:
// Create a new empty document
PDDocument document = new PDDocument();
// Create a new blank page and add it to the document
PDPage blankPage = new PDPage();
document.addPage( blankPage );
// Save the newly created document
document.save("BlankPage.pdf");
and this is how I am reading a page from pdf File.
PDDocument doc = PDDocument.load("Hello World.pdf");
PDPage firstPage = (PDPage) doc.getDocumentCatalog().getAllPages().get(67);
My question is, how do I get the contents of 'firstpage' into "blankPage.pdf".
If I can choose the x,y coordinates of the firstpage(where it is to be overlayed), it would be even better.
P.S.
The page sizes of my Hello World file are not A4.Each page is more like a thumbnail with text and shapes.So, overlaying is possible on A4. Also, I do not want to convert the files to image and then onverlay, I want the whole pdf file to be pasted as is(without converting to image first)
it turns out that iText can do the same thing.Here is the code for that :
PdfReader reader = new PdfReader("Hello World.pdf");
Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream("RESULT.PDF"));
document.open();
PdfContentByte canvas = writer.getDirectContent();
PdfImportedPage page;
for (int i = 3; i <=6; i++) {
page = writer.getImportedPage(reader, i);
canvas.addTemplate(page, 1f, 0, 0, 1, 0, i*30-250);
}
document.close();

Categories

Resources