iTextSharp aspx到pdf,Server.Execute错误

时间:2013-10-07 09:30:08

标签: c# asp.net css pdf itextsharp

在ASP.net中,我制作了一个用于填写报价单的网页。 有3个div。左侧div有TextBoxes填写。 中间层是形式本身。 右边的div有按钮。

到目前为止,我已经只能将midlesector导出为PDF,并且我在左边填充了所有内容。问题是它完全忽略了使用绝对定位制作的CSS。因此,我想问一下如何包含CSS样式,使其看起来应该是一个连续文本的方式。

这是中间人的代码:

<div id="middlesector" runat="server">
<%--><asp:GridView ID="GridView1" runat="server"></asp:GridView>--%>
<asp:Image ID="TextArea1" runat="server" ImageUrl="Images\VisserGroup.png"></asp:Image>
<label id="textarea2" runat="server" cols="20" name="S2"></label>
<label id="textarea3" runat="server" cols="20" name="S3">Visser International Trade & Engineering</label>
<label id="textarea4" runat="server" cols="20" name="S4">T.a.v</label>
<label id="textarea5" runat="server" cols="20" name="S5">Postbus</label>
<label id="textarea6" runat="server" name="S6"></label>
<label id="textarea7" runat="server" cols="20" name="S7"></label>
<label id="textarea8" runat="server" cols="20" name="S8"></label>
<label id="textarea9" runat="server" name="S9"></label>
<label id="textarea10" runat="server" cols="20" name="S10"></label>
<label id="textarea11" runat="server" cols="20" name="S11"></label>
<label id="textarea12" runat="server" cols="20" name="S12">-OFFERTE-</label>
<label id="textarea13" runat="server" name="S13">Geachte</label>
<label id="textarea14" runat="server" name="S14"></label>
<label id="textarea15" runat="server" cols="20" name="S15">Met referte aan het aangename onderhoud hebben wij het genoegen u bijgaand onze offerte te doen toekomen voor het leveren van:</label>
<label id="textarea16" runat="server" cols="20" name="S16"></label>
<label id="TextArea17" runat="server" cols="20" name="S17">Wij danken u voor deze aanvraag en hopen u hiermee een passend voorstel te hebben gedaan. Voor het bespreken van verdere details en toelichting op de offerte zullen wij op korte termijn contact met u opnemen. Indien u direct uw eventuele vragen aan ons wenst voor te leggen dan horen wij dit graag van u.</label>
<label id="TextArea18" runat="server" cols="20" name="S18">Wij vertrouwen u hiermee voldoende van dienst te zijn en houden ons van harte aanbevolen voor uw gewaardeerde opdracht.</label>
<label id="TextArea19" runat="server" cols="20" name="S19">Met vriendelijke groet,Visser Supplies B.V.</label>
<label id="TextArea20" runat="server" cols="20" name="S20">Personal info</label>
<label id="TextArea21" runat="server" cols="20" name="S21">Website          : www.vissersupplies.nl</label>
<label id="TextArea22" runat="server" cols="20" name="S22">E-mail           : personal info</label>
<label id="TextArea23" runat="server" cols="20" name="S23">Telefoonnummer   : personal info 9894</label>
<label id="TextArea24" runat="server" cols="20" name="S24">Copyright Visser Supplies B.V.:</label>
<label id="TextArea25" runat="server" cols="20" name="S25">Deze offerte en alle andere informatie mogen alleen door geadresseerde gebruikt worden. Overdracht aan derden, in welke vorm dan ook,  is slechts toegestaan na schriftelijk toestemming van Visser Supplies B.V.</label>
</div>

这是CSS:

#middlesector {
        position:absolute; left: 150px; top: 60px;
    }
