Display pdf in java

View, Convert, Print and Process PDF Files in Java

JPedal is a Java PDF Library that makes it easy for developers to display, convert, print and parse PDF files in Java.

Display PDF files in Java

JPedal provides a complete Java PDF Viewer which is easy to integrate and customise into your Java applications.

Convert PDF to image

JPedal includes a Java PDF Renderer which converts PDF Documents into image files in multiple image formats.

File security

JPedal runs on your Computers. It is not a cloud service and makes no calls outside. So your customer files are always secure.

How It Works

Add the IDRsolutions repository to your pom.xml file:

   IDRsolutions IDR Solutions https://maven.idrsolutions.com   

Add the JPedal dependency to your pom.xml file:

   com.idrsolutions jpedal-trial 2023.07   

Add the IDRsolutions repository to your build.gradle file:

repositories  maven  url = "https://files.idrsolutions.com/dl/maven/" > > 

Add the JPedal dependency to your build.gradle file:

dependencies  implementation 'com.idrsolutions:jpedal-trial:2023.07' > 
If you would like help with your trial experience, book a 15-minute call
Your download has started

Java PDF Viewer Convert PDF Files to Images Search PDF Files Print PDF Files Extract Images Extract Text Extract Metadata Add/edit annotations

Viewer viewer = new Viewer(); viewer.setupViewer(); viewer.executeCommand(ViewerCommands.OPENFILE, "pdfFile.pdf");
ConvertPagesToImages.writeAllPagesAsImagesToDir("inputFileOrDir", "outputDir", "bmp", 1.33f);
ArrayListFloat[]> resultsForPages = FindTextInRectangle.findTextOnAllPages("/path/to/file.pdf", "textToFind");
PrintPdfPages print = new PrintPdfPages("C:/pdfs/mypdf.pdf"); if (print.openPDFFile())  print.printAllPages("Printer Name"); >
ExtractClippedImages.writeAllClippedImagesToDir("inputFileOrDirectory", "outputDir", "outputImageFormat", new String[] "imageHeightAsFloat", "subDirectoryForHeight">);
ExtractTextAsWordList.writeAllWordlistsToDir("inputFileOrDirectory", "outputDir", -1);
final PdfUtilities utilities = new PdfUtilities("path/to/exampleFile.pdf"); try  if (utilities.openPDFFile())  // Returns a String containing all metadata fields for the document final String documentPropertiesAsXML = utilities.getDocumentPropertyFieldsInXML(); // Returns the total page count as an int final int totalPageCount = utilities.getPageCount(); for (int i = 1; i != totalPageCount; i++)  // Get the page dimensions for the specified page in the given units and type final float[] pageDimensions = utilities.getPageDimensions(i, PdfUtilities.PageUnits.Pixels, PdfUtilities.PageSizeType.CropBox); // Returns the image data as a String for the specified page final String xImageDataForPage = utilities.getXImageDataForPage(i); > > > catch (final PdfException e)  e.printStackTrace(); > utilities.closePDFfile();
WritableAnnotation[] annotations = new WritableAnnotation[2]; annotations[0] = new SquareAnnotation(1, 10, 10, 100, 100); annotations[1] = new CircleAnnotation(2, 10, 10, 100, 100); AnnotationWriter.writeAnnotations(inputFile, outputFile, annotation);

Источник

How to view PDF files in Java

Java PDF viewer

If you want to view PDF files in your Java application you will quickly find that Java has no native support for the PDF file format. It is for this reason that you would need a Java PDF viewer.

Today I’m going to walk you through the steps required using our product JPedal, which has over 20 years of development behind it!

Installing the Java PDF viewer

There are a few ways you can install JPedal, you can either download the trial JAR from our website, or you can add JPedal to your project dependencies.

In this tutorial, I am going to be using Maven however you can also use Gradle.

  IDRsolutions IDR Solutions https://maven.idrsolutions.com    com.idrsolutions jpedal-trial 2022.10  

