Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image is not getting inserted in a existing PDF #575

Open
surojitsalt opened this issue Aug 2, 2024 · 2 comments
Open

Image is not getting inserted in a existing PDF #575

surojitsalt opened this issue Aug 2, 2024 · 2 comments
Labels
type: bug Existing feature doesn't work correctly

Comments

@surojitsalt
Copy link

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 surojitsalt added the type: bug Existing feature doesn't work correctly label Aug 2, 2024
@surojitsalt
Copy link
Author

This PDF is generated using cognos.

@THausherr
Copy link

Please attach the result file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug Existing feature doesn't work correctly
Projects
None yet
Development

No branches or pull requests

2 participants