J'utilise PdfBox pour générer un document PDF avec un fichier PDF existant contenant le modèle qui doit être utilisé pour chaque fichier PDF que je souhaite générer. Mais quand j'essaye de charger le pdf de modèle et veut écrire quelque chose dedans, tous les précédents ont été enlevés. Donc, je veux à la fois le contenu devrait être montré.Impossible d'ajouter du contenu supplémentaire à un fichier PDF existant à l'aide de PDFBox
S'il vous plaît suggérer une solution pour cela.
Voici le code que je suis en train de faire:
//Loading an existing document
File file = new File("/home/spaneos/ScoringReports-TM-110617.pdf");
PDDocument document = PDDocument.load(file);
//Retrieving the pages of the document
PDPage page = document.getPage(0);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
//Begin the Content stream
contentStream.beginText();
//Setting the font to the Content stream
contentStream.setFont(PDType1Font.TIMES_ROMAN, 16);
//Setting the leading
contentStream.setLeading(14.5f);
//Setting the position for the line
contentStream.newLineAtOffset(25, 725);
String text1 = "This is an example of adding text to a page in the pdf document.we can add as many lines";
String text2 = "as we want like this using the ShowText() method of the ContentStream class";
//Adding text in the form of string
contentStream.showText(text1);
contentStream.newLine();
contentStream.showText(text2);
//Creating PDImageXObject object
PDImageXObject pdImage = PDImageXObject.createFromFile("/home/spaneos/Downloads/man-161282_960_720.png",document);
//creating the PDPageContentStream object
PDPageContentStream contents = new PDPageContentStream(document, page);
contentStream.endText();
System.out.println("Content added");
//Closing the PDPageContentStream object
contents.close();
//Closing the content stream
contentStream.close();
//Saving the document
document.save(System.getProperty("user.dir").concat("/PdfBox_Examples/sample.pdf"));
//Closing the document
document.close();
}
Veuillez inclure votre code. – perigon
Comme votre question n'illustre pas exactement ce que vous faites, nous pouvons seulement dire que vous faites quelque chose de mal ... – mkl
oui j'ai ajouté mon code maintenant. –