Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法将HTML表导出为具有精确格式的pdf_C#_Asp.net_Itext_Export To Pdf - Fatal编程技术网

C# 无法将HTML表导出为具有精确格式的pdf

C# 无法将HTML表导出为具有精确格式的pdf,c#,asp.net,itext,export-to-pdf,C#,Asp.net,Itext,Export To Pdf,您好,我想在asp.net中将一个html表格导出为pdf格式,格式与html表格完全相同(意思是html表格上应用了样式)。我的代码是 string html = Request.Form["Table_HTML"]; StringReader sr = new StringReader(html); Document pdfDoc = new Document(PageSize.A4.Rotate(), 2f, 2f, 2f, 2f);

您好,我想在asp.net中将一个html表格导出为pdf格式,格式与html表格完全相同(意思是html表格上应用了样式)。我的代码是

string html = Request.Form["Table_HTML"];
StringReader sr = new StringReader(html);
                Document pdfDoc = new Document(PageSize.A4.Rotate(), 2f, 2f, 2f, 2f);
                HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
                PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
                pdfDoc.Open();  
                htmlparser.Parse(sr);
                pdfDoc.Close();
                Response.ContentType = "application/pdf";
                Response.AddHeader("content-disposition", "attachment;filename=Production_Report.pdf");
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Response.Write(pdfDoc);
                Response.End();

    <div id="dvTable"> 
    <table style="border:1px solid red;">
    <tr><td colspan="2" style="border:1px solid red;"></td><td>Hours</td><td>Studies</td><td>Hrs/Studies</td></tr>
    <tr><td rowspan="3">APAC</td><td>Scripting</td><td><%=APAC_h_scri%></td><td><%=APAC_s_scri%></td><td><%=APAC_a_scri %></td></tr>
    <tr><td>DP</td><td><%=APAC_h_DP%></td><td><%=APAC_s_DP%></td><td><%=APAC_a_DP %></td></tr>
    <tr><td>Tabs</td><td><%=APAC_h_Tab%></td><td><%=APAC_s_Tab%></td><td><%=APAC_a_Tab%></td></tr>
    </table>
    </div>
string html=Request.Form[“Table_html”];
StringReader sr=新的StringReader(html);
Document pdfDoc=新文档(PageSize.A4.Rotate(),2f,2f,2f,2f);
HTMLWorker htmlparser=新的HTMLWorker(pdfDoc);
PdfWriter writer=PdfWriter.GetInstance(pdfDoc,Response.OutputStream);
pdfDoc.Open();
解析(sr);
pdfDoc.Close();
Response.ContentType=“application/pdf”;
AddHeader(“内容处置”、“附件;文件名=Production_Report.pdf”);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
响应写入(pdfDoc);
Response.End();
小时研究小时/研究
公寓预订
数据处理
标签
但我无法导出具有格式的表。 我的桌子看起来像

以pdf格式导出后,它看起来像


我的问题是,我需要和html表格中的格式相同,至少是表格的边框

我在其中一个项目中遇到过同样的问题

