導航:首頁 > 文件處理 > java獲取文件夾pdf頁數

java獲取文件夾pdf頁數

發布時間:2023-06-02 22:37:11

java中如何獲取pdf文件的總頁數

用Free Spire.PDF JAVA版和陸本
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import java.io.*;
public class CountPages{
public static void main(String[] args) {

//緩豎創建PdfDocument實例
PdfDocument doc=new PdfDocument();
//載入PDF文擾棚大件
doc.loadFromFile("test.pdf");
int pageCount = doc.getPages().getCount();
doc.close();
}
}

㈡ 使用 java 編程,獲取網路上的pdf文件

import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.widget.Toast;

public class MyService2 extends Service {

Thread thread = new Thread() {

@Override
public void run() {
try {
// 聲明拋出所有例外
URL tirc = new URL("http://www..com/img/_sylogo1.gif");
// 構建一URL對象
InputStream is = tirc.openStream();

FileOutputStream fos = new FileOutputStream(
"/mnt/sdcard/_sylogo21.gif");

byte[] buffer = new byte[1024];
int len = is.read(buffer);

while (len != -1) {

fos.write(buffer, 0, len);
len = is.read(buffer);
}

fos.close();
is.close();
handler.sendEmptyMessage(1);

} catch (Exception e) {
e.printStackTrace();
}
}

};

Handler handler = new Handler() {

@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
Toast.makeText(MyService2.this, "下載完成...", Toast.LENGTH_LONG)
.show();
break;
}
}

};

@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Toast.makeText(this, "Service onStart...", Toast.LENGTH_LONG).show();

thread.start();
}

@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "Service created...", Toast.LENGTH_LONG).show();
}

@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service destroyed...", Toast.LENGTH_LONG).show();
}

}
把鏈接地址改了就可以了

