How to check whether document is empty or not

@awais.hafeez,

Hello,
I am facing one new issue. I have one input file where I have added the bookmark the way you suggested above.
I have different docx files which has the same name of the bookmark in input file. Now I have to read that different files one by one, get the bookmark of similar name of file. Read the content from the file and add it inside the bookmark of input file.
I have start token of bookmark, one blank line (paragraph) and then the end bookmark token like,
[StartBookmark]

[/EndBookmark]
I want to add the content at that blank line ( paragraph) however it’s get added the inside the bookmark like,
[StartBookmark]
// Add content
[/EndBookmark]
Could you please have a look and comment on it.
I have shared you the all input file, expected output file, output I am getting and the other files from which I have to add the data into input files. Code snippet is there but it’s not the compiled as I have some different api other than java and to understand the logic I have kept it as it is.
There is one output.png file I have taken the screenshot of output.docx file which will give the more idea of issue.
File.zip (308.9 KB)

Thanks

@vke3,

If I understand you correctly, removing those empty paragraphs will resolve this issue. And you can use this code to detect empty paragraphs: para.toString(SaveFormat.TEXT).trim().equals(""). Please let me know if I can be of any further assistance.

@awais.hafeez,

Which empty paragraph?
[StartBookmark]
**** empty paragraph?
[/EndBookmark]

Is this the same empty paragraph which I * above?
If yes then I can’t remove that.
I have to add the content at that same empty paragraph and keep the bookmark also as it is.

@vke3,

Please check the following simple code to remove unwanted/empty paragraph against “RTE_Update_EffectiveDate_ContentData.docx”.

Document srcdoc = new Document("E:\\File\\RTE_Update_EffectiveDate_ContentData.docx");
Document doc = new Document("E:\\File\\Input File.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
Paragraph para = doc.FirstSection.Body.Paragraphs[1];
builder.MoveTo(para);
builder.InsertDocument(srcdoc, ImportFormatMode.KeepSourceFormatting);
builder.CurrentParagraph.Remove();
doc.Save("E:\\File\\19.3.docx");

@awais.hafeez,

Hello,
Well I don’t want to remove the paragraph from “RTE_Update_EffectiveDate_ContentData.docx”.
I want to add the content of “RTE_Update_EffectiveDate_ContentData.docx” to “Input File.docx”.

If you see the “Input File.docx”, it has hidden tokens <RTE_Update_EffectiveDate_ContentData>. This is the start of my bookmark. after that start token there is one empty line and after the empty line there is another hidden token </RTE_Update_EffectiveDate_ContentData> which is the end of bookmark.

So if you see the format of multi-line bookmark is like,
<RTE_Update_EffectiveDate_ContentData> -----> Start of bookmark
------> Empty line/paragraph
</RTE_Update_EffectiveDate_ContentData> ------> End of bookmark.

Now whatever content “RTE_Update_EffectiveDate_ContentData.docx”. file has, should get added into input file of respective bookmark. Consider my “RTE_Update_EffectiveDate_ContentData.docx” file has content HELLO WORLD. So expected output should be like,

<RTE_Update_EffectiveDate_ContentData>
HELLO WORLD
</RTE_Update_EffectiveDate_ContentData>

Currently I have shared one code snippet which gives me following result,
<RTE_Update_EffectiveDate_ContentData>

HELLO WORLD </RTE_Update_EffectiveDate_ContentData>

So could you please share the details why this is happening and what will be changes in code to get expected result. you can refer the shared code snippet to check what I have implemented.
Thank you for your continues support.

@vke3,

Please note that the DocumentBuilder.InsertDocument method mimics the MS Word behavior, as if CTRL+‘A’ (select all content) was pressed, then CTRL+‘C’ (copy selected into the buffer) inside one document and then CTRL+‘V’ (insert content from the buffer) inside another document. You can do the following steps to verify this:

  • Copy all content of ‘RTE_Update_EffectiveDate_ContentData.docx’ by using MS Word
  • Open ‘Input File.docx’ with MS Word
  • Move cursor to the first empty Paragraph location where you want to paste the copied content
  • Paste the content
  • Now, notice that even MS Word leaves that empty Paragraph as is

You need to write some logic to get rid of such empty Paragraphs like the one I shared in my previous post.