如何从RTF转换为SFDT?

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

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&lt;String&gt; 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 = &quot;output.&quot;+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>



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:

确定