Q: I am using a blank document as a template to which further documents are appended. After executing my code the first page of the generated document is a blank page.
A: Even a blank document is not actually completely empty. For a minimal document to be valid it must have at least one section which contains a body, which in turn contains at least one paragraph. Along with this information it is useful to remember that by default the first section of a document is set to start on a new page.
Due to the template document already containing one section it should become clear that the blank page appearing at the start of the document is actually the result of the next imported section appearing on a new page.
To remedy this, the document needs to be fully empty before anything is appended to it. The Document.RemoveAllChildren method is called to remove all sections from the document. This is demonstrated in the code below.
Example
Shows how to remove all content from a document before using it as a base to append documents to.
[Java]
// Use a blank document as the destination document.
Document dstDoc = new Document();
Document srcDoc = new Document(gDataDir + "TestFile.Source.doc");
// The destination document is not actually empty which often causes a blank page to appear before the appended document
// This is due to the base document having an empty section and the new document being started on the next page.
// Remove all content from the destination document before appending.
dstDoc.removeAllChildren();
dstDoc.appendDocument(srcDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
dstDoc.save(gDataDir + "TestFile.BaseDocument Out.doc");
Q: After appending my document to another document the line spacing between some paragraphs in the resulting document has changed and is affecting the layout of the document.
A: Occasionally documents do not define default values for formatting related options, such as font size or line spacing. This lack of default settings can cause line spacing to change when importing content from a different document.
A combination of these missing attributes can lead to this issue happening:
· The source document does not define any default line spacing.
· Paragraphs or the styles used in the source document do not explicitly specify any line spacing.
· The destination document has different default line spacing from what is specified on the paragraphs or styles affected as described above.
When these attributes are missing in the documents to be joined then the line spacing in the imported content will most likely change. This is because the source document is missing the options that should be defined for line spacing and therefore the imported content instead assumes the line spacing from the destination document. When the destination document uses a visibly different default line spacing compared to the source document then the line spacing changes from what it was in the source document and pushes content out of place.
Currently there is no available work around that can be used to avoid this issue in Aspose.Words. The property which stores the default line spacing does exist within the document model but has not been made public. There are plans to make this property public in the future.
You can however ensure this does not happen by ensuring that the document at least has a document-wide default line spacing specified. The steps below show how to set the default formatting for the document and the same general technique is used to set formatting defaults for styles and for paragraphs.
To set the default paragraph formatting for the document:
1. Open the paragraph formatting options as shown in the screenshot below. If you are using Word 2003 then this can be found in the Format menu under Paragraph.
2. Reset the spacing options to appropriate values and press “Default” to set these as default for the document.
The same technique can used to change default formatting for specific paragraph or styles. To set these settings for certain paragraphs you can choose to press “OK instead of “Default”. This will reset these settings for only the highlighted paragraphs.
To set these options for a style the same steps can be taken but the paragraph settings under the appropriate style should be opened instead.
3. A message dialog will appear. This will prompt you to choose if you want to use these settings for the template that is used in the document as well.
Choose “No” unless you want these changes to be made to the template attached to this document as well. If you find a number of documents created using this template exhibit this problem then it might be a good idea to choose “Yes”.
However, if you have no control over the documents being joined or are unable to manually do this for all documents then there is one further technique that may be used. If this issue does not occur when the source document is appended to the destination document instead and there is also no requirement to use destination styles then the work around described below can be used.
Instead of appending the source document to the destination document, the destination document can instead be prepended to the beginning of the source document. This will result in the same joined document but the source document is the one which content is imported into instead of the destination document. If this is used instead for any affected documents then this should avoid the line spacing issue.
The code below shows how to prepend a document to another. The code is based off the AppendDocument method as described at the beginning of this article and is called in the same way. The document produced will result in the same output but this time there should be no change in line spacing.
Example
Shows how to manually prepend the content from one document to the beginning of another document.
[Java]
public static void prependDocumentMain() throws Exception
{
Document dstDoc = new Document(gDataDir + "TestFile.Destination.doc");
Document srcDoc = new Document(gDataDir + "TestFile.Source.doc");
// Append the source document to the destination document. This causes the result to have line spacing problems.
dstDoc.appendDocument(srcDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
// Instead prepend the content of the destination document to the start of the source document.
// This results in the same joined document but with no line spacing issues.
prependDocument(srcDoc, dstDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
}
/**
* A modified version of the AppendDocument method which prepends the content of one document to the start
* of another.
*
* @param dstDoc The destination document where to prepend the source document to.
* @param srcDoc The source document.
*/
public static void prependDocument(Document dstDoc, Document srcDoc, int mode) throws Exception
{
// Loop through all sections in the source document.
// Section nodes are immediate children of the Document node so we can just enumerate the Document.
ArrayList sections = (ArrayList)Arrays.asList(srcDoc.getSections().toArray());
// Reverse the order of the sections so they are prepended to start of the destination document in the correct order.
Collections.reverse(sections);
for (Section srcSection : (Iterable<Section>) sections)
{
// Import the nodes from the source document.
Node dstSection = dstDoc.importNode(srcSection, true, mode);
// Now the new section node can be prepended to the destination document.
// Note how PrependChild is used instead of AppendChild. This is the only line changed compared
// to the original method.
dstDoc.prependChild(dstSection);
}
}
Q: I am joining two documents together and specifying settings so the joined documents will run together by using the SectionStart.Continuous setting. The appended document does not appear on the same page instead it still appears on a separate page.
A: The most likely cause is almost always because of a difference in PageSetup settings for the sections where the documents are joined together. Sections can be set as continuous to appear on the same page only if the settings which define the page structure are identical. For instance, a section which has larger PageSetup.PageWidth and PageSetup.PageHeight properties than what is found in the other document cannot be joined continuously. Even a very small difference in page size will cause the content to not flow continuously. For this reason the page sizes and orientation should be set exactly identical between documents to be joined. In this example below the two documents were to be joined continuously but as you can visibly see the two documents have different page sizes and therefore the source document must start on a new page.
In order to solve this, the first section of the source document should have the appropriate PageSetup properties made identical to the last section of the destination document. This is because these are the two sections that will be joined and therefore to appear on the same page they must have the same settings. The code below shows how to do this. In some instances other properties may need to be changed as well in order for page
Example
Shows how to append a document to another document continuously which has different page settings.
[Java]
Document dstDoc = new Document(gDataDir + "TestFile.Destination.doc");
Document srcDoc = new Document(gDataDir + "TestFile.SourcePageSetup.doc");
// Set the source document to continue straight after the end of the destination document.
// If some page setup settings are different then this may not work and the source document will appear
// on a new page.
srcDoc.getFirstSection().getPageSetup().setSectionStart(SectionStart.CONTINUOUS);
// To ensure this does not happen when the source document has different page setup settings make sure the
// settings are identical between the last section of the destination document.
// If there are further continuous sections that follow on in the source document then this will need to be
// repeated for those sections as well.
srcDoc.getFirstSection().getPageSetup().setPageWidth(dstDoc.getLastSection().getPageSetup().getPageWidth());
srcDoc.getFirstSection().getPageSetup().setPageHeight(dstDoc.getLastSection().getPageSetup().getPageHeight());
srcDoc.getFirstSection().getPageSetup().setOrientation(dstDoc.getLastSection().getPageSetup().getOrientation());
dstDoc.appendDocument(srcDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
dstDoc.save(gDataDir + "TestFile.DifferentPageSetup Out.doc");
This should often only need to be done for the first section of the source document although if there a multiple continuous sections from the first section in the source document then this will need to be repeated for these sections as well.
Q: When rendering a document (to PDF, XPS or Image etc) the document which is appended does not appear in the rendered output.
A: This is most likely because you have previously rendered the document or have called Document.UpdatePageLayout before appending the document. When either of these methods is called the document is laid out into pages and is stored into memory. If any content is inserted after this action then it will be inserted into the Document Object Model but the layout of the pages will not be rebuilt to include this change until the page layout is updated again.
To solve this you will need to either remove the calls before appending the document if they are unneeded or call Document.UpdatePageLayout again after appending document. This technique is demonstrated in the code below.
Example
Shows how to rebuild the document layout after appending further content.
[Java]
Document dstDoc = new Document(gDataDir + "TestFile.Destination.doc");
Document srcDoc = new Document(gDataDir + "TestFile.Source.doc");
// If the destination document is rendered to PDF, image etc or UpdatePageLayout is called before the source document
// is appended then any changes made after will not be reflected in the rendered output.
dstDoc.updatePageLayout();
// Join the documents.
dstDoc.appendDocument(srcDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
// For the changes to be updated to rendered output, UpdatePageLayout must be called again.
// If not called again the appended document will not appear in the output of the next rendering.
dstDoc.updatePageLayout();
// Save the joined document to PDF.
dstDoc.save(gDataDir + "TestFile.UpdatePageLayout Out.pdf");