I have found a number of posts in this forum on this subject but can’t find a suitable workaround or fix.
I am using Aspose.Words to generate a Word document which is then converted to PDF using Aspose.PDF. In creating the Word document we load a ‘template’ from an existing Word document and then insert data at various points using mail merge field or bookmark replacement with Aspose.Words. So far so good.
Part of that data may be an image URL. If the image URL is present we generate an image in the Word document. The image is generated and positioned at the start of our text replacement code (i.e. not at a bookmark or mail merge filed). If present the image must appear top-right. The resulting Word document is correctly formatted.
However when converting to PDF the text appears in front of the image and I cannot prevent this from occurring.
The following code inserts the image into the Word document.
private void WriteImage(DocumentBuilder builder)
{
string imageUrl = GetImageUrl();
if(imageUrl != string.Empty)
{
byte[] image = GetImage(imageUrl);
Shape shape = builder.InsertImage(image);
shape.WrapType = WrapType.None;
shape.BehindText = false;
shape.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
shape.RelativeVerticalPosition = RelativeVerticalPosition.Page;
shape.Left = builder.PageSetup.PageWidth - shape.Width - builder.PageSetup.RightMargin;
shape.Top = builder.PageSetup.TopMargin + VERTICAL_IMAGE_OFFSET;
}
}
Can anyone suggest a solution to my problem as this is a real showstopper for me. The PDFs are to be automatically published to a live web site.
Sorry, the line shape.WrapType = WrapType.None; should read shape.WrapType = WrapType.Square;
Hi,
Thank you for considering Aspose.
Can you please send us your sample Word document so that we can pin point the cause of this issue.
Thanks.
Adeel Ahmad
Support Developer
Aspose Changsha Team
Please find sample document attached but I really don’t think it will help.
We do something like this:
// Base class method
private DocumentBuilder GetBuilder()
{
// Loads an existing Word document
Document documentTemplate = GetWordTemplate();
// Does replaceement on mail merge fields
ReplacePlaceholders(documentTemplate, GetFieldNames(), GetFieldValues());
// Gets a DocumentBuilder and does bookmark replacement
DocumentBuilder builder = new DocumentBuilder(documentTemplate);
DoCustomReplacement(builder);
return builder;
}
// Subclass method
public override void DoCustomReplacement(DocumentBuilder builder)
{
// Code for this method in previous post
WriteImage(builder);
builder.MoveToBookmark(ORGANISATIONS_BOOKMARK_NAME);
WriteLines(builder, this.organisations);
builder.MoveToBookmark(REGIONS_BOOKMARK_NAME);
WriteRegions(builder, this.regions);
builder.MoveToBookmark(CONTACTS_BOOKMARK_NAME);
WriteContacts(builder, this.contacts);
}
Later in the flow of things the builder is used to generate a PDF:
private void WritePdf(DocumentBuilder builder, string filePathXml, string filePathPdf)
{
builder.Document.Save(filePathXml, SaveFormat.AsposePdf);
Aspose.Pdf.Pdf pdf = new Aspose.Pdf.Pdf();
pdf.BindXML(filePathXml, null);
pdf.IsImagesInXmlDeleteNeeded = true;
pdf.Save(filePathPdf);
}
There is no placeholder in the Word document for the image because it may or may not be required in the document.
Hi,
I didn’t see your attachment. Please attach again.
Thanks.
Adeel Ahmad
Support Developer
Aspose Changsha Team
Here it is.
Hi,
This is a template word document. Can you please send us the final Word Document. Please save the word document with the code and then send it to us:
builder.Document.Save(DOCfilePath);
Thanks.
Adeel Ahmad
Support Developer
Aspose Changsha Team
I have attached the final Word document. Due to confidentiality I have removed some content from the header and footer but the remainder of the document is intact.
Hi,
We have checked your document and found that The “Square” wrapping style for the picture is not supported in Aspose.Pdf. As a workaround you can set the wrapping style to “Behind Text” and set the text’s right margin. The modified document is attached.
Thanks.
Adeel Ahmad
Support Developer
Aspose Changsha Team
I was afraid this was going to be your suggestion as it is the approach that has been suggested in other posts in this forum. The problem I have is that the images, if included in the document at all, are not a fixed size and are added to the document at the discretion of the end user. This is also true of the surrounding text, which can be of any length (and therefore height in the document).
Are you suggesting I set builder.ParagraphFormat.RightIndent to a suitable value? If so, how do I know when to set the RightIndent back to the original value (to allow for a wrap around the image) given that the image can be any height? I am new to your API and would appreciate any suggestions.
I must admit I am rather concerned about all of this. I would expect, having purchased Aspose.Words to generate Word documents and Aspose.PDF to render them as PDF, that Aspose.PDF would be able to render Word documents generated by its sister API. It is beginning to look like we have wasted our money in purchasing the PDF license.
Hi,
Sorry for inconvenience. I will discuss this with the developers and we will let you know soon.
Thanks for your patience.
Adeel Ahmad
Support Developer
Aspose Changsha Team
If your text and images are not fixed size, we can’t provide a solution. This is the limitation of Aspose.Pdf. We are working on this issue but we can’t support it in short time. You can only use inline image like the attacment. Sorry for the inconvenience.