2011-12-28 11 views
6

enter image description hereमैं मौजूदा पीडीएफ फ़ाइल में एक पीडीएफ फ़ील्ड में एक छवि कैसे सेट कर सकता हूं?

मैं मौजूदा पीडीएफ फ़ाइल में एक पीडीएफ फ़ील्ड में एक छवि कैसे सेट कर सकता हूं?

मैं iTextSharp ऑब्जेक्ट का उपयोग कर रहा हूं।

टेक्स्ट फ़ील्ड सेट करना ठीक काम कर रहा है। इसमें कोई समस्या नहीं है।

pdfFormFields.SetField("Firstname", "Mujeeb"); 

कृपया मदद करें।

उत्तर

4

मेरे सर्वोत्तम ज्ञान के लिए आप तकनीकी रूप से एक मानक पीडीएफ फ़ील्ड को एक छवि के रूप में सेट नहीं कर सकते (हालांकि आप इसे एक्सएफए के साथ कर सकते हैं)।

हालांकि, वर्कअराउंड केवल एक मानक iTextSharp छवि बनाना है और इसे फॉर्म फ़ील्ड के आयामों में स्केल करना है और इसे फ़ील्ड कहां रखना है।

नीचे एक पूर्ण कामकाजी सी # 2010 WinForms ऐप लक्ष्यीकरण iTextSharp 5.1.1.0 है जो दिखाता है कि यह कैसे करें। यह एक बहुत ही सरल पीडीएफ बनाकर शुरू होता है जिसे "फर्स्टनाम" कहा जाता है। कार्यक्रम के दूसरे भाग को उस क्षेत्र की स्थिति और आयाम मिलते हैं और वहां एक छवि को उचित तरीके से स्केल किया जाता है। अधिक जानकारी के लिए कोड में टिप्पणियां देखें।

using System; 
using System.ComponentModel; 
using System.Text; 
using System.Windows.Forms; 
using System.IO; 
using iTextSharp.text; 
using iTextSharp.text.pdf; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      string baseFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "StartFile.pdf"); 
      string secondFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "SecondFile.pdf"); 
      string TestImage = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.jpg"); 

      //Create a very simple PDF with a single form field called "firstName" 
      using (FileStream fs = new FileStream(baseFile, FileMode.Create, FileAccess.Write, FileShare.None)) 
      { 
       using (Document doc = new Document(PageSize.LETTER)) 
       { 
        using (PdfWriter writer = PdfWriter.GetInstance(doc, fs)) 
        { 
         doc.Open(); 
         writer.AddAnnotation(new TextField(writer, new iTextSharp.text.Rectangle(0, 0, 100, 100), "firstName").GetTextField()); 
         doc.Close(); 
        } 
       } 
      } 


      //Create a second file "filling out" the form above 
      using (FileStream fs = new FileStream(secondFile, FileMode.Create, FileAccess.Write, FileShare.None)) 
      { 
       using (PdfStamper stamper = new PdfStamper(new PdfReader(baseFile), fs)) 
       { 
        //GetFieldPositions returns an array of field positions if you are using 5.0 or greater 
        //This line does a lot and should really be broken up for null-checking 
        iTextSharp.text.Rectangle rect = stamper.AcroFields.GetFieldPositions("firstName")[0].position; 
        //Create an image 
        iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(TestImage); 
        //Scale it 
        img.ScaleAbsolute(rect.Width, rect.Height); 
        //Position it 
        img.SetAbsolutePosition(rect.Left, rect.Bottom); 
        //Add it to page 1 of the document 
        stamper.GetOverContent(1).AddImage(img); 
        stamper.Close(); 
       } 
      } 

      this.Close(); 
     } 
    } 
} 
8

टेक्स्ट फ़ील्ड को हटाएं और इसे उसी आकार और स्थिति के पुशबटन क्षेत्र से प्रतिस्थापित करें। यदि आप पुशबटन को READ_ONLY पर सेट करते हैं तो इसे दबाया नहीं जा सकता है और यह एक स्थिर छवि की तरह दिखेगा। यह उस छवि को रखता है जिसे आप पृष्ठ सामग्री में जोड़ने के बजाय फ़ील्ड एनोटेशन के रूप में जोड़ने की कोशिश कर रहे हैं।

void ConvertTextFieldToImage(string inputFile, string fieldName, string imageFile, string outputFile) 
{ 
    using (PdfStamper stamper = new PdfStamper(new PdfReader(inputFile), File.Create(outputFile))) 
    { 
     AcroFields.FieldPosition fieldPosition = stamper.AcroFields.GetFieldPositions(fieldName)[0]; 

     PushbuttonField imageField = new PushbuttonField(stamper.Writer, fieldPosition.position, fieldName); 
     imageField.Layout = PushbuttonField.LAYOUT_ICON_ONLY; 
     imageField.Image = iTextSharp.text.Image.GetInstance(imageFile); 
     imageField.ScaleIcon = PushbuttonField.SCALE_ICON_ALWAYS; 
     imageField.ProportionalIcon = false; 
     imageField.Options = BaseField.READ_ONLY; 

     stamper.AcroFields.RemoveField(fieldName); 
     stamper.AddAnnotation(imageField.Field, fieldPosition.page); 

     stamper.Close(); 
    } 
} 
0

यह वह उत्तर है जो किसी छवि को किसी विशिष्ट स्थान पर रखने के लिए काम करता है। `

using (PdfStamper stamper = new PdfStamper(new PdfReader(fromFilePath), File.Create("toFilePath"))) 
      { 
       AcroFields.FieldPosition fieldPosition = stamper.AcroFields.GetFieldPositions("btn1")[0]; 

       PushbuttonField imageField = new PushbuttonField(stamper.Writer, fieldPosition.position, "btn1Replaced"); 
       imageField.Layout = PushbuttonField.LAYOUT_ICON_ONLY; 
       imageField.Image = iTextSharp.text.Image.GetInstance(ImageLocationPath); 
       imageField.ScaleIcon = PushbuttonField.SCALE_ICON_ALWAYS; 
       imageField.ProportionalIcon = false; 
       imageField.Options = BaseField.READ_ONLY; 

       stamper.AcroFields.RemoveField("btn1"); 
       stamper.AddAnnotation(imageField.Field, fieldPosition.page); 

       stamper.Close(); 
      } 
संबंधित मुद्दे