Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 有没有办法使用iTextSharp创建可编辑的PDF文件?_C#_.net 4.0_Pdf Generation_Itextsharp - Fatal编程技术网

C# 有没有办法使用iTextSharp创建可编辑的PDF文件?

C# 有没有办法使用iTextSharp创建可编辑的PDF文件?,c#,.net-4.0,pdf-generation,itextsharp,C#,.net 4.0,Pdf Generation,Itextsharp,我们正在使用iTextSharp使用C#创建PDF文件。但是,当我使用某些PDF编辑器对创建的PDF文件进行编辑时,我无法完美地进行编辑。因为有些编辑的文本重叠,有些未显示或隐藏。因此,我想选择其他方法使用iTextSharp创建可编辑的PDF文件 使用iTextSharp构建PDF文件时,是否需要添加任何参数(使PDF文档可编辑),以便创建可编辑的PDF文件 请引导我离开这个问题?首先,你的问题不是很清楚。 其次,我假设您正试图从C代码创建PDF 可视化改进的一种方法是使用openoffice

我们正在使用iTextSharp使用C#创建PDF文件。但是,当我使用某些PDF编辑器对创建的PDF文件进行编辑时,我无法完美地进行编辑。因为有些编辑的文本重叠,有些未显示或隐藏。因此,我想选择其他方法使用iTextSharp创建可编辑的PDF文件

使用iTextSharp构建PDF文件时,是否需要添加任何参数(使PDF文档可编辑),以便创建可编辑的PDF文件


请引导我离开这个问题?

首先,你的问题不是很清楚。 其次,我假设您正试图从C代码创建PDF

可视化改进的一种方法是使用
openoffice
创建
PDF模板
。 然后,在使用模板后,在模板中创建的可编辑字段中写入。 以下是一些帮助您完成以下任务的代码:

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));
    }

}
公共类文档下载:PdfTemplateHandler
{
受保护的重写字符串模板路径
{
获取{return“~/App\u Data/PdfTemplates/MyDocument\u 2011\u v1.pdf”;}
}
受保护的覆盖void LoadDataInternal()
{
documentType=Request[“docType”]!=null?Request[“docType”]。ToString():“”;
如果(uid.Length<1)
{
响应。写入(“无效请求!”);
Response.End();
}
//加载数据
DownloadFileName=string.Format(“MyDocument_{0}{1}.pdf”,1234,DateTime.Now.ToBinary());
}
受保护的覆盖无效设置字段内部(iTextSharp.text.pdf.AcroFields AcroFields)
{
//iTextSharp.text.pdf.BaseFont unicode=iTextSharp.text.pdf.BaseFont.createFont(unicodeFontPath、BaseFont.IDENTITY、BaseFont.EMBEDDED);
//var unicodeFont=iTextSharp.text.pdf.BaseFont.CreateFont(iTextSharp.text.FontFactory.TIMES\u-ROMAN,iTextSharp.text.pdf.BaseFont.IDENTITY\u H,iTextSharp.text.pdf.BaseFont.EMBEDDED);
acroFields.SetField(“txtrNumber”,Number.ToString());
acroFields.SetField(“cbTaxi”,“Yes”);
}
}
公共抽象类PdfTemplateHandler:IHttpHandler
{
公共虚拟bool下载附件
{
得到
{
返回true;
}
}
受保护的虚拟时间跨度PdfTemplateCacheDuration
{
得到
{
返回时间跨度从分钟(30);
}
}
受保护的虚拟字符串PdfTemplateCacheKey
{
得到
{
返回string.Format(“\uuuPDF\uTemplate[{0}]”,TemplatePath);
}
}
受保护字符串下载文件名{get;set;}
受保护的HttpContext上下文{get;private set;}
受保护的HttpResponse响应{get;private set;}
受保护的HttpRequest请求{get;private set;}
#区域IHttpHandler成员
公共布尔可重用
{
获取{return false;}
}
公共void ProcessRequest(HttpContext上下文)
{
上下文=上下文;
Response=context.Response;
Request=context.Request;
尝试
{
LoadDataInternal();
}
捕获(线程异常)
{
//无操作
}
捕获(例外情况除外)
{
//Logger.LogError(ex);
回答。写(“错误!”);
Response.End();
}
Response.BufferOutput=true;
Response.ClearHeaders();
Response.ContentType=“application/pdf”;
if(下载附件)
{
AddHeader(“内容处置”、“附件;文件名=”+
(string.IsNullOrEmpty(DownloadFileName)?context.Session.SessionID+“.pdf”:DownloadFileName));
}
PdfStamper pst=null;
尝试
{
PdfReader=newpdfReader(GetTemplateBytes());
pst=新的PdfStamper(读卡器、响应、输出流);
var acroFields=pst.acroFields;
pst.formflatting=真;
pst.freetextflating=true;
pst.SetFullCompression();
SetFields内部(acroFields);
pst.Close();
}
最后
{
如果(pst!=null)
pst.Close();
}
}
#端区
#用于覆盖和提供功能的区域抽象成员
受保护的抽象字符串模板路径{get;}
受保护的抽象void LoadDataInternal();
受保护的抽象无效集合字段内部(AcroFields AcroFields);
#端区
受保护的虚拟字节[]GetTemplateBytes()
{
var data=Context.Cache[PdfTemplateCacheKey]作为字节[];
如果(数据==null)
{
data=File.ReadAllBytes(Context.Server.MapPath(TemplatePath));
Context.Cache.Insert(PdfTemplateCacheKey,data,
null,DateTime.Now.Add(PdfTemplateCacheDuration),Cache.NoSlidingExpiration);
}
返回数据;
}
受保护的静态字符串unicode_iso8859(字符串src)
{
编码iso=Encoding.GetEncoding(“iso8859-2”);
编码unicode=Encoding.UTF8;
字节[]unicodeBytes=unicode.GetBytes(src);
返回iso.GetString(unicodeBytes);
}
受保护的静态字符串RemoveDiacritics(字符串stIn)
{
字符串stFormD=stIn.Normalize(NormalizationForm.FormD);
StringBuilder sb=新的StringBuilder();
对于(int ich=0;ich

PDF模板是可缓存的。调试时请记住这一点。

首先,您的问题不是很清楚。 其次,我假设您正试图从C代码创建PDF

维苏之路