I am facing two issues while using the <<doc>> tag as per the documentation on Inserting Documents Dynamically. I’d appreciate your guidance on the following:
1. Error When Using Simple <<doc>> Tag with -sourceStyles and -build Flags
When I try to use the following syntax to preserve child document styles:
I receive the following error during document generation: ERROR: <<doc [DocumentName] Error! Can not get the value of member 'DocumentName' on type 'System.Data.DataRow'. -sourceStyles -build >>
However, when I wrap the same tag inside an <<if>> block like this:
It works as expected and the document is generated correctly.
Questions:
Why does the direct usage of the <<doc>> tag with -sourceStyles and -build throw an error?
Is the <<if>> workaround the recommended approach, or is there a way to use the tag directly?
2. sourceStyles Does Not Preserve Page Layout Size
In a separate case, I observed that when I use the -sourceStyles flag, the layout size of the child document is not preserved.
For example:
My parent document has A4 page size.
The child document referenced by DocumentName has A3 size.
When I generate the final document using: <<doc [DocumentName] -sourceStyles -build >>
The inserted content appears on A4 instead of preserving its original A3 layout.
Question:
Is there a way to preserve the child document’s page layout size (e.g., A3) when inserting into a parent document with a different size using -sourceStyles?
There are other layout related issues like unexpected line breaks and title spacing for document, is there any way to handle those as well?
Issues with <<doc>> Tag and -sourceStyles Behavior in Aspose.Words
1. Error with <<doc>> Tag Using -sourceStyles and -build
The error you are encountering when using the <<doc>> tag directly with the -sourceStyles and -build flags is likely due to the way the Aspose.Words engine processes these tags. When you use the tag directly, it attempts to access the specified document name from the data source, and if it cannot find it or if the context is not set up correctly, it throws an error.
The workaround you found, wrapping the <<doc>> tag inside an <<if>> block, effectively prevents the engine from trying to access the document until the condition is evaluated. This can help avoid the error because the <<if>> block can control when the document is accessed, allowing for a more stable context.
Recommendation: While the <<if>> workaround is functional, it is not the only way to handle this situation. You might want to ensure that the data context is correctly set up before using the <<doc>> tag directly. If the context is correct and the error persists, using the <<if>> block is a valid approach.
2. Preserving Page Layout Size with -sourceStyles
Regarding the issue of the child document’s layout size not being preserved when using the -sourceStyles flag, this behavior is expected. The -sourceStyles flag is designed to preserve styles but does not automatically adjust the page size of the inserted document to match its original layout.
To preserve the child document’s page layout size (e.g., A3) when inserting it into a parent document with a different size (e.g., A4), you may need to manually adjust the page settings after the insertion. Unfortunately, there is no built-in flag to automatically handle this scenario.
Handling Layout Issues: For unexpected line breaks and title spacing issues, you may need to review the styles applied in both the parent and child documents. Ensuring that styles are consistent and that any specific formatting is applied correctly can help mitigate these layout-related issues.
Summary
The error with the <<doc>> tag can be avoided by using an <<if>> block or ensuring the data context is correctly set.
The -sourceStyles flag does not preserve the original page layout size; manual adjustments may be necessary after insertion.
Consistency in styles between documents can help address layout issues.
If you have further questions or need additional assistance, feel free to ask!
There is no need in any workaround involving if tags to use a doc tag with any of its switches. Most likely, the issue is caused by a mismatch in data and a template. The provided exception message indicates this. Please check a corresponding template and data, and correct any mismatches. If this does not help, then please provide your sample template, data, and code, so we could reproduce the issue on our end.
Unfortunately, this is out of scope of what sourceStyles is intended for. More information on what and how it actually affects can be found in remarks related to KeepSourceFormatting at ImportFormatMode Enum.
I am afraid, without requested details, we cannot reproduce the issue and hence, we cannot add anything new. The exception message alone indicates a mismatch between a template and data.
As a note, there is no need to upload the original template and data here. It is enough to provide any sample template and data reproducing the observed behavior (in addition to sample code).
Regarding KeepSourceFormatting, there is no need to use it as it works similarly to sourceStyles. That is why, it was mentioned, so you could explore document insertion limitations in Aspose.Words using the provided article. The same limitations also apply to sourceStyles. To recap, it affects only document styles and has nothing to do with page layout.