我正在使用此脚本循环浏览用户文件并填写 details.pdf 模板的副本并将它们合并为一个文件。
我遇到的问题是 tfTotalPrice 字段在我手动打开文档 javascript 并更改某些内容(例如添加警报)之前不会计算。我认为这可能与需要“/CO”字典键有关,但我不明白为什么。我刚刚在 ISO32000-1 文档中读到了这一点。
static void Main(string[] args)
{
int i = 0;
int partsTotal = args.Count();
//template page
string tempDetails = @"C:\temp\Details.pdf";
//final multi page quote
string finishedQuote = @"C:\temp\Quote.pdf";
DateTime processedDate = DateTime.Now;
string dateProc = processedDate.ToString("MM/dd/yyyy");
Console.WriteLine("Please Enter Your Customer Name and Press Enter");
string customer = Console.ReadLine();
//This will hold the memory streams and be used to merge them all
List<byte[]> pagesToMerge = new List<byte[]>();
Document doc = new Document();
PdfSmartCopy copy = new PdfSmartCopy(doc, new FileStream(finishedQuote, FileMode.Create));
doc.Open();
copy.SetMergeFields();
copy.SetPdfVersion(PdfWriter.PDF_VERSION_1_7);
List<PdfReader> readers = new List<PdfReader>();
for (i = 0; i < partsTotal; i++)
{
string partFile = args[i].ToString();
string partFileName = partFile.Substring(partFile.LastIndexOf(@"\") + 1);
string partFileLocation = partFile.Substring(0, partFile.LastIndexOf(@"\"));
Console.WriteLine("Processing : " + partFileName);
PdfReader reader = new PdfReader(RenameFields(tempDetails, i));
readers.Add(reader);
copy.AddDocument(reader);
}
doc.Close();
foreach (PdfReader reader in readers)
{
reader.Close();
}
//Should we open it up?
bool response = GetYorN();
if (response == true)
{
System.Diagnostics.Process.Start(finishedQuote);
}
}
static byte[] RenameFields(string src, int i)
{
//This method creates a new document in memory with newly named unique fields
MemoryStream baos = new MemoryStream();
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, baos);
stamper.Writer.SetPdfVersion(PdfWriter.PDF_VERSION_1_7);
stamper.AcroFields.GenerateAppearances = true;
stamper.FormFlattening = false;
AcroFields form = stamper.AcroFields;
//populate names with the dictionary keys for the current file(stream)
string[] names = new string[stamper.AcroFields.Fields.Keys.Count];
stamper.AcroFields.Fields.Keys.CopyTo(names, 0);
//rename all fields in this file(stream)
foreach (string name in names)
{
stamper.AcroFields.RenameField(name, name + i);
}
//Add the calculation action to totalPrice(i)
AcroFields.Item newTotalField = form.GetFieldItem("tfTotalPrice" + i);
PdfDictionary newTotalRefDict = (PdfDictionary)PdfReader.GetPdfObject(newTotalField.GetWidgetRef(0));
PdfString newTotalP = newTotalRefDict.GetAsString(PdfName.T);
AcroFields.Item newSetupField = stamper.AcroFields.GetFieldItem("tfSetupPrice" + i);
PdfDictionary newSetupRefDict = (PdfDictionary)PdfReader.GetPdfObject(newSetupField.GetWidgetRef(0));
PdfString newSetupP = newSetupRefDict.GetAsString(PdfName.T);
AcroFields.Item newToolField = stamper.AcroFields.GetFieldItem("tfToolPrice" + i);
PdfDictionary newToolRefDict = (PdfDictionary)PdfReader.GetPdfObject(newToolField.GetWidgetRef(0));
PdfString newToolP = newToolRefDict.GetAsString(PdfName.T);
AcroFields.Item newPieceField = stamper.AcroFields.GetFieldItem("tfPiecePrice" + i);
PdfDictionary newPieceRefDict = (PdfDictionary)PdfReader.GetPdfObject(newPieceField.GetWidgetRef(0));
PdfString newPieceP = newPieceRefDict.GetAsString(PdfName.T);
AcroFields.Item newFixtureField = stamper.AcroFields.GetFieldItem("tfFixturePrice" + i);
PdfDictionary newFixtureRefDict = (PdfDictionary)PdfReader.GetPdfObject(newFixtureField.GetWidgetRef(0));
PdfString newFixtureP = newFixtureRefDict.GetAsString(PdfName.T);
//Declare the sum action
string newJS = "this.getField(\"" + newTotalP + "\").value = (this.getField(\"" + newPieceP + "\").value + this.getField(\"" + newToolP + "\").value + this.getField(\"" + newFixtureP + "\").value + this.getField(\"" + newSetupP + "\").value);";
PdfAction pdfAction = PdfAction.JavaScript(newJS, stamper.Writer);
PdfDictionary widgetRefDict = (PdfDictionary)PdfReader.GetPdfObject(newTotalField.GetWidgetRef(0));
//AA = Additional Action Annotation Dictionary
PdfDictionary actionDict = widgetRefDict.GetAsDict(PdfName.AA);
//C = Calculation that lives inside of the AA dictionary
PdfDictionary calcDict = actionDict.GetAsDict(new PdfName("C"));
//Plug in the js calc with the new form field names
calcDict.Put(new PdfName("JS"), new PdfString(newJS));
//stamper.SetPageAction(PdfWriter.PAGE_OPEN, PdfAction.JavaScript(newJS, stamper.Writer), 1);
//close up shop and return the new doc in memory
stamper.Close();
reader.Close();
return baos.ToArray();
}
任何帮助将不胜感激,我一直坚持这一点:(-谢谢
这是PDF的链接