Java PDF ajoute des couches, prend en charge l'ajout de couche de plusieurs pages, comme suit
Code:
Importer java.io.file; import java.io.fileoutputStream; import java.io.ioexception; import java.util.list; import com.ithtpdf.text.documentException; import com.ithtpdf.text.image; importer com.ithtpdf.text.pdf.pdfcontentbyte; import; com.ithtpdf.text.pdf.pdfreader; Importer com.ithtpdf.text.pdf.pdfstamper; classe publique Pdfutils {/ ** * PDFADD Layer * * @param srcpdf * PATH FILE PDF ORIGINAL * @Param Distpdf Nodage PDF NAVEA Doit être numéro (combiné sur le nombre de pages correspondant à PDF dans l'ordre des noms et numéros d'images) * @return * @throws ioException * @throws documentException * / public static static markLocalimage42Dist (String srcpdf Fichier (srcpdf); if (! srcpdffile.exists ()) {lancez new illégalArgumentException ("Le fichier PDF qui doit être ajouté à la couche ne peut être trouvé"); } Pdfreader reader = new pdfreader (srcpdf); int n = reader.getNumberofPages (); // Nombre de pages PDF pdfstamper Stamp = new PdfStamper (Reader, new FileOutputStream (DISTPDF)); Pdfcontentbyte over; for (String LayerPath: LayerPatharr) {file coucheFile = new File (couchePath); String currentPageNo = coucheFile.getName (). Substring (0, coucheFile.getName (). LastIndexof (".")); // Nom d'image (correspondant au nombre de pages) Boolean isNum = currentPageno.matches ("[0-9] +"); if (! ISNUM) {Throw New illégalArgumentException ("est le nom de la couche un nombre"); } Image img = image.getInstance (couchePath); IMG.SetAbsoluTosition (0, 0); if (n> 0 && n> = Integer.ParseInt (currentPageNo)) {Over = Stamp.getOverContent (Integer.ParseInt (CurrentPageNo)); over.addimage (IMG); }} Stamp.Close (); Reader.Close (); retour DISTPDF; }}test:
public static void main (String [] args) lève ioException, documentException {list <string> imgurLlist = new ArrayList <> (); imgurllist.add ("d: /ts/testpdf/1.png"); //imgurllist.add("d:/ts/testpdf/2.png "); imgurllist.add ("d: /ts/testpdf/3.png"); MarkLocalimage42Dist ("d: /ts/testpdf/testpdf.pdf", "d: /ts/testpdf/testpdf2.pdf", imgurllist); }résultat:
PDF d'origine:
Après la synthèse PDF:
Ce qui précède est tout le contenu de cet article. J'espère que cela sera utile à l'apprentissage de tous et j'espère que tout le monde soutiendra davantage Wulin.com.