如何从RTF转换为SFDT?

huangapple go评论95阅读模式
英文:

How to transform from RTF to SFDT?

问题

我正在使用Syncfusion Rich Editor,并且我的数据以rtf格式存储在数据库中。我需要将其转换为sfdt格式,以便在React组件中渲染它。如何使用Java从rtf转换为sfdt?

我尝试了以下这个方法,我用它来将rtf转换为其他格式,但是当我使用它时,我收到一个与已知格式类型不符的异常。

  1. public static Optional<String> converter(String rtf, FormatType original, FormatType target) throws Exception {
  2. byte[] bytes = rtf.getBytes(StandardCharsets.UTF_8);
  3. InputStream stream = new ByteArrayInputStream(bytes);
  4. WordDocument document = new WordDocument(stream, original);
  5. final String filepath = "output." + target.toString().toLowerCase();
  6. document.save(filepath, target);
  7. document.close();
  8. return getFileContent(filepath);
  9. }
英文:

I am using Syncfusion Rich Editor, and my data is stored in database in rtf format. And, I need to transform it to sfdt to render it in that React component. How do I transform from rtf to sfdt using java?

I tried with this method that i use to transform from rtf to other formats, but when I use it I get an exception according to known format type.

  1. public static Optional&lt;String&gt; converter(String rtf, FormatType original, FormatType target) throws Exception {
  2. byte[] bytes = rtf.getBytes(StandardCharsets.UTF_8);
  3. InputStream stream = new ByteArrayInputStream(bytes);
  4. WordDocument document = new WordDocument(stream, original);
  5. final String filepath = &quot;output.&quot;+target.toString().toLowerCase();
  6. document.save(filepath, target);
  7. document.close();
  8. return getFileContent(filepath);
  9. }

答案1

得分: 0

  1. import java.io.ByteArrayInputStream;
  2. import java.io.InputStream;
  3. import java.nio.charset.StandardCharsets;
  4. import com.syncfusion.docio.FormatType;
  5. import com.syncfusion.docio.WordDocument;
  6. import com.syncfusion.ej2.wordprocessor.WordProcessorHelper;
  7. public class Conversor {
  8. public static String sfdtToRtf(String sfdt) throws Exception {
  9. return WordProcessorHelper.save(sfdt, com.syncfusion.ej2.wordprocessor.FormatType.Rtf).toString();
  10. }
  11. public static String rtfToSfdt(String rtf) throws Exception {
  12. byte[] bytes = rtf.getBytes(StandardCharsets.UTF_8);
  13. InputStream stream = new ByteArrayInputStream(bytes);
  14. String sfdt = WordProcessorHelper.load(stream, com.syncfusion.ej2.wordprocessor.FormatType.Rtf);
  15. stream.close();
  16. return sfdt;
  17. }
  18. }
英文:
  1. import java.io.InputStream;
  2. import java.nio.charset.StandardCharsets;
  3. import com.syncfusion.docio.FormatType;
  4. import com.syncfusion.docio.WordDocument;
  5. import com.syncfusion.ej2.wordprocessor.WordProcessorHelper;
  6. public class Conversor {
  7. public static String sfdtToRtf(String sfdt) throws Exception {
  8. return WordProcessorHelper.save(sfdt, com.syncfusion.ej2.wordprocessor.FormatType.Rtf).toString();
  9. }
  10. public static String rtfToSfdt(String rtf) throws Exception {
  11. byte[] bytes = rtf.getBytes(StandardCharsets.UTF_8);
  12. InputStream stream = new ByteArrayInputStream(bytes);
  13. String sfdt = WordProcessorHelper.load(stream, com.syncfusion.ej2.wordprocessor.FormatType.Rtf);
  14. stream.close();
  15. return sfdt;
  16. }
  17. }
  18. </details>

huangapple
  • 本文由 发表于 2023年6月22日 03:57:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76526734.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定