Skip to content Skip to sidebar Skip to footer

How To Insert A Piece Of Html Code In Xml Word Document (and Not Die Trying)

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 n

Solution 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

stringpathImagenPlantilla= Server.MapPath(<path_of_the_image_template>);

//generar el pdf al vuelostringfilenamePDF="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.Imagejpg= iTextSharp.text.Image.GetInstance(pathImagenPlantilla);

//define your HTML codeStringBuilderstrTextoHTML=newStringBuilder();
strTextoHTML.Append("<html>");
strTextoHTML.Append("<body>");
strTextoHTML.Append(<your HTML CODE right here>);
strTextoHTML.Append("</body>");
strTextoHTML.Append("</html>");

// margins, in milimetersfloatmargenIzquierdo=25;
floatmargenDerecho=25 ;
floatmargenSuperior=25 ;
floatmargenInferior=10 ;
DocumentpdfDoc=newDocument(PageSize.A4, margenIzquierdo, margenDerecho, margenSuperior, margenInferior);

//Adjust the size of image template , to the screen sizefloatpageWidth= pdfDoc.PageSize.Width - (margenIzquierdo + margenDerecho);
floatpageHeight= 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(newStringReader(strTextoHTML.ToString()), newStyleSheet()))
{
    pdfDoc.Add(E);
}

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

Regards,

Post a Comment for "How To Insert A Piece Of Html Code In Xml Word Document (and Not Die Trying)"