Free Support Forum - aspose.com

Problem removing a bookmark (Bookmark still present when saving to PDF)

Hi,

I append the "chap_000_Banalise.docx" document to the "chap_HDG_Banalise.docx" file.
I add the table of contents below the bookmark.
I update the table of contents and I remove the bookmark in the document content.

I save document in DOCX or PDF file.

The DOCX Document is saved correctly.
The bookmark is not deleted in PDF file (The text of the bookmark still exists).

Source code of the "TOC" insertion:
DocumentBuilder builder = new DocumentBuilder(document);
builder.moveToBookmark(printOptions.get(DO_OPTION_TOCBOOKMARKID), false, true);
builder.writeln();
Field tocField = builder.insertTableOfContents("\\o \"1-" + printOptions.get(DO_OPTION_TOCLEVEL) + "\" \\h \\z \\u");
tocField.update();
deleteBookmark(document, printOptions.get(DO_OPTION_TOCBOOKMARKID), true);

Source code used to remove the bookmark:
if (document.getRange().getBookmarks().get(bookmarkName) != null)
{
if (content)
{
document.getRange().getBookmarks().get(bookmarkName).setText("");
}
document.getRange().getBookmarks().remove(bookmarkName);
}

Aspose.Words for java : 11.8.0

See attached documents for a concrete example:
- chap_HDG_Banalise.docx : The first document to be merged.
- chap_000_Banalise.docx : The second document to be merged in the first one.
- finalDoc.docx : The DOCX result of the merge with Aspose.
- finalDoc.pdf : The PDF result of the merge with Aspose.
I look forward to your response.

Best Regards,
Patrice ANTOINE
Hi Patrice,

Thanks for your inquiry. I have tested the scenario and have not faced any issue with output PDF file while using latest version of Aspose.Words for Java 11.10.0. Please use the latest version of Aspose.Words for Java. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.

Hi,

Thank you for your quick support, the new version (11.10.0.0) of Aspose.Words fixes this problem. But I have to remove the bookmark before updating the table of contents.

Thanks for your time!

Best Regards,
Patrice ANTOINE

Hi Patrice,


Thanks for your feedback. It is nice to hear from you that your problem has solved. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.

Hello,

I was able to reproduce the bug using Aspose 11.10.0.

Can you test this code source, please?

Result : The bookmark is not deleted in PDF file (The text of the bookmark still exists).

Source code :

@Test

public void testmergeTwoDocument()

{

File outputFile;

try

{

outputFile = File.createTempFile("testmergeTwoDocument", ".pdf", new File("TempDir"));

OutputStream outputStream = new FileOutputStream(outputFile);

Resource docBase;


docBase = new ClassPathResource("/words/chap_HDG_Banalise.docx");

InputStream firstDoc = docBase.getInputStream();

docBase = new ClassPathResource("/words/chap_000_Banalise.docx");

InputStream docToAppend = docBase.getInputStream();

this.documentManager.mergeTwoDocuments(firstDoc, docToAppend, outputStream, SaveFormat.PDF);


outputStream.close();

}

catch (IOException e)

{

e.printStackTrace();

fail("Fail to create inputStream or outputFile : " + e.getMessage());

}

catch (Exception e)

{

fail("mergeDocument failed : " + e.getMessage());

}

}


public void mergeTwoDocuments(InputStream firstDoc, InputStream docToAppend, OutputStream outputStream, int outFormat) throws Exception

{

Document finalDocument = new Document(firstDoc);


Document subDoc = new Document(docToAppend);

subDoc.getFirstSection().getHeadersFooters().linkToPrevious(false);

subDoc.getFirstSection().getPageSetup().setSectionStart(SectionStart.NEW_PAGE);

subDoc.getFirstSection().getPageSetup().setRestartPageNumbering(true);


finalDocument.appendDocument(subDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);

finalDocument.setTrackRevisions(false);

finalDocument.acceptAllRevisions();

DocumentBuilder builder = new DocumentBuilder(finalDocument);

builder.moveToBookmark("TocBookmark", false, true);

builder.writeln();

Field tocField = builder.insertTableOfContents("\\o \"1-3\" \\h \\z \\u");

tocField.update();


finalDocument.getRange().getBookmarks().get("TocBookmark").setText("");

finalDocument.getRange().getBookmarks().remove("TocBookmark");


finalDocument.setAttachedTemplate("");

finalDocument.save(outputStream, outFormat);

}

