PdfBox-Android icon indicating copy to clipboard operation
PdfBox-Android copied to clipboard

Image is not getting inserted in a existing PDF

Open surojitsalt opened this issue 1 year ago • 2 comments

I have an existing PDF, and I want to insert an image at a specific location. There are no errors when using the PDF or executing the code, but the image is not appearing. However, it works with other PDF files.

private String FILE_NAME ="my.pdf";
private int FILE_ID = R.raw.sam;
private String FILE_NAME1 ="my1.pdf";
private String IMAGE_FILE_NAME ="myImage.png";

public void addImage(File pFile) { try {

        if (!pFile.exists()) {
            pFile.createNewFile();
        }
        PDDocument vDocument = PDDocument.load(pFile);

        //get the number of pages
        PDPage vPage =  vDocument.getPage(0);
        PDPageContentStream vContentStream;

        File vFile = new File(getCacheDir(), IMAGE_FILE_NAME);
        File vFileNew = new File(getCacheDir(), FILE_NAME1);
        copyToCache(vFile, R.raw.sample1);

        // Define a content stream for adding to the PDF
        vContentStream = new PDPageContentStream(vDocument, vPage, PDPageContentStream.AppendMode.APPEND, true, true);


        // Draw the falcon base image
        PDImageXObject vImage = PDImageXObject.createFromFileByContent(vFile,vDocument);
        /*
        Bitmap vBitMap = BitmapFactory.decodeResource(getResources(), R.drawable.sample1);
        ByteArrayOutputStream vBAOS = new ByteArrayOutputStream();
        vBitMap.compress(Bitmap.CompressFormat.JPEG, 100, vBAOS);
        InputStream vInStream = new ByteArrayInputStream(vBAOS.toByteArray());
        PDImageXObject vImage = JPEGFactory.createFromStream(vDocument, vInStream);
        */
        vContentStream.drawImage(vImage, 50, 600,100,100);
        vContentStream.saveGraphicsState();
        vContentStream.restoreGraphicsState();
        vContentStream.close();

        // Save the final pdf document to a file
        vDocument.save(vFileNew);
        vDocument.close();

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

private void copyToCache(File pFile, @RawRes int pResourceID) throws IOException {
    //Get input stream object to read the pdf
    InputStream vInput = getResources().openRawResource(pResourceID);
    FileOutputStream vOutput = new FileOutputStream(pFile);
    byte[] vBuffer = new byte[1024];
    int vSize;

    // Copy the entire contents of the file
    while ((vSize = vInput.read(vBuffer)) != -1) {
        vOutput.write(vBuffer, 0, vSize);
    }

    //Close the buffer
    vInput.close();
    vOutput.close();
}

sam.pdf

Expected behavior The image should be inserted at the correct location.

Actual behavior No exception is occurring, but the image is not appearing.

Environment details: implementation 'com.tom-roush:pdfbox-android:2.0.27.0' compileSdk 34

defaultConfig {
    applicationId "com.sample.pdfwriter"
    minSdk 24
    //noinspection EditedTargetSdkVersion
    targetSdk 34
    versionCode 1
    versionName "1.0"

    multiDexEnabled true
}

surojitsalt avatar Aug 02 '24 12:08 surojitsalt

This PDF is generated using cognos.

surojitsalt avatar Aug 02 '24 13:08 surojitsalt

Please attach the result file.

THausherr avatar Aug 26 '24 10:08 THausherr