How to open a PDF document by clicking a button?
Two parts in there:
- Use the file chooser to select a file (you already got that)
- Then use that file name with either some Java component that is able to open/present the PDF file; or use javas ProcessBuilder to start a new external process that uses some tool like Adobe Reader to open that file.
More ideas could be found here for example.
HTML - Open local PDF in a new window using a button
You shouldn't be linking to a local file on your own pc. You need to upload the file to your server, like you would do with images, and then link to the path that you place it in.
You should also be able to use target="_blank"
for this rather than onclick.
Here is an example set up for the html:
<a href="/books/a-great-book.pdf" target="_blank">
Open A PDF file on button click
This code works for me after I set PDF file build action to content and copy always to output directory.
private async void Button_Click(object sender, RoutedEventArgs e)
{
string imageFile = @"images\somepdffile.pdf";
// Get the image file from the package's image directory
var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(imageFile);
if (file != null)
{
bool success = await Windows.System.Launcher.LaunchFileAsync(file, options);
if (success)
{
// File launched
}
else
{
// File launch failed
}
}
else
{
// Could not find file
}
}
Opening a .pdf file in windows form through a button click
To open a file with a system default viewer you need call
System.Diagnostics.Process.Start(filename);
But I haven't understood the problem with a filepath.
If you need a relative path from the program .exe file to a folder with resources, then you can add "Resources\" or "..\Resources\" (if Resources folder is higher) to your filepath.
Or you can add your pdf to a project as an embedded resource and then, when you need to open it, you can save it to some temporal location using
Path.GetTempPath()
and open it.
Button that opens a .PDF file
I managed to solve the problem, I want to thank Jason Yang immensely for having the availability to help me.
the result was like this:
the cong.txt file is available for download here: https://www.mediafire.com/file/irtg21gi8zl52xf/cong.txt/file
import PySimpleGUI as sg
import os, sys, subprocess
#how to open a file with graphical interface in python?
#como abrir um arquivo com interface grafica em python?
#¿Cómo abrir un archivo con GUI en python?
file_base64 = b'iVBORw0KGgoAAAANSUhEUgAAAEYAAABGCAYAAABxLuKEAAAACXBIWXMAAA7DAAAOwwHHb6hkAAAAGXRFWHRTb2Z0d2FyZQB3d3cuaW5rc2NhcGUub3Jnm+48GgAAB89JREFUeJztnHtQVNcdgL+7dxeWp7zVmuACatT4qojGRCsmhuqAtCbWTjKmxj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AQrw0UtKZE8/nM0YMeO/hL86Q4LmSk0DE2CkuPi4SaLxh/7Lz08FeXbsMeH0r8uWYSPm0eUt8TF+q5YU08cXXQCboi6iS14ee+Vp7eGHhQpQBsZWsGkAFEyAJS16aufbe4mLvl20iXycU1az2NwQG54D0MEuMLQgjxVGYmNdXVAU3kbmNAjPEtgQRDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjARDjASfxTidzo6Y2NjbmctdQVxCAk6ns81nMW63+3RmdvY3+58Od4DMpUs1t9t92p8/Tpym63rl3j171JLCwnvyp5iGw2q1sjgry71y1SpdVdVZfgULIWbf6z/eJUPTNGdPT89pIUQawP8AMkNitAzTmWUAAAAASUVORK5CYII='
layout = [
[sg.Push(),
sg.Button('', image_data=file_base64, button_color=
(sg.theme_background_color(),sg.theme_background_color()), border_width=0, key='-PDF-'),
sg.Push()],
]
window = sg.Window('File!', layout, finalize=True)
sg.theme('DarkBlue4')
while True:
event, values = window.read()
if event in (sg.WIN_CLOSED, 'Exit'):
break
elif event == '-PDF-':
path_dir_port=('C:/Users/your_name/dir/cong.txt')
os.startfile(path_dir_port)
"""
this part of the code is for a next project. Thanks!
sg.Window('Base64 PDF', [[sg.Multiline(flower_base64.decode(), size=(40, 16), disabled=True)]], margins=(0, 0), modal=True).read(close=True)
"""
window.close()
opening pdf file in external storage in default pdf viewer app on a button click
finally I got the answer , now its working , Had to use FileProvider , now it's more like
if anyone facing same problem let me know I will post step by step solution
public void openfile(View v) {
if (!checkstoragepermission()) {
requestReadpermission();`
}else {
File pdfFile = new File(Environment.getExternalStorageDirectory() + "/Zio
app/" + "/materials/"+"notes.pdf"); // -> filename = maven.pdf
if (pdfFile.exists()) {
File imagePath = new File(Environment.getExternalStorageDirectory() + "/"
+ "Zio app" ,"materials");
File newFile = new File(imagePath, "notes.pdf");
Uri path = FileProvider.getUriForFile(this,
"com.example.zio.fileprovider", newFile);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY |
Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent intent = Intent.createChooser(pdfIntent, "Open File");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY |
Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(getApplicationContext(), "No Application available to
view PDF", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(NewActivity.this, "No such file exists",
Toast.LENGTH_SHORT).show();
}
}
}
on Button click redirect to open PDF from link
If your pdf source is a link, you can either download the pdf first or open it using WebView. Here is some link that might be of help.
Related Topics
Randomly Choosing an Item from a Swift Array Without Repeating
Core Data Does Not React on Changed Predicate
What Is Nsmanagedobjectcontext's Performblock: Used For
Drawrect Circle and Animate Size/Color
Apply Gradient Color to Arc Created with Uibezierpath
Change Order of Read Items with Voiceover
How to Convert Including Timezone Date in Swift
Undefined Symbols for Architecture Armv7 When Using Zxing Library in Xcode 4.5
How to Programmatically Check Support of 'Face Id' and 'Touch Id'
How to Set Fake Gps Location on iOS Real Device
Bridge Google Drive API to Swift
Swift Array Append Overwriting Other Array Values
Converting a Vision Vntextobservation to a String
Pod Install Displaying Error in Cocoapods Version 1.0.0.Beta.1
Uiscrollview in Storyboard Not Working with iOS 8 Size Classes and Autolayout
How to Use Settitletextattributes:Forstate in Uibaritem