Javaを使用してMarkdownをXPSに変換する方法

このシンプルで簡単なトピックは、Javaを使用してMarkdownXPSに変換する方法に焦点を当てています。また、MarkdownファイルにMDファイルという名前を付けており、前提条件のソフトウェアやサードパーティツールに依存しない単純なAPI呼び出しを使用して、簡単にMDをJavaでXPSに変換**できます。コードの実装は、Windows、Linux、およびmacOSプラットフォームで実行されているJavaベースのアプリケーションで使用できます。

Javaを使用してMarkdownをXPSに変換する手順

  1. プロジェクトのMavenリポジトリからAspose.HTMLJARファイルを追加します
  2. HTMLDocument Classオブジェクトインスタンスを作成して、Markdownファイルをロードします
  3. MarkdownMDファイルを中間HTMLファイルとして変換します
  4. HTMLDocumentオブジェクトインスタンスを使用して中間HTMLファイルをロードします
  5. 中間HTMLファイルをXPS形式でディスクに保存します

MDファイルは、実際には、テキストフォーマット用のインラインテキストシンボル、フォント、ヘッダー、およびインデントで構成されるMarkdown言語を使用する通常のテキストファイルです。 * Javaを使用してMarkdownをXPSにエクスポートするために、最初のステップでMarkdownファイルをロードし、それをディスク上の中間HTMLファイルに変換します。次に、2番目のステップで、XPSオプションを設定し、Javaを使用して中間の*HTMLをXPSに保存します。この2ステップのプロセスは、Javaでの単純なAPI呼び出しを使用して実現されます。

JavaでマークダウンをXPSにエクスポートするコード

package htmlexamples;
import com.aspose.html.HTMLDocument;
import com.aspose.html.License;
import com.aspose.html.converters.Converter;
import com.aspose.html.drawing.Size;
import com.aspose.html.drawing.Unit;
import com.aspose.html.rendering.xps.XpsDevice;
import com.aspose.html.rendering.xps.XpsRenderingOptions;
public class MarkdownToXPS {
public static void main(
String[] argumentsMarkdowntoXPS) throws Exception{
// Applying the Aspsoe.HTML license to export MD to XPS
// without any restrictions
License lic = new License();
lic.setLicense("HTML.Total.Java.lic");
// In order to load the source markdown MD file, Create HTMLDocument object
HTMLDocument markdownToHTMLobj = Converter.
convertMarkdown("SourceMarkdownFile.md");
// First, convert the Markdown MD file to HTML format
markdownToHTMLobj.save("MarkdownSavedToHTML.html");
// Load the saved HTML file to a new HTMLDocument instance
HTMLDocument hTMLToXPSobj = new HTMLDocument("MarkdownSavedToHTML.html");
// Use XPSRenderingOptions to set the page size for output XPS
XpsRenderingOptions outputXPSOptions = new XpsRenderingOptions();
outputXPSOptions.getPageSetup().getAnyPage().
setSize(new Size(Unit.fromInches(8.5f), Unit.fromInches(11.0f)));
// Create XpsDevise object that will render intermediate HTML file to XPS format
XpsDevice xPSDevice = new
XpsDevice(outputXPSOptions, "HTMLToXPSExported.xps");
// Converting the HTML file using XPS Device to a XPS
hTMLToXPSobj.renderTo(xPSDevice);
}
}

前のトピックでは、Javaを使用してHTMLファイルを作成する方法について学習しました。このトピックでは、2段階のアプローチでJavaを使用したMarkdownからXPSへの変換*に焦点を当てています。

 日本語