2009-12-04 8 views
12

itextsharp के साथ मौजूदा पीडीएफ में कोई फॉर्म फ़ील्ड कैसे जोड़ें?itextsharp के साथ मौजूदा पीडीएफ में कोई फॉर्म फ़ील्ड कैसे जोड़ें?

मेरे पास एक मौजूदा पीडीएफ दस्तावेज़ है, मैं प्रतिलिपि बनाकर और एक नया दस्तावेज़ लिखने के बिना फॉर्म फ़ील्ड जोड़ना चाहता हूं।

उत्तर

7

आगे की समीक्षा के बाद, क्षेत्र पर शासन उलटा हुआ है। बाहर निकलता है यदि आप स्टैमर को फ़्लैटन करते हैं तो फ़ील्ड परिणामस्वरूप दस्तावेज़ पर दिखाई नहीं देते हैं (क्योंकि उनमें 'उपस्थिति' सेटिंग नहीं होती है)। बीटीडब्लू, फॉर्म फ़्लैटिंग फॉर्म फॉर्म के आगे के संपादन को रोकता है। अब हम फॉर्म में उपस्थिति जोड़ सकते हैं, हालांकि, टेक्स्टफिल्ड क्लास का उपयोग करना और 'उपस्थिति' ऑब्जेक्ट्स को स्पष्ट रूप से सेट करने के बारे में चिंता करने का एक आसान तरीका नहीं है।

public void ABetterWayToAddFormFieldToExistingPDF() 
{ 
    PdfReader reader = new PdfReader(@"c:\existing.pdf"); 

    FileStream out = new FileStream(@"C:\existingPlusFields.pdf", FileMode.Create, FileAccess.Write); 

    PdfStamper stamp = new PdfStamper(reader, out);   

    TextField field = new TextField(stamp.Writer, new iTextSharp.text.Rectangle(40, 500, 360, 530), "some_text"); 

    // add the field here, the second param is the page you want it on   
    stamp.AddAnnotation(field.GetTextField(), 1); 

    stamp.FormFlattening = true; // lock fields and prevent further edits. 

    stamp.Close(); 
} 
+2

AddAnotation के साथ जोड़े गए फ़ील्ड वैसे भी फ़्लैट नहीं किए गए हैं, Formexatthar प्रॉपर्टी पर itextsharp के स्रोत कोड में टिप्पणी देखें http://sourceforge.net/p/itextsharp/code/453/tree/trunk/src/core/iTextSharp/text /pdf/PdfStamper.cs –

2

मैं थोड़ी देर तो मैं पोस्ट होता प्रश्न

उत्तर & का उपयोग PdfStamper iText वर्ग की कुंजी है सोचा के लिए इस के साथ संघर्ष किया। (मुझे लगता है कि यह एक प्रतिलिपि बनाता है लेकिन यह Iext PdfCopy कक्षाओं का उपयोग करने से कहीं अधिक क्लीनर है)।

public void AddFormFieldToExistingPDF() 
{ 
    PdfReader reader = new PdfReader(@"c:\existing.pdf"); 

    FileStream out = new FileStream(@"C:\existingPlusFields.pdf", FileMode.Create, FileAccess.Write); 

    PdfStamper stamp = new PdfStamper(reader, out);   

    PdfFormField field = PdfFormField.CreateTextField(stamp.Writer, false, false, 50); 

    // set a field w/some position and size 
    field.SetWidget(new iTextSharp.text.Rectangle(40, 500, 360, 530), 
      PdfAnnotation.HIGHLIGHT_INVERT); 

    field.SetFieldFlags(PdfAnnotation.FLAGS_PRINT); 
    field.FieldName = "some_field"; 

    // add the field here, the second param is the page you want it on 
    stamp.AddAnnotation(field, 1);       
    stamp.Close(); 
} 
+0

मेरा कोड आपके जैसा ही है। हस्ताक्षर फ़ील्ड स्क्रीन पर पूरी तरह से सामान्य व्यवहार करता है, लेकिन एक बार जब मैं इसे प्रिंट करने का प्रयास करता हूं, तो यह कागज़ पर खाली दिखाई देता है। क्या आपके साथ ऐसा होता है? – Haoest

0

pdfStamper का उपयोग करके आप इसे पूरा कर सकते हैं।

PdfStamper Stamper= new PdfStamper(new PdfReader(sourcefile), File.Create(NewOutputFile)); 

TextField moreText = new TextField(Stamper.Writer, 
          new iTextSharp.text.Rectangle(20, 20, 590, 780), "moreText"); 

      moreText.Visibility = TextField.VISIBLE_BUT_DOES_NOT_PRINT; 
      moreText.Text = "Use this space for any additional information"; 
      moreText.Options = (TextField.MULTILINE); 

PdfFormField Fieldtxt = moreText.GetTextField(); 

Stamper.AddAnnotation(Fieldtxt, n); 
संबंधित मुद्दे