簡體   English   中英

將PDF與iTextSharp合並:javascript丟失

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

我有兩種形式的pdf,我必須以編程方式進行合並。 這2個文檔都嵌入了javascript,並帶有用於在三種不同語言(法語,德語,意大利語)之間切換文本的按鈕。 另外,它們還有很多AcroForm表單域。 有一個javascript函數可檢測應用程序語言並以常規用戶界面語言顯示文檔。

當我使用pdfSmartCopy合並時,結果是同時顯示三種語言的pdf文檔。 我已經看到合並文檔中不再有JavaScript。 我試圖手動添加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();

但是文檔仍然顯示三種語言,並且按鈕不起作用。 我試圖尋找XFA的一部分,但沒有。 有沒有辦法保留原始文檔的js功能並將其注入合並結果中?

編輯

感謝您的OCG提示。 我嘗試了以下方法:

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

它給了我一個非常有趣的列表,其中包含fre,ger,ita等。 對於單個原始文檔,我使用以下方法測試了“禁用”圖層的方法:

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

只有第三個給出了結果。 然后我做了以下事情:

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

但是當然不行,因為新文檔的圖層列表為空! 它適用於單個原始文檔。 然后,我嘗試對每個原始文檔分別進行此操作,然后將它們合並。 但是它仍然同時顯示三種語言。

編輯2

@Bruno Lowagie要求我分享原始文件:

http://www.filedropper.com/source1

http://www.filedropper.com/source2

(我需要10的聲譽才能發布2個以上的鏈接?您可以在評論中找到合並文檔的鏈接)

我真的希望專家可以給我進一步了解的方式。 據我了解,這是有關文檔級別的OCG和JS的問題。 如何檢索它們並將其應用於新的合並文檔。

可能值得檢查以下庫: http : //www.nrecosite.com/pdf_generator_net.aspx

它是免費的,並且支持JS和HTML5,您可以將pdf批量合並為一個文件。 過去,我曾使用它來隱藏JS中的某些元素以進行打印。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM