英文:
How to transform from RTF to SFDT?
问题
我正在使用Syncfusion Rich Editor,并且我的数据以rtf格式存储在数据库中。我需要将其转换为sfdt格式,以便在React组件中渲染它。如何使用Java从rtf转换为sfdt?
我尝试了以下这个方法,我用它来将rtf转换为其他格式,但是当我使用它时,我收到一个与已知格式类型不符的异常。
public static Optional<String> converter(String rtf, FormatType original, FormatType target) throws Exception {
byte[] bytes = rtf.getBytes(StandardCharsets.UTF_8);
InputStream stream = new ByteArrayInputStream(bytes);
WordDocument document = new WordDocument(stream, original);
final String filepath = "output." + target.toString().toLowerCase();
document.save(filepath, target);
document.close();
return getFileContent(filepath);
}
英文:
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.
public static Optional<String> converter(String rtf, FormatType original, FormatType target) throws Exception {
byte[] bytes = rtf.getBytes(StandardCharsets.UTF_8);
InputStream stream = new ByteArrayInputStream(bytes);
WordDocument document = new WordDocument(stream, original);
final String filepath = "output."+target.toString().toLowerCase();
document.save(filepath, target);
document.close();
return getFileContent(filepath);
}
答案1
得分: 0
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import com.syncfusion.docio.FormatType;
import com.syncfusion.docio.WordDocument;
import com.syncfusion.ej2.wordprocessor.WordProcessorHelper;
public class Conversor {
public static String sfdtToRtf(String sfdt) throws Exception {
return WordProcessorHelper.save(sfdt, com.syncfusion.ej2.wordprocessor.FormatType.Rtf).toString();
}
public static String rtfToSfdt(String rtf) throws Exception {
byte[] bytes = rtf.getBytes(StandardCharsets.UTF_8);
InputStream stream = new ByteArrayInputStream(bytes);
String sfdt = WordProcessorHelper.load(stream, com.syncfusion.ej2.wordprocessor.FormatType.Rtf);
stream.close();
return sfdt;
}
}
英文:
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import com.syncfusion.docio.FormatType;
import com.syncfusion.docio.WordDocument;
import com.syncfusion.ej2.wordprocessor.WordProcessorHelper;
public class Conversor {
public static String sfdtToRtf(String sfdt) throws Exception {
return WordProcessorHelper.save(sfdt, com.syncfusion.ej2.wordprocessor.FormatType.Rtf).toString();
}
public static String rtfToSfdt(String rtf) throws Exception {
byte[] bytes = rtf.getBytes(StandardCharsets.UTF_8);
InputStream stream = new ByteArrayInputStream(bytes);
String sfdt = WordProcessorHelper.load(stream, com.syncfusion.ej2.wordprocessor.FormatType.Rtf);
stream.close();
return sfdt;
}
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论