㈢ java 如何讀取PDF文件內容

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URL;
import org.pdfbox.pdmodel.PDDocument;
import org.pdfbox.util.PDFTextStripper;
public class PdfReader {
public void readFdf(String file) throws Exception {
// 是否排序
boolean sort = false;
// pdf文件名
String pdfFile = file;
// 輸入文本文件名稱
String textFile = null;
// 編碼方式
String encoding = "UTF-8";
// 開始提取頁數
int startPage = 1;
// 結束提取頁數
int endPage = Integer.MAX_VALUE;
// 文件輸入流,生成文本文件
Writer output = null;
// 內存中存儲的PDF Document
PDDocument document = null;
try {
try {
// 首先當作一個URL來裝載文件,如果得到異常再從本地文件系統//去裝載文件
URL url = new URL(pdfFile);
//注意參數已不是以前版本中的URL.而是File。
document = PDDocument.load(pdfFile);
// 獲取PDF的文件名
String fileName = url.getFile();
// 以原來PDF的名稱來命名新產生的txt文件
if (fileName.length() > 4) {
File outputFile = new File(fileName.substring(0, fileName
.length() - 4)
+ ".txt");
textFile = outputFile.getName();
}
} catch (MalformedURLException e) {
// 如果作為URL裝載得到異常則從文件系統裝載
//注意參數已不是以前版本中的URL.而是File。
document = PDDocument.load(pdfFile);
if (pdfFile.length() > 4) {
textFile = pdfFile.substring(0, pdfFile.length() - 4)
+ ".txt";
}
}
// 文件輸入流,寫入文件倒textFile
output = new OutputStreamWriter(new FileOutputStream(textFile),
encoding);
// PDFTextStripper來提取文本
PDFTextStripper stripper = null;
stripper = new PDFTextStripper();
// 設置是否排序
stripper.setSortByPosition(sort);
// 設置起始頁
stripper.setStartPage(startPage);
// 設置結束頁
stripper.setEndPage(endPage);
// 調用PDFTextStripper的writeText提取並輸出文本
stripper.writeText(document, output);
} finally {
if (output != null) {
// 關閉輸出流
output.close();
}
if (document != null) {
// 關閉PDF Document
document.close();
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
PdfReader pdfReader = new PdfReader();
try {
// 取得E盤下的SpringGuide.pdf的內容
pdfReader.readFdf("E://SpringGuide.pdf");
} catch (Exception e) {
e.printStackTrace();
}
}
}

㈣ java操作pdf文件,能否找到某個關鍵詞所在的頁碼 謝謝!

我剛剛做得一個PDF輸出的一個小功能 發給你不知道對你有沒有幫助
public static void getPDFDocument(ArrayList<UserTbl> list) throws DocumentException, IOException, BadElementException{

// ドキュメントの初期化
Document document = new Document(PageSize.A4);
// システム時間を取得する
Date date = new Date();
SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH'時'mm'分'ss'秒'");
SimpleDateFormat formatter2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString=formatter.format(date);
String dateString2=formatter2.format(date);
// ドキュメントURLの設定
String filePath = "e://"+dateString+".pdf";
PdfWriter.getInstance(document, new FileOutputStream(filePath));
// ドキュメントをオープンする
document.open();
// 文字様式を設定
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font f11 = new Font(bfChinese, 11, Font.NORMAL);
Font f8 = new Font(bfChinese, 8, Font.NORMAL);
Font f12 = new Font(bfChinese, 12, Font.BOLD);
// タイトルを作成する

Paragraph p1 = new Paragraph("利用者情報一覧\n", f12);
p1.setAlignment(Element.ALIGN_CENTER);
document.add(p1);
Paragraph p2 = new Paragraph("時間:"+dateString2+"\n\n", f11);
p2.setAlignment(Element.ALIGN_RIGHT);
document.add(p2);

// テーブルを作成する
PdfPTable table = new PdfPTable(7);
PdfPCell cell = new PdfPCell();
cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
cell.setPhrase(new Paragraph("PCログインID", f8));
table.addCell(cell);
cell.setPhrase(new Paragraph("お名前我", f8));
table.addCell(cell);
cell.setPhrase(new Paragraph("メールアドレス", f8));
table.addCell(cell);
cell.setPhrase(new Paragraph("攜帯ログインID", f8));
table.addCell(cell);
cell.setPhrase(new Paragraph("所屬", f8));
table.addCell(cell);
cell.setPhrase(new Paragraph("屬性", f8));
table.addCell(cell);
cell.setPhrase(new Paragraph("データ種別", f8));
table.addCell(cell);
document.add(table);
table.setWidthPercentage(100);
// テーブルの中の內容
for(UserTbl bean : list){

document.add(getPdfHeader(bean,list));
}

document.close();
}

public static PdfPTable getPdfHeader(UserTbl bean,ArrayList<UserTbl> list) throws DocumentException, IOException{
PdfPTable table = new PdfPTable(7);
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font f8 = new Font(bfChinese, 8, Font.NORMAL);
// PCログインID
String userId = StringUtils.trim(bean.getUser_id());
// お名前
String userName = StringUtils.trim(bean.getUser_name());
// メールアドレス
String mobileMail = StringUtils.trim(bean.getMobile_mail());
// 攜帯ログインID
String mobileId = StringUtils.trim(bean.getMobile_id());
// 所屬
String MobilePass = StringUtils.trim(bean.getMobile_pass());
// 屬性
String attr = StringUtils.trim(bean.getAttr());
// データ種別
String secName = StringUtils.trim(bean.getSec_name());
//寫入table
PdfPCell cell = new PdfPCell();
cell.setPhrase(new Paragraph(userId, f8));
table.addCell(cell);
cell.setPhrase(new Paragraph(userName, f8));
table.addCell(cell);
cell.setPhrase(new Paragraph(mobileMail, f8));
table.addCell(cell);
cell.setPhrase(new Paragraph(mobileId, f8));
table.addCell(cell);
cell.setPhrase(new Paragraph(MobilePass, f8));
table.addCell(cell);
cell.setPhrase(new Paragraph(attr, f8));
table.addCell(cell);
cell.setPhrase(new Paragraph(secName, f8));
table.addCell(cell);
return table;

}
}

㈤ 用java讀取pdf

可以使用PDFBOX0.7.3控制項:
import java.io.InputStream;import java.io.IOException;
import org.apache.lucene.document.Document;import org.pdfbox.cos.COSDocument;
import org.pdfbox.pdfparser.PDFParser;import org.pdfbox.pdmodel.PDDocument;
import org.pdfbox.pdmodel.PDDocumentInformation;import org.pdfbox.util.PDFTextStripper;
import com.search.code.Index;
public Document getDocument(Index index, String url, String title, InputStream is)throws DocCenterException {COSDocument cosDoc = null;<br/> try {cosDoc = parseDocument(is);<br/> } catch (IOException e) {
closeCOSDocument(cosDoc);
throw new DocCenterException("無法處理該PDF文檔", e);
}
if (cosDoc.isEncrypted()) {
if (cosDoc != null)
closeCOSDocument(cosDoc);
throw new DocCenterException("該PDF文檔是加密文檔,無法處理");
}
String docText = null;
try {
PDFTextStripper stripper = new PDFTextStripper();
docText = stripper.getText(new PDDocument(cosDoc));
} catch (IOException e) {
closeCOSDocument(cosDoc);
throw new DocCenterException("無法處理該PDF文檔", e);
}
PDDocument pdDoc = null;
try {pdDoc = new PDDocument(cosDoc);<br/> PDDocumentInformation docInfo = pdDoc.getDocumentInformation();<br/> if(docInfo.getTitle()!=null && !docInfo.getTitle().equals("")){<br/> title = docInfo.getTitle();}
} catch (Exception e) {
closeCOSDocument(cosDoc);
closePDDocument(pdDoc);
System.err.println("無法取得該PDF文檔的元數據" + e.getMessage());
} finally {
closeCOSDocument(cosDoc);
closePDDocument(pdDoc);
}
return null;
}
private static COSDocument parseDocument(InputStream is) throws IOException {
PDFParser parser = new PDFParser(is);parser.parse();return parser.getDocument();
}
private void closeCOSDocument(COSDocument cosDoc) {
if (cosDoc != null) {try {cosDoc.close();} catch (IOException e) {}
}}
private void closePDDocument(PDDocument pdDoc) {
if (pdDoc != null) {
try { pdDoc.close();
} catch (IOException e) {
}}}

㈥ 怎麼用java讀取pdf文件內容

你可以把pdf轉成word在進行讀取
推薦使用轉轉大師pdf轉word轉換器,免費的在線工具
網路搜索下,在線免費轉換就行了,不用下載注冊,很方便

㈦ android 如何獲得pdf文件的頁數

分頁方法:

