亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何確保 apache poi xwpf 中的 ctdocument 屬性序列

如何確保 apache poi xwpf 中的 ctdocument 屬性序列

牧羊人nacy 2022-01-19 15:28:52
MS office 在所有文檔中生成此元素,并具有一些基本知識,即必須在一定程度上強制執行 NS 約束......MS 文檔示例:<w:documentxmlns: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"    <!--whatever--></w:document>請注意 MC:IGNORABLE 元素已移動...手動修改 docx 存檔中的此元素本身“修復”了 MSOffice 程序拋出的錯誤,該錯誤在第 2 行第 0 列中指定了一個錯誤。到目前為止,我已經嘗試過:xwpfDocument.getDocument().getDomNode()^ 已嘗試以正確的順序修改、刪除和添加屬性...不起作用,因為這種轉變似乎發生在 XWPFDocument.write() 過程中的某處。我還嘗試從 docx 存檔中提取 document.xml 文件,對其進行修改并將其注入回存檔中……這似乎不起作用……CRC問題……有沒有辦法訪問 OPCPackage 或部分或任何東西以允許直接調用此元素中的內置“recalculateAttributesRelativeToNS”?...或者作為替代方案..我如何在存檔內的文檔 xml 文件中自動更改 dom 并仍然確保 ms 兼容性?[編輯:“XY”]這種行為在以下場景中很明顯:fis = new FileInputStream(new File(path));XWPFPicture picture = imageRun.addPicture(fis, XWPFDocument.PICTURE_TYPE_PNG, path, Units.toEMU(450), Units.toEMU(290));文檔打開正常。問題不存在。添加這一行(我知道它在技術上不正確,但是不應該在第 2 行第 0 列報告錯誤,因為它應該在 xml 的下方進一步報告)搞砸了一切。picture.getCTPicture().getSpPr().addNewEffectLst().addNewOuterShdw().setBlurRad(10000);添加此行后,w:document 屬性變為狀態 2(poi 示例),從而觸發 ms Office 的 MS 架構 NS 失效例程。屬性是正確的。順序是錯誤的。手動將 mc:ignorable 屬性移動到屬性列表的末尾可以解決問題,并且文檔加載沒有問題。這很可能是因為(在正確的實現中)xmlns:mc 屬性在屬性列表中的 mc:ignorable 屬性之前,而在 poi 輸出中它沒有(mc:Ignorable 是列表中的第一個元素)。
查看完整描述

1 回答

?
拉莫斯之舞

TA貢獻1820條經驗 獲得超10個贊

錯誤中第 2 行第 0 列的指向具有誤導性。如果你看,/word/document.xml那么根本只有 2 行。第 1 行中的 XML 聲明和第 2 行中的所有其他 XML。


您的問題是架構定義CT_OuterShadowEffect是:


<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>

如您所見,EG_ColorChoice必須發生 1 次。換句話說: 必須有一個顏色設置CTOuterShadowEffect。


以下代碼對我有用:


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

 }

}



查看完整回答
反對 回復 2022-01-19
  • 1 回答
  • 0 關注
  • 267 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號