Replace function ( InsertValueAtReplaceHandler) replaces the whole line of document

@tahir.manzoor,

Thanks with the latest version page break and white space issue gets resolved.
Still one problem I am facing here. If you check my code(InsertValueAtReplaceHandler), for File token where I am directly inserting a file to doc using DocumentBuilder.insertDocument. I face the indentation issue(though Used ImportFormatMode.USE_DESTINATION_STYLES /ImportFormatMode.KEEP_SOURCE_FORMATTING)The inserted text
won’t come under header. Please find the attached screen shot of output file.
Indentation Issue.JPG (158.6 KB)

This issue is due to left margin. the template margin is left and text in template is at hanging margin.The text from another document get insert at the left margin of document and the heading are at hanging margin of the scale. Is there any solution to insert text from another document at the hanging margin using DocumentBuilder.insertDocument method?

@vke3,

Thanks for your inquiry. Please share your document that you want to insert in your input document along with Java code example to reproduce this issue at our end. Unfortunately, it is difficult to say what the problem is with document and code example. As soon as you get these pieces of information to us we’ll start our investigation into your issue.

@tahir.manzoor,

Thanks. Please check the attached template. You can check the left indent of template. and as I mention the above the same shared code you can check (InsertValueAtReplaceHandler.java, line number 46-52). Left indentation causing the issue. When we insert the text using inserbuilder, it insert the text at the start of margin not under the header (like summary).

Template.zip (63.3 KB)

@vke3,

Thanks for sharing the document. Unfortunately, you have not shared the document that you are inserting into template document e.g. RTE_Summary.docx and RTE_EffectiveDate.docx.

We have created test document (RTE_EffectiveDate.docx) and tested the scenario using latest version of Aspose.Words for Java 18.6. We have not found the shared issue. We have attached the test document and output PDF with this post for your kind reference. Docs.zip (39.8 KB)

If you still face problem, please share RTE_Summary.docx and RTE_EffectiveDate.docx documents here for testing.

@tahir.manzoor,

Sorry I forgot to shared the other documents which I am merging. sharing you the same ( template and other documents). You can check the left indentation due to which causing this issue. I have executed the code and sharing the output file too.
you can see the indentation issue under Resume and Date header.
Template.zip (163.0 KB)

@vke3,

Thanks for sharing the detail. Please note that Aspose.Words mimics the behavior of MS Word. If you perform the same scenario using MS Word, you will get the same output. However, you can achieve your requirement using Aspose.Words. In this case, we suggest you following solution.

  1. Get the left indent of paragraph where you want to insert document e.g. value1.
  2. Iterate through all paragraphs of document that you want to insert and set paragraphs’ left indent to value1.
  3. Insert the document.

Please check the following code snippet. Hope this helps you.

Document doc = new Document(inputStream);
doc.getFirstSection().getPageSetup().setPaperSize(builder.getCurrentParagraph().getParentSection().getPageSetup().getPaperSize());
NodeCollection paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);
for (Paragraph  paragraph : (Iterable<Paragraph>) paragraphs)
{
    if(paragraph.isListItem())
        paragraph.getParagraphFormat().setLeftIndent(paragraph.getParagraphFormat().getLeftIndent() + builder.getCurrentParagraph().getParagraphFormat().getLeftIndent());
    else
        paragraph.getParagraphFormat().setLeftIndent(builder.getCurrentParagraph().getParagraphFormat().getLeftIndent());
}

builder.insertDocument(doc, ImportFormatMode.USE_DESTINATION_STYLES);

@tahir.manzoor,

Thank you for your support. One more thing when I tried remove the section using below code,
for (int i = doc.getSections().getCount() - 2; i >= 0; i–)
{
doc.getLastSection().prependContent(doc.getSections().get(i));
doc.getSections().get(i).remove();
}

it removes the header image of my document. margin a document adds a extra pagebreak. I have used the compatibility options too as mentioned above and working on latest ASPOSE version 18.6. what would be the reason of this issue?
I tried to remove page breaks using below solution,
NodeCollection paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);

// Iterate through all paragraphs
for (Paragraph para : (Iterable<Paragraph>) paragraphs) {
    // If the paragraph has a page break before set then clear it.
    if (para.getParagraphFormat().getPageBreakBefore())
        para.getParagraphFormat().setPageBreakBefore(false);

    // Check all runs in the paragraph for page breaks and remove them.
    for (Run run : (Iterable<Run>) para.getRuns()) {
        if (run.getText().contains(ControlChar.PAGE_BREAK))
            run.setText(run.getText().replace(ControlChar.PAGE_BREAK, ""));
    }
}

but still the problem is there.

@tahir.manzoor,

Hi, at least I am able to find the root cause of this issue. If you see the template the first page header and footer is different and second page is different.
When I replace the tokens which are on first page, the generated docx file took the header of the first page on new page. However I want the header and footer of second page to all the pages getting added( in between page 1 and 2 of the template). is there any solution for this?

@vke3,

Thanks for your inquiry. We suggest you please read about node classes from here:
Node Classes

HeaderFooter is a section-level node and can only be a child of Section. There can only be one HeaderFooter or each HeaderFooterType in a Section. We suggest you please read following article.
Delete or change a header or footer

