Hi,
We have two issues with a word document. The document has a lot of large images in it ((14 large images, each on a seperate page)).
- When we try to sign the word document, Aspose library throws OutOfMemoryException. After trying to sign a few times, it might succeed at some point.
- Then we tried to render the signed word document to PDF file, but it throws below exception:
"System.ApplicationException: Image top or bottom margin is too large 1.65,0 image file:C:\WINDOWS\TEMP\Aspose.Words.5351c6aa-89e5-4f89-8e17-8536b10d2de9.005.png"
Can you please have a look at the two issues here and let us know what could have been the cause of the issue?
Signing process description:
- Client/user post Base64 encoded word document in XML format to Aspose server
- Aspose server receives the xml string and decode it to be be raw bytes and keep it in memeory stream
- Aspose server sign the document - replace a predefined token in word doc with a signature image
Below is the simplified code scriptlets for signing the attached word document, which throws OutOfMemory exception.
xml = "base64 encoded string of the bytes content of the word document to be signed"
XPathNavigator docNav = CreateDocNavigatoe(xml);
protected static XPathNavigator CreateDocNavigatoe(String xml)
{
byte[] bytesXml = ASCIIEncoding.ASCII.GetBytes(xml);
MemoryStream memoryStream = new MemoryStream(bytesXml);
XPathDocument doc = new XPathDocument(memoryStream);
return doc.CreateNavigator();
}
private MemoryStream GetSigningConfirmStream()
{
byte[] base64EncodedDocBytes = Convert.FromBase64String(GetDocumentContent());
return new MemoryStream(base64EncodedDocBytes);
}
private String GetDocumentContent()
{
return GetNodeValue(docNav, _xpath_RequestDetail_Document_Content);
}
private Signature[] GetSignatures(bool isWordDocType)
{
XPathExpression expr = docNav.Compile(_xpath_RequestDetail_Signatures_signature);
XPathNodeIterator iter = docNav.Select(expr);
Signature[] signatures = new Signature[iter.Count];
int i = 0;
while (iter.MoveNext())
{
XPathNavigator cloneNav = iter.Current.Clone();
signatures[i++] = CreateSignatureFromRequest(cloneNav, isWordDocType);
}
return signatures;
}
private Document _asposeDocument; //memory stream of the word document
private bool SignAtToken(Signature[] _signatures, int index)
{
String signatureToken = GetTokenOrBookMark(_signatures, index, SIG_TOKEN_VALUE);
if (!(signatureToken.StartsWith("@") && signatureToken.EndsWith("@")))
return false;
// Get all of the Run nodes in the body (includes tables)
NodeList runNodes = _asposeDocument.FirstSection.Body.SelectNodes("//Run");
bool signed = false;
int iBookMarkPosition = 0;
int latestMatchTokenPosition = -1;
// Check each Run for the token. If found, replace it with signature.
foreach (Run run in runNodes)
{
string text = run.Text;
int iMatchTokenPosition = index;
// Check to see the signature
int iToken = text.IndexOf(signatureToken);
if (iToken != -1)
// if (text.Equals(signatureToken) )
{
DocumentBuilder builder = new DocumentBuilder(_asposeDocument);
// Move to where the token is
builder.MoveTo(run);
// Clear the token (leave the run), so when user resend the request, it will not get signed again.
run.Text = "";
// Start a bookmark to contain the signature token (so we can unsign it later)
// We use the signatureMark here so we can unsign the doc if we need to.
// if we find a new token, then start the book mark position from 0 again,
// So when we unsign it will be easier.
// use latestMatchTokenPosition to flag a new token found here.
if (latestMatchTokenPosition != iMatchTokenPosition)
iBookMarkPosition = 0;
String signatureMark = SIG_TOKEN_BOOKMARK_PREFIX + (iBookMarkPosition++) + GetTokenOrBookMark(_signatures, iMatchTokenPosition, SIG_TOKEN_VALUE);
builder.StartBookmark(signatureMark);
// Insert the number of space before the token if any.
builder.Write(GetSpaceStringBeforeToken(iToken));
// Insert the image in place of the token
builder.InsertImage(_signatures[iMatchTokenPosition].AsImage());
// End the bookmark
builder.EndBookmark(signatureMark);
signed = true;
latestMatchTokenPosition = iMatchTokenPosition;
}
}
return signed;
}
Versions:
Aspose.Words 5.3.0.0
Aspose.Pdf 3.6.2.9
For the second issue, looks like there was a similar issue reported before. I attached a new document 07ML05789_assign_1.3.doc that is causing the same kind of exception "“System.ApplicationException: Image top or bottom margin is too large 1.65,0 image file:C:\WINDOWS\TEMP\Aspose.Words.5351c6aa-89e5-4f89-8e17-8536b10d2de9.005.png”. "
https://forum.aspose.com/t/exception-converting-word-document-to-pdf/123807
Hi
Thank you for additional information.
- I cannot reproduce OutOfMemory exception on my side. I use the latest versions of Aspose.Words (6.0.1) and Aspose.Pdf (3.9.0) for testing.
- Regarding the second problem, I managed to reproduce the problem. However, the error occurs on Aspose.Pdf side. Therefore, I think you should ask this question in Aspose.Pdf forum.
In additional, In the current version of Aspose.Words there are two ways to convert Word document to PDF.
- Direct conversion to PDF using Aspose.Words (without using Aspose.Pdf). See the following link for mor einformation:
https://docs.aspose.com/words/net/convert-a-document-to-pdf/
here is code:
Document doc = new Document(@"Test012\in.doc");
doc.SaveToPdf(@"Test012\aw_out.pdf");
- Another way is using Aspose.Words and Aspose.Pdf (old method):
Best regards.