Utilizzo delle opzioni di caricamento in Aspose.Words per Java

Introduzione all’utilizzo delle opzioni di caricamento in Aspose.Words per Java

In questo tutorial esploreremo come lavorare con le opzioni di caricamento in Aspose.Words per Java. Le opzioni di caricamento ti consentono di personalizzare il modo in cui i documenti vengono caricati ed elaborati. Tratteremo vari scenari, tra cui l’aggiornamento dei campi sporchi, il caricamento di documenti crittografati, la conversione di forme in Office Math, l’impostazione della versione di MS Word, la specifica di una cartella temporanea, la gestione degli avvisi e la conversione dei metafile in PNG. Immergiamoci passo dopo passo.

Aggiorna campi sporchi

LoadOptions loadOptions = new LoadOptions();
loadOptions.setUpdateDirtyFields(true);

Document doc = new Document("Your Directory Path" + "Dirty field.docx", loadOptions);
doc.save("Your Directory Path" + "WorkingWithLoadOptions.UpdateDirtyFields.docx");

Questo frammento di codice mostra come aggiornare i campi dirty in un documento. ILsetUpdateDirtyFields(true) viene utilizzato per garantire che i campi sporchi vengano aggiornati durante il caricamento del documento.

Carica documento crittografato

@Test
public void loadEncryptedDocument() throws Exception {
    Document doc = new Document("Your Directory Path" + "Encrypted.docx", new LoadOptions("docPassword"));
    doc.save("Your Directory Path" + "WorkingWithLoadOptions.LoadAndSaveEncryptedOdt.odt", new OdtSaveOptions("newPassword"));
}

Qui carichiamo un documento crittografato utilizzando una password. ILLoadOptions Il costruttore accetta la password del documento ed è anche possibile specificare una nuova password quando si salva il documento utilizzandoOdtSaveOptions.

Converti forma in matematica di Office

LoadOptions loadOptions = new LoadOptions();
loadOptions.setConvertShapeToOfficeMath(true);

Document doc = new Document("Your Directory Path" + "Office math.docx", loadOptions);
doc.save("Your Directory Path" + "WorkingWithLoadOptions.ConvertShapeToOfficeMath.docx", SaveFormat.DOCX);

Questo codice illustra come convertire le forme in oggetti Office Math durante il caricamento del documento. ILsetConvertShapeToOfficeMath(true)Il metodo consente questa conversione.

Imposta la versione di MS Word

@Test
public void setMsWordVersion() throws Exception {
    LoadOptions loadOptions = new LoadOptions();
    loadOptions.setMswVersion(MsWordVersion.WORD_2010);

    Document doc = new Document("Your Directory Path" + "Document.docx", loadOptions);
    doc.save("Your Directory Path" + "WorkingWithLoadOptions.SetMsWordVersion.docx");
}

È possibile specificare la versione di MS Word per il caricamento del documento. In questo esempio, impostiamo la versione su Microsoft Word 2010 utilizzandosetMswVersion.

Utilizza la cartella temporanea

@Test
public void useTempFolder() throws Exception {
    LoadOptions loadOptions = new LoadOptions();
    loadOptions.setTempFolder("Your Directory Path");

    Document doc = new Document("Your Directory Path" + "Document.docx", loadOptions);
}

Impostando la cartella temporanea utilizzandosetTempFolder, puoi controllare dove vengono archiviati i file temporanei durante l’elaborazione del documento.

Richiamata di avviso

@Test
public void warningCallback() throws Exception {
    LoadOptions loadOptions = new LoadOptions();
    loadOptions.setWarningCallback(new DocumentLoadingWarningCallback());

    Document doc = new Document("Your Directory Path" + "Document.docx", loadOptions);
}

public static class DocumentLoadingWarningCallback implements IWarningCallback {
    public void warning(WarningInfo info) {
        // Gestire gli avvisi che si presentano durante il caricamento del documento.
        System.out.println(MessageFormat.format("WARNING: {0}, source: {1}", info.getWarningType(), info.getSource()));
        System.out.println(MessageFormat.format("\tDescription: {0}", info.getDescription()));
    }
}

Questo codice illustra come impostare una richiamata di avviso per gestire gli avvisi durante il caricamento del documento. Puoi personalizzare il comportamento della tua applicazione quando si verificano gli avvisi.

Converti metafile in PNG

@Test
public void convertMetafilesToPng() throws Exception {
    LoadOptions loadOptions = new LoadOptions();
    loadOptions.setConvertMetafilesToPng(true);

    Document doc = new Document("Your Directory Path" + "WMF with image.docx", loadOptions);
}

Per convertire metafile (ad esempio WMF) in immagini PNG durante il caricamento del documento, è possibile utilizzare il filesetConvertMetafilesToPng(true) metodo.

