Adjusting the text box after changing the font style or font size

I am using the below code to resize the text box around the sentences and paragraphs after changing the font style or size but I find sometimes it doesn’t execute well giving shape variable a null, Is there a more precise workaround for this issue.

Shape shape = (Shape)doc.GetChild(NodeType.Shape,0, true);
shape.TextBox.FitShapeToText = true;

@Duminda Could you please attach your input document here for testing? We will check the issue and provide you more information.

1 Like

WorkOrder.docx (485.9 KB)

Here is the file

@Duminda Your document has a lot of shapes and no al of them are textboxes.

You can use the following code to get shapes that are textboxes:

Document doc = new Document(@"C:\Temp\in.docx");

// Get shapes the are texboxes.
List<Shape> textboxes = doc.GetChildNodes(NodeType.Shape, true).Cast<Shape>()
    .Where(s => s.TextBox != null).ToList();
1 Like

With above code block i see there’s an overlapping of content with footer how can i fix this
WorkOrder (15).docx (2.9 MB)

@Duminda The whole document is built with floating objects, such document are very hard to edit in MS Word or programmatically due to the flow nature of MS Word documents. Changing shapes properties might lead to the document content reflowing, that might have unexpected results with document build with floating object, like yours.

Is there a solution for this?

@Duminda I am afraid there is no solution. It is not normally recommended to use absolutely positioned content to build MS Word documents if it is supposed to edit them.

Is there a method to open a html file using aspose word so that content will be absolutely positioned, or any properties tha needs to be added to html file

@Duminda No, Unfortunately, there is no option to import HTML content as absolutely positioned.

Please note, Aspose.Words is designed to work with MS Word documents. HTML documents and MS Word documents object models are quite different and it is not always possible to provide 100% fidelity after conversion one format to another. In most cases Aspose.Words mimics MS Word behavior when importing HTML documents.

1 Like

Original aspose converted doc.docx (2.4 MB)

I am giving this document as input to change the font styles and i’m getting it left aligned as follows
After Applying Arial as font.docx (3.4 MB)

Is there a work around to fix this issue and the code block is

List<Shape> textboxes = doc.GetChildNodes(NodeType.Shape, true).Cast<Shape>()
    .Where(s => s.TextBox != null).ToList();
if (textboxes != null) {
    foreach(var textbox in textboxes)
        textbox.TextBox.FitShapeToText = true;
}

@Duminda The behavior is expected since each line in your document is represented as a shape with only one paragraph:


So after changing the font due to difference in font metrics text becomes narrower And since each line is a separate paragraph in a shape the text is not reflowed.

How was the original document generated? It looks like it is a result of conversion of Fixed Page document (like PDF or XPS) to Word. Or the result of image to text conversion.

Original document is converted from html to docx using aspose.html cloud API

@Duminda Have you tried using Aspose.Words to convert HTML to DOCX? Aspose.Words in most cases behaves the same way as MS Word does when deal with HTML documents.

We initially planned on using aspose.cloud html API to convert html to docx but is there a aspose.cloud word API to convert html to docx and get a better result. or to get a better document after changing fonts of an converted document.

@Duminda Yes, you can use Aspose.Words cloud and on-premises API to convert HTML to Word. Please see Aspose.Words for Cloud documentation for more information:
https://docs.aspose.cloud/words/convert/specified-format/

It would be good if you attach your original HTML document here for testing. We will test conversion on our side.

var wordsApi = new WordsApi(config);
using var requestDocument = File.OpenRead(htmlFilePath);
var convertRequest = new ConvertDocumentRequest(requestDocument, "docx", @"C:\home\site\wwwroot\", "testFile");
var conversion = await wordsApi.ConvertDocument(convertRequest);

The file fails to convert from html to docx, Am i missing something ?

@Duminda Since you have encountered problems with using Aspose.Words Cloud API, it would be better to ask in the dedicated support forum:
https://forum.aspose.cloud/c/words/17
My colleagues from Cloud team will help you shortly.

But anyways it would be better to attach your input HTML document for testing, so we could reproduce the problem on our side.

1 Like

Original afterConversion.docx (9.4 KB)

Changing fonts seems fine
AfterChangingFonts.docx (9.5 KB)

But expected result is
blankpages.zip (2.2 KB)

There must be a space between end of first page and starting of second

@Duminda Unfortunately, it is not quite clear what space between pages you are talking about. If possible could you please provide the expected output in MS Word document form.
You should note, that actually there is no “page” concept in MS Word documents, since they have flow nature, the same as HTML. The consumer applications like MS Word or Open Office layout the document into pages on the fly. So when you change font of the document, it is reflowed accordingly.