使用此逻辑对html文档进行适当的对齐

        var pagesize = new iTextSharp.text.Rectangle(iTextSharp.text.PageSize.A4);

        //Set Background color of pdf    
        pagesize.BackgroundColor = iTextSharp.text.BaseColor.WHITE;

        var left_margin = 25;
        var top_margin = 25;
        var bottom_margin = 25;

        iTextSharp.text.Document doc = new iTextSharp.text.Document(pagesize, left_margin, 10, top_margin, bottom_margin);

        string filePath = "D:\\myTask\\";
        string tempFileName = "tempXmlToWebPdf.pdf";
        string finalFileName = "XmlToWebPdf.pdf";

        string tempFilePath = filePath + tempFileName;
        string finalFilePath = filePath + finalFileName;


        if (!Directory.Exists(filePath))
        {
            Directory.CreateDirectory(filePath);
        }

        try
        {

            PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(tempFilePath, FileMode.Create));

            iTextSharp.text.Font mainFont = new iTextSharp.text.Font();
            iTextSharp.text.Font boldFont = new iTextSharp.text.Font();

            mainFont = FontFactory.GetFont("Arial", 10, iTextSharp.text.Font.NORMAL, new iTextSharp.text.BaseColor(System.Drawing.ColorTranslator.FromHtml("#000000")));
            boldFont = FontFactory.GetFont("Arial", 12, iTextSharp.text.Font.BOLD);

            Paragraph para = new Paragraph();

            // Read in the contents of the Receipt.htm file...
            string contents = System.IO.File.ReadAllText(Server.MapPath("~/Design/Template.html"));

            // Replace the placeholders with the user-specified text
            contents = contents.Replace("@FirstName", Model.FirstName);
            contents = contents.Replace("@CourseName", Model.CourseName);
            contents = contents.Replace("@Score", Convert.ToString(Model.Score));
            string date = DateTime.Now.Day + "/" + DateTime.Now.Month + "/" + DateTime.Now.Year;
            contents = contents.Replace("@Date", date);
            contents = contents.Replace("@Path1", Server.MapPath("~/Content/Frontendcss/assets/Icon/elearning-logo.png").ToString());
            contents = contents.Replace("@Path2", Server.MapPath("~/Content/Frontendcss/assets/images/x.jpg").ToString());


            doc.Open();

            var titleFont = FontFactory.GetFont("Arial", 18, Font.BOLD);
            var subTitleFont = FontFactory.GetFont("Arial", 14, Font.BOLD);
            var boldTableFont = FontFactory.GetFont("Arial", 12, Font.BOLD);
            var endingMessageFont = FontFactory.GetFont("Arial", 10, Font.ITALIC);
            var bodyFont = FontFactory.GetFont("Arial", 12, Font.NORMAL);

            var parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(contents), null);

            // Enumerate the elements, adding each one to the Document...
            foreach (var htmlElement in parsedHtmlElements)
                doc.Add(htmlElement as IElement);

            //var logo = iTextSharp.text.Image.GetInstance(Server.MapPath("~/Content/Frontendcss/assets/Icon/elearning-logo.png"));
            //logo.SetAbsolutePosition(20, 20);
            //doc.Add(logo);

            //doc.Add(new Paragraph("Northwind Traders Receipt", titleFont));


            //var orderInfoTable = new PdfPTable(2);
            //orderInfoTable.HorizontalAlignment = 0;
            //orderInfoTable.SpacingBefore = 10;
            //orderInfoTable.SpacingAfter = 10;
            //orderInfoTable.DefaultCell.Border = 0;
            //orderInfoTable.SetWidths(new int[] { 1, 4 });

            //orderInfoTable.AddCell(new Phrase("Order:", boldTableFont));
            //orderInfoTable.AddCell(Model.CourseName);
            //orderInfoTable.AddCell(new Phrase("Price:", boldTableFont));
            //orderInfoTable.AddCell(Convert.ToString(Model.Score));

            //doc.Add(orderInfoTable);

            doc.Close();

        }
        catch (Exception Ex)
        {
            throw Ex;
        }


        Response.AddHeader("Content-Disposition", "attachment;filename=eLearningCertificate.pdf");
        Response.ContentType = "application/pdf";
        Response.TransmitFile(tempFilePath);
        Response.End();

我相信这会对你有用

看看这个,请不要使用
HTMLWorker
。改为使用
XMLWorker
。hi代码工作正常。我通过你的代码得到的结果与我通过我的代码得到的结果相同。但我的问题是,当我导出pdf时,我得到了一个没有格式的表格,正如我在图片中提到的。我需要在导出的表格中设置pdf格式,就像html表格一样。是的,我知道你的问题。您可以将HTML文件存储在单独的文件夹中。应用css给出正确的对齐,然后运行我提供的代码。Sukhvindra,你得到答案了吗?我也在寻找同样的