  1. 打開網路pdf 思路整體還是來源與圖片的載入。

  2. android中載入網路圖片的框架有很多個。如image-laoder, fresco、glide等,首先都是從內存中找圖片,如果內存中沒有,接著從本地找,本地沒有在從網路下載。

  3. android中載入pdf也是類似,首先從本地找pdf文件,如果本地存在該pdf文件,直接打開,如果本地不存在,將該pdf文件下載到本地在打開。

  4. 下載文件用到了retrofit2的庫,已經封裝到android_pdf中了。

  5. 2.依賴android_pdf庫方法

  6. 2.1 在項目的gradle中增加如下代碼:

  7. compile 'com.lidong.pdf:android_pdf:1.0.1'

  8. 2.2 一句代碼就可以載入網路pdf。

  9. ```

  10. pdfView.fileFromLocalStorage(this,this,this,fileUrl,fileName); //設置pdf文件地址

  11. ```

  12. 2.3對fileFromLocalStorage(this,this,this,fileUrl,fileName)的解析

  13. /**

  14. ```

  15. * 載入pdf文件

  16. * @param onPageChangeListener

  17. * @param onLoadCompleteListener

  18. * @param onDrawListener

  19. * @param fileUrl

  20. * @param fileName

  21. */

  22. public void fileFromLocalStorage(

  23. final OnPageChangeListener onPageChangeListener,

  24. final OnLoadCompleteListener onLoadCompleteListener,

  25. final OnDrawListener onDrawListener,

  26. String fileUrl,

  27. final String fileName)

  28. ```

  29. 1. OnPageChangeListener onPageChangeListener :翻頁回調

  30. 2. OnLoadCompleteListener onLoadCompleteListener:載入完成的回調

  31. 3. OnDrawListener:頁面繪制的回調

  32. 4. String fileUrl : 文件的網路地址

  33. 5. String fileName 文件名稱

  34. 3.使用android_pdf庫方法

  35. 3.1寫一個布局文件

  36. ```

  37. <?xml version="1.0" encoding="utf-8"?>

  38. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

  39. xmlns:tools="http://schemas.android.com/tools"

  40. android:id="@+id/activity_main"

  41. android:layout_width="match_parent"

  42. android:layout_height="match_parent"

