簡體   English   中英

iText在Android中將圖像轉換為pdf太短

[英]iText converted image to pdf is too short in android

我正在將圖像從sdcard轉換為pdf文件,但不幸的是,該圖像並未占用pdf文件的全長。 我如何設置圖像以覆蓋所有pdf文件。 這是我的代碼

File f = new File(filePath);
Bitmap bmp = BitmapFactory.decodeFile(f.getAbsolutePath());
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
Image companyLogo = Image.getInstance(stream.toByteArray());
companyLogo.setAbsolutePosition(10,700);
companyLogo.scalePercent(100);
document.add(companyLogo); 

誰能告訴我該怎么做才能覆蓋整個pdf頁面。 任何幫助將非常感激。 謝謝 :)

File f = new File(filePath);
Bitmap bmp = BitmapFactory.decodeFile(f.getAbsolutePath());
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
Image companyLogo = Image.getInstance(stream.toByteArray());

//get size of document
Rectangle documentRect = document.getPageSize();

if(bmp.getWidth()>documentRect.getWidth() || bmp.getHeight()>documentRect.getHeight())
{
    //bitmap is larger than page,so set bitmap's size similar to the whole page 
    companyLogo.scaleAbsolute(documentRect.getWidth(), documentRect.getHeight());
}
else
{
    //bitmap is smaller than page, so add bitmap simply.[note: if you want to fill page by stretching image, you may set size similar to page as above]
    companyLogo.scaleAbsolute(bmp.getWidth(), bmp.getHeight());
}

//set the image with center of the page
companyLogo.setAbsolutePosition((documentRect.getWidth()-image.getScaledWidth())/2, (documentRect.getHeight()-image.getScaledHeight())/2);

注意:

要使用上述代碼強制圖像覆蓋整個頁面,

只需更換

if(bmp.getWidth()>documentRect.getWidth() || bmp.getHeight()>documentRect.getHeight())
{
    //bitmap is larger than page,so set bitmap's size similar to the whole page 
    companyLogo.scaleAbsolute(documentRect.getWidth(), documentRect.getHeight());
}
else
{
    //bitmap is smaller than page, so add bitmap simply.[note: if you want to fill page by stretching image, you may set size similar to page as above]
    companyLogo.scaleAbsolute(bmp.getWidth(), bmp.getHeight());
}

    companyLogo.scaleAbsolute(documentRect.getWidth(), documentRect.getHeight());

我希望這會有所幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM