Как мне преобразовать панель Asp.Net в PDF, используя iTextSharp или любую другую библиотеку? - PullRequest
0 голосов
/ 12 апреля 2019

Я пытаюсь распечатать квитанцию ​​в своем проекте веб-приложения, используя itextsharp, и я не получаю ни ответа на нажатие кнопки, ни пустого PDF-файла, когда пытаюсь преобразовать его в xml.

1stфункция использует itextsharp и выдает ошибку ниже: ошибка говорит о том, что htmlworker устарел1007 * 1-я функция выдает ошибку ниже

private void exportpdf()
        {
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=OrderInvoice.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            Panel1.RenderControl(hw);
            StringReader sr = new StringReader(sw.ToString());
            Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
            PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
            pdfDoc.Open();
#pragma warning disable CS0612 // Type or member is obsolete
            HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
#pragma warning restore CS0612 // Type or member is obsolete
            htmlparser.Parse(sr);
            pdfDoc.Close();
            Response.Write(pdfDoc);
            Response.End();
        }

2-я функция пустая pdf

        protected void exportpdf()
        {
            using (StringWriter sw = new StringWriter())
            {
                using (HtmlTextWriter hw = new HtmlTextWriter(sw))
                {
                    Panel2.RenderControl(hw);
                    Response.ContentType = "application/pdf";
                    Response.AddHeader("content-disposition", "attachment;filename=OrderInvoice.pdf");
                    Response.Cache.SetCacheability(HttpCacheability.NoCache);
                    StringReader sr = new StringReader(sw.ToString());
                    Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
                    PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
                    pdfDoc.Open();
                    //                 XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
                    pdfDoc.Close();
                    Response.Write(pdfDoc);
                    Response.End();
                }
            }
        }

Вывод:


Server Error in '/' Application.
The document has no pages.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.IO.IOException: The document has no pages.

Source Error: 


Line 49: #pragma warning restore CS0612 // Type or member is obsolete
Line 50:             htmlparser.Parse(sr);
Line 51:             pdfDoc.Close();
Line 52:             Response.Write(pdfDoc);
Line 53:             Response.End();

PharmaStore\PlacedSuccessfully.aspx.cs    Line: 51 

Stack Trace: 


[IOException: The document has no pages.]
   iTextSharp.text.pdf.PdfPages.WritePageTree() +1042
   iTextSharp.text.pdf.PdfWriter.Close() +285
   iTextSharp.text.pdf.PdfDocument.Close() +318
   iTextSharp.text.Document.Close() +110
   PharmaStore.PlacedSuccessfully.exportpdf() in source\repos\Pharmastore\PharmaStore\PlacedSuccessfully.aspx.cs:51
   PharmaStore.PlacedSuccessfully.Button1_Click(Object sender, EventArgs e) in source\repos\Pharmastore\PharmaStore\PlacedSuccessfully.aspx.cs:32
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +109
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +108
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +31
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3466

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.7.3282.0
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...