  43. tools:context="com.lidong.pdf.androidpdf.MainActivity">

  44. <com.lidong.pdf.PDFView

  45. android:id="@+id/pdfView"

  46. android:layout_width="match_parent"

  47. android:layout_height="match_parent"

  48. />

  49. </RelativeLayout>

  50. ```

  51. 3.2在MainActivity中載入

  52. ```

  53. import android.graphics.Canvas;

  54. import android.os.Environment;

  55. import android.support.v7.app.AppCompatActivity;

  56. import android.os.Bundle;

  57. import android.util.Log;

  58. import android.widget.Toast;

  59. import com.lidong.pdf.PDFView;

  60. import com.lidong.pdf.api.ApiManager;

  61. import com.lidong.pdf.listener.OnDrawListener;

  62. import com.lidong.pdf.listener.OnLoadCompleteListener;

  63. import com.lidong.pdf.listener.OnPageChangeListener;

  64. import com.lidong.pdf.util.FileUtils;

  65. import java.io.File;

  66. import java.io.FileOutputStream;

  67. import java.io.IOException;

  68. import java.io.InputStream;

  69. import okhttp3.ResponseBody;

  70. import rx.android.schelers.AndroidSchelers;

  71. import rx.functions.Action1;

  72. import rx.schelers.Schelers;

  73. public class MainActivity extends AppCompatActivity implements OnPageChangeListener

  74. ,OnLoadCompleteListener, OnDrawListener {

  75. private PDFView pdfView ;

  76. @Override

  77. protected void onCreate(Bundle savedInstanceState) {

  78. super.onCreate(savedInstanceState);

  79. setContentView(R.layout.activity_main);

  80. pdfView = (PDFView) findViewById( R.id.pdfView );

  81. displayFromFile1("http://file.chmsp.com.cn/colligate/file/00100000224821.pdf", "00100000224821.pdf");

  82. }

  83. /**

  84. * 獲取打開網路的pdf文件

  85. * @param fileUrl

  86. * @param fileName

  87. */

  88. private void displayFromFile1( String fileUrl ,String fileName) {

  89. pdfView.fileFromLocalStorage(this,this,this,fileUrl,fileName); //設置pdf文件地址

  90. }

  91. /**

  92. * 翻頁回調

  93. * @param page

  94. * @param pageCount

  95. */

  96. @Override

  97. public void onPageChanged(int page, int pageCount) {

  98. Toast.makeText( MainActivity.this , "page= " + page +

  99. " pageCount= " + pageCount , Toast.LENGTH_SHORT).show();

  100. }

  101. /**

  102. * 載入完成回調

  103. * @param nbPages 總共的頁數

  104. */

  105. @Override

  106. public void loadComplete(int nbPages) {

  107. Toast.makeText( MainActivity.this , "載入完成" + nbPages , Toast.LENGTH_SHORT).show();

  108. }

  109. @Override

  110. public void onLayerDrawn(Canvas canvas, float pageWidth, float pageHeight, int displayedPage) {

  111. // Toast.makeText( MainActivity.this , "pageWidth= " + pageWidth + "

  112. // pageHeight= " + pageHeight + " displayedPage=" + displayedPage , Toast.LENGTH_SHORT).show();

  113. }

  114. ```

閱讀全文

與java獲取文件夾pdf頁數相關的資料

熱點內容
解壓神器魔術 瀏覽:770
寬頻連接2如何連接伺服器地址 瀏覽:365
隨機信號估計演算法 瀏覽:860
安卓如何重壓開槍 瀏覽:377
航天時代飛鵬圖像處理演算法 瀏覽:521
php比較兩個文件 瀏覽:737
加密貨幣市場活躍 瀏覽:334
最便宜的雲盤伺服器架設傳奇 瀏覽:790
java反向工程 瀏覽:110
pdf文檔轉換excel 瀏覽:8
主角叫楚天的都市小說 瀏覽:754
程序員三重境界 瀏覽:871
菜雞方舟上怎麼開伺服器 瀏覽:727
馬林固件編譯錯誤 瀏覽:910
市場營銷案例pdf 瀏覽:770
魔爪閱讀網 瀏覽:19
app地推業績統計在哪裡 瀏覽:993
維語電影網站大全 瀏覽:958
程序員骨腫瘤上熱搜 瀏覽:847
聚優電影 瀏覽:45