如何将字节数组转换为pdf

时间:2018-03-19 12:57:05

标签: c#

我正在构建一个修改单词2010文档的应用程序,它应该以pdf格式下载。我编写一个代码将单词转换为pdf,但它正在转换我上传的文档而不是修改后的单词。如何将修改后的单词转换为PDF.Below是不转换修饰单词的功能。  protected void btnUpload_Click(object sender,EventArgs e)         {

        if (FileUploadControl.HasFile)

            {
                string fileNameFromUser = FileUploadControl.FileName;
                var fiFileName = new System.IO.FileInfo(fileNameFromUser);

                using (MemoryStream ms = new MemoryStream())
                {
                    ms.Write(FileUploadControl.FileBytes, 0, FileUploadControl.FileBytes.Length);

                    using (WordprocessingDocument sDoc = WordprocessingDocument.Open(ms, true))
                    {

                    }
                    lblMessage.Text = "Dokumenti u ngarkua me sukses!";
                    Session["ByteArray"] = FileUploadControl.FileBytes;
                    Session["fileNameFromUser"] = fileNameFromUser;
                }



        }
        byte[] byteArray = (byte[])(Session["ByteArray"]);
        if (byteArray != null)
        {
            try
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    ms.Write(byteArray, 0, byteArray.Length);
                    using (WordprocessingDocument wDoc = WordprocessingDocument.Open(ms, true))
                    {
                        var body = wDoc.MainDocumentPart.Document.Body;
                        var lastParagraf = body.Elements<Paragraph>().LastOrDefault();
                        var newParagraf = new Paragraph(

                            new Run(
                                new Text("Perdoruesi:" + " " + User.Identity.Name)));

                        var newParagraf2 = new Paragraph(

                           new Run(
                               new Text("Data dhe ora:" + " " + DateTime.Now.ToString())));

                        var newParagraf3 = new Paragraph(

                           new Run(
                               new Text("Kodi unik:" + " " + randomstring(14))));

                        var newParagraf4 = new Paragraph(

                          new Run(
                              new Text("Shifra:" )));

                        lastParagraf.InsertAfterSelf(newParagraf);
                        lastParagraf.InsertAfterSelf(newParagraf2);
                        lastParagraf.InsertAfterSelf(newParagraf3);
                        lastParagraf.InsertAfterSelf(newParagraf4);
                    }
                    Session["ByteArray"] = ms.ToArray();
                    lblMessage.Text = "U ngarkua dhe u vulos dokumenti!";

                    Guid pdfFileGuid = Guid.NewGuid();

                    var filePath = Path.GetTempFileName();
                    FileUploadControl.SaveAs(filePath);
                    var appWord = new Microsoft.Office.Interop.Word.Application();
                    var wordDoc = appWord.Documents.Open(filePath);
                    var convertedFilePath = Path.GetTempFileName();
                    wordDoc.ExportAsFixedFormat(convertedFilePath, Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF);
                    Response.Clear();
                    Response.AddHeader("content-disposition", "attachment; filename=Converted.Pdf");
                    Response.AddHeader("content-type", "application/pdf");
                    Response.TransmitFile(convertedFilePath);
                    File.Delete(filePath);
                    File.Delete(convertedFilePath);
                }
            }
            catch (Exception ex)
            {
                lblMessage.Text = "ERROR:" + ex.Message.ToString();
            }
        }
        else
        {
            lblMessage.Text = "Nuk e keni zgjedhur dokumentin apo formati i dokumentit nuk pershtatet!";
        }
    }

1 个答案:

答案 0 :(得分:0)

目前,您可以在此处将int pageCountBefore = builder.Document.PageCount;中的数据加载到会话中:

FileUploadControl

然后,您将会话中的数据加载到用于打开文档进行编辑的Session["ByteArray"] = FileUploadControl.FileBytes; 中:

MemoryStream

在编辑结束时,您将更新的内容复制回会话:

    byte[] byteArray = (byte[])(Session["ByteArray"]);
    if (byteArray != null)
    {
        try
        {
            using (MemoryStream ms = new MemoryStream())
            {
                ms.Write(byteArray, 0, byteArray.Length);
                using (WordprocessingDocument wDoc = WordprocessingDocument.Open(ms, true))

但;这是我们最后一次看到会话使用的时间;然后将上传的文件写入临时文件,并使用 执行Word自动化:

            lastParagraf.InsertAfterSelf(newParagraf3);
            lastParagraf.InsertAfterSelf(newParagraf4);
        }
        Session["ByteArray"] = ms.ToArray();

是的:这会将原始文件转换为上传,因为var filePath = Path.GetTempFileName(); FileUploadControl.SaveAs(filePath); var appWord = new Microsoft.Office.Interop.Word.Application(); var wordDoc = appWord.Documents.Open(filePath); 与您所做的更改无关。相反,也许尝试:

FileUploadControl

然后应该包括更改。

补充侧面观察:

  • 不支持Word自动化,不建议在Web服务器上使用;如果它对你有用:太棒了,但要注意,如果它突然断裂,你就会自己动手
  • 请注意,您不要将文件分散在磁盘上,也不要留下许多打开的Word实例/文件;基本上:检查你是不是“泄漏”(var filePath = Path.GetTempFileName(); File.WriteAllBytes(filePath, (byte[])Session["ByteArray"]); var appWord = new Microsoft.Office.Interop.Word.Application(); var wordDoc = appWord.Documents.Open(filePath); appWordwordDocfilePath等 - 所有可能都需要一些convertedFilePath爱)
  • 将Word文件放入会话状态(finally可能是并不是一个好主意或必要的想法;我建议您在此方法中使用本地Session或类似内容,并完全删除byte[]