2012-09-07 72 views
0

我們使用iTextSharp來創建使用C#的PDF文件。但是,當我使用某種PDF編輯器編輯創建的PDF文件時,我無法完美地進行編輯。因爲一些編輯過的文本是重疊的,有些未顯示或隱藏。所以,我想選擇其他方法使用iTextSharp創建可編輯的PDF文件。有什麼辦法可以通過iTextSharp來創建可編輯的PDF文件?

當使用iTextSharp構建PDF文件時,是否有任何參數(使PDF文檔可編輯)添加以創建editable PDF files

請指引我走出這個問題的?

回答

1

首先你的問題不是很清楚。其次,我假設你正試圖從C#代碼創建PDF。

vissualy改進此方法是使用Open Office來創建PDF template。 然後,在使用您在模板中創建的可編輯字段中編寫的模板之後。 下面是一些代碼,以幫助您:

public class DocumentDownload : PdfTemplateHandler 
{ 

    protected override string TemplatePath 
    { 
     get { return "~/App_Data/PdfTemplates/MyDocument_2011_v1.pdf"; } 
    } 

    protected override void LoadDataInternal() 
    { 

     documentType = Request["docType"] != null ? Request["docType"].ToString() : ""; 

     if (uid.Length < 1) 
     { 
      Response.Write("Invalid request!"); 
      Response.End(); 
     } 
     // load data 

     DownloadFileName = string.Format("MyDocument_{0}_{1}.pdf", 1234, DateTime.Now.ToBinary()); 
    } 

    protected override void SetFieldsInternal(iTextSharp.text.pdf.AcroFields acroFields) 
    { 

     //iTextSharp.text.pdf.BaseFont unicode = iTextSharp.text.pdf.BaseFont.createFont(unicodeFontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);    
     //var unicodeFont = iTextSharp.text.pdf.BaseFont.CreateFont(iTextSharp.text.FontFactory.TIMES_ROMAN, iTextSharp.text.pdf.BaseFont.IDENTITY_H, iTextSharp.text.pdf.BaseFont.EMBEDDED); 
     acroFields.SetField("txtrNumber", Number.ToString()); 

     acroFields.SetField("cbTaxi", "Yes"); 

    } 
} 


public abstract class PdfTemplateHandler : IHttpHandler 
{ 
    public virtual bool DownloadAsAttachment 
    { 
     get 
     { 
      return true; 
     } 
    } 

    protected virtual TimeSpan PdfTemplateCacheDuration 
    { 
     get 
     { 
      return TimeSpan.FromMinutes(30); 
     } 
    } 

    protected virtual string PdfTemplateCacheKey 
    { 
     get 
     { 
      return string.Format("__PDF_Template[{0}]", TemplatePath); 
     } 
    } 

    protected string DownloadFileName { get; set; } 

    protected HttpContext Context { get; private set; } 

    protected HttpResponse Response { get; private set; } 

    protected HttpRequest Request { get; private set; } 

    #region IHttpHandler Members 

    public bool IsReusable 
    { 
     get { return false; } 
    } 

    public void ProcessRequest(HttpContext context) 
    { 

     Context = context; 
     Response = context.Response; 
     Request = context.Request; 

     try 
     { 
      LoadDataInternal(); 
     } 
     catch (ThreadAbortException) 
     { 
      // no-op 
     } 
     catch (Exception ex) 
     { 
      //Logger.LogError(ex); 
      Response.Write("Error!"); 
      Response.End(); 
     } 

     Response.BufferOutput = true; 
     Response.ClearHeaders(); 
     Response.ContentType = "application/pdf"; 

     if (DownloadAsAttachment) 
     { 
      Response.AddHeader("Content-Disposition", "attachment; filename=" + 
       (string.IsNullOrEmpty(DownloadFileName) ? context.Session.SessionID + ".pdf" : DownloadFileName)); 
     } 

     PdfStamper pst = null; 
     try 
     { 
      PdfReader reader = new PdfReader(GetTemplateBytes()); 
      pst = new PdfStamper(reader, Response.OutputStream); 
      var acroFields = pst.AcroFields; 

      pst.FormFlattening = true; 
      pst.FreeTextFlattening = true; 
      pst.SetFullCompression(); 

      SetFieldsInternal(acroFields); 
      pst.Close(); 
     } 
     finally 
     { 
      if (pst != null) 
       pst.Close(); 
     } 
    } 

