Questo argomento semplice e breve si concentra su come convertire Markdown in XPS utilizzando Java. Nominiamo anche i file Markdown come file MD e puoi facilmente convertire MD in XPS in Java utilizzando semplici chiamate API che non dipendono da alcun software prerequisito o strumento di terze parti. L’implementazione del codice può essere utilizzata in applicazioni basate su Java in esecuzione su piattaforme Windows, Linux e macOS.
Passaggi per convertire Markdown in XPS utilizzando Java
- Aggiungi Aspose.HTML file JAR dal repository Maven nel tuo progetto
- Crea un’istanza dell’oggetto HTMLDocument Class per caricare il file Markdown
- Converti il file Markdown MD come file HTML intermedio
- Carica il file HTML intermedio utilizzando l’istanza dell’oggetto HTMLDocument
- Salva il file HTML intermedio in formato XPS su disco
Il file MD è in effetti un normale file di testo che utilizza il linguaggio Markdown composto da simboli di testo in linea per la formattazione del testo, caratteri, intestazioni e rientri. Per esportare Markdown in XPS usando Java, nel primo passaggio caricheremo il file Markdown e lo convertiremo in un file HTML intermedio su disco. Quindi, nel secondo passaggio, imposteremo le opzioni XPS e salveremo l’intermedio HTML in XPS usando Java. Questo processo in due fasi si ottiene utilizzando semplici chiamate API in Java.
Codice per esportare Markdown in XPS in Java
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); | |
} | |
} |
Nell’argomento precedente, abbiamo appreso come creare file HTML utilizzando Java. In questo argomento ci siamo concentrati sulla conversione Markdown in XPS utilizzando Java con un approccio in due fasi.