Hey guys,
I’m making a code that replaces text and a base 64 of the document, transforming it into an image and adding it to the doc.
However, when I save in PDF after replacing it does not bring the document with the image, but if I save in DOCX it works perfectly, could you tell me what is wrong in my code?
{
Document doc = new Document(stream);
FindReplaceOptions options = new();
if (highLight)
options.ApplyFont.HighlightColor = Color.Silver;
foreach (var wc in wildCards)
{
if (!wildCardValues.Exists(x => x.WildCard.Equals(wc)))
{
doc.Range.Replace(wc, wc, options);
continue;
}
var value = wildCardValues.Find(x => x.WildCard.Equals(wc));
if (StorageTemplateConstants.TEMPLATE_VALUE_KEEP_BLANK.Equals(value.Field))
{
doc.Range.Replace(wc, string.Empty);
continue;
}
if (StorageTemplateConstants.TEMPLATE_VALUE_NOT_REPLACE.Equals(value.Field))
continue;
if (value.Field.StartsWith("data:image/png;base64"))
{
string strBase64 = value.Field;
string convert = strBase64.Replace("data:image/png;base64,", String.Empty);
byte[] bytes = Convert.FromBase64String(convert);
ReplaceWithImageHandler findElem = new ReplaceWithImageHandler(bytes);
FindReplaceOptions opt = new FindReplaceOptions()
{
ReplacingCallback = findElem,
Direction = FindReplaceDirection.Backward,
};
doc.Range.Replace(wc, String.Empty, opt);
continue;
}
doc.Range.Replace(wc, value.Field, options);
}
MemoryStream ms = new MemoryStream();
doc.Save(ms, saveFormat == TemplateSaveFormat.PDF ? SaveFormat.Pdf : SaveFormat.Docx);
ms.Seek(0, SeekOrigin.Begin);
return ms;
}
@anemam sorry I can’t reproduce your issue. Can you please complete your code or build a simplified version of it and share.
Alternatively you can try calling the method doc.UpdatePageLayout(); before save the document as PDF.
Hello Eduardo, unfortunately the function doc.UpdatePageLayout(); It didn’t solve my problem, here is the attachment of the complete code and two documents, one in DOCX that is working and the one in PDF that is not workingTesteAssinatura (2).pdf (24.2 KB) TesteAssinatura (2).docx (19.6 KB) cod.docx (5.4 KB)
@anemam sorry I can’t reproduce your issue. Are you sure that you meet all the required conditions in order to replace text by image when TemplateSaveFormat.PDF is true. How are you saving the files from MemoryStream to file storage?
This is the code that I use is a simplified version of yours without so many conditionals:
byte[] byteImage;
using (FileStream image = File.OpenRead("C:\\Temp\\img.png"))
{
// You dont need this because your image is actually a byte array
byteImage = ReadFully(image);
}
MemoryStream msPdfOutput = new MemoryStream();
MemoryStream msDocxOutput = new MemoryStream();
using (FileStream fsInput = File.OpenRead("C:\\Temp\\input.docx"))
{
MemoryStream msInput = new MemoryStream(ReadFully(fsInput));
Document doc = new Document(msInput, new Aspose.Words.Loading.LoadOptions { LoadFormat = LoadFormat.Docx });
FindReplaceOptions opt = new FindReplaceOptions()
{
ReplacingCallback = new ReplaceWithImageHandler(byteImage),
Direction = FindReplaceDirection.Backward,
};
doc.Range.Replace("##image", string.Empty, opt);
doc.UpdatePageLayout();
doc.Save(msDocxOutput, SaveFormat.Docx);
doc.Save(msPdfOutput, SaveFormat.Pdf);
}
using (FileStream fileStream = new FileStream("C:\\Temp\\output.pdf", FileMode.Create, FileAccess.Write))
{
msPdfOutput.WriteTo(fileStream);
}
using (FileStream fileStream = new FileStream("C:\\Temp\\output.docx", FileMode.Create, FileAccess.Write))
{
msDocxOutput.WriteTo(fileStream);
}
@anemam the save functionality is the same, in your case you are using a condition. Is like:
if(saveFormat == TemplateSaveFormat.PDF){
doc.Save(ms, SaveFormat.Pdf);
}
else {
doc.Save(ms, SaveFormat.Docx);
}
// The Seek method point the cursor to an specific position of the Stream
ms.Seek(0, SeekOrigin.Begin);
but in your case you a are use the inline form of the condition, I don’t use that condition in my code because I always want to save in both formats to review and compare both results.
I tried to use your format but my document corrupts
Exactly what error message are you receiving, and what operation are you doing when you receive that message.
Additionally can you please attach a sample of your current document input (the base doc, docx, etc. file that you are using as base document).
From the tests I’ve done, I’m starting to think that the problem is not when saving, but when inserting the image in the document, which is not working for the PDF and is working only for the docx.
Because I changed my save to save only in PDF without any conditional and even so the PDF has the problem of not inserting an image correctly.
I’m attaching the template document I’m using, the docx already modified to have the example of how the PDF should look with the cropped image which is my problem.Template.docx (4.9 KB) TesteAssinatura (1).docx (19.6 KB) TesteAssinatura (1).pdf (24.2 KB)
@anemam sorry, I can see the image properly in the PDF file that you posted. I also use your template and your image to test in my local environment and everything is displaying properly. outputAssinatura.docx (14.3 KB) outputAssinatura.pdf (28.1 KB)
I’m using the latest version of Aspose.Words (v23.4.0), but I don’t think that this is the problem since I can see correctly the image in the files that you uploaded.
Make sure that you are viewing your result on a proper monitor. The color of the image is almost white and may blend with the background if your monitor doesn’t offer sufficient contrast and color accuracy. In my case, I can see the image on two of my monitors, but it is completely missed on the third one of lower quality. As an exercise add an image that offer more contrast or set the color of the pages to gain in contrast, for example:
@anemam I can see the issue, and it’s caused by the format of the template file that you are using (assuming that you created that file using Open Office Writer app). This is how I see the same output in Open Office Writer and MS Word. The PDF file that I created looks the same as the document rendered by MS Word, which is the intended behavior with the Aspose.Words API.
Please confirm how you created the Template document so that I can escalate this to our development team. However, since the PDF is rendering the same as the docx document in MS Word, you are actually getting the expected output.
This is exactly the problem, as Libre Office uses the 2007 docx there was this reading problem, now I tested it using Word 365 and both came with the correct image.