Deleting Blank Line

Hi,
I’m using Aspose.Word for Java.
I have 2 documents, where the second document is appended directly to the end of the first one.
After the append, there is a blank line between the two, that I need to get rid of. Is there a way to position the cursor at this line and delete it?
I used SectionStart.CONTINUOUS to append the doc. I need the transition to be seamless, because both docs contain tables, and there should be a single table with no breaks/gaps after the append.
In other words, if there was a way to press the Del key at the current line (programmatically), this would solve my problem, and join the tables from the two docs seamlessly.
Thanks

Hi

Thanks for your request. Could you please attach your source document, your code, output document and expected output here for testing? I will investigate the issue and provide you more information.
Best regards.

Hi Alexey, thanks for the reply.
I have attached two documents: Doc1 and Doc2. I would like to append Doc2 directly to the end of Doc1. They both contain tables, and the result should be a single table, with Doc2 as the next row. No gaps, no blank lines.
Until today, I was using the Evaluation version, which inserted the line “Evaluation Version” at the top of each document. This broke up the flow of the single table.
To get a better idea of what the table would look like, I tried artificially removing this line by replacing with a blank:

doc.getRange().replace("Evaluation Only. Created with Aspose.Words. Copyright 2003-2008 Aspose Pty Ltd.", "", false, false);

This created a blank line between the tables that I need to remove. Thanks for your help.
P.S.: As mentioned in my other thread (“30-Day License”), I’m having problems applying the license file. So, is there a workaround I can use to achieve my goal of joining the tables from the docs seamlessly, without the license file? Thanks again.

Hi

Thank you for additional information. I think in this case you can use InsertDocument method. Please see the following link for more information:
https://docs.aspose.com/words/java/insert-and-append-documents/
Here is code example:

// Open documents
Document doc1 = new Document(@"Test182\doc1.doc");
Document doc2 = new Document(@"Test182\doc2.doc");
// Get table from the first document
Table tab = doc1.FirstSection.Body.Tables[0];
if (tab != null)
{
    // Insert another docuemnt into the main document
    InsertDocument(tab, doc2);
}
// Save output document
doc1.Save(@"Test182\out.doc");

The output document produced on my side is also attached.
Hope this helps.
Best regards.

Thanks for the reply. Can you show me how to get rid of the enhanced new Java for-loops in insertDocument()? My version of Java doesn’t support this collection loop. Thanks.

Hi

Thanks for your inquiry. Here is changed insertDocuemnt method.

///
/// Inserts content of the external document after the specified node.
/// Section breaks and section formatting of the inserted document are ignored.
///
/// Node in the destination document after which the content
/// should be inserted. This node should be a block level node (paragraph or table).
/// The document to insert.
private static void InsertDocument1(Node insertAfterNode, Document srcDoc) throws Exception
{
    // Make sure that the node is either a pargraph or table.
    if ((insertAfterNode.getNodeType() != NodeType.PARAGRAPH) &
        (insertAfterNode.getNodeType() != NodeType.TABLE))
        throw new Exception("The destination node should be either a paragraph or table.");
    // We will be inserting into the parent of the destination paragraph.
    CompositeNode dstStory = insertAfterNode.getParentNode();
    // This object will be translating styles and lists during the import.
    NodeImporter importer = new NodeImporter(srcDoc, insertAfterNode.getDocument(), ImportFormatMode.KEEP_SOURCE_FORMATTING);
    // Loop through all sections in the source document.
    for (int sectIdx = 0; sectIdx <srcDoc.getSections().getCount(); sectIdx++)
    {
        Section srcSection = srcDoc.getSections().get(sectIdx);
        // Loop through all block level nodes (paragraphs and tables) in the body of the section.
        for (int nodeIdx = 0; nodeIdx <srcSection.getBody().getChildNodes().getCount(); nodeIdx++)
        {
            Node srcNode = srcSection.getBody().getChildNodes().get(nodeIdx);
            // Let's skip the node if it is a last empty paragarph in a section.
            if (srcNode.getNodeType() == NodeType.PARAGRAPH)
            {
                Paragraph para = (Paragraph) srcNode;
                if (para.isEndOfSection() && !para.hasChildNodes())
                    continue;
            }
            // This creates a clone of the node, suitable for insertion into the destination document.
            Node newNode = importer.importNode(srcNode, true);
            // Insert new node after the reference node.
            dstStory.insertAfter(newNode, insertAfterNode);
            insertAfterNode = newNode;
        }
    }
}

