Export images from Textbox to Word document


#1

Hi,

I am exporting data from textbox to word document bookmarks.

Data is exported properly. Only image is not get scaling properly as per word document.

My Code:

        SetLicencse();
        string Dir = Server.MapPath("~") + "Documents\\";
        string fileName = Dir + "RC Note Word Template.doc";            

        // Load the template document.
        Document doc = new Document(fileName);
        DocumentBuilder builder = new DocumentBuilder(doc);       


         builder.MoveToBookmark("CurrentMinutes", false, true);
         builder.InsertHtml(txtCurrentMinutes.Text);         

        string filePath = Server.MapPath("~") + "\\Documents\\Export\\ExportFileName.docx";
        doc.Save(filePath);

Please check the exported word file .

JSW Steel Limited_RCN.zip (385.3 KB)


#2

@pravinghadge

Thanks for your inquiry. Please ZIP and attach your input Word document and HTML here for testing. We will investigate the issue on our side and provide you more information.


#3

Hi Tahir,

Please find the attached code file

Code - Copy.zip (3.1 MB)

My Code is written in RCNController.cs.
In that there is ExportRCN Method.


#4

@pravinghadge

We need the HTML for testing. Please ZIP and attach the HTML that you are using for this case.


#5

Hi tahir.manzoor,

Thanks for your reply.

Please find attached file

CurrentMinutes.zip (341.6 KB)


#6

@pravinghadge

Please use the following code example to resize the big size images in output document.

Document doc = new Document(MyDir + "RC Note Word Template.doc");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToBookmark("CurrentMinutes", false, true);
builder.InsertHtml(File.ReadAllText(MyDir + "CurrentMinutes.html"));
foreach (Shape shape in doc.GetChildNodes(NodeType.Shape, true))
{
    ResizeLargeImage(shape);
}
doc.Save(MyDir + "out.docx");

public static void ResizeLargeImage(Shape image)
{
    // Return if this shape is not an image.
    if (!image.HasImage)
        return;

    // Calculate the free space based on an inline or floating image. If inline we must take the page margins into account.
    PageSetup ps = image.ParentParagraph.ParentSection.PageSetup;
    double freePageWidth = image.IsInline ? ps.PageWidth - ps.LeftMargin - ps.RightMargin : ps.PageWidth;
    double freePageHeight = image.IsInline ? ps.PageHeight - ps.TopMargin - ps.BottomMargin : ps.PageHeight;


    ImageSize size = image.ImageData.ImageSize;
    Boolean exceedsMaxPageSize = size.WidthPoints > freePageWidth || size.HeightPoints > freePageHeight
        || image.Width > freePageWidth || image.Height > freePageHeight;

    if (exceedsMaxPageSize)
    {
        // Calculate the ratio to fit the page size based on which side is longer.
        Boolean widthLonger = (size.WidthPoints > size.HeightPoints);
        double ratio = widthLonger ? freePageWidth / size.WidthPoints : freePageHeight / size.HeightPoints;

        if (ratio > .90)
            ratio = ratio - .10;
        // Set the new size.
        image.Width = size.WidthPoints * .50;
        image.Height = size.HeightPoints * .50;
    }
}

#7

Thanks you so much tahir.manzoor,

Great solution!!!

It is working for images perfectly.

I need same for HTML tables also.

Can you please look into it

Please find the attached html zip fileTable.zip (3.2 KB)


#8

@pravinghadge

Please use the following code snippet to set the table’s width.

foreach (Table table in doc.GetChildNodes(NodeType.Table, true))
{
    table.PreferredWidth = PreferredWidth.FromPercent(100);
}

#9

Thanks for your reply

Autoresize code for images is not working attached html.

Table.zip (101.6 KB)


#10

@pravinghadge

Please use the following modified method to achieve your requirement.

public static void ResizeLargeImage(Shape image)
{
    // Return if this shape is not an image.
    if (!image.HasImage)
        return;

    // Calculate the free space based on an inline or floating image. If inline we must take the page margins into account.
    PageSetup ps = image.ParentParagraph.ParentSection.PageSetup;
    double freePageWidth = image.IsInline ? ps.PageWidth - ps.LeftMargin - ps.RightMargin : ps.PageWidth;
    double freePageHeight = image.IsInline ? ps.PageHeight - ps.TopMargin - ps.BottomMargin : ps.PageHeight;


    ImageSize size = image.ImageData.ImageSize;
    Boolean exceedsMaxPageSize = size.WidthPoints > freePageWidth || size.HeightPoints > freePageHeight
        || image.Width > freePageWidth || image.Height > freePageHeight;

    if (exceedsMaxPageSize)
    {
        // Set the new size.
        image.AspectRatioLocked = true;
        image.Width = ps.PageWidth - ps.LeftMargin - ps.RightMargin;
    }
}

#11

Hi,

It is working fine now.
Thanks