Aspose.Words for Java에서 로드 옵션 사용

Aspose.Words for Java의 로드 옵션 작업 소개

이 튜토리얼에서는 Aspose.Words for Java에서 로드 옵션을 사용하는 방법을 살펴보겠습니다. 로드 옵션을 사용하면 문서 로드 및 처리 방법을 사용자 정의할 수 있습니다. 더티 필드 업데이트, 암호화된 문서 로드, 도형을 Office Math로 변환, MS Word 버전 설정, 임시 폴더 지정, 경고 처리, 메타파일을 PNG로 변환 등 다양한 시나리오를 다룹니다. 단계별로 살펴보겠습니다.

더티 필드 업데이트

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");

이 코드 조각은 문서의 더티 필드를 업데이트하는 방법을 보여줍니다. 그만큼setUpdateDirtyFields(true) 메서드는 문서를 로드하는 동안 더티 필드가 업데이트되도록 하는 데 사용됩니다.

암호화된 문서 로드

@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"));
}

여기서는 비밀번호를 사용하여 암호화된 문서를 로드합니다. 그만큼LoadOptions 생성자는 문서 비밀번호를 허용하며 다음을 사용하여 문서를 저장할 때 새 비밀번호를 지정할 수도 있습니다.OdtSaveOptions.

도형을 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);

이 코드는 문서를 로드하는 동안 도형을 Office Math 개체로 변환하는 방법을 보여줍니다. 그만큼setConvertShapeToOfficeMath(true)메서드를 사용하면 이 변환이 가능해집니다.

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");
}

문서 로딩을 위해 MS Word 버전을 지정할 수 있습니다. 이 예에서는 다음을 사용하여 버전을 Microsoft Word 2010으로 설정했습니다.setMswVersion.

임시 폴더 사용

@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);
}

다음을 사용하여 임시 폴더를 설정하여setTempFolder를 사용하면 문서 처리 중에 임시 파일이 저장되는 위치를 제어할 수 있습니다.

경고 콜백

@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) {
        // 문서를 로드하는 동안 발생하는 경고를 처리합니다.
        System.out.println(MessageFormat.format("WARNING: {0}, source: {1}", info.getWarningType(), info.getSource()));
        System.out.println(MessageFormat.format("\tDescription: {0}", info.getDescription()));
    }
}

이 코드는 문서를 로드하는 동안 경고를 처리하기 위해 경고 콜백을 설정하는 방법을 보여줍니다. 경고가 발생할 때 애플리케이션의 동작을 사용자 정의할 수 있습니다.

메타파일을 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);
}

문서를 로드하는 동안 메타파일(예: WMF)을 PNG 이미지로 변환하려면 다음을 사용할 수 있습니다.setConvertMetafilesToPng(true) 방법.

Aspose.Words for 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 {
	// 기본적으로 MS Word 2019 사양에 따라 문서를 로드하는 새 LoadOptions 개체를 만듭니다.
	// 로딩 버전을 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) {
		//문서를 로드하는 동안 발생하는 경고와 세부 정보를 인쇄합니다.
		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);
}

결론

이 튜토리얼에서는 Aspose.Words for Java에서 로드 옵션 작업의 다양한 측면을 살펴보았습니다. 로드 옵션은 문서 로드 및 처리 방법을 사용자 정의하는 데 중요한 역할을 하여 특정 요구 사항에 맞게 문서 처리를 맞춤화할 수 있습니다. 이 가이드에서 다루는 주요 사항을 요약해 보겠습니다.

FAQ

문서를 로드하는 동안 경고를 어떻게 처리합니까?

다음과 같이 경고 콜백을 설정할 수 있습니다.warningCallback() 위의 방법. 사용자 정의DocumentLoadingWarningCallback 애플리케이션의 요구 사항에 따라 경고를 처리하는 클래스입니다.

문서를 로드할 때 도형을 Office Math 개체로 변환할 수 있나요?

예, 다음을 사용하여 도형을 Office Math 개체로 변환할 수 있습니다.loadOptions.setConvertShapeToOfficeMath(true).

문서 로딩을 위해 MS Word 버전을 어떻게 지정합니까?

사용loadOptions.setMswVersion(MsWordVersion.WORD_2010) 문서 로딩을 위한 MS Word 버전을 지정합니다.

의 목적은 무엇입니까?setTempFolder method in Load Options?

그만큼setTempFolder방법을 사용하면 문서 처리 중에 임시 파일이 저장되는 폴더를 지정할 수 있습니다.