问题
EDIT[FIX] - see at bottom.
MS office genrates this element in all documents with some underlying knowledge that the NS constraints must be enforced to a degree...
MS document example:
<w:document
xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
xmlns:w10="urn:schemas-microsoft-com:office:word"
xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml"
xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml"
xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml"
xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing"
xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas"
xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup"
xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk"
xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape"
mc:Ignorable="w14 w15 wp14">
<!--whatever-->
</w:document>
POI output example
<w:document
mc:Ignorable="w14 w15 wp14"
xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
xmlns:w10="urn:schemas-microsoft-com:office:word"
xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml"
xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml"
xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml"
xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing"
xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas"
xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup"
xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk"
xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape">
<!--whatever-->
</w:document>
Notice the MC:IGNORABLE element is shifted ... Manually modifying this element inside the docx archive itself "fixes" an error thrown off by the MSOffice program which specifies an error in line 2, column 0.
So far i've tried:
xwpfDocument.getDocument().getDomNode()
^ have tried modifying, deleting and adding attributes in correct order... does not work as the shift seems to happen somewhere in the XWPFDocument.write() procedure.
I've also tried extracting the document.xml file from the docx archive, modifying it and injecting it back into the archive ... and this doesn't seem to work... CRC issues...
Is there a way to access the OPCPackage or part or anything to allow direct invocation of a built in "recalculateAttributesRelativeToNS" in this element?
... or as an alternative.. how do i automate dom changes in the document xml file inside the archive and still ensure ms compatibility?
[EDIT: "XY"]
This behavior is evident in the following scenario:
fis = new FileInputStream(new File(path));
XWPFPicture picture = imageRun.addPicture(fis, XWPFDocument.PICTURE_TYPE_PNG, path, Units.toEMU(450), Units.toEMU(290));
Document opens fine. issue not present.
The addition of this line (i know it's not technically correct, however the error should not be reported at line 2- column 0, as it should be reported further down the xml) screws everything up.
picture.getCTPicture().getSpPr().addNewEffectLst().addNewOuterShdw().setBlurRad(10000);
With the addition of this line, the w:document attributes change into state 2 (the poi example) hereby triggering ms Office's MS schema NS invalidation routine.
The attributes are correct. The order is wrong. Manually moving the mc:ignorable attribute to the end of the attribute list solves the issue and the document loads without issues.
This is most likely due to the fact that (in the correct implementation) the xmlns:mc attribute is preceeding the mc:ignorable attribute in the attribute list whereas in the poi output it does not (mc:Ignorable is the first element in the list).
hence... the question.
additionally... the import list:
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.poi.POIXMLProperties;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFPicture;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.xmlbeans.XmlException;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDocument1;
everything in this class is self-contained.
EDIT [FIX]
The fix consists of adding the second line in this snippet.
picture.getCTPicture().getSpPr().addNewEffectLst().addNewInnerShdw().setBlurRad(Units.toEMU(3));
picture.getCTPicture().getSpPr().getEffectLst().getInnerShdw().addNewPrstClr().setVal(org.openxmlformats.schemas.drawingml.x2006.main.STPresetColorVal.BLACK);
Thanks again, @AxelRichter.
回答1:
The pointing on line 2 column 0 in the error is misleading. If you look at /word/document.xml
then there are only 2 lines at all. The XML declaration in line 1 and all the other XML in line 2.
Your problem is that the schema definition of CT_OuterShadowEffect
is:
<xsd:complexType name="CT_OuterShadowEffect">
<xsd:sequence>
<xsd:group ref="EG_ColorChoice" minOccurs="1" maxOccurs="1"/>
</xsd:sequence>
<xsd:attribute name="blurRad" type="ST_PositiveCoordinate" use="optional" default="0"/>
<xsd:attribute name="dist" type="ST_PositiveCoordinate" use="optional" default="0"/>
<xsd:attribute name="dir" type="ST_PositiveFixedAngle" use="optional" default="0"/>
<xsd:attribute name="sx" type="ST_Percentage" use="optional" default="100%"/>
<xsd:attribute name="sy" type="ST_Percentage" use="optional" default="100%"/>
<xsd:attribute name="kx" type="ST_FixedAngle" use="optional" default="0"/>
<xsd:attribute name="ky" type="ST_FixedAngle" use="optional" default="0"/>
<xsd:attribute name="algn" type="ST_RectAlignment" use="optional" default="b"/>
<xsd:attribute name="rotWithShape" type="xsd:boolean" use="optional" default="true"/>
</xsd:complexType>
As you see, the EG_ColorChoice
must occur 1 times. In other words: There must be a color setting for the CTOuterShadowEffect
.
Following code works for me:
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.poi.util.Units;
public class WordInsertPicturesWithShadow {
public static void main(String[] args) throws Exception {
XWPFDocument document = new XWPFDocument(new FileInputStream("source.docx"));
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText("The picture: ");
InputStream in = new FileInputStream("samplePict.jpeg");
XWPFPicture picture = run.addPicture(in, Document.PICTURE_TYPE_JPEG, "samplePict.jpeg", Units.toEMU(100), Units.toEMU(100));
in.close();
picture.getCTPicture().getSpPr().addNewEffectLst().addNewOuterShdw().setBlurRad(50000);
//picture.getCTPicture().getSpPr().getEffectLst().getOuterShdw().setDist(100000);
//picture.getCTPicture().getSpPr().getEffectLst().getOuterShdw().setDir(2700000);
//picture.getCTPicture().getSpPr().getEffectLst().getOuterShdw().setAlgn(
// org.openxmlformats.schemas.drawingml.x2006.main.STRectAlignment.TL);
//picture.getCTPicture().getSpPr().getEffectLst().getOuterShdw().setRotWithShape(false);
picture.getCTPicture().getSpPr().getEffectLst().getOuterShdw().addNewPrstClr().setVal(
org.openxmlformats.schemas.drawingml.x2006.main.STPresetColorVal.BLACK);
run.setText(" text after the picture.");
paragraph = document.createParagraph();
FileOutputStream out = new FileOutputStream("result.docx");
document.write(out);
out.close();
document.close();
}
}
来源:https://stackoverflow.com/questions/53286216/how-to-ensure-ctdocument-attribute-sequence-in-apache-poi-xwpf