Exemple #1
0
 public void DownloadDocument()
 {
     byte[] byteArray = File.ReadAllBytes(Path);
     using (MemoryStream mem = new MemoryStream())
     {
         //Grab the word document
         mem.Write(byteArray, 0, (int)byteArray.Length);
         using (DocumentFormat.OpenXml.Packaging.WordprocessingDocument wordDoc = DocumentFormat.OpenXml.Packaging.WordprocessingDocument.Open(mem, true))
         {
             //Write it back to word
             using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
             {
                 sw.Write(document);
             }
         }
         //Write the file out
         using (Stream fileStream = context.Response.OutputStream)
         {
             context.Response.AddHeader("content-disposition", "attachment; filename=\"" + agreement.PurchaseOrderNumber + "_JFA.docx\"");
             context.Response.ContentType = "application/msword";
             mem.WriteTo(fileStream);
         }
         context.Response.End();
     }
 }
Exemple #2
0
        public static int ReplaceParameters(string documentFileName, Dictionary <string, string> parameters, params SupportWordTabelModel[] items)
        {
            int         count          = 0;
            string      parameterName  = null;
            List <Text> parameterTexts = new List <Text>();
            int         icount         = 1;

            using (DocumentFormat.OpenXml.Packaging.WordprocessingDocument document = DocumentFormat.OpenXml.Packaging.WordprocessingDocument.Open(documentFileName, true))
            {
                var body = document.MainDocumentPart.Document.Body;

                foreach (SupportWordTabelModel item in items)
                {
                    if (item.Data != null)
                    {
                        foreach (Table t in body.Descendants <Table>().Where(tbl => tbl.InnerText.Contains(item.TableKeyword)))
                        {
                            foreach (List <string> tableLine in item.Data)
                            {
                                TableRow  row = new TableRow();
                                TableCell no  = new TableCell(new Paragraph(new Run(new Text(icount.ToString()))));
                                row.Append(no);
                                foreach (string tabelElement in tableLine)
                                {
                                    TableCell cell = new TableCell(new Paragraph(new Run(new Text(tabelElement))));
                                    row.Append(cell);
                                }

                                t.Append(row);
                                icount++;
                            }
                        }
                    }
                }

                // Process all paragraphs
                foreach (var para in body.Elements <Paragraph>())
                {
                    count += ParseParagraph(para, parameters, ref parameterName, ref parameterTexts);
                }

                // Process all tables
                foreach (var table in body.Elements <Table>())
                {
                    foreach (var row in table.Elements <TableRow>())
                    {
                        foreach (var cell in row.Elements <TableCell>())
                        {
                            // Process all paragraphs
                            foreach (var para in cell.Elements <Paragraph>())
                            {
                                count += ParseParagraph(para, parameters, ref parameterName, ref parameterTexts);
                            }
                        }
                    }
                }
            }

            return(count);
        }
Exemple #3
0
        private void MatchFont2()
        {
            string s = System.IO.Directory.GetCurrentDirectory();

            workingDoc.SaveAs("C:/Users/minad/AppData/Local/WordTestData/tmp.docx");
            DocumentFormat.OpenXml.Packaging.WordprocessingDocument doc = null;
            try
            {
                doc = DocumentFormat.OpenXml.Packaging.WordprocessingDocument.Open(
                    "C:/Users/minad/AppData/Local/WordTestData/con_Rong_chau_Tien.docx", false);
            }
            catch (DocumentFormat.OpenXml.Packaging.OpenXmlPackageException)
            {
                return;
            }
            catch (System.IO.IOException)
            {
                return;
            }
            DocumentFormat.OpenXml.Wordprocessing.Body body = doc.MainDocumentPart.Document.Body;
            //int idx = -1;
            foreach (DocumentFormat.OpenXml.Wordprocessing.Paragraph p in body.ChildElements.OfType <DocumentFormat.OpenXml.Wordprocessing.Paragraph>())
            {
                DocumentFormat.OpenXml.Wordprocessing.Run r = p.ChildElements.First <DocumentFormat.OpenXml.Wordprocessing.Run>();
                while (r != null)
                {
                    Console.WriteLine(r.InnerText);
                    r = r.NextSibling <DocumentFormat.OpenXml.Wordprocessing.Run>();
                }
            }
            doc.Close();
        }
Exemple #4
0
        public JFADocument(int AgreementID, bool dumb = false)
        {
            if (!dumb)
            {
                Path = context.Server.MapPath("Forms/JFATemplate.docx");
            }
            else
            {
                Path = context.Server.MapPath("Forms/JFATemplateDumb.docx");
            }
            doc = DocumentFormat.OpenXml.Packaging.WordprocessingDocument.Open(Path, false);
            //get document text
            using (StreamReader sr = new StreamReader(doc.MainDocumentPart.GetStream()))
            {
                document = sr.ReadToEnd();
            }
            agreement = siftaDB.spAgreementDocument(AgreementID).FirstOrDefault();
            var a = siftaDB.Agreements.FirstOrDefault(p => p.AgreementID == AgreementID);

            center = a.Customer.Center;
            ReplaceValues();
        }
