Saturday, February 5, 2011

كيفية استخدام iText لتعديل ملفات pdf

مكتبة iText من افضل المكتبات للتعامل برمجيا مع ملفات pdf. وهناك نسخة منها لأغلب لغات البرمجة مثل iTextSharp للغة C#.
لتعديل حقل معين كالاسم الاول في فورم بملف pdf باستخدام جافا:

 public class ModifyFN  
 {  
   public static void main(String[] args) throws DocumentException, IOException  
   {  
     Document document = new Document (PageSize.A4);  
     PdfWriter pdfwriter= PdfWriter.getInstance (document , new FileOutputStream ("c:\\hp\\newform.pdf"));  
     PdfStamper stamp = new PdfStamper(reader, new FileOutputStream("c:\\hp\\oldform.pdf"));  
     AcroFields form = stamp.getAcroFields();  
     HashMap hs = form.getFields();  
     System.out.println(“hashmap size = ” + hs.size());  
     Iterator iter = hs.keySet().iterator();  
     while(iter.hasNext())  
     {  
       Object key = iter.next();  
       System.out.println(“field = ” + key.toString());  
     }  
     //set the field values in the pdf form  
     form.setField(“FirstName”,”Mohammad”);  
     stamp.close();  
   }  
 }  

No comments:

Post a Comment