Foot note is not coming in the each page

Hi All,
I am using the aspose word to generate the word report,now i have a requirement to insert the footnote and currently footnote is coming in the last page of the report.

below is the code

finalvalue=p.getText().substring(37);
builder.insertFootnote(FootnoteType.FOOTNOTE, finalvalue);

@c-prakashk,

Thanks for contacting support.

Can you please share some details regarding the report which you are generating ? and also please share either you are using Aspose.Words for .NET or Aspose.Words for Reporting Services to generate the report.

If possible, please share the input files, which can help us in replicating the issue in our environment. We are sorry for this inconvenience.

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

@c-prakashk

Thanks for sharing additional information. However we need some further information to test your issue at our end. Please ZIP the following resources and attach here for testing. As soon as you get these pieces of information ready, we will start investigation and will provide you more information accordingly.

  • Your input Word document
  • Please attach the output Word file that shows the undesired behavior
  • Please share your expected output Word document, you can use MS Word to create it
  • Please share a sample java code (source code without compilation errors/missing references) that helps us to reproduce your problem on our end

Aspose_Footnote_Bug.zip (311.0 KB)
Hi Nayyer Shahbaz,
I have attached the required files that you have mention,PFA For the Same and try to resolve this issue as its too late for me.

Thanks
Prakash Kumar

@c-prakashk

Thanks for sharing the additional resources. I am unable to test your code exactly due to missing references. However I have tested the scenario after some changes in your code. It seems you did not share your expected output. Please find my sample output document and code. Please check the document and confirm the problem in it with screenshots and expected output document in this case.updated_code.zip (135.7 KB)

Aspose_Footnote_Bug.zip (311.5 KB)
Hi Nayyer Shahbaz,
I have attached the sample output file name as Outputfile.docx,
PFA for the same.
can you please share your contact number.so that I can reach you as soon as possible and fix this issue.
That will be really great help.

Thanks
Prakash Kumar

@c-prakashk

Thanks for your feedback. Please note as requested above we need customer resources to replicate the issue at our end and we start investigation as soon as we receive these resources. We can not suggest you anything until we reproduce your issue at our end.

We are unable to test your sample code due to missing references. Please share a simplified code with hard coded values to replicate your issue exactly and if Outputfile.docx is your expected document then please also share Aspose.Words generated output file.

Furthermore as per policy, we do not provide technical support over phone but sales related support.

Hi tilal,
Outputfile.docx is my expected document and Aspose.Words generated output file is Input_File.docx,
The java code what I have given you with that only I am generating the Input_File.docx.

To insert footnote
Document doc1 = new Document();
DocumentBuilder builder = new DocumentBuilder(doc1);
builder.insertFootnote(FootnoteType.FOOTNOTE, “Text Hardcoded”);

so the footnote is coming in the last page.even we have tried FootnoteLocation.BENEATH_TEXT and all other options as well but footnote is always going to the last page of the report ideally it should go to the respective page end not the last page end.

Thanks
Prakash kumar

@c-prakashk

Thanks for your feedback. I am afraid your provided information is not sufficient to replicate your issue at our end. If you have shared your Aspose.Words output(Input_File.docx) and expected output(Outpufile.docx) then please share your input document as well. I have tested the scenario using sample document using following code with Aspose.Words for Java 17.7 and unable to replicate your reported issue. If you are using some old version of Aspose.Words for Java then please download and try latest version of Aspose.Words for Java, it will resolve the issue. However if the issue persists then we need your input document to replicate the issue at our end.FootnoteOut_177.zip (6.6 KB)

// load word document
com.aspose.words.Document doc = new com.aspose.words.Document("input.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

// move cursor to the paragraph
builder.moveTo(doc.getFirstSection().getBody().getFirstParagraph());

//insert a footnote
builder.insertFootnote(FootnoteType.FOOTNOTE, "Footnote text.");
// save the resultant Word document
doc.save("FootnoteOut_177.docx");

We are sorry for the inconvenience.

hey
Thanks for the Feedback, now the code you have given is going in the first page itself but my requirement is to print the footnote in the respective pages not in the first page or in the last page.
// move cursor to the paragraph
builder.moveTo(doc.getFirstSection().getBody().getFirstParagraph());
builder.insertFootnote(FootnoteType.FOOTNOTE, “Footnote text.”);

please suggest how I can print my footnote in the respective page.(Assume I have 10 pages and each pages I am having footnote).

Thanks
Prakash

@c-prakashk

Thanks for your feedback. Please check following code snippet, it adds footnote on respective page of searched text. Hopefully it will help you to accomplish the task.

com.aspose.words.Document doc = new com.aspose.words.Document("Test.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
                        
// move cursor to the paragraph
builder.moveTo(doc.getFirstSection().getBody().getFirstParagraph());

//insert a footnote
builder.insertFootnote(FootnoteType.FOOTNOTE, "Footnote text 1.");


for (Node para : doc.getChildNodes(NodeType.PARAGRAPH, true).toArray())
{
	if (para.toString(com.aspose.words.SaveFormat.TEXT).contains("Example of an ADS"))
    {
    	// move cursor to the paragraph
        builder.moveTo(para);

        //insert a footnote
        builder.insertFootnote(FootnoteType.FOOTNOTE, "Footnote text 2.");
    };
}
            
// save the resultant Word document
doc.save("Test_AW178.docx");