Guest User

Untitled

a guest
Oct 24th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. public void RemoveDocumentJavascript(Stream inputStream, Stream outputStream)
  2. {
  3. PdfFixedDocument doc = new PdfFixedDocument(inputStream);
  4. // Remove document level JS code
  5. doc.JavaScriptBlocks.Clear();
  6.  
  7. RemoveDocumentActions(doc);
  8.  
  9. // Remove JavaScript from annotations.
  10. for (int i = 0; i < doc.Pages.Count; i++)
  11. {
  12. for (int j = 0; j < doc.Pages[i].Annotations.Count; j++)
  13. {
  14. RemoveAnnotationActions(doc.Pages[i].Annotations[j]);
  15. }
  16. }
  17.  
  18. // Remove Javascript from fields
  19. for (int i = 0; i < doc.Form.Fields.Count; i++)
  20. {
  21. RemoveFieldActions(doc.Form.Fields[i]);
  22. }
  23.  
  24. doc.Save(outputStream);
  25. }
  26.  
  27. private void RemoveDocumentActions(PdfFixedDocument doc)
  28. {
  29. if (doc.OpenAction is PdfJavaScriptAction)
  30. {
  31. doc.OpenAction = null;
  32. }
  33. if (doc.BeforeCloseAction is PdfJavaScriptAction)
  34. {
  35. doc.BeforeCloseAction = null;
  36. }
  37. if (doc.BeforeSaveAction is PdfJavaScriptAction)
  38. {
  39. doc.BeforeSaveAction = null;
  40. }
  41. if (doc.AfterSaveAction is PdfJavaScriptAction)
  42. {
  43. doc.AfterSaveAction = null;
  44. }
  45. if (doc.BeforeSaveAction is PdfJavaScriptAction)
  46. {
  47. doc.BeforeSaveAction = null;
  48. }
  49. if (doc.AfterSaveAction is PdfJavaScriptAction)
  50. {
  51. doc.AfterSaveAction = null;
  52. }
  53. if (doc.BeforePrintAction is PdfJavaScriptAction)
  54. {
  55. doc.BeforePrintAction = null;
  56. }
  57. if (doc.AfterPrintAction is PdfJavaScriptAction)
  58. {
  59. doc.AfterPrintAction = null;
  60. }
  61. }
  62.  
  63. private void RemoveAnnotationActions(PdfAnnotation annotation)
  64. {
  65. if (annotation.PageOpen is PdfJavaScriptAction)
  66. {
  67. annotation.PageOpen = null;
  68. }
  69. if (annotation.PageClose is PdfJavaScriptAction)
  70. {
  71. annotation.PageClose = null;
  72. }
  73. if (annotation.PageVisible is PdfJavaScriptAction)
  74. {
  75. annotation.PageVisible = null;
  76. }
  77. if (annotation.PageInvisible is PdfJavaScriptAction)
  78. {
  79. annotation.PageInvisible = null;
  80. }
  81. if (annotation.MouseDown is PdfJavaScriptAction)
  82. {
  83. annotation.MouseDown = null;
  84. }
  85. if (annotation.MouseUp is PdfJavaScriptAction)
  86. {
  87. annotation.MouseUp = null;
  88. }
  89. if (annotation.MouseEnter is PdfJavaScriptAction)
  90. {
  91. annotation.MouseEnter = null;
  92. }
  93. if (annotation.MouseLeave is PdfJavaScriptAction)
  94. {
  95. annotation.MouseLeave = null;
  96. }
  97. PdfLinkAnnotation link = annotation as PdfLinkAnnotation;
  98. if ((link != null) && (link.Action is PdfJavaScriptAction))
  99. {
  100. link.Action = null;
  101. }
  102. }
  103.  
  104. private void RemoveFieldActions(PdfField field)
  105. {
  106. field.CalculateAction = null;
  107. field.FormatAction = null;
  108. field.KeyPressAction = null;
  109. field.ValidateAction = null;
  110.  
  111. for (int i = 0; i < field.Widgets.Count; i++)
  112. {
  113. if (field.Widgets[i].Focus is PdfJavaScriptAction)
  114. {
  115. field.Widgets[i].Focus = null;
  116. }
  117. if (field.Widgets[i].Blur is PdfJavaScriptAction)
  118. {
  119. field.Widgets[i].Blur = null;
  120. }
  121. }
  122. }
Add Comment
Please, Sign In to add comment