Hope this helps.
Best regards.

Thank you, I appreciate your responses. However, there’s still a problem.
I get the red line “Evaluation Version” stuck between the two documents. And when I try to remove it manually, e.g.

doc2.getRange().replace("Evaluation Only. Created with Aspose.Words. Copyright 2003-2008 Aspose Pty Ltd.", "", false, false);

– I get a blank line, still.
I’ve attached the results from the operation you told me to use: doc3_eval.doc and doc3_NoEval.doc. In both of these, I use insertDocument() as instructed. But neither one is what I need. The first one has the evaluation line between the tables, and the second one has a blank line after I remove that string.
The problem seems to be the Evaluation line–your method would probably work if it wasn’t there.
Thanks

Hi

Thanks for your request. Did you resolve the problem with evaluation license? If no, please try requesting evaluation license one more time, but to another e-mail account. In addition, I think, you can ask our sales team to send the evaluation license to your e-mail in zip archive. In this case, the license file should not be corrupted.
Best regards.

Ok - everything works great. Thanks so much for your replies! The tables get appended to each other seamlessly, just as I need. (I got the license.)
But I need to ask you something else: A few of my tables need page breaks after them. For example, if we use Doc1 and Doc2 as previously, I tried your code, with a Page Break Insert statement, to insert a page break between the two tables, yet the result is still a seamless table:

Table tab = doc1.getFirstSection().getBody().getTables().get(0);

DocumentBuilder dbld = new DocumentBuilder(doc1);
dbld.insertBreak(BreakType.PAGE_BREAK);
InsertDocument(tab, doc2);

