[英]droidtext adding image doesn't work
我拼命試圖將圖像插入到具有droidtext的現有pdf中。
該項目的原始版本是使用iText制作的。 因此該代碼已經存在,並已進行了修改以適合Android。
我要做的是將現有的PDF作為背景。 在此pdf文件中的指定位置插入文字和十字。 就像填寫表格一樣。 到目前為止,此方法運行良好,無需徹底更改代碼。
現在,我想在頁面底部設置一個圖像以對表單簽名。 我使用了原始代碼並對它進行了一些修改。 這根本不起作用。 我試圖將圖像設置在特定位置。 也許那是錯誤。
因此,我嘗試以“官方”方式進行操作。 Pengiuns.jpg圖像位於sd卡上。
try {
Document document = new Document();
File f=new File(Environment.getExternalStorageDirectory(), "SimpleImages.pdf");
PdfWriter.getInstance(document,new FileOutputStream(f));
document.open();
document.add(new Paragraph("Simple Image"));
String path = Environment.getExternalStorageDirectory()+"/Penguins.jpg";
if (new File(path).exists()) {
System.out.println("filesize: " + path + " = " + new File(path).length());
}
Image image =Image.getInstance(path);
document.add(image);
document.close();
} catch (Exception ex) {
System.out.println("narf");
}
但是仍然沒有任何圖像。 我得到的是一個帶有“簡單圖像”字樣的PDF,僅此而已。 我可以訪問圖片。 我通過if()得到正確的文件大小。 沒有異常被拋出。
所以我的問題是,如何將SD卡中的圖像獲取到pdf中? 這是什么錯誤? 但是最重要的是,如何將圖像設置為PDF格式的特定位置? 在我的原始代碼中,我為此使用setAbsolutePosition(x,y)。 當我在代碼中使用Eclipse時,它並沒有抱怨,但是它真的有效嗎?
之所以得到“簡單圖像”,是因為您在Paragraph
有它。 為了添加圖像,請使用:
Image myImg1 = Image.getInstance(stream1.toByteArray());
如果要在最后一頁中將其用作頁腳,則可以使用以下代碼,但它可以處理文本。 您可以嘗試對圖像進行操作:
Phrase footerText = new Phrase("THIS REPORT HAS BEEN GENERATED USING INSPECTIONREPORT APP");
HeaderFooter pdfFooter = new HeaderFooter(footerText, false);
doc.setFooter(pdfFooter);
這是示例代碼。 在這里,我已將圖像上傳到Imageview,然后添加到pdf。 希望這可以幫助。
private String NameOfFolder = "/InspectionReport";
Document doc = new Document();
try { //Path to look for App folder
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + NameOfFolder;
String CurrentDateAndTime= getCurrentDateAndTime();
// If App folder is not there then create one
File dir = new File(path);
if(!dir.exists())
dir.mkdirs();
//Creating new file and naming it
int i = 1;
File file = new File(dir, "Inspection"+Integer.toString(i)+"-" + CurrentDateAndTime+".pdf");
while(file.exists()) {
file = new File(dir, "Inspection"+Integer.toString(i++)+"-" + CurrentDateAndTime+".pdf");}
String filep= file.toString();
FileOutputStream fOut = new FileOutputStream(file);
Log.d("PDFCreator", "PDF Path: " + path);
PdfWriter.getInstance(doc, fOut);
Toast.makeText(getApplicationContext(),filep , Toast.LENGTH_LONG).show();
//open the document
doc.open();
ImageView img1 = (ImageView)findViewById(R.id.img1);
ByteArrayOutputStream stream1 = new ByteArrayOutputStream();
Bitmap bitmap1 = ((BitmapDrawable)img1.getDrawable()).getBitmap();
bitmap1.compress(Bitmap.CompressFormat.JPEG, 100 , stream1);
Image myImg1 = Image.getInstance(stream1.toByteArray());
myImg1.setAlignment(Image.MIDDLE);
doc.add(myImg1);
嘗試以下代碼:
/* Inserting Image in PDF */
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Bitmap bitmap = BitmapFactory.decodeResource(getBaseContext().getResources(), R.drawable.android);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100 , stream);
Image myImg = Image.getInstance(stream.toByteArray());
myImg.setAlignment(Image.MIDDLE);
//add image to document
doc.add(myImg);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.