Codice sorgente completo per lavorare con le opzioni di caricamento in Aspose.Words per Java

public void updateDirtyFields() throws Exception {
	LoadOptions loadOptions = new LoadOptions();
	{
		loadOptions.setUpdateDirtyFields(true);
	}
	Document doc = new Document("Your Directory Path" + "Dirty field.docx", loadOptions);
	doc.save("Your Directory Path" + "WorkingWithLoadOptions.UpdateDirtyFields.docx");
}
@Test
public void loadEncryptedDocument() throws Exception {
	Document doc = new Document("Your Directory Path" + "Encrypted.docx", new LoadOptions("docPassword"));
	doc.save("Your Directory Path" + "WorkingWithLoadOptions.LoadAndSaveEncryptedOdt.odt", new OdtSaveOptions("newPassword"));
}
@Test
public void convertShapeToOfficeMath() throws Exception {
	LoadOptions loadOptions = new LoadOptions();
	{
		loadOptions.setConvertShapeToOfficeMath(true);
	}
	Document doc = new Document("Your Directory Path" + "Office math.docx", loadOptions);
	doc.save("Your Directory Path" + "WorkingWithLoadOptions.ConvertShapeToOfficeMath.docx", SaveFormat.DOCX);
}
@Test
public void setMsWordVersion() throws Exception {
	// Crea un nuovo oggetto LoadOptions, che caricherà i documenti in base alle specifiche di MS Word 2019 per impostazione predefinita
	// e cambia la versione di caricamento in Microsoft Word 2010.
	LoadOptions loadOptions = new LoadOptions();
	{
		loadOptions.setMswVersion(MsWordVersion.WORD_2010);
	}
	Document doc = new Document("Your Directory Path" + "Document.docx", loadOptions);
	doc.save("Your Directory Path" + "WorkingWithLoadOptions.SetMsWordVersion.docx");
}
@Test
public void useTempFolder() throws Exception {
	LoadOptions loadOptions = new LoadOptions();
	{
		loadOptions.setTempFolder("Your Directory Path");
	}
	Document doc = new Document("Your Directory Path" + "Document.docx", loadOptions);
}
@Test
public void warningCallback() throws Exception {
	LoadOptions loadOptions = new LoadOptions();
	{
		loadOptions.setWarningCallback(new DocumentLoadingWarningCallback());
	}
	Document doc = new Document("Your Directory Path" + "Document.docx", loadOptions);
}
public static class DocumentLoadingWarningCallback implements IWarningCallback {
	public void warning(WarningInfo info) {
		//Stampa gli avvisi e i relativi dettagli man mano che si presentano durante il caricamento del documento.
		System.out.println(MessageFormat.format("WARNING: {0}, source: {1}", info.getWarningType(), info.getSource()));
		System.out.println(MessageFormat.format("\tDescription: {0}", info.getDescription()));
	}
}
@Test
public void convertMetafilesToPng() throws Exception {
	LoadOptions loadOptions = new LoadOptions();
	{
		loadOptions.setConvertMetafilesToPng(true);
	}
	Document doc = new Document("Your Directory Path" + "WMF with image.docx", loadOptions);
}
@Test
public void loadChm() throws Exception {
	LoadOptions loadOptions = new LoadOptions();
	{
		loadOptions.setEncoding(Charset.forName("windows-1251"));
	}
	Document doc = new Document("Your Directory Path" + "HTML help.chm", loadOptions);
}

Conclusione

In questo tutorial, abbiamo approfondito vari aspetti del lavoro con le opzioni di caricamento in Aspose.Words per Java. Le opzioni di caricamento svolgono un ruolo cruciale nella personalizzazione del modo in cui i documenti vengono caricati ed elaborati, consentendoti di adattare l’elaborazione dei documenti alle tue esigenze specifiche. Ricapitoliamo i punti chiave trattati in questa guida:

Domande frequenti

Come posso gestire gli avvisi durante il caricamento del documento?

È possibile impostare una richiamata di avviso come mostrato nel filewarningCallback() metodo sopra. Personalizza ilDocumentLoadingWarningCallback class per gestire gli avvisi in base ai requisiti dell’applicazione.

Posso convertire forme in oggetti Office Math durante il caricamento di un documento?

Sì, puoi convertire forme in oggetti Office Math utilizzandoloadOptions.setConvertShapeToOfficeMath(true).

Come posso specificare la versione di MS Word per il caricamento dei documenti?

UtilizzoloadOptions.setMswVersion(MsWordVersion.WORD_2010) per specificare la versione di MS Word per il caricamento del documento.

Qual è lo scopo delsetTempFolder method in Load Options?

ILsetTempFolderIl metodo consente di specificare la cartella in cui vengono archiviati i file temporanei durante l’elaborazione del documento.