You can find the latest version number of JPedal on our support site.

Running the standalone viewer

If you want to quickly view PDF files, you may use the JPedal Viewer. To launch the viewer you can double-click to open the JAR or you can run the following:

java -jar jpedal.jar --view [optional/filename.pdf]

Embedding in an existing Java application

Creating an instance of the JPedal Viewer only requires two lines of code:

Viewer viewer = new Viewer(); viewer.setupViewer();

You can then use the following to open and display a PDF file:

viewer.executeCommand(Commands.OPENFILE, new Object[] );

Advanced usage

You may want to customise the look and feel of the viewer, for example you might not want customers to be able to print your PDFs, or you might want to hide the menu buttons.

All of this and more is possible using a properties profile:

  1. Create an XML properties file by exporting and modifying the default one. In JPedal go to Edit -> Preferences and click Save As.
  2. Customise the properties file to your requirements. For example:
Viewer viewer = new Viewer("path/to/prefs.xml");

Learn more

Visit www.idrsolutions.com/jpedal to learn more about JPedal.

Visit support.idrsolutions.com/jpedal for comprehensive documentation and tutorials.

Источник

How to Build an Android PDF Viewer Using Java

Sanity Image

In order to let users view PDF documents in an Android app, it's common practice to defer this functionality to a third-party app on the user’s device. By doing so, developers won't have to build their own PDF viewer from scratch. However, this isn't always the best solution. What if you want to view PDF documents directly in your app? Luckily, the Android SDK provides classes to handle PDF documents in apps running Android 5.0 or higher.

In this blog post, we’ll use PdfRenderer from the android.graphics.pdf package to create a basic PDF viewer in your Android app. Then, we'll see how easy it is to implement a document viewer using the Apryse SDK. Here are a few steps to get you started.

For reference, the sample code for this post can be found at Github.

View a PDF Using the Android SDK

To keep things simple, we're going to view a PDF file stored in the raw resource folder (i.e. res/raw/sample.pdf ). Your app will also need to set the minimum API level to 21 (Android 5.0).

 // . private PdfRenderer mPdfRenderer; private PdfRenderer.Page mPdfPage; private ImageView mImageView; @Override protected void onDestroy()   super.onDestroy(); if (mPdfPage != null)  mPdfPage.close(); > if (mPdfRenderer != null)  mPdfRenderer.close(); > > 

Add PdfRenderer and PdfRenderer.Page member variables to your activity so that we can clean them up later. You'll also need to add an ImageView to your activity in order to display the PDF:

 // Copies the resource PDF file locally so that PdfRenderer can handle the file void copyToLocalCache(File outputFile, @RawRes int pdfResource) throws IOException   if (!outputFile.exists())  InputStream input = getResources().openRawResource(pdfResource); FileOutputStream output; output = new FileOutputStream(outputFile); byte[] buffer = new byte[1024]; int size; // Just copy the entire contents of the file while ((size = input.read(buffer)) != -1)  output.write(buffer, 0, size); > input.close(); output.close(); > > 

In order for PdfRenderer to handle PDFs from the res/raw folder, we'll need to copy the file and cache it locally. Add this method to your activity:

 // Display a page from the PDF on an ImageView void openPdfWithAndroidSDK(ImageView imageView, int pageNumber) throws IOException   // Copy sample.pdf from 'res/raw' folder into local cache so PdfRenderer can handle it File fileCopy = new File(getCacheDir(), FILE_NAME); copyToLocalCache(fileCopy, R.raw.sample); // We will get a page from the PDF file by calling openPage ParcelFileDescriptor fileDescriptor = ParcelFileDescriptor.open(fileCopy, ParcelFileDescriptor.MODE_READ_ONLY); mPdfRenderer = new PdfRenderer(fileDescriptor); mPdfPage = mPdfRenderer.openPage(pageNumber); // Create a new bitmap and render the page contents on to it Bitmap bitmap = Bitmap.createBitmap(mPdfPage.getWidth(), mPdfPage.getHeight(), Bitmap.Config.ARGB_8888); mPdfPage.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY); // Set the bitmap in the ImageView so we can view it imageView.setImageBitmap(bitmap); > 

