本文介绍了代码从C#.net中的openoffice文件(.odt)生成PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protected void btnGeneratePDF_Click(object sender, EventArgs e)
{
        Document document = new Document(PageSize.A4, 80, 50, 30, 65);
        StringBuilder strData = new StringBuilder(string.Empty);
       
        try
        {
            string strHTMLpath = @"D:\Projects\PP.odt";
            string strPDFpath = @"D:\Projects\MyPDF4.pdf";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            MemoryStream ms = new MemoryStream();
            StreamWriter strWriter = new StreamWriter(strHTMLpath, true, Encoding.Default);
            strWriter.WriteLine(htw.InnerWriter.ToString());
            strWriter.Close();
            strWriter.Dispose();
            iTextSharp.text.html.simpleparser.StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
            //styles.LoadTagStyle("ol", "leading", "16,0");

            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(strPDFpath, FileMode.Create));

            document.Open();
            ArrayList objects = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StreamReader(strHTMLpath, Encoding.Default), styles);

            //objects = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StreamReader(strHTMLpath, Encoding.Default), styles);
            for (int k = 0; k < objects.Count; k++)
            {
                document.Add((IElement)objects[k]);
            }

            document.Close();
            //Response.Write(Server.MapPath(strPDFpath));
            Response.Write(strPDFpath);
            Response.ClearContent();
            Response.ClearHeaders();
            Response.ContentType = "application/pdf";
            Response.AddHeader("Content-Disposition", "attachment; filename=MyPDF.pdf");// + strPDFpath);
            //Response.ContentType = "application/octet-stream";
            Response.WriteFile(strPDFpath);
            Response.Flush();
            Response.Close();
            writer.Close();
        }
        catch (Exception ex)
        {
            document.Close();
            throw (ex);
        }
}


Pdf已生成,但由于已损坏,.odt(openoffice文件)也已损坏.感谢您的帮助.


Pdf is generated but as a corrupted and .odt(openoffice file) is also getting corrupted. Any help is appreciated.

推荐答案


这篇关于代码从C#.net中的openoffice文件(.odt)生成PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 21:50