The PageSetup.DifferentFirstPageHeaderFooter property returns true if a different header or footer is used on the first page. In your template document, there are two sections with headers and footers. Both sections have different header and footer for first page. You can enable/disable this property according to your requirement in both sections.

In your case, we suggest you please remove child nodes of desired HeaderFooter and add the content of HeaderFooter from second section into it. Following method copies the header footer of one section into another. Hope this answers your query.

If you still face problem, please share your expected output Word document. We will then provide you more information on it along with code.

public static void CopyHeaderFooter(Section srcSection, Section dstSection) throws Exception
{
    //Remove existing header/footer
    dstSection.getHeadersFooters().clear();

    for (HeaderFooter headersFooter : srcSection.getHeadersFooters())
    {
        // There is no header of the specified type in the current section, create it.
        HeaderFooter header = new HeaderFooter(dstSection.getDocument(), headersFooter.getHeaderFooterType());
        dstSection.getHeadersFooters().add(header);

        //Copy nodes from source document to destination
        for (Node srcNode : (Iterable<Node>)headersFooter.getChildNodes())
        {
            Node dstNode = dstSection.getDocument().importNode(srcNode, true, ImportFormatMode.KEEP_SOURCE_FORMATTING);
            header.appendChild(dstNode);
        }
    }
}

@tahir.manzoor,

I changed the template ( changed the header and margin) and it works properly.
Thank you so much for your support :slight_smile:
One more thing I observed when I add a check box in my RTE_Summary.docx it’s not appearing on my generated PDF file but it’s there in generated docx file (generating the docx and pdf file).
I din’t find any proper solution on portal. Is there any solution for this?
Please find the attached documents where I have added RTE_Summary.docx (input file), generated PDF screenshot and generated Docx file. Template I have already shared.
Documents.zip (184.3 KB)

The other issue is when I some comment after changing the content in doc and generate PDF with track changes, then I am getting the changes and comments. And same operation I perform by without track changes then I didn’t see the changes in doc but comments are visible in generated PDF. And I don’t want those comments in PDF. How to handle the comments in ASPOSE?
Thanks in advance

@vke3,

Thanks for your inquiry.

We have tested the scenario using latest version of Aspose.Words for Java 18.7 and have not found the shared issue. Please use Aspose.Words for Java 18.7. We have attached the output PDF with this post for your kind reference. RTE_Summary-18.7.pdf (45.2 KB)

Please call Document.UpdatePageLayout method before saving document to PDF. If you still face problem, please ZIP and attach your input Word document and code example here for testing. We will investigate the issue and provide you more information on it.

@tahir.manzoor,

I tried with latest jar but still problem persist :frowning:
The unchecked box are display in proper manner, problem is only with checked box. After doing some analysis, one thing I notice for checked ticked box it’s using wingdings as font family and for unchecked box ( which is working fine) its using MS Gothic font family. I am attaching the HTML code of RTE_Summary.docx file.
we have linux OS, office 10 and firefox 42.0.2 on our system.
I run the html code for checkbox on windows 10 machine and it’s working fine but the same code is not working on linux (Please check attached RTE_Summary_HTML_Code.html file to check HTML code).
what else can be used instead of wingdings?
for track changes, I have already added document.UpdatePageLayout method before saving it to PDF still I face the same issue. For without track change I have added doc.acceptAllRevisions() method. to clear the comment I used comments.clear() which clears the track changes also. I want track changes but not the comments after pdf generation.Please find the attached WithoutTrackchange.png file where it shows comments in generated pdf.
attaching the HTML code for checkbox, the track change and without track change screenshots.
Documents.zip (254.9 KB)

@vke3,

Thanks for your inquiry. Please note that Aspose.Words requires TrueType fonts when rendering document to fixed-page formats (JPEG, PNG, PDF or XPS). You need to install fonts e.g. MS Gothic that are used in your document on the machine where you’re converting documents to PDF. Please refer to the following articles:

How Aspose.Words Uses True Type Fonts
How to Install True Type Fonts on Linux

@tahir.manzoor,

Thank you for your support :slight_smile:
For track changes, I have already added document.UpdatePageLayout method before saving it to PDF still I face the same issue. For without track change I have added doc.acceptAllRevisions() method. to clear the comment I used comments.clear() which clears the track changes also. I want track changes but not the comments after pdf generation.Please find the attached WithoutTrackchange.png file where it shows comments in generated pdf.

@tahir.manzoor,

Well track changes part is done now. solved the issue. Installing the True Fonts on Linux solve the PDF issue with latest jar. only now the HTML view has the same font issue.
Thank you for your continuous support :+1:

@vke3,

Thanks for your inquiry. It is nice to hear from you that your issue with PDF has been solved. We have opened the shared HTML at our end and have not found any issue with checkbox. The HTML issue is also related to missing fonts.

@tahir.manzoor,

Yes, when I saved it as HTML it’s working fine.
In my case I am displaying converted HTML view on browser, where I found that it’s a browser specific issue. The Firefox won’t recognize the wingdings font where as IE does.
Thank you for the support.