如何在PDF文件中保存图像/形状

时间:2013-04-04 04:51:42

标签: asp.net-mvc itextsharp

在我的应用程序中允许用户创建PDF文件。用户还可以在PDF文件中添加形状(如矩形,线条),表单字段(textarea,checkbox ..)。当用户点击保存按钮时,PDF会成功创建,但用户在其文档中包含的形状和表单字段不会出现。

function create_PDF(box) {
        var filename = $("#filename").val();
        if (filename == "") {
            alert("Enter filename");
            return false;
        }
        var content = CKEDITOR.instances["message"].getData();
        if (content == "") {
            alert("Enter Content in The Editor");
            return false;
        }
        content = content.trim();
        dhtmlx.modalbox.hide(box);
        dhtmlx.message("Saving file...");
        $.post("/FileUpload/CreateNewPDFile",
    {
        filename: '' + filename + '', content: '' + content + ''
    }, function (data) {

        alert("PDF File created succesfully");
        CKEDITOR.instances["message"].setData("");
        CKEDITOR.instances["editor1"].setData("");
    });
    }

        [AcceptVerbs(HttpVerbs.Post)]
    [ValidateInput(false)]
    public ActionResult CreateNewPDFile(FormCollection data)
    {
        var filename = data["filename"];
        var htmlContent = data["content"];
        string sFilePath = Server.MapPath(_createdPDF + filename + ".pdf");
        htmlContent = htmlContent.Trim();
        htmlContent = htmlContent.Replace("<p>", "\n").Replace("</p>", "\n");
        Regex reg = new Regex("<[^>]+>", RegexOptions.IgnoreCase);
        if (!System.IO.File.Exists(sFilePath))
        {
            Document doc = new Document(PageSize.A4, 10, 10, 10, 10);
            doc.SetMargins(50, 50, 50, 50);
            doc.SetPageSize(new iTextSharp.text.Rectangle(iTextSharp.text.PageSize.LETTER.Width, iTextSharp.text.PageSize.LETTER.Height));
            PdfWriter.GetInstance(doc, new System.IO.FileStream(sFilePath, System.IO.FileMode.Create));
            iTextSharp.text.Font fnt = FontFactory.GetFont("Times New Roman", 14);
            doc.Open();
            PdfPTable pdfTab = new PdfPTable(1);
            PdfPCell pdfCell = null;
            pdfCell = new PdfPCell(new Phrase(new Chunk(htmlContent)));
            pdfTab.AddCell(pdfCell);
            doc.Add(pdfTab);
            doc.Close();
            Response.ContentType = "application/pdf";

            System.Web.HttpContext.Current.Response.Write(doc);
            Response.Flush();
            Response.End();
        }

        return View();
    }

如果在新创建的PDF中使用包含textarea,矩形和复选标记而不是textarea,矩形和复选标记,则会出现以下内容:如何避免此代码并显示在文档中的相应图像上

0 个答案:

没有答案