📄 hwpfdocument.java
字号:
{ TextPiece tp = (TextPiece)textIt.next(); length += tp.characterLength(); } return length; } public ListTables getListTables() { return _lt; } /** * Gets a reference to the saved -by table, which holds the save history for the document. * * @return the saved-by table. */ public SavedByTable getSavedByTable() { return _sbt; } /** * @return PicturesTable object, that is able to extract images from this document */ public PicturesTable getPicturesTable() { return _pictures; } /** * Writes out the word file that is represented by an instance of this class. * * @param out The OutputStream to write to. * @throws IOException If there is an unexpected IOException from the passed * in OutputStream. */ public void write(OutputStream out) throws IOException { // initialize our streams for writing. HWPFFileSystem docSys = new HWPFFileSystem(); HWPFOutputStream mainStream = docSys.getStream("WordDocument"); HWPFOutputStream tableStream = docSys.getStream("1Table"); //HWPFOutputStream dataStream = docSys.getStream("Data"); int tableOffset = 0; // FileInformationBlock fib = (FileInformationBlock)_fib.clone(); // clear the offsets and sizes in our FileInformationBlock. _fib.clearOffsetsSizes(); // determine the FileInformationBLock size int fibSize = _fib.getSize(); fibSize += POIFSConstants.BIG_BLOCK_SIZE - (fibSize % POIFSConstants.BIG_BLOCK_SIZE); // preserve space for the FileInformationBlock because we will be writing // it after we write everything else. byte[] placeHolder = new byte[fibSize]; mainStream.write(placeHolder); int mainOffset = mainStream.getOffset(); // write out the StyleSheet. _fib.setFcStshf(tableOffset); _ss.writeTo(tableStream); _fib.setLcbStshf(tableStream.getOffset() - tableOffset); tableOffset = tableStream.getOffset(); // get fcMin and fcMac because we will be writing the actual text with the // complex table. int fcMin = mainOffset; // write out the Complex table, includes text. _fib.setFcClx(tableOffset); _cft.writeTo(docSys); _fib.setLcbClx(tableStream.getOffset() - tableOffset); tableOffset = tableStream.getOffset(); int fcMac = mainStream.getOffset(); // write out the CHPBinTable. _fib.setFcPlcfbteChpx(tableOffset); _cbt.writeTo(docSys, fcMin); _fib.setLcbPlcfbteChpx(tableStream.getOffset() - tableOffset); tableOffset = tableStream.getOffset(); // write out the PAPBinTable. _fib.setFcPlcfbtePapx(tableOffset); _pbt.writeTo(docSys, fcMin); _fib.setLcbPlcfbtePapx(tableStream.getOffset() - tableOffset); tableOffset = tableStream.getOffset(); // write out the SectionTable. _fib.setFcPlcfsed(tableOffset); _st.writeTo(docSys, fcMin); _fib.setLcbPlcfsed(tableStream.getOffset() - tableOffset); tableOffset = tableStream.getOffset(); // write out the list tables if (_lt != null) { _fib.setFcPlcfLst(tableOffset); _lt.writeListDataTo(tableStream); _fib.setLcbPlcfLst(tableStream.getOffset() - tableOffset); _fib.setFcPlfLfo(tableStream.getOffset()); _lt.writeListOverridesTo(tableStream); _fib.setLcbPlfLfo(tableStream.getOffset() - tableOffset); tableOffset = tableStream.getOffset(); } // write out the saved-by table. if (_sbt != null) { _fib.setFcSttbSavedBy(tableOffset); _sbt.writeTo(tableStream); _fib.setLcbSttbSavedBy(tableStream.getOffset() - tableOffset); tableOffset = tableStream.getOffset(); } // write out the FontTable. _fib.setFcSttbfffn(tableOffset); _ft.writeTo(docSys); _fib.setLcbSttbfffn(tableStream.getOffset() - tableOffset); tableOffset = tableStream.getOffset(); // write out the DocumentProperties. _fib.setFcDop(tableOffset); byte[] buf = new byte[_dop.getSize()]; _fib.setLcbDop(_dop.getSize()); _dop.serialize(buf, 0); tableStream.write(buf); // set some variables in the FileInformationBlock. _fib.setFcMin(fcMin); _fib.setFcMac(fcMac); _fib.setCbMac(mainStream.getOffset()); // make sure that the table, doc and data streams use big blocks. byte[] mainBuf = mainStream.toByteArray(); if (mainBuf.length < 4096) { byte[] tempBuf = new byte[4096]; System.arraycopy(mainBuf, 0, tempBuf, 0, mainBuf.length); mainBuf = tempBuf; } // write out the FileInformationBlock. //_fib.serialize(mainBuf, 0); _fib.writeTo(mainBuf, tableStream); byte[] tableBuf = tableStream.toByteArray(); if (tableBuf.length < 4096) { byte[] tempBuf = new byte[4096]; System.arraycopy(tableBuf, 0, tempBuf, 0, tableBuf.length); tableBuf = tempBuf; } byte[] dataBuf = _dataStream; if (dataBuf == null) { dataBuf = new byte[4096]; } if (dataBuf.length < 4096) { byte[] tempBuf = new byte[4096]; System.arraycopy(dataBuf, 0, tempBuf, 0, dataBuf.length); dataBuf = tempBuf; } // spit out the Word document. POIFSFileSystem pfs = new POIFSFileSystem(); pfs.createDocument(new ByteArrayInputStream(mainBuf), "WordDocument"); pfs.createDocument(new ByteArrayInputStream(tableBuf), "1Table"); pfs.createDocument(new ByteArrayInputStream(dataBuf), "Data"); pfs.writeFilesystem(out); } public CHPBinTable getCharacterTable() { return _cbt; } public PAPBinTable getParagraphTable() { return _pbt; } public SectionTable getSectionTable() { return _st; } public TextPieceTable getTextTable() { return _cft.getTextPieceTable(); } public byte[] getDataStream() { return _dataStream; } public int registerList(HWPFList list) { if (_lt == null) { _lt = new ListTables(); } return _lt.addList(list.getListData(), list.getOverride()); } public FontTable getFontTable() { return _ft; } public void delete(int start, int length) { Range r = new Range(start, start + length, this); r.delete(); } /** * Takes two arguments, 1) name of the Word file to read in 2) location to * write it out at. * @param args */ public static void main(String[] args) { try { HWPFDocument doc = new HWPFDocument(new FileInputStream(args[0])); Range r = doc.getRange(); String str = r.text(); int x = 0;// CharacterRun run = new CharacterRun();// run.setBold(true);// run.setItalic(true);// run.setCapitalized(true);//// Range range = doc.getRange();// range.insertBefore("Hello World!!! HAHAHAHAHA I DID IT!!!", run);//// OutputStream out = new FileOutputStream(args[1]);// doc.write(out);//// out.flush();// out.close(); } catch (Throwable t) { t.printStackTrace(); } }// public Object clone()// throws CloneNotSupportedException// {// _tpt;//// _cbt;//// _pbt;//// _st;//// }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -