简体   繁体   English

将PDF与iTextSharp合并:javascript丢失

[英]Merging PDF's with iTextSharp: javascript lost

I have two forms pdf that I have to merge programmatically. 我有两种形式的pdf,我必须以编程方式进行合并。 These 2 documents have javascript embedded and buttons to switch texts between three different languages (french, german, italian). 这2个文档都嵌入了javascript,并带有用于在三种不同语言(法语,德语,意大利语)之间切换文本的按钮。 Plus they are plenty AcroForm formfields. 另外,它们还有很多AcroForm表单域。 There is a javascript function that detects the app language and displays the document in the general user interface language. 有一个javascript函数可检测应用程序语言并以常规用户界面语言显示文档。

When I merge using pdfSmartCopy, the result is a pdf document displaying the three languages simultaneously. 当我使用pdfSmartCopy合并时,结果是同时显示三种语言的pdf文档。 I've seen that there is no javascript anymore in the merged document. 我已经看到合并文档中不再有JavaScript。 I've tried to add Javascript manually : 我试图手动添加Javascript:

Document doc = new Document();
PdfCopy copy = new PdfSmartCopy(doc, new FileStream(destFileName, FileMode.Create));
copy.SetMergeFields();
doc.Open();
foreach (PdfReader r in listPdfSource)
{
    copy.AddDocument(r);
    PdfAction action = PdfAction.JavaScript(r.JavaScript, copy);
    copy.AddJavaScript(action);
}
doc.Close();

But the document still displays the three languages and the buttons don't work. 但是文档仍然显示三种语言,并且按钮不起作用。 I've tried to look for XFA part but there is none. 我试图寻找XFA的一部分,但没有。 Is there a way to keep the js functionnalities of the original documents and inject them in the merged result? 有没有办法保留原始文档的js功能并将其注入合并结果中?

EDIT 编辑

Thank you for the OCG tip. 感谢您的OCG提示。 I've tried the following : 我尝试了以下方法:

Dictionary<string, PdfLayer> layers = stamper.GetPdfLayers();
foreach (KeyValuePair<string, PdfLayer> layer in layers)
{
    Console.WriteLine(layer.Key);
}

And It gave me a very interesting list containing fre, ger, ita among other things. 它给了我一个非常有趣的列表,其中包含fre,ger,ita等。 For a single original document I tested the way to "disable" a layer by using : 对于单个原始文档,我使用以下方法测试了“禁用”图层的方法:

layer.On = false;
// or
layer.OnPanel = false;
// and
layer.View = false;

Only the third one gave a result. 只有第三个给出了结果。 Then I made the following: 然后我做了以下事情:

Document doc = new Document();
PdfCopy copy = new PdfSmartCopy(doc, new FileStream(tempFileName, FileMode.Create));
copy.SetMergeFields();
doc.Open();
foreach (PdfReader r in listPdfSource)
{
    copy.AddDocument(r);
    PdfAction action = PdfAction.JavaScript(r.JavaScript, copy);
    copy.AddJavaScript(action);
}
doc.Close();
//re-open the document and create a stamper for getting the layers
PdfReader reader = new PdfReader(tempFileName);
PdfStamper stamper = new PdfStamper(reader, new FileStream(destFileName, FileMode.Create));
stamper.GetPdfLayers()["ger"].View = false;
stamper.GetPdfLayers()["ita"].View = false;
stamper.Close();

But of course it doesn't work because the new document's layers list is empty! 但是当然不行,因为新文档的图层列表为空! It worked for a single original document. 它适用于单个原始文档。 Then I tried to make this manipulation for each original document separately and merge them after. 然后,我尝试对每个原始文档分别进行此操作,然后将它们合并。 But it's still displaying the three languages simultaneously. 但是它仍然同时显示三种语言。

EDIT 2 编辑2

I was asked by @Bruno Lowagie to share the original documents : @Bruno Lowagie要求我分享原始文件:

http://www.filedropper.com/source1 http://www.filedropper.com/source1

http://www.filedropper.com/source2 http://www.filedropper.com/source2

(I need a reputation of 10 to post more than 2 links? You can find the link for the merged document in the comments) (我需要10的声誉才能发布2个以上的链接?您可以在评论中找到合并文档的链接)

I really hope that a specialist can give me the way where to look further. 我真的希望专家可以给我进一步了解的方式。 As I understood it's a question about OCG and JS involved at document level. 据我了解,这是有关文档级别的OCG和JS的问题。 How to retrieve and apply them to the new merged document. 如何检索它们并将其应用于新的合并文档。

It might be worth checking out the following library: http://www.nrecosite.com/pdf_generator_net.aspx 可能值得检查以下库: http : //www.nrecosite.com/pdf_generator_net.aspx

Its free and it supports JS and HTML5, and you can bulk combine pdfs into one file. 它是免费的,并且支持JS和HTML5,您可以将pdf批量合并为一个文件。 I have used it in the past to hide certain elements with JS for printing purposes . 过去,我曾使用它来隐藏JS中的某些元素以进行打印。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM