1
votes

this is the scenario I have a Word document ( .docx ) which i want to convert in a template, saving it as a "XML Word 2003" file. Inside the document content, i put a custom tag named {MY_CONTENT} which will be replaced with HTML code.

When I generate a word document like this, it didn't open properly. This is the HTML code i'm trying to insert into the Word Xml document:

<div style='text-align: center;'>
<b><font size='3'>Contract</font></b>
</div>
<div>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin quis justo elementum, 
vehicula ante vel, facilisis ante. Aenean dolor lectus, cursus in mattis sed, ullamcorper ut quam. 
Quisque fringilla erat sit amet blandit euismod. Integer nec odio vel erat molestie fringilla. 
Aliquam tempor ac urna vitae convallis. Praesent mattis massa eget lectus mattis, 
non imperdiet ipsum suscipit. Phasellus gravida eros turpis, et faucibus libero gravida  
non. 
Aliquam ultricies nisl eget magna tincidunt tincidunt. Proin feugiat interdum nibh nec rutrum. 
In hac habitasse platea dictumst. Etiam ac condimentum nisl, et volutpat mauris. 
Mauris erat dui, aliquam ut urna vel, euismod placerat est.
<font size='3'></font>
</div>

I tried to "htmlencode" the html code above, but still the document is not opened property.

If it was possible to "traslate" a HTML piece of code into Word xml tags, i think it could be resolved.

... or is there a way to display the html code into the word xml document, without converting it or applying sorcery ?

Thanx in advance.

1
Well… Why? What's the point of trying to convert beautiful HTML into a 20-years-out-of-date format? - bjb568
Do you need to insert the HTML before someone opens the document, and will they open it in Word? If you need to do that, it's either "put the WordML for an INCLUDETEXT field in your XML, put your HTML in a separate file that will be included by that field, and get the user to update the field", or "use Word OOXML format instead and put the HTML in an Altchunk" - you can probably do that for the single-file OOXML format if you can work out how to encode the Altchunk. - user1379931
First of all, thanks for your kind replies. bjb568 :it's my client's requirement. What more can i do ...? LOL bibadia: i'll take a look at the INCLUDETEXT approach, it sounds "buildable". If OK, i'll post the code Thanks in advance, best regards and sorry for my English :) - Javier Campo

1 Answers

0
votes

Thanx for your kind comments. I stored them in my PKDB ( personal knowledge database ) :) for further uses.

Finally, I decided to use ITEXTSHARP library to generate the document, because it gives me the tools to insert HTML code without formatting isues. I inserted an image template as background, wrote the HTML code, and that's all. Hope this piece of code to be useful for any person

This is the code

string pathImagenPlantilla = Server.MapPath(<path_of_the_image_template>);

//generar el pdf al vuelo
string filenamePDF = "Your_Document_Name.pdf";
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=" + filenamePDF);
Response.Cache.SetCacheability(HttpCacheability.NoCache);

//load the image template
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(pathImagenPlantilla);

//define your HTML code
StringBuilder strTextoHTML = new StringBuilder();
strTextoHTML.Append("<html>");
strTextoHTML.Append("<body>");
strTextoHTML.Append(<your HTML CODE right here>);
strTextoHTML.Append("</body>");
strTextoHTML.Append("</html>");

// margins, in milimeters
float margenIzquierdo = 25;
float margenDerecho = 25 ;
float margenSuperior = 25 ;
float margenInferior = 10 ;
Document pdfDoc = new Document(PageSize.A4, margenIzquierdo, margenDerecho, margenSuperior, margenInferior);

//Adjust the size of image template , to the screen size
float pageWidth = pdfDoc.PageSize.Width - (margenIzquierdo + margenDerecho);
float pageHeight = pdfDoc.PageSize.Height - (margenInferior + margenSuperior);
jpg.SetAbsolutePosition(margenIzquierdo, margenInferior);
jpg.ScaleToFit(pageWidth, pageHeight);

//If you want to choose image as background then,
jpg.Alignment = iTextSharp.text.Image.UNDERLYING;

PdfWriter.GetInstance(pdfDoc, Response.OutputStream);

pdfDoc.Open();
pdfDoc.NewPage();

//add image template
pdfDoc.Add(jpg);

//add html code
foreach (IElement E in HTMLWorker.ParseToList(new StringReader(strTextoHTML.ToString()), new StyleSheet()))
{
    pdfDoc.Add(E);
}

//close doc  and display/download
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();

Regards,