Adding droidtext image does not work

I am desperately trying to insert an image into an existing pdf with droidtext.

The original version of this project was made using iText. Thus, the code already exists and has been modified for Android.

What I do, I take an existing PDF as a background. Paste text and crosses at the indicated positions in this PDF file. Like filling out a form. This works very well so far without changing the code.

Now I want to set the image at the bottom of the page to sign the form. I used my original code and adapted it a bit. Which does not work at all. I tried to set the image at a specific position. Perhaps it was a mistake.

So I tried to do it in an “official” way. The image of Pengiuns.jpg is on the SD card.

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 , , ?

0
2

, " ", , 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);
+1

:

/* 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);
0

Source: https://habr.com/ru/post/1533150/


All Articles