#TextArea1 {
        height: 250px;
        width:  640px;
        position: absolute; top:-40px;
        border:none;
        scrollbar-3dlight-color: #fff;
        scrollbar-arrow-color: #fff;
        scrollbar-base-color: #fff;
    }

   #textarea2 {
       border-style: none;
       border-color: inherit;
       border-width: medium;
       height: 105px;
       width: 320px;
       position:absolute; top:220px;
       scrollbar-3dlight-color: #fff;
       scrollbar-arrow-color: #fff;
       scrollbar-base-color: #fff;
   }

   #textarea3 {
       border-style: none;
       border-color: inherit;
       border-width: medium;
       height: 30px;
       width: 310px;
       position:absolute; top:215px;
       left: 410px;
       scrollbar-3dlight-color: #fff;
       scrollbar-arrow-color: #fff;
       scrollbar-base-color: #fff;
   }

   #textarea4 {
       border-style: none;
       border-color: inherit;
       border-width: medium;
       height: 15px;
       width: 310px;
       position:absolute; top:250px;
       left: 410px;
       scrollbar-3dlight-color: #fff;
       scrollbar-arrow-color: #fff;
       scrollbar-base-color: #fff;
   }

   #textarea5 {
       border-style: none;
       border-color: inherit;
       border-width: medium;
       height: 15px;
       width: 310px;
       position:absolute; top:270px;
       left: 410px;
       scrollbar-3dlight-color: #fff;
       scrollbar-arrow-color: #fff;
       scrollbar-base-color: #fff;
   }

   #textarea6 {
       border-style: none;
       border-color: inherit;
       border-width: medium;
       height: 15px;
       width: 100px;
       position:absolute; top:290px;
       left: 410px;
       scrollbar-3dlight-color: #fff;
       scrollbar-arrow-color: #fff;
       scrollbar-base-color: #fff;
   }

   #textarea7 {
       border-style: none;
       border-color: inherit;
       border-width: medium;
       height: 15px;
       width: 310px;
       position:absolute; top:310px;
       left: 410px;
       scrollbar-3dlight-color: #fff;
       scrollbar-arrow-color: #fff;
       scrollbar-base-color: #fff;
   }

   #textarea8 {
       border-style: none;
       border-color: inherit;
       border-width: medium;
       height: 15px;
       width: 210px;
       position:absolute; top:290px;
       left: 480px;
       scrollbar-3dlight-color: #fff;
       scrollbar-arrow-color: #fff;
       scrollbar-base-color: #fff;
   }

   #textarea9 {
       border-style: none;
       border-color: inherit;
       border-width: medium;
       height: 15px;
       width: 130px;
       position:absolute; top: 360px; 
       left: 360px;
       scrollbar-3dlight-color: #fff;
       scrollbar-arrow-color: #fff;
       scrollbar-base-color: #fff;
   }
   #textarea10 {
       height: 15px;
       width: 180px;
       position:absolute; top:360px;
       left: 450px;
       border:none;
       scrollbar-3dlight-color: #fff;
       scrollbar-arrow-color: #fff;
       scrollbar-base-color: #fff;
   }

   #textarea11 {
       border-style: none;
       border-color: inherit;
       border-width: medium;
       height: 15px;
       width: 320px;
       position:absolute; top:360px;
       scrollbar-3dlight-color: #fff;
       scrollbar-arrow-color: #fff;
       scrollbar-base-color: #fff;
       left: 0px;
   }

   #textarea12 {
       border-style: none;
       border-color: inherit;
       border-width: medium;
       font-size: large;
       font-weight: bold;
       height: 25px;
       width: 120px;
       position:absolute; top:400px;
       scrollbar-3dlight-color: #fff;
       scrollbar-arrow-color: #fff;
       scrollbar-base-color: #fff;
       left: 270px;
   }

   #textarea13 {
       border-style: none;
       border-color: inherit;
       border-width: medium;
       height: 15px;
       width: 75px;
       position:absolute; top:450px;
       scrollbar-3dlight-color: #fff;
       scrollbar-arrow-color: #fff;
       scrollbar-base-color: #fff;
       left: 0px;
   }

   #textarea14 {
       border-style: none;
       border-color: inherit;
       border-width: medium;
       height: 15px;
       width: 200px;
       position:absolute; top:450px;
       scrollbar-3dlight-color: #fff;
       scrollbar-arrow-color: #fff;
       scrollbar-base-color: #fff;
       left: 65px;
       margin-left: auto;
   }

   #textarea15 {
        height: 30px;
        width:  640px;
        text-align:start;
        text-anchor:end;
        position: absolute; top:480px;
        border:hidden;
        scrollbar-3dlight-color: #fff;
        scrollbar-arrow-color: #fff;
        scrollbar-base-color: #fff;
        left: 0px;
    }

   #textarea16 {
       border-style: none;
       border-color: inherit;
       border-width: medium;
       font-size:large;
       font-weight: bold;
       height: 25px;
       width: 250px;
       position:absolute; top:540px;
       scrollbar-3dlight-color: #fff;
       scrollbar-arrow-color: #fff;
       scrollbar-base-color: #fff;
       left: 270px;
   }

   #TextArea17 {
        height: 75px;
        width:  645px;
        text-align:start;
        text-anchor:end;
        position: absolute; top:580px;
        border:hidden;
        scrollbar-3dlight-color: #fff;
        scrollbar-arrow-color: #fff;
        scrollbar-base-color: #fff;
        left: 0px;
   }

   #TextArea18 {
        height: 30px;
        width:  640px;
        text-align:start;
        text-anchor:end;
        position: absolute; top:660px;
        border:hidden;
        scrollbar-3dlight-color: #fff;
        scrollbar-arrow-color: #fff;
        scrollbar-base-color: #fff;
        left: 0px;
    }

   #TextArea19 {
       width: 240px;
       position: absolute; top: 700px;
       scrollbar-3dlight-color: #fff;
       scrollbar-arrow-color: #fff;
       scrollbar-base-color: #fff;
       border:hidden;
       left: 0px;
   }

   #TextArea20 {
       height: 15px;
       width: 400px;
       position: absolute; top: 760px;
       scrollbar-3dlight-color: #fff;
       scrollbar-arrow-color: #fff;
       scrollbar-base-color: #fff;
       border:hidden;
       left: 0px;
   }

   #TextArea21 {
       height: 15px;
       width: 400px;
       position: absolute; top: 800px;
       scrollbar-3dlight-color: #fff;
       scrollbar-arrow-color: #fff;
       scrollbar-base-color: #fff;
       border:hidden;
       left: 0px;
   }

   #TextArea22 {
       height: 15px;
       width: 400px;
       position: absolute; top: 820px;
       scrollbar-3dlight-color: #fff;
       scrollbar-arrow-color: #fff;
       scrollbar-base-color: #fff;
       border:hidden;
       left: 0px;
   }

   #TextArea23 {
       height: 15px;
       width: 400px;
       position: absolute; top: 840px;
       scrollbar-3dlight-color: #fff;
       scrollbar-arrow-color: #fff;
       scrollbar-base-color: #fff;
       border:hidden;
       left: 0px;
   }

   #TextArea24 {
       height: 10px;
       width: 400px;
       font-size:smaller;
       position: absolute; top: 880px;
       scrollbar-3dlight-color: #fff;
       scrollbar-arrow-color: #fff;
       scrollbar-base-color: #fff;
       border:hidden;
       left: 0px;
   }

   #TextArea25 {
       height: 40px;
       width: 550px;
       font-size: x-small;
       position: absolute; top: 895px;
       scrollbar-3dlight-color: #fff;
       scrollbar-arrow-color: #fff;
       scrollbar-base-color: #fff;
       border:hidden;
       left: 0px;
   }

