2011-12-28 38 views

Trả lời

4

Theo hiểu biết tốt nhất của bạn, bạn không thể thiết lập kỹ thuật trường PDF chuẩn làm hình ảnh (mặc dù bạn có thể thực hiện điều này bằng XFA).

Cách giải quyết, tuy nhiên, là chỉ cần tạo một hình ảnh chuẩn iTextSharp và chia tỷ lệ cho kích thước của trường biểu mẫu và đặt nó ở vị trí của trường.

Dưới đây là công việc đầy đủ C# 2010 ứng dụng WinForms nhắm mục tiêu iTextSharp 5.1.1.0 cho biết cách thực hiện việc này. Nó bắt đầu bằng cách tạo một tệp PDF rất đơn giản với một trường biểu mẫu duy nhất trên nó được gọi là "firstName". Phần thứ hai của chương trình sau đó nhận vị trí và kích thước của trường đó và đặt một hình ảnh ở đó một cách thích hợp. Xem các nhận xét trong mã để biết thêm chi tiết.

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

Xóa trường văn bản và thay thế bằng trường Nút có cùng kích thước và vị trí. Nếu bạn đặt nút bấm thành READ_ONLY thì nó không thể được nhấn và nó sẽ trông giống như một hình ảnh tĩnh. Việc này sẽ giữ hình ảnh bạn đang cố gắng thêm dưới dạng chú thích trường thay vì thêm nó vào nội dung trang.

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

Đây là câu trả lời phù hợp để đặt hình ảnh ở một vị trí cụ thể. `

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(); 
      } 
Các vấn đề liên quan