第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

通過 Android PDFDocument 生成的 PDF 尺寸太大。

通過 Android PDFDocument 生成的 PDF 尺寸太大。

侃侃無極 2024-01-05 14:39:25
我正在使用 android pdf 文檔庫將圖像轉(zhuǎn)換為 pdf,但它生成非常大的 pdf。PdfDocument document = new PdfDocument();PdfDocument.PageInfo pageInfo =new PdfDocument.PageInfo.Builder(bitmap.getWidth(), bitmap.getHeight(), 1).create();                            PdfDocument.Page  page = document.startPage(pageInfo);Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight(),false);                        Canvas canvas = page.getCanvas();canvas.drawBitmap(scaledBitmap, 0f, 0f, null);document.finishPage(page);document.writeTo(new FileOutputStream(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)+"/"+newPDFNameSingle));document.close();這是 apache pdf box 實現(xiàn),但它正在剪切輸出 pdf 中的圖像PDDocument document = new PDDocument();PDPage page = new PDPage();document.addPage(page);PDPageContentStream contentStream = new PDPageContentStream(document, page);InputStream inputStream = new FileInputStream(tempFile);PDImageXObject ximage = JPEGFactory.createFromStream(document,inputStream);contentStream.drawImage(ximage, 20, 20);contentStream.close();document.save(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)+"/"+newPDFNameSingle);                            document.close();如何實現(xiàn)常規(guī)尺寸的 pdf 生成?我的圖像大小為 100 kb,但 pdf 生成 1 mb 文件。
查看完整描述

1 回答

?
holdtom

TA貢獻1805條經(jīng)驗 獲得超10個贊

在您的 Android pdf 文檔庫代碼中,您將頁面大小設(shè)置為圖像高度和寬度值


PdfDocument.PageInfo.Builder(bitmap.getWidth(), bitmap.getHeight(), 1).create();                            

并在原點繪制圖像:


canvas.drawBitmap(scaledBitmap, 0f, 0f, null);

您可以在 PDFBox 代碼中執(zhí)行相同的操作:


PDDocument document = new PDDocument();


PDImageXObject ximage = JPEGFactory.createFromStream(document,imageResource);


PDPage page = new PDPage(new PDRectangle(ximage.getWidth(), ximage.getHeight()));

document.addPage(page);


PDPageContentStream contentStream = new PDPageContentStream(document, page);

contentStream.drawImage(ximage, 0, 0);

contentStream.close();

(繪制圖像測試testDrawImageToFitPage)


或者,正如評論中所討論的,您可以在繪制圖像之前設(shè)置當前變換矩陣,以將其縮小以適合頁面。


查看完整回答
反對 回復(fù) 2024-01-05
  • 1 回答
  • 0 關(guān)注
  • 171 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號