Here, I’m again using InsertDocument() from before. But it seems to ignore the page break. The result is the same as your output attachment when you were helping me with seamless tables.
So, in this particular case, what can I do? Most tables are seamlessly appended, but some (let’s say just Table #10) need a page break after them. Thanks again!!

Hi

Thanks for your request. In the code I provided you earlier, I inserted sub document right after the table in the main document. You can insert sub document after any node in your document. If you need to insert sub document at the end of the main document, just insert it after the last paragraphs of the main document.
In your case you can use code like the following:

DocumentBuilder dbld = new DocumentBuilder(doc1);
dbld.insertBreak(BreakType.PAGE_BREAK);
InsertDocument(dbld.getCurrentParagraph(), doc2);

Best regards.

After this page break, am I in a new section? So, if I need to continue seamless tables after this special case, will this no longer work?

table1 = mainDoc.getFirstSection().getBody().getTables().get(0)

and do I have to do
table1 = mainDoc.getSections().get(1).getBody().getTables().get(0)?
Thanks so much

Hi

Thanks for your request. Page Break is not a section break. But after the page break you will have one more table, you need to get the last table in the section I think:

table1 = mainDoc.getFirstSection().getBody().getTables().get(mainDoc.getFirstSection().getBody().getTables().getCount() - 1);

Hope this helps.
Best regards.

Thanks. Still having difficulties. The page break gets ignored and the subsequent tables get inserted within previous ones (maybe some offset issue).
Is it possible to insert a page break after a given table, rather than before a given table?
Also, is there a way to move to the end of the page, or the end of a section?

Hi

Thanks for your inquiry. Could you please attach here set of your documents and show me how the expected result looks? You can create the expected document in MS Word, just to demonstrate what you would like to achieve. I will check your documents and created sample code.
Best regards.

Thanks a lot. I’ve attached several documents.
On the first page of a document, I need:

  1. Doc1 (top table)
  2. Sequential rows of Doc2, attached seamlessly
  3. If I hit an X-th (say 7th, in this example) row of Doc2, then, after this 7th table (or before the 8th), I need to insert Doc3 (a single bottom table) and then a page break. Any other sequential rows of Doc2 must continue on the next page, after the page break.
  4. Sequential rows of Doc2 continue on the next page.

Notes: Doc2 is the same template used for all of these middle sections, it’s just that the data may be different there. Doc3 (a single bottom table) occurs only on the 1st page.
The attached file result.doc shows how this would look after everything was done. We would get Doc1 + 7 rows of Doc2 + Doc3 + PAGE BREAK, then any remaining Doc2’s.
Thanks for your help

Hi

Thank you for additional information. Here is the sample code. Also, see the attached documents.

public static void main(String[] args) throws Exception
{
    // Open all documents
    Document doc1 = new Document("C:\\Temp\\doc1.doc");
    Document doc2 = new Document("C:\\Temp\\doc2.doc");
    Document doc3 = new Document("C:\\Temp\\doc3.doc");
    // Create DocumentBuilder object, which will help us to manipulate Doc1
    DocumentBuilder builder = new DocumentBuilder(doc1);
    // Sinece we will work only with tabels, we do not need to copy whole documents in to the main document
    // That is why we get tables from our documents and will work with them
    Table mainTable = doc1.getFirstSection().getBody().getTables().get(0);
    // We should import tables from sub docuemnt to be able to insert them into the main document
    Table tab1 = (Table) doc1.importNode(doc2.getFirstSection().getBody().getTables().get(0), true);
    Table tab2 = (Table) doc1.importNode(doc3.getFirstSection().getBody().getTables().get(0), true);
    // Insert table 1 into teh main table 7 times
    for (int i = 0; i <7; i++)
        appendTable(mainTable, (Table) tab1.deepClone(true));
    // Insett table 2
    appendTable(mainTable, (Table) tab2.deepClone(true));
    // Now we should insert page break and start new table
    // move docuemtn Builder cursor to the end of docuemnt and insert page break
    builder.moveToDocumentEnd();
    builder.insertBreak(BreakType.PAGE_BREAK);
    // Start new table
    mainTable = builder.startTable();
    // Insert table 1 5 times
    for (int i = 0; i <5; i++)
        appendTable(mainTable, (Table) tab1.deepClone(true));
    // Save output document
    doc1.save("C:\\Temp\\out.doc");
}

/**
* Append one table to another
*/
private static void appendTable(Table dstTable, Table srcTable) throws Exception
{
    for (int i = 0; i <srcTable.getRows().getCount(); i++)
        dstTable.appendChild(srcTable.getRows().get(i));
}

Hope this helps.
Best regards.

Thanks for this reply, which you gave me some time ago.
However, although it works with very simple docs, it doesn’t work with more complicated tables. I’ve attached a new set of 3 files, each containing a table which needs to be appended to the end of the previous table, just as you did in your example.
However, when I apply the same code to this new set of 3 files, some rows of the last table (in File 3) get squashed and lost. Could you take a look? I’ve also attached the output which shows what I mean, Result.doc.
My suspicion is that some tables, even though they look like a single table, actually consist of several subnodes or subsections that aren’t visible, so your example of appendTable() fails. Maybe it only expects one row per table?
My goal is to have an all-purpose appendTable() which always works and doesn’t squash anything, regardless of the complexity of the table which it’s appending. I need a general-purpose, consistent solution.
Thanks a lot

Hi

Thank you for additional information. You are right; there was mistake in my code. Please use the following code:

/**
* Append one table to another
*/
private static void appendTable(Table dstTable, Table srcTable) throws Exception
{
    while (srcTable.hasChildNodes())
        dstTable.appendChild(srcTable.getFirstRow());
}

Best regards.