0

I have this code to Merge documents to .PDF using Spire but i get the following code in this line result.Save(outputFile,Spire.Pdf.FileFormat.PDF);

CS0012 C# The type 'HttpContext' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

I tried to add this Assembly code to App.config file but no luck.

<assemblies>
        <add assembly="MyAssembly" Version="4.0.0.0" Culture="neutral" PublicKeyToken="b03f5f7f11d50a3a"/>   

Here is the code below

private void button1_Click(object sender, EventArgs e)
        {

            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Filter = "All files (*.docx, *.pdf, *.pptx, *.pdf)| *.docx; *.pdf; *.pptx; *.xlsx";

            ofd.Multiselect = true;

            if (DialogResult.OK == ofd.ShowDialog())
            {

                string[] files = ofd.FileNames;

                listBox1.Items.AddRange(files);
            }

    }

        private void button2_Click(object sender, EventArgs e)
        {
            string ext = string.Empty;
            List<Stream> filesStreams = new List<Stream>();
            MemoryStream ms1 = new MemoryStream();
            MemoryStream ms2 = new MemoryStream();
            MemoryStream ms3 = new MemoryStream();
            foreach (object item in listBox1.Items)
            {
                ext = Path.GetExtension(item.ToString());
                switch (ext)
                {
                    case ".docx":
                        Document doc = new Document(item.ToString());
                        doc.SaveToStream(ms1, Spire.Doc.FileFormat.PDF);
                        filesStreams.Add(ms1);

                        break;
                    case ".pdf":
                        filesStreams.Add(File.OpenRead(item.ToString()));
                        break;
                    case ".pptx":
                        Presentation ppt = new Presentation(item.ToString(), Spire.Presentation.FileFormat.Auto);
                        ppt.SaveToFile(ms2, Spire.Presentation.FileFormat.PDF);
                        filesStreams.Add(ms2);

                        break;
                    case ".xlsx":
                        Workbook xls = new Workbook();
                        xls.LoadFromFile(item.ToString());
                        xls.SaveToStream(ms3, Spire.Xls.FileFormat.PDF);
                        filesStreams.Add(ms3);

                        break;
                    default:
                        break;

                }
            }
            string outputFile = "result.doc";
            PdfDocumentBase result = PdfDocument.MergeFiles(filesStreams.ToArray());
            //result.SaveToDoc(outputFile);
            result.Save(outputFile,Spire.Pdf.FileFormat.PDF);
            ms1.Close();
            ms2.Close();
            ms3.Close();

    }

Thank you

afri
  • 49
  • 2
  • 12
  • 2
    The error message means your project needs to have a reference to "System.Web". – Lex Li Dec 31 '16 at 07:43
  • To me there's a bigger point here. The Spire library is asking you to add web libraries, which likely means that their tools are sending the PDF to their servers for processing. I don't have proof of this but that would make sense. And then you need to ask yourself, am I okay with a third party having access to my documents? – Jordan Ryder Oct 22 '19 at 19:29

1 Answers1

0

Add reference of System.Web library by right clicking the project in Solution Explorer window and then clicking Add Reference option from the context menu.

The above action will open up the Add Reference Dialog and from the .Net Tab, you need to choose System.Web library and click OK.

Dheeraj Malik
  • 703
  • 1
  • 4
  • 8
  • i dont have that error now i get `An unhandled exception of type 'System.TypeInitializationException' occurred in Spire.Doc.dll Additional information: The type initializer for 'sprᳶ' threw an exception.` when i tried to merge two .docx files – afri Jan 03 '17 at 07:37
  • Can you share your complete project to us(support@e-iceblue.com) with detailed information. Disclaimer: I'm working for the vendor of Spire. – Dheeraj Malik Jan 03 '17 at 07:39
  • Of course. Thank you. I am able to merge two pdf files fine the problem is on when i tried to merge two docx files. my goal is to achieve duplex printing. Is there anyways to print result.pdf duplex with a single click or automatically? – afri Jan 03 '17 at 07:48
  • This code is used for merging pdf files. For merging docx files, try use following code: Document document = new Document(); document.LoadFromFile("test1.docx", FileFormat.Docx); document.InsertTextFromFile("test2.docx",FileFormat.Docx); document.SaveToFile("MergeFiles.docx",FileFormat.Docx); – Dheeraj Malik Jan 03 '17 at 07:51
  • Yes, you can print pdf files duplex if your printer supports this feature. Code for your reference: bool value = pdf.PrintDocument.PrinterSettings.CanDuplex; pdf.PrintDocument.PrinterSettings.Duplex = System.Drawing.Printing.Duplex.Horizontal; – Dheeraj Malik Jan 03 '17 at 07:58