Giridhar Kumar
Giridhar Kumar

Reputation: 1

Open a PDF file from shared path in Java

I have a PDF file in my shared path. I have tried accessing it through the normal method but it's not happening. How to access that file?

This is the code I have tried.

import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;

/**
 * This class is used to read an existing
 *  pdf file using iText jar.
 * @author codesjava
 */
public class PDFReadExample {
  public static void main(String args[]){
    try {
    //Create PdfReader instance.
    PdfReader pdfReader = new PdfReader("D:\\testFile.pdf");    

    //Get the number of pages in pdf.
    int pages = pdfReader.getNumberOfPages(); 

    //Iterate the pdf through pages.
    for(int i=1; i<=pages; i++) { 
      //Extract the page content using PdfTextExtractor.
      String pageContent = 
        PdfTextExtractor.getTextFromPage(pdfReader, i);

      //Print the page content on console.
      System.out.println("Content on Page "
                          + i + ": " + pageContent);
      }

      //Close the PdfReader.
      pdfReader.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
  }
}

But what if my path is like this: http://team.net/po/kc ape/Platform%20Symbol/form%7D

Upvotes: 0

Views: 305

Answers (1)

Conffusion
Conffusion

Reputation: 4465

PdfReader has a constructor which accepts an InputStream. To access content behind an URL you can use the class URL:

import java.net.URL;
import java.io.InputStream;
import com.itextpdf.text.pdf.PdfReader;

URL url=new URL("http://...").
InputStream is = url.openStream();
PdfReader reader=new PdfReader(is);

Upvotes: 2

Related Questions