我们使用iTextSharp来创建使用C#的PDF文件。但是,当我使用某种PDF编辑器编辑创建的PDF文件时,我无法完美地进行编辑。因为一些编辑过的文本是重叠的,有些未显示或隐藏。所以,我想选择其他方法使用iTextSharp创建可编辑的PDF文件。有什么办法可以通过iTextSharp来创建可编辑的PDF文件?
当使用iTextSharp构建PDF文件时,是否有任何参数(使PDF文档可编辑)添加以创建editable PDF files
?
请指引我走出这个问题的?
我们使用iTextSharp来创建使用C#的PDF文件。但是,当我使用某种PDF编辑器编辑创建的PDF文件时,我无法完美地进行编辑。因为一些编辑过的文本是重叠的,有些未显示或隐藏。所以,我想选择其他方法使用iTextSharp创建可编辑的PDF文件。有什么办法可以通过iTextSharp来创建可编辑的PDF文件?
当使用iTextSharp构建PDF文件时,是否有任何参数(使PDF文档可编辑)添加以创建editable PDF files
?
请指引我走出这个问题的?
首先你的问题不是很清楚。其次,我假设你正试图从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格式的模板是缓存。在调试时记住这一点。
目前尚不清楚你在找什么。
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表单压扁。
@德拉戈什Durlut:我不是在创建PDF files.my PDF文件的任何领域都有唯一的文字,我想用PDF editor.Can我这样做修改吗? – Saravanan
您应该可能会问这样的问题: “如何使用iTextSharp将可编辑PDF字段添加到我的PDF文档?”。 –
作为一种替代方法,您可以尝试使用Aspose.Pdf for .NET,因为它提供了很好的添加和处理PDF文档中现有表单域的功能。请查看http://www.aspose.com/docs/display/pdfnet/Working+with+Forms – codewarior