最后这里是功能背后的代码:(编辑:10-10-2013:14:22:GTM + 1)

protected void SaveAsPDF(object sender, EventArgs e)
    {
        System.Web.UI.HtmlControls.HtmlGenericControl middlesector = new System.Web.UI.HtmlControls.HtmlGenericControl("middlesector");

        string strHtml = null;
        //MemoryStream memStream = new MemoryStream();
        StringWriter strWriter = new StringWriter();
        Server.Execute("OfferteForm1.aspx", strWriter);
        strHtml = strWriter.ToString();
        strWriter.Close();
        strWriter.Dispose();
        iTextSharp.text.Image addLogo = default(iTextSharp.text.Image);
        addLogo = iTextSharp.text.Image.GetInstance(Server.MapPath("Images") + "/VisserSupplies.jpg");
        iTextSharp.text.Document docWorkingDocument = new iTextSharp.text.Document(PageSize.A4, 40, 40, 40, 40);
        StringReader srdDocToString = null;
        try
        {
            PdfWriter pdfWrite = default(PdfWriter);
            var fileCounter = 1;
            while (File.Exists("C:/Users/milans/Documents/PDFs/" + textarea11.InnerText + " - " + fileCounter + ".pdf"))
            {
                fileCounter++;
            }
            pdfWrite = PdfWriter.GetInstance(docWorkingDocument, new FileStream("C:/Users/milans/Documents/PDFs/" + textarea11.InnerText + " - " + fileCounter + ".pdf", FileMode.Create));
            srdDocToString = new StringReader(strHtml);
            docWorkingDocument.Open();
            addLogo.ScalePercent(50);
            addLogo.Alignment = iTextSharp.text.Image.ALIGN_CENTER;
            docWorkingDocument.Add(addLogo);
            XMLWorkerHelper.GetInstance().ParseXHtml(pdfWrite, docWorkingDocument, srdDocToString);
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
        finally
        {
            if ((docWorkingDocument != null))
            {
                docWorkingDocument.Close();
                docWorkingDocument.Dispose();
            }
            if ((srdDocToString != null))
            {
                srdDocToString.Close();
                srdDocToString.Dispose();
            }
        }
            //Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "myscript", "window.open('C:/Users/milans/Documents/PDFs/" + textarea11.InnerText + ".pdf" + "','_blank','location=0,menubar=0,status=0,titlebar=0,toolbar=0');", true);
    }

    public override void VerifyRenderingInServerForm(Control control)
    {

    }

我收到错误:     Server.Execute(“middlesector”,strWriter); 同样的错误:     Server.Execute(“OfferteForm1.aspx”,strWriter);

它与IUSRS_machinename和ASPNET用户的权限有关。 但是我不知道如何添加权限,因为我没有Manifest而且无法创建一个。

我也尝试过:     Server.HtmlDecode(“middlesector”,strWriter); 和:     Server.HtmlDecode(“OfferteForm1.aspx”,strWriter);

它确实编译PDF但它只有图像。 我想要的只是让XMLWorker将填写的表单包含在中间部分Div。

0 个答案:

没有答案