a .NET library that can read/write Office formats without Microsoft Office installed. No COM+, no interop.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
4.0 KiB

  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for Additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. namespace NPOI
  16. {
  17. using System;
  18. using System.Text;
  19. using NPOI.OpenXml4Net.OPC;
  20. using NPOI.OpenXml4Net.Util;
  21. public abstract class POIXMLTextExtractor : POITextExtractor
  22. {
  23. /** The POIXMLDocument that's open */
  24. private readonly POIXMLDocument _document;
  25. /**
  26. * Creates a new text extractor for the given document
  27. */
  28. public POIXMLTextExtractor(POIXMLDocument document)
  29. {
  30. _document = document;
  31. }
  32. /**
  33. * Returns the core document properties
  34. */
  35. public CoreProperties GetCoreProperties()
  36. {
  37. return _document.GetProperties().CoreProperties;
  38. }
  39. /**
  40. * Returns the extended document properties
  41. */
  42. public ExtendedProperties GetExtendedProperties()
  43. {
  44. return _document.GetProperties().ExtendedProperties;
  45. }
  46. /**
  47. * Returns the custom document properties
  48. */
  49. public CustomProperties GetCustomProperties()
  50. {
  51. return _document.GetProperties().CustomProperties;
  52. }
  53. /**
  54. * Returns opened document
  55. */
  56. public POIXMLDocument Document
  57. {
  58. get{
  59. return _document;
  60. }
  61. }
  62. /**
  63. * Returns the opened OPCPackage that Contains the document
  64. */
  65. public OPCPackage Package
  66. {
  67. get
  68. {
  69. return _document.Package;
  70. }
  71. }
  72. /**
  73. * Returns an OOXML properties text extractor for the
  74. * document properties metadata, such as title and author.
  75. */
  76. public override POITextExtractor MetadataTextExtractor
  77. {
  78. get
  79. {
  80. return new POIXMLPropertiesTextExtractor(_document);
  81. }
  82. }
  83. public override void Close()
  84. {
  85. // e.g. XSSFEventBaseExcelExtractor passes a null-document
  86. if (_document != null)
  87. {
  88. OPCPackage pkg = _document.Package;
  89. if (pkg != null)
  90. {
  91. pkg.Revert();
  92. }
  93. }
  94. base.Close();
  95. }
  96. protected void CheckMaxTextSize(StringBuilder text, String string1)
  97. {
  98. if(string1 == null)
  99. {
  100. return;
  101. }
  102. int size = text.Length + string1.Length;
  103. if(size > ZipSecureFile.GetMaxTextSize())
  104. {
  105. throw new InvalidOperationException("The text would exceed the max allowed overall size of extracted text. "
  106. + "By default this is prevented as some documents may exhaust available memory and it may indicate that the file is used to inflate memory usage and thus could pose a security risk. "
  107. + "You can adjust this limit via ZipSecureFile.setMaxTextSize() if you need to work with files which have a lot of text. "
  108. + "Size: " + size + ", limit: MAX_TEXT_SIZE: " + ZipSecureFile.GetMaxTextSize());
  109. }
  110. }
  111. }
  112. }