Exemple #5
0
        public void GenerarArchivoDocxDesdeTextosHtml(string[] textosHtml, System.IO.Stream archivo)
        {
            using (DocumentFormat.OpenXml.Packaging.WordprocessingDocument objPaquete = DocumentFormat.OpenXml.Packaging.WordprocessingDocument.Create(archivo, DocumentFormat.OpenXml.WordprocessingDocumentType.Document))
            {
                DocumentFormat.OpenXml.Packaging.MainDocumentPart objDocumentoPrincipal = objPaquete.MainDocumentPart;
                if (objDocumentoPrincipal == null)
                {
                    objDocumentoPrincipal = objPaquete.AddMainDocumentPart();
                    new DocumentFormat.OpenXml.Wordprocessing.Document(new DocumentFormat.OpenXml.Wordprocessing.Body()).Save(objDocumentoPrincipal);
                }

                NotesFor.HtmlToOpenXml.HtmlConverter       objConversorHtml = new NotesFor.HtmlToOpenXml.HtmlConverter(objDocumentoPrincipal);
                DocumentFormat.OpenXml.Wordprocessing.Body objCuerpo        = objDocumentoPrincipal.Document.Body;
                objConversorHtml.ImageProcessing = NotesFor.HtmlToOpenXml.ImageProcessing.ManualProvisioning;
                objConversorHtml.ProvisionImage += eventoHtmlDoc_ProveerImagenes;

                for (int intIndiceHtml = 0; intIndiceHtml < textosHtml.Length; intIndiceHtml++)
                {
                    if (intIndiceHtml > 0)
                    {
                        Paragraph PageBreakParagraph = new Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(new DocumentFormat.OpenXml.Wordprocessing.Break()
                        {
                            Type = BreakValues.Page
                        }));
                        objCuerpo.Append(PageBreakParagraph);
                    }

                    var arrParrafos = objConversorHtml.Parse(textosHtml[intIndiceHtml]);
                    foreach (var objParrafo in arrParrafos)
                    {
                        objCuerpo.Append(objParrafo);
                    }
                }

                objDocumentoPrincipal.Document.Save();
            }
        }
        //+++++++++++++++++++++++++++++++ Dosyaları Aktarma  İşlemi +++++++++++++++++++++++++++++++++++++++++++++++

        private void button1_Click(object sender, EventArgs e)
        {
            string[] dosyalar = file.FileNames; // seçilen tüm dosyalar
            string   uzanti   = file.FileName;  //uzantı kontrolü için aldık

            int l = 0;

            try
            {
                richTextBox1.Clear();                       // önce temizlensin
                string[] dosyaIsimler = file.SafeFileNames; //Çoklu seçimdeki dosyaların ismi

                int isim = 0;


                foreach (var dosya in dosyalar)
                {
                    if (dosya.EndsWith(".txt"))
                    {
                        string etiket = "\n\n#############################################> " + dosyaIsimler[isim] + " <###########################################\n";


                        string metin = System.IO.File.ReadAllText(dosya, Encoding.GetEncoding("windows-1254"));
                        richTextBox1.Text = richTextBox1.Text + " " + etiket + " " + metin;

                        isim++;
                        l++;
                    }


                    else if (dosya.EndsWith(".docx") || dosya.EndsWith(".doc"))
                    {
                        string etiket = "\n\n#############################################> " + dosyaIsimler[isim] + " <###########################################\n";
                        DocumentFormat.OpenXml.Packaging.WordprocessingDocument doc = DocumentFormat.OpenXml.Packaging.WordprocessingDocument.Open(dosya, false);

                        string yazilar = doc.MainDocumentPart.Document.Body.InnerText.ToString();


                        richTextBox1.Text = richTextBox1.Text + etiket + yazilar;


                        isim++;
                        l++;
                    }


                    else if (dosya.EndsWith(".pdf"))
                    {
                        string yol = dosya;

                        using (PdfReader reader = new PdfReader(yol))
                        {
                            StringBuilder text = new StringBuilder();

                            for (int i = 1; i <= reader.NumberOfPages; i++)
                            {
                                text.Append(PdfTextExtractor.GetTextFromPage(reader, i));
                            }
                            string etiket  = "\n\n#############################################> " + dosyaIsimler[isim] + " <###########################################\n";
                            string yazilar = text.ToString();

                            richTextBox1.Text = richTextBox1.Text + " " + etiket + " " + yazilar;
                            isim++;
                            l++;
                            //EN SON PDF OKUMA PARÇALAMA YAPILDI..
                        }
                    }
                }
            }

            catch (Exception)
            {
                MessageBox.Show("Hata Oluştu \nDosya bozuk olabilir.\nDosya zaten kullanılıyor olabilir.\nSeçtiğiniz dosyanın kapalı olduğundan emin olun!\n\nCTRL Shift ve ESC tuşlarına aynı anda basın\nÇalışan Word Dosyalarını Kapatın.\nTekrar Deneyin...\n\n Hata Oluşturan Dosya =" + dosyalar[l],
                                "Uyarı",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Exclamation,
                                MessageBoxDefaultButton.Button1);
            }
        }