    #endregion 

    #region Abstract Members for overriding and providing functionality 

    protected abstract string TemplatePath { get; } 

    protected abstract void LoadDataInternal(); 

    protected abstract void SetFieldsInternal(AcroFields acroFields); 

    #endregion 

    protected virtual byte[] GetTemplateBytes() 
    { 
     var data = Context.Cache[PdfTemplateCacheKey] as byte[]; 
     if (data == null) 
     { 
      data = File.ReadAllBytes(Context.Server.MapPath(TemplatePath)); 
      Context.Cache.Insert(PdfTemplateCacheKey, data, 
       null, DateTime.Now.Add(PdfTemplateCacheDuration), Cache.NoSlidingExpiration); 
     } 
     return data; 
    } 

    protected static string unicode_iso8859(string src) 
    { 
     Encoding iso = Encoding.GetEncoding("iso8859-2"); 
     Encoding unicode = Encoding.UTF8; 
     byte[] unicodeBytes = unicode.GetBytes(src); 
     return iso.GetString(unicodeBytes); 
    } 
    protected static string RemoveDiacritics(string stIn) 
    { 
     string stFormD = stIn.Normalize(NormalizationForm.FormD); 
     StringBuilder sb = new StringBuilder(); 

     for (int ich = 0; ich < stFormD.Length; ich++) 
     { 
      UnicodeCategory uc = CharUnicodeInfo.GetUnicodeCategory(stFormD[ich]); 
      if (uc != UnicodeCategory.NonSpacingMark) 
      { 
       sb.Append(stFormD[ich]); 
      } 
     } 

     return (sb.ToString().Normalize(NormalizationForm.FormC)); 
    } 

} 

PDF格式的模板是緩存。在調試時記住這一點。

+0

@德拉戈什Durlut:我不是在創建PDF files.my PDF文件的任何領域都有唯一的文字,我想用PDF editor.Can我這樣做修改嗎? – Saravanan

+0

您應該可能會問這樣的問題: 「如何使用iTextSharp將可編輯PDF字段添加到我的PDF文檔?」。 –

+0

作爲一種替代方法,您可以嘗試使用Aspose.Pdf for .NET,因爲它提供了很好的添加和處理PDF文檔中現有表單域的功能。請查看http://www.aspose.com/docs/display/pdfnet/Working+with+Forms – codewarior

2

目前尚不清楚你在找什麼。

PDF不是用於編輯文本的格式。請閱讀「iText in Action」的Chapter 6的介紹。 http://www.manning.com/lowagie2/samplechapter6.pdf

但是,創建交互式PDF文件的方式。

瀏覽section 6.3.5,你將瞭解一個類型的互動形式:基於AcroForm技術形式。在section 6.3.5中,這種表單是使用OpenOffice創建的。

chapter 8,您將學習如何創建一個利用iText AcroForm形式。當然:在這種形式下,所有座標都是固定的。定義了一個矩形,並且不適合該矩形的內容可以縮小(如果font = 0)或剪裁。我想這就是你所描述的,但你不是很清楚。

另一種類型的形式是基於所述XML Forms Architecture。在這種情況下,PDF用作XML的容器。您可以使用Adobe LiveCycle Designer創建此類表單。我不知道有哪個「圖書館」能夠在自動化過程中創建此類表單。 iTextSharp可以用這種形式注入XML來填充它們;我們還有一個名爲XFA Worker的封閉源代碼產品,可以將XFA表單壓扁。

相關問題