none
Cannot read that as a zip file. How can I fix this? RRS feed

  • Question

  • I am trying to store output of a program into word file and using the concept shown in code below.

    However, doing that I am getting this error "Cannot read that as a zip file" at the Document.docx stage.

    How can I fix this error?

    using GemBox.Document; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp4 { class Program { static void Main(string[] args) { int a = 42; int b = 119; int c = a + b; Console.WriteLine(c); var previousWriter = Console.Out; var textWriter = new StringWriter(); Console.SetOut(textWriter); Console.SetOut(previousWriter); ComponentInfo.SetLicense("FREE-LIMITED-KEY"); var document = DocumentModel.Load("Document.docx"); document.Content.End.LoadText(textWriter.ToString()); document.Save("Document.docx"); } } }

    Friday, February 14, 2020 4:20 AM

Answers

  • Hi Akhil963,
    You can creat the DocumentModel firstly and use the Sections.Add method() to add the text.
    Here is a code example you can refer to.

    static void Main(string[] args)
    {
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");
        var document = new DocumentModel();
        int a = 42;
        int b = 119;
        int c = a + b;
        Console.WriteLine(c);
        var previousWriter = Console.Out;
        var textWriter = new StringWriter();
        Console.SetOut(textWriter);
        Console.WriteLine(c);
        Console.SetOut(previousWriter);
        // Add document content.
        document.Sections.Add(
                new Section(document,
                new Paragraph(document, textWriter.ToString())));
        // Save the document to a DOCX file format.
        document.Save("Document.docx");
        // Load a DOCX file.
        document = DocumentModel.Load("Document.docx");
        Console.ReadKey();
    }

    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, February 14, 2020 8:35 AM

All replies

  • Did you check if Document.docx is a valid file (using Word, for example)?

    Also try a simplified example:

        var document = DocumentModel.Load("Document.docx");

        document.Save("Document2.docx");

    You can also contact Technical Support.

     

    Friday, February 14, 2020 6:25 AM
  • Hi Akhil963,
    You can creat the DocumentModel firstly and use the Sections.Add method() to add the text.
    Here is a code example you can refer to.

    static void Main(string[] args)
    {
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");
        var document = new DocumentModel();
        int a = 42;
        int b = 119;
        int c = a + b;
        Console.WriteLine(c);
        var previousWriter = Console.Out;
        var textWriter = new StringWriter();
        Console.SetOut(textWriter);
        Console.WriteLine(c);
        Console.SetOut(previousWriter);
        // Add document content.
        document.Sections.Add(
                new Section(document,
                new Paragraph(document, textWriter.ToString())));
        // Save the document to a DOCX file format.
        document.Save("Document.docx");
        // Load a DOCX file.
        document = DocumentModel.Load("Document.docx");
        Console.ReadKey();
    }

    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, February 14, 2020 8:35 AM
  • Hi Akhil963,
    Has your problem been solved? If it is resolved, we suggest that you mark it as the answer. So it can help other people who have the same problem find a solution quickly. If not solved, what problem did you encounter?
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, February 19, 2020 7:57 AM