Then you can display a page from your PDF by loading it to an ImageView as a bitmap:

PDFRenderer Viewer

As you can see, using PdfRenderer is an easy way to create a simple single-page PDF viewer in your Android app. However, if you need users to interact with a PDF document, then I'm afraid you're out of luck. The Android SDK does not provide basic UI controls for PDF viewing like page zooming and scrolling, and PdfRenderer only supports opening one page at a time - you'll need to write your own UI to handle user interaction and multi-page documents.

Additionally, PdfRenderer does not support password-protected or encrypted files, and uses PDFium for rendering which only works well with simple documents. You could run in to some rendering inconsistencies or failures when working with more complex or unsupported PDF documents. For a more stable and interactive viewing experience, you'll have to look elsewhere for a solution.

Fortunately, Apryse's Android PDF SDK comes with a fully-featured document viewer that includes these basic controls in addition to features such as:

  • Annotation creation and editing
  • Form filling
  • Office file viewing and conversion (including .doc, .docx, .xlsx, .pptx formats)
  • Page manipulation
  • Reflow
  • Custom viewing modes
  • And more!

Also, it's easy as pie to integrate with your Android app. Here's how!

Источник

How To Create a Java PDF Viewer In A Snap

Last month, we released Version 2.00 of PDFOne Java. In this major update of our Java PDF library, we introduced two new components - PdfViewer and PdfPrinter. In this article, we will see how to create a Java PDF viewer application using NetBeans IDE. This application will your end-users to view PDF documents.

  1. Create a new Java Application project.
  2. Add a JFrame Form to Source Packages.
  3. Add a JToolBar to the top of the form.
  4. Add the following components to the toolbar (see screenshot).
    • JTextField - 1 No.
      • In Properties window, change name of the field to txtFilePath
    • JButton - 1 No.
      • Change the name of the button to btnLoad and specify an icon image for the button.
    • JButton - 4 Nos.
      • Change their Text property to First Page, Previous Page, Next Page, and Last Page.
      • Change their names to btnFirstpage, btnPreviousPage, btnNextPage and btnLastPage.
  5. Resize the JToolBar component so that it looks like the address bar of a browser.
  6. Add PdfViewer component to Palette Window:
    • On Palette Window, right-click Beans, and select Palette Manager. on the shortcut menu.
    • On the Palette Manager dialog, click on Add from JAR. , select the file PDFOne.jar, and click on Next >.
    • Select PdfViewer and PdfPrinter from Marked JavaBeans list and click Next >.
    • Select Beans as the palette catagory for the selected "marked JavaBeans" and click Finish.
  7. Drag and drop GnosticePDFViewer bean to the JFrame, and resize it so that it occupies the whole of the JFrame minus the toolbar.
  8. Add the following declarations to the class declaration.

try < // Read PDF document specified by the user // in the text field d = new PdfDocument(); d.load(txtFilePath.getText()); // Display the document in viewer viewer.loadDocument(d); setTitle(txtFilePath.getText() + " - Gnostice PDF Viewer"); > catch(PdfException pdfEx) < JOptionPane.showMessageDialog( this, pdfEx.getMessage(), "Gnostice PDF Viewer", JOptionPane.ERROR_MESSAGE); setTitle("Gnostice PDF Viewer"); >catch(IOException ioEx)
try < // Display first page of the document viewer.firstPage(); > catch(PdfException pdfe) < JOptionPane.showMessageDialog( this, pdfe.getMessage(), "Gnostice PDF Viewer", JOptionPane.ERROR_MESSAGE); >catch(IOException ioe)

  1. zoom (page magnification)
  2. page number display
  3. viewer resizing - in sync with window resizing
---o0O0o---

Источник

Читайте также:  Centos get php version
Оцените статью