问题
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:
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,
来源:https://stackoverflow.com/questions/22798403/how-to-insert-a-piece-of-html-code-in-xml-word-document-and-not-die-trying