填充PDF文件使用iTextSharp的经过,形式不删除可保存/使用权 [英] After filling PDF with iTextSharp, form is not savable/ usage rights removed

查看:336
本文介绍了填充PDF文件使用iTextSharp的经过,形式不删除可保存/使用权的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前工作在将使用iTextSharp的,以部分填补PDF表单的过程。这种形式,然后通过电子邮件发送给用户,并且该用户将完成填写表格。然后提交表单通过电子邮件回到我们的电子邮件帐户,然后再进行处理。

I am currently working on a process that will use iTextSharp to partially fill a PDF form. This form is then emailed to a user and the user will finish completing the form. Then the form is submitted by email back to our email account and then processed.

我的问题是,一旦PDF被发送到一个用户,当用户打开的形式和数据无法保存。被用作模板原始的PDF可填写一个保存。不知何故,使用iTextSharp的过程中,该形式是失去其使用权。有没有办法保留使用权在使用iTextSharp的?

My problem is that once the PDF is sent out to a user, when the user opens the form and the data can not be saved. The original PDF that is used as a template can be filled out an saved. Somehow, during the process of using iTextSharp, the form is losing its usage rights. Is there a way to retain the usage rights while using iTextSharp?

我希望这里有人能指出什么错误,或点我在正确的方向。感谢您的时间和帮助。

I am hoping that someone here can point out what is going wrong, or point me in the right direction. Thanks for your time and help.

下面是code:

using Dapper;
using iTextSharp.text.pdf;
using NLog;
using PilotDispatch.Domain.Model;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data.OleDb;
using System.IO;
using System.Linq;

namespace eTicketPdfFactory
{
    class Program
    {
        const string TemplateItextPdfPath = @"C:\PDF\Factory\eTicketFormPortrait.pdf";
        const string OutputItextPdfPath = @"C:\PDF\Factory\eTicketFormPortraitOut.pdf";


    private static readonly Logger Logger = LogManager.GetCurrentClassLogger();

        static void Main(string[] args)
        {
            var logid = string.Empty;
            if (args.Length > 0)
            {
                logid = args[0];
            }
            else
            {
                Logger.Error("No LogId supplied");
                return;
            }

            var rundownQuery =
                string.Format(
                    "SELECT * FROM Rundown_Table LEFT JOIN Vessels ON Vessels.CallSign = Rundown_Table.Call_Sign WHERE Rundown_Table.Log_ID = '{0}'",
                    logid);


            ETicketPdf ticket;
            IEnumerable<PilotTransportation> pilotTransportations = null;

            using (var cn = new OleDbConnection(ConfigurationManager.ConnectionStrings["PervasiveConnection"].ConnectionString))
            {
                cn.Open();
               // ticket = cn.Query<ETicketPdf>(PervasiveQueryString, new { Log_ID = logid }).FirstOrDefault(); // not working with Pervasive
                ticket = cn.Query<ETicketPdf>(rundownQuery).FirstOrDefault();

                if (ticket != null)
                {
                    var transportationQuery =
                        string.Format(
                            "SELECT * FROM PilotTransportation WHERE PilotCode = '{0}'",
                            ticket.Pilot_Code);

                    pilotTransportations = cn.Query<PilotTransportation>(transportationQuery);
                }

                cn.Close();
            }

            if (ticket == null)
            {
                Logger.Error("No records found for given LogId");
                return;
            }

           var pilotOptions = pilotTransportations.Select(opt => string.Format("{0} - {1}", opt.VendorID, opt.Name)).ToArray();


            var reader = new PdfReader(TemplateItextPdfPath);
            var stamper = new PdfStamper(reader, new FileStream(OutputItextPdfPath, FileMode.Open));
            //var stamper = new PdfStamper(reader, new FileStream(OutputItextPdfPath, FileMode.CreateNew, FileAccess.Write), '\0', true);
            var formFields = stamper.AcroFields;

            formFields.SetListOption("Form[0].Page1[0].VendorIdFrom1[0]", null, pilotOptions);
            formFields.SetListOption("Form[0].Page1[0].VendorIdTo1[0]", null, pilotOptions);
            formFields.SetListOption("Form[0].Page1[0].VendorIdFrom2[0]", null, pilotOptions);
            formFields.SetListOption("Form[0].Page1[0].VendorIdTo2[0]", null, pilotOptions);

            var properties = ticket.GetType().GetProperties();

            foreach (var prop in properties)
            {
                var name = prop.Name;
                var propval = prop.GetValue(ticket, null);

                if (propval != null)
                {
                    if (name == "Order_Date")
                    {
                        if(Convert.ToDateTime(propval).Year < 1902)continue;
                    }

                    formFields.SetField(name, propval.ToString());
                }

            }

            reader.RemoveUsageRights();
            stamper.Close();
            reader.Close();

            File.Copy(OutputItextPdfPath, Path.GetDirectoryName(OutputItextPdfPath) + "/" + logid + ".pdf");

            Console.WriteLine("finished");
            Console.ReadLine();
        }
    }
}

再次感谢您的任何指针或帮助,你可以给我。

Once again, thanks in advance for any pointers or help that you can offer me.

推荐答案

答案被注释掉在你自己的源$ C ​​$ C。

The answer is commented out in your own source code.

您需要:

var stamper = new PdfStamper(reader, new FileStream(...), '\0', true);

来代替:

var stamper = new PdfStamper(reader, new FileStream(...));

您的问题是<一个副本href="http://stackoverflow.com/questions/21945972/how-to-correctly-fill-in-xfa-form-data-using-itextsharp-to-allow-editing-and-sav">How在XFA表单数据使用iTextSharp的,允许在Acrobat XI 编辑和保存结果正确填写 你可以在这里找到更详尽的答案

Your question is a duplicate of How to correctly fill in XFA form data using iTextSharp to allow editing and saving result in Acrobat XI and you can find a more elaborate answer here.

另外:你抱怨的使用权被删除,但如果是这样的话,为什么我看到这条线在code:

Also: you complain that the usage rights are removed, but if that is so, why do I see this line in your code:

reader.RemoveUsageRights();

这行删除的使用权和therfore形式可以不再在本地使用Adobe Reader的时候保存。

That line removes the usage rights and therfore the form can no longer be saved locally when using Adobe Reader.

这篇关于填充PDF文件使用iTextSharp的经过,形式不删除可保存/使用权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