Hi Nayyer Shahbaz,
Thanks for the reply,I have given the java controller where I am taking the data from the html and
Reading as a paragraph and in between the paragraph I am trying to insert the footnote but footnote is always going to the last page of the report.
Kindly suggest some solution for the above mention problem.
@RequestMapping(value = “/generateWordDoc”, method = RequestMethod.POST, produces = “application/vnd.openxmlformats-officedocument.wordprocessingml.document”) // “application/vnd.openxmlformats-officedocument.spreadsheetml.sheet”)//
@Timed
public @ResponseBody void generateWordDoc(HttpServletRequest request, HttpServletResponse response,
@RequestBody String data) throws Exception {
// System.out.println("inside generateWordDoc " + data);
// String templateDir="\\\\mumchrsh07\\Share\\Reports_Template\\";
// Document doc = new Document();
String templateDir = Constants.Aspose_License;
// String reportname="";
Map<String, String> mapData = new HashMap<String, String>();
String dirPath = Constants.Aspose_License;
License license = new License();
license.setLicense(new java.io.FileInputStream(dirPath + "Aspose.Words.lic"));
try {
ObjectMapper mapper = new ObjectMapper();
mapData = mapper.readValue(data, new TypeReference<Map<String, String>>() {
});
String docHtmlContent = mapData.get("docHtmlContent");
String flag = mapData.get("flag");
String tagid = mapData.get("tagid");
String isDocTypeLandscape = "true";
// System.out.println("docHtmlContent------->> "+docHtmlContent);
List<Object[]> temp = tagCreditRepository.getCompanyIDBytagId(Long.parseLong(tagid.trim()));
Iterator it = temp.iterator();
List<Cma_reportDTO> clobDtoList = new ArrayList<Cma_reportDTO>();
while (it.hasNext()) {
Cma_reportDTO clobDto = new Cma_reportDTO();
Object[] obj = (Object[]) it.next();
BigDecimal cid = (BigDecimal) obj[0];
clobDto.setCOMPANY_ID(cid.longValue());
BigDecimal sid = (BigDecimal) obj[1];
BigDecimal retempid = (BigDecimal) obj[2];
clobDto.setTEMPLATE_ID(retempid.longValue());
// This is current Status
// clobDto.setStageId(sid.longValue());
clobDtoList.add(clobDto);
}
// System.out.println("Flagggggg------------->" + flag);
if (docHtmlContent == null) {
throw new NullPointerException();
}
/* Replace relative file pathe with absolute path */
String replacePath = "src=\"/sparc-cma/userfiles/";
String localPath = "src=\"userfiles/";
String actualPath = "src=\"" + Constants.IMAGE_UPLOAD_PATH_Word;
// String replacePath = "src=\"/sparc-cma/userfiles/";
// String localPath = "src=\"/userfiles/";
// String actualPath = "src=\""+Constants.IMAGE_UPLOAD_PATH_test ;
String footnotereplacepath="<section class=\"footnotes\">";
String footnotereplacepathfooter="</section>";
String footnotepath="<p>";
String footnotepathfooter="</p>";
/* String foot= "<cite style=font-size:10.5px";
String pagebreak = "PAGE BREAK";*/
// this is for local
//docHtmlContent = docHtmlContent.replaceAll(replacePath, localPath);
// this is for server
docHtmlContent = docHtmlContent.replaceAll(replacePath, actualPath);
/*docHtmlContent=docHtmlContent.replaceAll(footnotereplacepath, footnotepath);
docHtmlContent = docHtmlContent.replaceAll(footnotereplacepathfooter, footnotepathfooter);*/
System.out.println("After Replace docHtmlContent------->> "+docHtmlContent);
// docHtmlContent = docHtmlContent.replaceAll(pagebreak, "");
// docHtmlContent = docHtmlContent.replaceAll(localPath,
// actualPath);//this for local
// System.out.println("docHtmlContent : "+docHtmlContent);
// SectionDataController sec=new SectionDataController();
// sec.doConversion2(docHtmlContent, "d:/SampleTest.pdf");
String mimeType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";// **"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";*//*
/*
* InputStream is = new
* ByteArrayInputStream(docHtmlContent.getBytes());
*
*
* LoadOptions loadOptions = new LoadOptions(); // loadOptions.set
* loadOptions.setLoadFormat(LoadFormat.HTML); Document doc = new
* Document(is, loadOptions);
*/
// Document doccc=new Doc
// doc.setAttachedTemplate(Constants.Support_File_UPLOAD_PATH +
// "temppp.docx");
String dataPath = Constants.Support_File_UPLOAD_PATH;
Document doc1 = new Document(dataPath + "temppp.docx");
DocumentBuilder builder = new DocumentBuilder(doc1);
builder.insertHtml(docHtmlContent);
// System.out.println("Html Content----->>>" + docHtmlContent);
Section currentSection = builder.getCurrentSection();
PageSetup pageSetup = currentSection.getPageSetup();
// 36,36
pageSetup.setLeftMargin(36);
pageSetup.setRightMargin(36);
pageSetup.setTopMargin(58);
pageSetup.setBottomMargin(50);
pageSetup.setPaperSize(PaperSize.A4);
// for (Node section : doc1.getChildNodes(NodeType.SECTION, true).toArray()) {
// Section s=(Section)section;
//
// System.out.println("—Section—"+s.getText());
// }
// Boolean b = docHtmlContent.contains(“footnotes”);
// // System.out.println(“valueeee” + b);
// String footnoteArray[];
// String footnoteArray2[];
//
// String searchstring = “#footnote-marker”;
//
// String strArray[] = docHtmlContent.split(searchstring);
// System.out.println(“strArray”+strArray.length);
// // System.out.println(“footnoteArray”+footnoteArray.length);
//
// for (int i = 1; i < strArray.length; i++) {
//
// if (b == true)
//
// {
// footnoteArray = docHtmlContent.split("#footnote-marker-" + i + “-1”);
//
// footnoteArray2 = footnoteArray[1].split("");
//
// System.out.println(“footnoteArray”+footnoteArray.length);
// System.out.println(“footnoteArray2”+footnoteArray2.length);
//
// System.out.println(“footnoteArray—2” + footnoteArray2[0].substring(45));
// builder.insertFootnote(FootnoteType.FOOTNOTE, footnoteArray2[0].substring(45)) ;
// }
//
// }
/*for (Node section : doc1.getChildNodes(NodeType.SECTION, true).toArray()) {
Section p = (Section) section;
}
*/
int i = 1;
for (Node paragraph : doc1.getChildNodes(NodeType.PARAGRAPH, true).toArray()) {
Paragraph p = (Paragraph) paragraph;
// System.out.println("Counter-->"+i);
System.out.println("Text-->" + p.getText());
/* String footnoteArray[];
String footnoteArray2[];
String searchstring = "#footnote-marker";*/
String finalvalue="";
Boolean link = p.getText().contains("HYPERLINK");
System.out.println("link"+link);
if(link==true)
{
String searchstring = "footnote-marker";
Boolean link1 = p.getText().contains(searchstring);
System.out.println("link1"+link1);
if(link1==true)
{
finalvalue=p.getText().substring(37);
builder.insertFootnote(FootnoteLocation.END_OF_SECTION, finalvalue);
}
// System.out.println("footnoteArray"+footnoteArray.length);
/*for (int i = 1; i < strArray.length; i++) {
if (b == true)
{
footnoteArray = docHtmlContent.split("#footnote-marker-" + i + "-1");
footnoteArray2 = footnoteArray[1].split("</cite>");
System.out.println("footnoteArray"+footnoteArray.length);
System.out.println("footnoteArray2"+footnoteArray2.length);
System.out.println("footnoteArray---2" + footnoteArray2[0].substring(45));
builder.insertFootnote(FootnoteType.FOOTNOTE, footnoteArray2[0].substring(45)) ;
}
}*/
System.out.println("finalvalue"+finalvalue);
}
// System.out.println("Length of the
// Text"+p.getText().length());
// System.out.println("Size-->"+p.getParagraphBreakFont().getSize());
String arr[] = p.getText().split(":");
String Assesdate = arr[0].trim();
// System.out.println("Assesdate "+Assesdate);
// System.out.println("p.getText()"+p.getText());
// CharSequence ch="PAGE BREAK";
// builder.cle
CharSequence hyperlink = "footnote-marker";
if (p.getText().trim().equals("PAGEBREAK"))
{
// System.out.println("Inside The Page Break calll");
// System.out.println("Page Break Text--->"+p.getText());
p.getParagraphFormat().setLeftIndent(-100);
// builder.insertFootnote(FootnoteType.FOOTNOTE, "raaa");
Run pageBreakRun = new Run(doc1, ControlChar.PAGE_BREAK);
// pageBreakRun.setText("hello");
p.appendChild(pageBreakRun);
//System.out.println("Foot note Testing");
}
// Header
if (p.getParagraphBreakFont().getSize() == Double.valueOf(16.0))
{
// reportname=p.getText();
// System.out.println("Inside the Bigger Font 16 ");
p.getParagraphFormat().setSpaceAfter(0);
p.getParagraphFormat().setLeftIndent(0);
p.getParagraphFormat().setSpaceAfter(3);
p.getParagraphFormat().setSpaceBefore(3);
}
// Date Of Assement
if (p.getParagraphBreakFont().getSize() == Double.valueOf(9.0)
&& Assesdate.equalsIgnoreCase("Date of Assessment"))
{
// System.out.println("Inside the Condition of Date of
// Assesment");
p.getParagraphFormat().setSpaceAfter(0);
p.getParagraphFormat().setLeftIndent(0);
p.getParagraphFormat().setSpaceAfter(3);
p.getParagraphFormat().setSpaceBefore(3);
// System.out.println("Inside the Bigger Font 9 ");
}
// Section content
if (p.getParagraphBreakFont().getSize() == Double.valueOf(9.0) && p.getText().length() >= 40)
{
p.getParagraphFormat().setAlignment(ParagraphAlignment.JUSTIFY);
// System.out.println("Inside the Bigger Font 12 ");
p.getParagraphFormat().setSpaceAfter(0);
p.getParagraphFormat().setLeftIndent(0);
p.getParagraphFormat().setSpaceAfter(4);
p.getParagraphFormat().setSpaceBefore(0);
// p.getParagraphFormat().setAlignment(ParagraphAlignment.JUSTIFY);
}
// section
if (p.getParagraphBreakFont().getSize() == Double.valueOf(12.0) && p.getText().length() <= 40)
{
// System.out.println("Inside the Bigger Font 12 and length
// <=40 ");
p.getParagraphFormat().setSpaceAfter(0);
p.getParagraphFormat().setLeftIndent(0);
p.getParagraphFormat().setSpaceAfter(2);
p.getParagraphFormat().setSpaceBefore(0);
}
// subsection
if (p.getParagraphBreakFont().getSize() == Double.valueOf(12.0) && p.getText().length() > 40)
{
// System.out.println("Inside the Bigger Font 12 and length
// >40 ");
p.getParagraphFormat().setSpaceAfter(0);
p.getParagraphFormat().setLeftIndent(0);
p.getParagraphFormat().setSpaceAfter(4);
p.getParagraphFormat().setSpaceBefore(0);
}
// System.err.println("p.getParagraphBreakFont().getItalic();"+p.getParagraphBreakFont().gets);
// System.err.println("p"+p.getParagraphBreakFont().getBold());
/*
* if(p.getParagraphBreakFont().getSize()==Double.valueOf(8.0)&&
* p.getText().length()>25)
*
* { p.getParagraphFormat().getPageBreakBefore(); //
* System.out.println("This is the font Size of 8.0---->"
* +p.getText().length()); //
* System.err.println("p.getParagraphBreakFont().getItalic();"+p
* .getParagraphBreakFont().getItalic()); // System.err.println(
* "Style name"+p.getParagraphFormat().getStyleName()); //
* System.err.println("IS Bold"
* +p.getParagraphBreakFont().getBold()); // System.out.println(
* "Before FootNote Insert"); //
* builder.insertFootnote(FootnoteType.FOOTNOTE, p.getText());
* // builder.insertBreak(BreakType.PAGE_BREAK);
*
* // builder.insertBreak(breakType); // System.out.println(
* "After FootNote Insert");
* //builder.insertBreak(BreakType.PAGE_BREAK); //
* builder.insertFoo // System.out.println(
* "Inside the Bigger Font 12 and length >40 ");
* p.getParagraphFormat().setSpaceAfter(0);
* p.getParagraphFormat().setLeftIndent(0);
* p.getParagraphFormat().setSpaceAfter(0);
* p.getParagraphFormat().setSpaceBefore(0);
*
* }
*/
// System.out.println("==Finish==="+i);
// boolean footnote=p.getText().contains(hyperlink);
/*
* if(footnote==true)
*
* {
*
*
*
*
* System.out.println("Inside the footnote method call");
* System.out.println("-FootNote Text-"+p.getText()); String
* footnoteArray[]=p.getText().split("footnote-marker"); String
* value=footnoteArray[1];
* //System.out.println("value[0]"+footnoteArray[0]+"value[1]"+
* footnoteArray[1]); //builder.mo // FootnoteType.n
* setfOOTnOTES(builder, value);
* //builder.insertFootnote(FootnoteType.FOOTNOTE, value); //
* builder.insertFootnote(FootnoteNumberingRule.CONTINUOUS,
* value);
*
*
* }
*/
i++;
}
// builder.insertBreak();
// builder = setHeaderAndFooter(builder, templateDir);
if (null != isDocTypeLandscape && "true".equals(isDocTypeLandscape)) {
builder.getPageSetup().setOrientation(Orientation.PORTRAIT);
}
response.setContentType(mimeType);
String fileName = URLEncoder.encode("output.docx", "UTF-8");
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
// String
// testpath="file://mumchwlg12//CuttingEdgeDemo//Sparc_Dev//CuttingEdge//CKFinderJava//userfiles//sparc-cda-file//";
if (flag.equalsIgnoreCase("C")) {
String reportname = clobDtoList.get(0).getCOMPANY_ID() + "_" + tagid.trim() + "_"
+ clobDtoList.get(0).getTEMPLATE_ID();
// System.out.println("reportname" + reportname);
// System.out.println("Before File writing"+reportname);
// doc.save(Constants.IMAGE_UPLOAD_PATH_test , SaveFormat.DOCX);
doc1.save(Constants.IMAGE_UPLOAD_PATH_test + reportname + ".docx");
// System.out.println("After File writing");
} else {
doc1.save(response.getOutputStream(), SaveFormat.DOCX);
}
// * File theDir = new File("D://Report"); if (!theDir.exists()){
// * theDir.mkdirs(); } doc.save("D://Report.docx");
// int res =
// userWiseModelReportService.updateUserWiseReportDownload(reportName+"_"+reportId,
// Constants.PATH_REPORT, Long.parseLong(reportId));
log.debug("update userwise report download:");
} catch (FileNotFoundException e) {
log.debug("File not found exception:" + e.getStackTrace());
} catch (Exception e) {
System.out.println("Catch Blcok--2");
log.debug("Exception:" + e.getStackTrace());
}
}
Thanks
Prakash Kumar