Dies ist der Code in meiner Hauptfunktion.
Code: Select all
import jakarta.xml.bind.JAXBException;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.wml.CTTrackChange;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
public class App {
public static void main(String[] args) throws IOException, Docx4JException {
try {
// Replace "path/to/your/document.docx" with the actual file path
String inputFilePath = "src/main/java/shapes.docx";
String outputFilePath = "output_no_user_info.docx";
// Load the document
WordprocessingMLPackage wmlPackage = WordprocessingMLPackage.load(new File(inputFilePath));
// Remove user information
removeUserInfo(wmlPackage);
// Save the modified document
FileOutputStream fos = new FileOutputStream(outputFilePath);
wmlPackage.save(fos);
fos.close();
System.out.println("User information removed successfully. Saved as: " + outputFilePath);
} catch (Docx4JException | JAXBException e) {
e.printStackTrace();
System.err.println("Error processing the document: " + e.getMessage());
}
}
public static void removeUserInfo(WordprocessingMLPackage wmlPackage) throws Docx4JException, JAXBException {
MainDocumentPart documentPart = wmlPackage.getMainDocumentPart();
List trackedChanges = documentPart.getJAXBNodesViaXPath("//w:ins | //w:del", true);
for (Object change : trackedChanges) {
if (change instanceof CTTrackChange) {
CTTrackChange trackChange = (CTTrackChange) change;
trackChange.setAuthor("External");
}
}
}
}
Code: Select all
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4.0.0
org.example
Docx4jTest
1.0-SNAPSHOT
Archetype - Docx4jTest
http://maven.apache.org
org.docx4j
docx4j-core
11.4.9
org.docx4j
docx4j-JAXB-MOXy
11.4.5
org.eclipse.persistence
org.eclipse.persistence.moxy
4.0.2
org.apache.maven.plugins
maven-compiler-plugin
3.11.0
11
11
Code: Select all
Root xr = (Root) originalObject;
Ich bin ziemlich neu in der Bibliothek Und lassen Sie mich bitte wissen, ob es eine Möglichkeit gibt, das Problem zu umgehen.
Ich habe versucht, ReferenceImpl anstelle von Moxy zu verwenden. ReferenceImpl verursacht das folgende Problem nicht.
Fehlermeldung :-
class org.docx4j.com.microsoft.schemas.office.word.x2010.wordprocessingShape.CTWordprocessingShape kann nicht in eine Klasse umgewandelt werden org.eclipse.persistence.internal.oxm.Root (org.docx4j.com.microsoft.schemas.office.word.x2010.wordprocessingShape.CTWordprocessingShape und org.eclipse.persistence.internal.oxm.Root befinden sich im unbenannten Modul des Loaders ' app')
Code: Select all
Exception in thread "main" java.lang.ClassCastException: class org.docx4j.com.microsoft.schemas.office.word.x2010.wordprocessingShape.CTWordprocessingShape cannot be cast to class org.eclipse.persistence.internal.oxm.Root (org.docx4j.com.microsoft.schemas.office.word.x2010.wordprocessingShape.CTWordprocessingShape and org.eclipse.persistence.internal.oxm.Root are in unnamed module of loader 'app')
at org.eclipse.persistence.internal.oxm.record.AbstractMarshalRecordImpl.addXsiTypeAndClassIndicatorIfRequired(AbstractMarshalRecordImpl.java:211)
at org.eclipse.persistence.oxm.record.XMLRecord.addXsiTypeAndClassIndicatorIfRequired(XMLRecord.java:406)
at org.eclipse.persistence.internal.oxm.XMLObjectBuilder.buildIntoNestedRow(XMLObjectBuilder.java:462)
at org.eclipse.persistence.oxm.mappings.XMLAnyCollectionMapping.buildCompositeRow(XMLAnyCollectionMapping.java:683)
at org.eclipse.persistence.oxm.mappings.XMLAnyCollectionMapping.writeFromObjectIntoRow(XMLAnyCollectionMapping.java:554)
at org.eclipse.persistence.internal.oxm.XMLObjectBuilder.writeOutMappings(XMLObjectBuilder.java:349)
at org.eclipse.persistence.internal.oxm.XMLObjectBuilder.buildIntoNestedRow(XMLObjectBuilder.java:465)
at org.eclipse.persistence.internal.oxm.XMLObjectBuilder.buildIntoNestedRow(XMLObjectBuilder.java:445)
at org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping.buildCompositeRowForDescriptor(XMLCompositeObjectMapping.java:466)
at org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping.buildCompositeRow(XMLCompositeObjectMapping.java:447)
at org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping.writeSingleValue(XMLCompositeObjectMapping.java:680)
at org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping.writeFromObjectIntoRow(XMLCompositeObjectMapping.java:645)
at org.eclipse.persistence.internal.oxm.XMLObjectBuilder.writeOutMappings(XMLObjectBuilder.java:349)
at org.eclipse.persistence.internal.oxm.XMLObjectBuilder.buildIntoNestedRow(XMLObjectBuilder.java:465)
at org.eclipse.persistence.internal.oxm.XMLObjectBuilder.buildIntoNestedRow(XMLObjectBuilder.java:445)
at org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping.buildCompositeRowForDescriptor(XMLCompositeObjectMapping.java:466)