I look forward to your response.


Best Regards,
Patrice ANTOINE

Hi Patrice,


Thanks for your inquiry. I have worked with your shared documents and code and have not found any issue with output document. You just need to use Document.updateFields method before saving your final document.

When you open, modify and then save a document, Aspose.Words does not update fields automatically, it keeps them intact. Therefore, you would usually want to call this method before saving if you have modified the document programmatically and want to make sure the proper (calculated) field values appear in the saved document. Please find the output PDF file in attachment.


<span lang=“EN-GB” style=“font-size:10.0pt;font-family:“Courier New”;color:#2B91AF;mso-font-kerning:
0pt;mso-no-proof:yes”>Document<span lang=“EN-GB” style=“font-size:10.0pt;
font-family:“Courier New”;mso-font-kerning:0pt;mso-no-proof:yes”> subDoc = new Document(docToAppend);<o:p></o:p>

subDoc.getFirstSection().getHeadersFooters().linkToPrevious(false);

subDoc.getFirstSection().getPageSetup().setSectionStart(SectionStart.NEW_PAGE);

subDoc.getFirstSection().getPageSetup().setRestartPageNumbering(true);

finalDocument.appendDocument(subDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);

finalDocument.setTrackRevisions(false);

finalDocument.acceptAllRevisions();

DocumentBuilder builder = new DocumentBuilder(finalDocument);

builder.moveToBookmark("TocBookmark", false, true);

builder.writeln();

Field tocField = builder.insertTableOfContents("\\o \"1-3\" \\h \\z \\u");

finalDocument.getRange().getBookmarks().get("TocBookmark").setText("");

finalDocument.getRange().getBookmarks().remove("TocBookmark");

finalDocument.setAttachedTemplate("");

finalDocument.updateFields();

finalDocument.save(outputStream, outFormat);

Hi,


Thank you very much for answer, but the problem is that I can’t update all the fields.

I modify the TOC programmatically so if I update all the fields I will erase my modifications.

My problem is that the method com.aspose.words.Document.save(OutputStream stream, int saveFormat) work when I save in DOCX and not in PDF. In PDF, my modifications on the TOC are not saved.

Same as when I want to delete a bookmark after tocField.update();. In DOCX the bookmark is deleted and not in PDF.

Best Regards,

Patrice ANTOINE

Hi Patrice,


Thanks for sharing the details. I have managed to reproduce the bookmark (TocBookmark) issue at my side. I have logged this issue as WORDSNET-7480 in our issue tracking system. I have linked this forum thread to the same issue and you will be notified via this forum thread once this issue is resolved.

Patrice ANTOINE:
My problem is that the method com.aspose.words.Document.save(OutputStream stream, int saveFormat) work when I save in DOCX and not in PDF. In PDF, my modifications on the TOC are not saved.
Could you please attach your input Word document along with code here for testing? I will investigate the issue on my side and provide you more information.

We apologize for your inconvenience.

Hi Patrice,


Thanks for your patience. I have received response from our development team about this issue WORDSNET-7480. Please call Document.updatePageLayout method before saving your final document, this will fix the bookmark (TocBookmark) issue in output PDF file.

Alternatively, you may use bookmark without text in your input document. This way there is no need to remove the bookmark text and call of Document.updatePageLayout method.

Please let us know if you have any more queries.

Hi,

Thank you for your support, the "Document.updatePageLayout" method fixes my problem (The document is rendered correctly in PDF).

Thanks for your time!


Best Regards,
Patrice ANTOINE

Hi Patrice,


Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.