I'm using Xamarin.android and I followed this
I Want to save a print to a specific path without showing dialogue screen to user.
How can I say the path I want to save without asking user ?
I have that code:
myWebView = new Android.Webkit.WebView(this.Context);
var printManager = (Android.Print.PrintManager)Forms.Context.GetSystemService(Android.Content.Context.PrintService);
var text = new ContractHTML() { Model = new Model.Model() { img = null } };
myWebView.LoadDataWithBaseURL("file:///android_asset/", text.GenerateString(), "text/html", "UTF-8", null);
string fileName = "MyPrint_" + Guid.NewGuid().ToString() + ".pdf";
var printAdapter = myWebView.CreatePrintDocumentAdapter(fileName);
Android.Print.PrintJob printJob = printManager.Print("MyPrintJob", printAdapter, null);
This code is working and generate a pdf, but it's asking user where to save it, I want to save it in some path.
Is it possible ?
Save pdf without showing the user the print dialogue screen
AFAIK you can not implement this feature in Android.
The user needs to be able to choose some configuration, such as choosing what printer to print on. So when you use printManager.Print() method a print dialog will show up. You could find that In android PrintManager Source code, PrintManager class is final(In C# it's sealed), we are not allowed to override this method to prevent the dialog.
When you execute printManager.Print("MyPrintJob", printAdapter, null) method from an Activity, it starts PrintJob also it will bringing up the system print UI. This is did by Android printing framework, we cannot silently print by using the platform API.
There is an open issue on AOSP issue tracker : https://code.google.com/p/android/issues/detail?id=160908.
After searching far and wide, I used this GitHub project to solve this problem. I was able to silently print the document without the user's knowledge.
It seems like the developer was able to add some classes to the android.print package to access private members, making this possible.
https://github.com/UttamPanchasara/PDF-Generator?utm_source=android-arsenal.com&utm_medium=referral&utm_campaign=7355
Related
Im making and android app which let the user select an image from the phone and load it to an ImageView. The further process is to send the image as a POST request to a PHP script
It all works fine if I load the image to a File object from the file location, but this require that the user open a setting on the phone which allow the app to access and manage local files..
So, is there a way to read the image from the imagview into a File object?
This is my current working code
val fil = File(getRealPathFromURI(imageUri!!).toString())
if (Build.VERSION.SDK_INT >= 30){
if (!Environment.isExternalStorageManager()){
var getpermission = Intent()
getpermission.setAction(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
startActivity(getpermission);
}
}
var reqB = RequestBody.create("image/*".toMediaTypeOrNull(),fil)
var part = MultipartBody.Part.createFormData("file",fil.name,reqB)
Kotlin throws an access error if I try to load it to a File object if the users dont allow it through the settings on the phone. I believe its a security issue on android.
Really what I want to do is
Let the user select an existing image on the phone
Display it in an imageview
Create a function which get the selected image into an MultipartBody.part without the user having to grant the app access to special rights
I'm looking for an easy way of making this printing HTML code fragment compatible backwards with older Android versions:
#TargetApi(Build.VERSION_CODES.KITKAT)
private void createWebPrintJob(WebView webView) {
// Get a PrintManager instance
PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE);
// Get a print adapter instance
PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter();
// Create a print job with name and adapter instance
String jobName = getString(R.string.app_name) + " Document";
PrintJob printJob = printManager.print(jobName, printAdapter, new PrintAttributes.Builder().build());
// Save the job object for later status checking
mPrintJob = printJob;
}
This works from Android 19 (Kit-Kat), but i need some previous versions to work also.
I don't need to download and print a webpage from the internet, i already have the HTML code as a String, which i show in a webview (as yo can see in the code).
Is there an easy way of printing HTML code before Android 19?
Thanks in advance!
Prior to Kitkat there is no Android API to support printing. In the 2016 Google I/O printing training, the developers mentioned that prior to Kitkat your only way to print is to "throw it over the wall". That is, send an intent and hope that the user has the suitable application to print. Look at this question for examples on apps that provide printing (HP ePrint and Google Cloud Print).
Using flash builder I have developed an app for both android and IPhone. I would like to be able to open the default email client on the phone and send an email with an attachment. I have seen many examples using "mailto:" however this is not supported with an attachment. I have googled this extensively and have found nothing that is current with in the last 3 years.
I already make the pdf file I wish to attach and can move it to a temp directory if needed to satisfy the access issue. I would like to use the default mailer like other programs use if that is not doable please tell me how to directly send an email from the app.
Just for reference to help expand the Action Script 3.0 Information base. I was unable to find any way to do what I was asking above. Here is the Method that I used to send email with attachment.
https://code.google.com/p/airxmail/
http://flex.coltware.com/as3-flex-air/airxmail/
import com.coltware.airxmail.INetAddress;
import com.coltware.airxmail.MailSender.SMTPSender;
import com.coltware.airxmail.MimeMessage;
import com.coltware.airxmail.RecipientType;
private function send_plain_email():void{
// How to send plain text email
var sender:SMTPSender = new SMTPSender();
sender.setParameter(SMTPSender.HOST,"your.smtp.hostname");
sender.setParameter(SMTPSender.PORT,25); // default port is 25
// If you use SMTP-AUTH
sender.setParameter(SMTPSender.AUTH,true);
sender.setParameter(SMTPSender.USERNAME,"username");
sender.setParameter(SMTPSender.PASSWORD,"password");
// Create email message
var message:MimeMessage = new MimeMessage();
// Set from email address and reciepients
var from:INetAddress = new INetAddress("from-email-address#xxxx.yyyy","from label");
message.setFrom(from);
var toRecpt:INetAddress = new INetAddress("to-email-address#xxxx.yyyy","to label");
message.addRcpt(RecipientType.TO,toRecpt);
var ccRecpt:INetAddress = new INetAddress("cc-email-address#xxxx.yyyy","cc label");
message.addRcpt(RecipientType.CC,ccRecpt);
//
message.setSubject("hello world");
//
// Plain Text Part
//
var textPart:MimeTextPart = new MimeTextPart();
message.setSubject("Reciept for #" + job.jobs.JobID);
textPart.contentType.setParameter("charset","UTF-8");
textPart.transferEncoding = "8bit";
textPart.setText("Please see attached PDF \n You will need a PDF viewer to open \n To download the latest version of Adobe Acrobat reader, Please follow the link: http://www.adobe.com/products/acrobat/readstep.html");
message.addChildPart(textPart);
//
// Attachment part
//
var filePart:MimeImagePart = new MimeImagePart();
filePart.contentType.setMainType("application");
filePart.contentType.setSubType("pdf");
filePart.setAttachementFile(File.desktopDirectory.resolvePath(sfile),"WorkOrder.pdf");
message.addChildPart(filePart);
sender.send(message);
sender.close();
}
I am trying to print an HTML-page from a webview on KitKat (4.4.4) basically using the sample code provided in Google's API documentation. Accordingly, I set a print job name as follows:
String jobName = getString(R.string.app_name) + " Document";
PrintJob printJob = printManager.print(jobName, printAdapter,
new PrintAttributes.Builder().build());
My code runs fine and the page is printed as intended. Even if the user chooses the option "save as PDF" in the Android standard printing dialog, the codes renders a nice PDF and the user can choose a file name. At this point I would expect Android to use the string stored in the jobName field as file name. Instead it always uses webview as file name, although my code does not contain webview as string.
Is there a way to set another default name for storing a PDF file that is generated in my application?
Thanks for your hints...
Update:
I spent some additional time investigating this issue and I found that the WebView uses AwPrintDocumentAdapter as print adapter if the adapter is created by calling createPrintDocumentAdapter() as proposed in the API documentation. That class then calls
PrintDocumentInfo.Builder("webview") which seems to be the reason why the PDF always is named "webview". Some research reveals the following code snippet in AwPrintDocumentAdapter:
#Override
public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes,
CancellationSignal cancellationSignal, LayoutResultCallback callback,
Bundle metadata) {
mAttributes = newAttributes;
// TODO(sgurun) pass a meaningful string once b/10705082 is resolved
PrintDocumentInfo documentInfo = new PrintDocumentInfo
.Builder("webview")
.build();
// TODO(sgurun) once componentization is done, do layout changes and
// generate PDF here, set the page range information to documentinfo
// and call onLayoutFinished with true/false depending on whether
// layout actually changed.
callback.onLayoutFinished(documentInfo, true);
}
Hence, this seems to be the root cause for my problem - at least if that code made its way to my Nexus 4 test device... Finally, the best way to deal with this naming issue seems to be a custom print adapter implementation.
Are there any other solutions that do not require a custom print adapter (which should contain code for calculating the number of pages etc...)?
I'm developing a EBookreading application, EBookDroid is a library which we are using for the PDF reading. If we have any links in the PDF like www.stackoverflow.com, when the user clicks on it, it supposed to open the link, for that i need to find out the coordinates of that link in the Document.
I am writing like the below.
final RectF linkRect = page.getLinkSourceRect(pageBounds, link);
But it's always giving the null back.
Try this may be use full
THIS EXAMPLE