#Region Project Attributes
#MainFormWidth: 600
#MainFormHeight: 600
#End Region
#AdditionalJar:pdfbox-app-2.0.16.jar
Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Private xui As XUI
Private Button1 As B4XView
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("Layout1")
MainForm.Show
Dim jo As JavaObject = Me
Dim inPDFPath As String= File.Combine(File.DirApp,"test.pdf")
Dim outPDFPath As String= File.Combine(File.DirApp,"modified.pdf")
Dim imgPath As String= File.Combine(File.DirApp,"SampleJPG.jpg")
Dim pageNo As Int = 0
jo.RunMethod("InsertingImageInPdf",Array(inPDFPath,outPDFPath,imgPath, pageNo))
End Sub
#if Java
import java.io.File;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
public static void InsertingImageInPdf(String inpPDFFile, String outpPDFFile, String imgFile, int pageNumber) throws Exception {
//Loading an existing document
File file = new File(inpPDFFile);
PDDocument doc = PDDocument.load(file);
//Retrieving the page
PDPage page = doc.getPage(pageNumber);
//Creating PDImageXObject object
PDImageXObject pdImage = PDImageXObject.createFromFile(imgFile, doc);
//creating the PDPageContentStream object
PDPageContentStream contents = new PDPageContentStream(doc, page);
//Drawing the image in the PDF document. The numbers are the coordinates, width and height of the image
contents.drawImage(pdImage, 10, 650,100,100);
System.out.println("Image inserted");
//Closing the PDPageContentStream object
contents.close();
//Saving the document
doc.save(outpPDFFile);
//Closing the document
doc.close();
}
#End If