From efdccbfcb1f20afba47e0abbd0f1319160d44df2 Mon Sep 17 00:00:00 2001 From: 许鹏程 <1821349743@qq.com> Date: 星期五, 04 八月 2023 16:14:32 +0800 Subject: [PATCH] word 转pdf时不保留修订痕迹 --- src/main/java/com/product/file/util/AsposeUtil.java | 274 +++++++++++++++++++++++++++++++++++++++--------------- 1 files changed, 199 insertions(+), 75 deletions(-) diff --git a/src/main/java/com/product/file/util/AsposeUtil.java b/src/main/java/com/product/file/util/AsposeUtil.java index 15ae32d..d584bae 100644 --- a/src/main/java/com/product/file/util/AsposeUtil.java +++ b/src/main/java/com/product/file/util/AsposeUtil.java @@ -5,29 +5,28 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; -import java.math.RoundingMode; -import java.text.DecimalFormat; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; import com.aspose.cells.Cells; -import com.aspose.cells.HtmlSaveOptions; import com.aspose.cells.PdfSaveOptions; import com.aspose.cells.Workbook; import com.aspose.cells.Worksheet; import com.aspose.cells.WorksheetCollection; import com.aspose.slides.Presentation; -import com.aspose.words.Document; -import com.aspose.words.ImportFormatMode; -import com.aspose.words.NodeCollection; -import com.aspose.words.NodeType; -import com.aspose.words.Paragraph; -import com.aspose.words.SaveFormat; -import com.aspose.words.Section; -import com.aspose.words.SectionStart; +import com.aspose.words.*; import com.product.core.exception.BaseException; import com.product.core.spring.context.SpringMVCContextHolder; import com.product.file.config.FileCode; public class AsposeUtil { + private static int OFFICE_XLS=1; + private static int OFFICE_WORD=2; + private static int OFFICE_PPT=3; + private static boolean OFFICE_XLS_=false; + private static boolean OFFICE_WORD_=false; + private static boolean OFFICE_PPT_=false; //鏄惁鍒濆浜唋icense private static boolean isInitLicense=false; /** @@ -42,20 +41,24 @@ boolean result = true; InputStream is = null; try { - is = AsposeUtil.class.getClassLoader().getResourceAsStream("license.xml"); - com.aspose.cells.License excel = new com.aspose.cells.License(); - excel.setLicense(is); - - is = AsposeUtil.class.getClassLoader().getResourceAsStream("license.xml"); - com.aspose.words.License word = new com.aspose.words.License(); - word.setLicense(is); - - is = AsposeUtil.class.getClassLoader().getResourceAsStream("license.xml"); - com.aspose.slides.License ppt = new com.aspose.slides.License(); - ppt.setLicense(is); - - isInitLicense=true; + if(type==OFFICE_XLS && !OFFICE_XLS_ ) { + is = AsposeUtil.class.getClassLoader().getResourceAsStream("license.xml"); + com.aspose.cells.License excel = new com.aspose.cells.License(); + excel.setLicense(is); + OFFICE_XLS_=true; + }else if(type==OFFICE_WORD && !OFFICE_WORD_ ){ + is = AsposeUtil.class.getClassLoader().getResourceAsStream("license.xml"); + com.aspose.words.License word = new com.aspose.words.License(); + word.setLicense(is); + OFFICE_WORD_=true; + }else if(type==OFFICE_PPT && ! OFFICE_PPT_) { + is = AsposeUtil.class.getClassLoader().getResourceAsStream("license.xml"); + com.aspose.slides.License ppt = new com.aspose.slides.License(); + ppt.setLicense(is); + OFFICE_PPT_=true; + } } catch (Exception e) { + e.printStackTrace(); result=false; throw new BaseException(FileCode.DOC_CONVERT_FALL,e); }finally{ @@ -70,7 +73,7 @@ return result; } /** - * + * * @param officePath * @param OutPutPath * @throws BaseException @@ -106,7 +109,7 @@ } } /** - * + * * @param officePath * @param OutPutPath * @throws BaseException @@ -197,7 +200,7 @@ } } /** - * + * * @param officePath * @param OutPutPath * @throws BaseException @@ -215,6 +218,8 @@ } Document doc = new Document(officePath);// 鍘熷word璺緞 fileOS = new FileOutputStream(OutPutPath); + //涓嶄繚鐣欎慨璁㈢棔杩� + doc.acceptAllRevisions(); doc.save(fileOS, com.aspose.words.SaveFormat.PDF); } catch (Exception e) { throw new BaseException(FileCode.DOC_CONVERT_FALL,e); @@ -230,7 +235,7 @@ } } /** - * + * * @param officePath * @param OutPutPath * @throws BaseException @@ -283,8 +288,8 @@ Word2Pdf(officePath,outFile); }else if(officeType.equals("xls")||officeType.equals("xlsx")){ // Excel2Pdf(officePath,outFile); -// Excel2Html(officePath,outFile); - xls2xlsx(officePath,outFile,officeType); + Excel2Html(officePath,outFile); +// xls2xlsx(officePath,outFile,officeType); }else if(officeType.equals("ppt")||officeType.equals("pptx")){ PPT2Pdf(officePath,outFile); }else{ @@ -348,13 +353,13 @@ } /** * word鏂囨。鐨勫悎骞� - * @param targetWordFile - * @param sourceWordFile + * @param + * @param * @return * @throws Exception */ public static File mergeWord(File target,File source ) throws BaseException{ - + if(!target.exists() && !source.exists()) { return null; }else if(!source.exists() && target.exists()) { @@ -363,7 +368,7 @@ return source; } // 楠岃瘉License - if (!getLicense(2)) { + if (!getLicense(OFFICE_WORD)) { return null; } FileInputStream tin=null; @@ -375,16 +380,12 @@ tin=new FileInputStream(target); sin=new FileInputStream(source); Document targetDoc = new Document(tin); - Document sourceDoc = new Document(sin); - docBlankProcess(targetDoc); + Document sourceDoc = new Document(sin); +// docBlankProcess(targetDoc); targetDoc.appendDocument(sourceDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING); - - -// for(Section sect : targetDoc.getSections()) -// { -// if (sect.getBody().getParagraphs().getCount() == 0) { -// sect.remove(); -// } +// NodeCollection t =sourceDoc.getChildNodes(); +// for(int c=0;c<t.getCount();c++) { +// targetDoc.getChildNodes().add(t.get(c)); // } File dir=new File(temp); if(!dir.exists()) { @@ -398,6 +399,7 @@ targetDoc.save(out, SaveFormat.DOCX); } }catch(Exception e) { + e.printStackTrace(); throw new BaseException(FileCode.DOC_MERGE_FALL,e); } finally{ @@ -418,50 +420,172 @@ } return temp_target; } - + public static void docBlankProcess(Document targetDoc) { NodeCollection nodes=targetDoc.getChildNodes(NodeType.PARAGRAPH, true); System.out.println(nodes.getCount()); + List <Node> ss=new ArrayList<>(); for(Paragraph para :(Iterable<Paragraph>) nodes) { + System.out.println("==========================="); + System.out.println(para.isEndOfSection()); + System.out.println(para.hasChildNodes()); String text=para.getText(); - if("\r".equals(text)) { - System.out.println("rrrrrrrrrrrr"); - }else if("\f".equals(text)) { - System.out.println("ffffffffffff"); - }else { - System.out.println(text); + if("\r".equals(text))System.out.println("\\\\r"); + else if("\f".equals(text))System.out.println("\\\\f"); + else if("".equals(text.trim())) System.out.println("绌烘牸"); + else System.out.println(text); + + NodeCollection pnodes =para.getChildNodes(); + if(pnodes.getCount()==0)para.appendChild(new Run(targetDoc,"a")); + if(para.isEndOfSection())para.appendChild(new Run(targetDoc,"B")); + System.out.println("sub nodes:"+pnodes.getCount()); + for(int i=0;i<pnodes.getCount();i++) { + Node d=pnodes.get(i); + System.out.println("type:"+NodeType.getName(d.getNodeType())); + + if("\r".equals(text))System.out.println("\\\\r"); + else if("\f".equals(text))System.out.println("\\\\f"); + else if("".equals(text.trim())) System.out.println("绌烘牸"); + else System.out.println(text); + if("BOOKMARK_END".equals(NodeType.getName(d.getNodeType()))) { + BookmarkEnd b=(BookmarkEnd)d; + } + if(d.getNodeType()==NodeType.SHAPE) { + ss.add(d); + } } - if ("\r".equals(text) - //|| "\f".equals(text) - ) { - para.remove(); - } + + } - } - public static void docProcess(Document targetDoc) { - NodeCollection nodes=targetDoc.getChildNodes(); - for(Paragraph para :(Iterable<Paragraph>) nodes) { - String text=para.getText(); - if("\r".equals(text)) { - System.out.println("rrrrrrrrrrrr"); - }else if("\f".equals(text)) { - System.out.println("ffffffffffff"); - }else { - System.out.println(text); - } - if ("\r".equals(text) - //|| "\f".equals(text) - ) { - para.remove(); - } + for(Node d:ss) { + d.remove(); } + } + + private static String appendDoc(String sohuyepath, String xuyepath, boolean includeSection) throws Exception { + // 楠岃瘉License + if (!getLicense(OFFICE_WORD)) { + return null ; + } + Document dstDoc = new Document(sohuyepath); + Document srcDoc = new Document(xuyepath); + System.out.println("鐩爣鏂囨。椤垫暟锛�"+dstDoc.getPageCount()+" | 婧愭枃妗i〉鏁帮細"+srcDoc.getPageCount()); + if (includeSection) { + Iterator<Section> var3 = srcDoc.getSections().iterator(); + while (var3.hasNext()) { + Section srcSection = (Section) var3.next(); + Node dstNode = dstDoc.importNode(srcSection, true, 0); + dstDoc.appendChild(dstNode); + } + } else { + Node node = dstDoc.getLastSection().getBody().getLastParagraph(); + if (node == null) { + node = new Paragraph(srcDoc); + dstDoc.getLastSection().getBody().appendChild(node); + } + + if (node.getNodeType() != 8 & node.getNodeType() != 5) { + throw new Exception("Use appendDoc(dstDoc, srcDoc, true) instead of appendDoc(dstDoc, srcDoc, false)"); + } + + insertDocumentAfterNode(node, dstDoc, srcDoc); + } + File f=new File(sohuyepath); + dstDoc.save(f.getParent()+"/merge/"+f.getName()); + return f.getParent()+"/merge/"+f.getName(); + } + + private static void insertDocumentAfterNode(Node insertAfterNode, Document mainDoc, Document srcDoc) + throws Exception { + if (insertAfterNode.getNodeType() != 8 & insertAfterNode.getNodeType() != 5) { + throw new Exception("The destination node should be either a paragraph or table."); + } else { + CompositeNode dstStory = insertAfterNode.getParentNode(); + + while (null != srcDoc.getLastSection().getBody().getLastParagraph() + && !srcDoc.getLastSection().getBody().getLastParagraph().hasChildNodes()) { + srcDoc.getLastSection().getBody().getLastParagraph().remove(); + } + + NodeImporter importer = new NodeImporter(srcDoc, mainDoc, 1); + int sectCount = srcDoc.getSections().getCount(); + + for (int sectIndex = 0; sectIndex < sectCount; ++sectIndex) { + Section srcSection = srcDoc.getSections().get(sectIndex); + int nodeCount = srcSection.getBody().getChildNodes().getCount(); + + for (int nodeIndex = 0; nodeIndex < nodeCount; ++nodeIndex) { + Node srcNode = srcSection.getBody().getChildNodes().get(nodeIndex); + Node newNode = importer.importNode(srcNode, true); + dstStory.insertAfter(newNode, insertAfterNode); + insertAfterNode = newNode; + } + } + + } + } + + /** + * 鎶婃簮鏂囨。鍐呭鏀惧埌鏈�鍚庝竴椤电殑椤垫湯 + * @param sohuyepath + * @return + * @throws Exception + */ + private static void appendDocToPageTail(String sohuyepath) throws Exception { + Document dstDoc = new Document(sohuyepath); + int destPages=dstDoc.getPageCount(); + System.out.println("鍚堝苟鍚堢殑鏈�鍚庢�婚〉鏁帮細"+destPages); + Node lastNode=dstDoc.getLastSection().getBody().getLastChild(); + Paragraph lp=null; + if(lastNode.getNodeType()==NodeType.PARAGRAPH){ + lp=(Paragraph)lastNode; + Node ld=lp.getFirstChild(); + while(ld!=null && ld.getNodeType()!=NodeType.SHAPE && ld.getNodeType()!=NodeType.RUN && ld.getNodeType()!=NodeType.TABLE){ + System.out.println("type name "+NodeType.getName(ld.getNodeType())); + ld=ld.getPreviousSibling(); + } + } + System.out.println("鏈�鍚庝竴鑺傜偣锛�"+NodeType.getName(lastNode.getNodeType())); + for(int i=0;i<40;i++) { + Run t = new Run(dstDoc, "======\r"); + if(lp!=null){ + lp.insertBefore(t,lp.getLastChild()); + System.out.println("瀛愬厓绱犳暟锛�"+lp.getChildNodes().getCount()); + } + System.out.println("鍚堝苟鍚堢殑鏈�鍚庢�婚〉鏁帮細"+dstDoc.getPageCount()); + if(destPages < dstDoc.getPageCount()){ + System.out.println("================鍒嗛〉================="); + } + } + File ff=new File(sohuyepath); + dstDoc.save(ff.getParent()+"/merge/"+ff.getName(),SaveFormat.DOCX); + } + public static void main(String args[]) { - String file="E:/宸ヤ綔鍐呭.xlsx"; +// String file="E:/宸ヤ綔鍐呭.xlsx"; // String file="E:/鐖嗙牬姣嶇嚎.xls"; // String file="E:/閲囬泦鎻愬彇鏃ュ織琛ㄧ粨鏋�.xlsx"; -// String file="E:/FE淇℃伅绯荤粺鍥戒骇鍖栬В鍐虫柟妗圴1.0.docx"; - AsposeUtil.OfficeToPdf(file); + String file="E:/FE淇℃伅绯荤粺鍥戒骇鍖栬В鍐虫柟妗圴1.0.docx"; +// AsposeUtil.OfficeToPdf(file); + // AsposeUtil.mergeWord("E:\\QQ\\浣忔埧鎷呬繚缃崲鍏徃.docx","E:\\QQ\\浣忔埧鎷呬繚缃崲鍏徃2.docx"); +// try { +// processDocPage(); +// }catch(Exception e) { +// e.printStackTrace(); +// } + try { +// String sohuyepath="E:/QQ/浣忔埧鎷呬繚缃崲鍏徃.docx"; +// String xuyepath="E:/QQ/FE淇℃伅绯荤粺鍥戒骇鍖栬В鍐虫柟妗圴1.0.docx"; +// String npath=appendDoc(sohuyepath, xuyepath, false); + String tail="E:/QQ/浣忔埧鎷呬繚缃崲鍏徃2.docx"; +// appendDoc(npath, tail, false); + appendDocToPageTail(tail); + + + }catch(Exception e) { + e.printStackTrace(); + } } } -- Gitblit v1.9.2