locked
Problem downloading a pdf RRS feed

  • Question

  • User-1634117515 posted

    Hi,

    I have this Page and its supposed that when the server creates the document on word it's saved on .pdf and after that, the .pdf must be downloaded but when the client downloads it is corrupted, on the server the pdf it's saved well

    here is the code for creating the pdf and the ode for downloading 

    public static void SolicitudVacaciones(Vaca v)
        {
            Words w = new Words();
            var word = new Word.Application();
            var Documento = new Word.Document();
            Documento = word.Documents.Open(@"\\192.168.10.21\j\Docs\Solicitud de vacaciones.docx");
            Documento.Bookmarks["Numero"].Range.Text = v.ndias;
            Documento.Bookmarks["Dias"].Range.Text = v.dias;
            Documento.Bookmarks["Año"].Range.Text = v.año;
            Documento.Bookmarks["Nombre"].Range.Text = v.Nombre;
            Documento.Bookmarks["Fecha"].Range.Text = DateTime.Today.ToLongDateString();
            Documento.Bookmarks["Fecha1"].Range.Text = DateTime.Today.ToLongDateString();
            Documento.Bookmarks["Fecha2"].Range.Text = DateTime.Today.ToLongDateString();
            Documento.Bookmarks["Jefe"].Range.Text = v.Autoriza;
            word.Visible = false;
            w.wordDocument = Documento;
            w.wordDocument.ExportAsFixedFormat(@"\\192.168.10.21\j\Solicitudes\Solicitud de Vaciones de " + v.Nombre + ".pdf", Word.WdExportFormat.wdExportFormatPDF, false, Word.WdExportOptimizeFor.wdExportOptimizeForPrint);
            w.wordDocument.Close(Word.WdSaveOptions.wdDoNotSaveChanges);
            word.Quit();
        }
     Procesos.SolicitudVacaciones(v);
                        Response.Clear();
                        Response.ContentType = "application/pdf";
                        Response.AddHeader("Content-Disposition", "attachment; filename=Solicitud de Vaciones de " + v.Nombre + ".pdf");
                        Response.TransmitFile(@"\\192.168.10.21\j\Solicitudes\" + "Solicitud de Vaciones de " + v.Nombre + ".pdf");
                        Response.End();

    Friday, June 14, 2019 7:58 PM

All replies

  • User303363814 posted

    It is not supported to run office on a server.  Maybe this is why?

    Sunday, June 16, 2019 7:17 AM
  • User665608656 posted

    Hi markdirtyboy,

    According to your description, I recommend that you use Aspose.Words library to convert the word document of your page into PDF document.

    When you convert it into PDF document, you could download it to the current page through the path currently stored by PDF for viewing.

    To use this library, you need to go to the official website to download the corresponding DLL files.

    This is the download link: https://downloads.aspose.com/words/net

    In my example, I use DocX library to create a word document.

    After successful creation, I create a Document object of Aspose.Words with the file path of the word document as a parameter, then save the object, and save the path as the path you want to store, and make sure that the type of storage is pdf.

    It should be noted that the reference file is not free, so the PDF background you see will contain some information of his own, as you could see in the figure below.

    Another libray that converts word documents into PDF documents is ITextSharp, but it only supports the conversion of the first three pages to PDF free of charge.

    You could refer to the details:
    https://www.c-sharpcorner.com/forums/convert-word-template-to-pdf-use-itextsharp
    https://www.e-iceblue.com/Introduce/word-for-net-introduction.html#.XQddk4gzaUl

    For more details,you could refer to the following code:

    using Aspose.Words;
    using System;
    using Xceed.Words.NET;
    using Table = Xceed.Words.NET.Table;
    
    namespace WebApplication1.Cases
    {
        public partial class WebForm_0617_2156680 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
            }
         
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                string path = @"C:\Users\yongqy\Desktop\test1.docx";
                using (var document = DocX.Create(path))
                {
                    document.InsertParagraph().Append("PersonalInformation").Alignment = Alignment.center;
                    Table table = document.AddTable(2, 3);
                    table.Rows[0].Cells[0].Paragraphs[0].Append("Name");
                    table.Rows[0].Cells[1].Paragraphs[0].Append("Gender");
                    table.Rows[0].Cells[2].Paragraphs[0].Append("Age");
                    table.Rows[1].Cells[0].Paragraphs[0].Append("Lily");
                    table.Rows[1].Cells[1].Paragraphs[0].Append("Femail");
                    table.Rows[1].Cells[2].Paragraphs[0].Append("23");
                    document.InsertTable(table);
                    document.Save();
                    //----------------------- 
                    DOCConvertToPDF(path, path.Replace(".docx", ".pdf"));
                    Response.Clear();
                    Response.ContentType = "application/pdf";
                    Response.AddHeader("Content-Disposition", "attachment; filename=test1.pdf");
                    Response.TransmitFile(path.Replace(".docx", ".pdf"));
                    Response.End();
                }
            }
    
           private static void DOCConvertToPDF(string sourcePath, string targetPath)
            {
                bool result;
                try
                {
                    Document document = new Document(sourcePath);
                    document.Save(targetPath, SaveFormat.Pdf);
                    result = true;
    
                }
                catch (Exception ex)
                {
                    result = false;
                }
            }
        }
    }

    The result of my work demo:

    Best Regards,

    YongQing.

    Monday, June 17, 2019 10:02 AM