Free Support Forum - aspose.com

Convert part of a word document to an image or file?

Hi I want to know if there is a simple way of converting one part of a word file ( in my case a table) to an image or pdf?


The only way I can think of doing this is by cloning a table putting it into a empty aspose.words document then saving it? Would this way work or is the a simple method?




Hi Matthew,


Thanks for your inquiry. Yes, your understanding is correct. You can extract node(s) into empty document and convert it to Image. Please let me know if I can be of any further assistance.

Best regards,
Hello I have done this and technically works but currently what I get is a image with the table on top which is correct but a lot of white space below which is the rest of the page.

Ideally I would have a image that just contains just the table and all the white space is ignored. I assume this can't be done?
Hi Matthew,

Thanks for your inquiry. You can get idea from the following code to meet this requirement (See attachments).

Document doc = new Document(MyDir + @"input.docx");

Table tab = doc.FirstSection.Body.Tables[0];

Paragraph before = new Paragraph(doc);

before.ParagraphBreakFont.Size = 1;

before.ParagraphFormat.SpaceBefore = 0;

before.ParagraphFormat.SpaceAfter = 0;

Paragraph after = new Paragraph(doc);

after.ParagraphBreakFont.Size = 1;

after.ParagraphFormat.SpaceBefore = 0;

after.ParagraphFormat.SpaceAfter = 0;

tab.ParentNode.InsertBefore(before, tab);

tab.ParentNode.InsertAfter(after, tab);

LayoutCollector collector = new LayoutCollector(doc);

LayoutEnumerator enumerator = new LayoutEnumerator(doc);

enumerator.Current = collector.GetEntity(before);

RectangleF rectBefore = enumerator.Rectangle;

enumerator.Current = collector.GetEntity(after);

RectangleF rectAfter = enumerator.Rectangle;

Shape txtBox = new Shape(doc, ShapeType.TextBox);

txtBox.AppendChild(tab);

txtBox.Top = 0;

txtBox.Left = 0;

txtBox.Width = doc.FirstSection.PageSetup.PageWidth - doc.FirstSection.PageSetup.LeftMargin - doc.FirstSection.PageSetup.RightMargin;

txtBox.Height = rectAfter.Top - rectBefore.Top;

before.AppendChild(txtBox);

doc.UpdatePageLayout();

ShapeRenderer renderer = txtBox.GetShapeRenderer();

renderer.Save(MyDir + "tab.png", new ImageSaveOptions(SaveFormat.Png));


Hope, this helps.

Best regards,