Beispiel #1
0
        public void deletePage(object page)
        {
            object oMissing = System.Reflection.Missing.Value;
            int    pages    = wordDoc.ComputeStatistics(Microsoft.Office.Interop.Word.WdStatistic.wdStatisticPages, ref oMissing);
            object objWhat  = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage;
            object objWhich = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToAbsolute;

            Microsoft.Office.Interop.Word.Range range1 = wordDoc.GoTo(ref objWhat, ref objWhich, ref page, ref oMissing);
            Microsoft.Office.Interop.Word.Range range2 = range1.GoToNext(Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage);
            object bjStart = range1.Start;
            object objEnd  = range2.Start;
            object Unit    = (int)Microsoft.Office.Interop.Word.WdUnits.wdCharacter;
            object Count   = 1;

            wordDoc.Range(ref bjStart, ref objEnd).Delete(ref Unit, ref Count);

            object what  = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToLine;
            object which = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToFirst;
            object count = 1;

            wordApp.Selection.GoTo(ref what, ref which, ref count, ref oMissing).Delete();
        }
Beispiel #2
0
    //static MSWord.Application wordApp = new MSWord.Application();

    private static string SaveDocx(object sourceFileName, bool isDelContext = false)
    {
        MSWord.Application wordApp = new MSWord.Application();
        wordApp.Visible = false;
        object targetFileName = Path.ChangeExtension((string)sourceFileName, "docx");
        object missingValue   = System.Reflection.Missing.Value;

        try
        {
            MSWord.Document doc = wordApp.Documents.Open(
                ref sourceFileName,
                ref missingValue, ref missingValue, ref missingValue, ref missingValue,
                ref missingValue, ref missingValue, ref missingValue, ref missingValue,
                ref missingValue, ref missingValue, ref missingValue, ref missingValue,
                ref missingValue, ref missingValue, ref missingValue);

            if (isDelContext)
            {
                doc.Tables[1].Range.Delete(missingValue, missingValue);
                doc.Tables[1].Delete();//.Range.Delete(missingValue, missingValue);
                var sectionCount = wordApp.ActiveDocument.Sections.Count;
                for (int i = 1; i <= sectionCount; i++)
                {
                    wordApp.ActiveDocument.Sections[i].Headers[MSWord.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Delete();
                    wordApp.ActiveDocument.Sections[i].Footers[MSWord.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Delete();
                }

                //---------------------------------------------

                object Unit1     = (int)MSWord.WdUnits.wdCharacter;
                object Count1    = 1;
                var    firstLine = doc.Paragraphs.First.Range;
                var    content   = firstLine.Text.Trim();
                while (string.IsNullOrEmpty(content))
                {
                    firstLine.Delete(ref Unit1, ref Count1);
                    firstLine = doc.Paragraphs.First.Range;
                    content   = firstLine.Text.Trim();
                }
                //------------------------删除空白页-------------------
                int    pages    = doc.ComputeStatistics(MSWord.WdStatistic.wdStatisticPages, ref missingValue);
                object objWhat  = MSWord.WdGoToItem.wdGoToPage;
                object objWhich = MSWord.WdGoToDirection.wdGoToAbsolute;
                object objPage  = 1;//指定页

                MSWord.Range range1 = doc.GoTo(ref objWhat, ref objWhich, ref objPage, ref missingValue);

                MSWord.Range range2 = range1.GoToNext(MSWord.WdGoToItem.wdGoToPage);

                object objStart = range1.Start;

                object objEnd = range2.Start;

                if (range1.Start == range2.Start)
                {
                    objEnd = doc.Characters.Count;//最后一页
                }
                string str = doc.Range(ref objStart, ref objEnd).Text;
                Console.WriteLine(str);

                if (string.IsNullOrEmpty(str.Trim()))
                {
                    object Unit  = (int)MSWord.WdUnits.wdCharacter;
                    object Count = 1;
                    doc.Range(ref objStart, ref objEnd).Delete(ref Unit, ref Count);
                }
            }


            object FileFormat              = MSWord.WdSaveFormat.wdFormatDocumentDefault;
            object LockComments            = false;
            object Password                = missingValue;
            object AddToRecentFiles        = false;
            object WritePassword           = missingValue;
            object ReadOnlyRecommended     = false;
            object EmbedTrueTypeFonts      = true;
            object SaveNativePictureFormat = missingValue;
            object SaveFormsData           = missingValue;
            object SaveAsAOCELetter        = missingValue;
            object Encoding                = missingValue;
            object InsertLineBreaks        = missingValue;
            object AllowSubstitutions      = missingValue;
            object LineEnding              = missingValue;
            object AddBiDiMarks            = missingValue;
            object CompatibilityMode       = missingValue;

            doc.SaveAs(ref targetFileName, ref FileFormat,
                       ref LockComments, ref Password, ref AddToRecentFiles, ref WritePassword,
                       ref ReadOnlyRecommended, ref EmbedTrueTypeFonts, ref SaveNativePictureFormat, ref SaveFormsData,
                       ref SaveAsAOCELetter, ref Encoding, ref InsertLineBreaks, ref AllowSubstitutions,
                       ref LineEnding, ref AddBiDiMarks);

            wordApp.Documents.Close(ref missingValue, ref missingValue, ref missingValue);

            //关闭进程
            object saveOption = MSWord.WdSaveOptions.wdDoNotSaveChanges;
            wordApp.Application.Quit(ref saveOption, ref missingValue, ref missingValue);
        }
        catch (Exception ex)
        {
            Console.WriteLine("转换错误=====" + ex.Message);
            object saveOption = MSWord.WdSaveOptions.wdDoNotSaveChanges;

            wordApp.Application.Quit(ref saveOption, ref missingValue, ref missingValue);
        }
        return(targetFileName as string);
    }
Beispiel #3
0
        public bool ReadPdf(string pdfFile, ref Documents doc, ref int pages)
        {
            bool success = false;

            try
            {
                if (pdfFile.ToLower().Contains("pdf"))
                {
                    StringBuilder textBuilder = new StringBuilder();
                    PdfReader     r           = new PdfReader(pdfFile);
                    pages = r.NumberOfPages;

                    for (int i = 1; i <= pages; i++)
                    {
                        PdfReaderContentParser  parser = new PdfReaderContentParser(r);
                        ITextExtractionStrategy st     = parser.ProcessContent <SimpleTextExtractionStrategy>(i, new SimpleTextExtractionStrategy());
                        string text = st.GetResultantText().Trim('\r', '\n', '\t', (char)32, (char)160);

                        if (!string.IsNullOrEmpty(text))
                        {
                            doc.DocBodyDic.Add(i, text);
                        }
                        else
                        {
                            text = PdfTextExtractor.GetTextFromPage(r, i).Trim('\r', '\n', '\t', (char)32, (char)160);

                            if (!string.IsNullOrEmpty(text))
                            {
                                doc.DocBodyDic.Add(i, text);
                            }
                        }
                    }

                    r.Close();
                    success = true;
                }
                else if (pdfFile.ToLower().Contains("doc"))
                {
                    MsWord.Application newApp = null;
                    MsWord.Document    msdoc  = null;

                    try
                    {
                        int retry = 2;
                        while (retry > 0)
                        {
                            try
                            {
                                //newApp = (MsWord.Application)Marshal.GetActiveObject("Word.Application");
                                newApp = newApp == null ? new MsWord.Application() : newApp;
                                System.Threading.Thread.Sleep(1000);
                                //msdoc = newApp.ActiveDocument;
                                msdoc = newApp.Documents.Open(pdfFile);
                                System.Threading.Thread.Sleep(1000);
                                object             nothing = Missing.Value;
                                MsWord.WdStatistic stat    = MsWord.WdStatistic.wdStatisticPages;
                                int num = msdoc.ComputeStatistics(stat, ref nothing);

                                for (int i = 1; i <= num; i++)
                                {
                                    if (doc.DocBodyDic.ContainsKey(i))
                                    {
                                        continue;
                                    }

                                    object objWhat  = MsWord.WdGoToItem.wdGoToPage;
                                    object objWhich = MsWord.WdGoToDirection.wdGoToAbsolute;

                                    object       objPage = (object)i;
                                    MsWord.Range range1  = msdoc.GoTo(ref objWhat, ref objWhich, ref objPage, ref nothing);
                                    MsWord.Range range2  = range1.GoToNext(MsWord.WdGoToItem.wdGoToPage);

                                    object objStart = range1.Start;
                                    object objEnd   = range2.Start;
                                    if (range1.Start == range2.Start)
                                    {
                                        objEnd = msdoc.Characters.Count;
                                    }

                                    Console.ForegroundColor = ConsoleColor.Red;
                                    Console.WriteLine("DEBUG: Path: {0}, {1}-{2}........", pdfFile, objStart, objEnd);
                                    Console.ResetColor();

                                    if ((int)objStart <= (int)objEnd)
                                    {
                                        string innerText = msdoc.Range(ref objStart, ref objEnd).Text;
                                        doc.DocBodyDic.Add(i, innerText);
                                    }
                                }

                                success = true;
                                break;
                            }
                            catch (Exception ex)
                            {
                                Console.ForegroundColor = ConsoleColor.Red;
                                Console.WriteLine("Retry to read word {0}, Exception: {1}..", pdfFile, ex.ToString());
                                Console.ResetColor();
                                System.Threading.Thread.Sleep(1000);
                                retry--;
                            }
                            finally
                            {
                                if (newApp != null)
                                {
                                    newApp.NormalTemplate.Saved = true;

                                    if (msdoc != null)
                                    {
                                        msdoc.Close(false);
                                    }

                                    newApp.Quit();
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                    }
                }
            }
            catch (Exception ex)
            {
            }

            return(success);
        }
        private Bitmap[] Scan4Word(string filePath)
        {
            //复制目标文件,后续将操作副本
            string tmpFilePath = AppDomain.CurrentDomain.BaseDirectory + "\\" + Path.GetFileName(filePath) + ".tmp";

            File.Copy(filePath, tmpFilePath);

            List <Bitmap> bmList = new List <Bitmap>();

            MSWord.ApplicationClass wordApplicationClass = new MSWord.ApplicationClass();
            wordApplicationClass.Visible = false;
            object missing = System.Reflection.Missing.Value;

            try
            {
                object readOnly       = false;
                object filePathObject = tmpFilePath;

                MSWord.Document document = wordApplicationClass.Documents.Open(ref filePathObject, ref missing,
                                                                               ref readOnly, ref missing, ref missing, ref missing,
                                                                               ref missing, ref missing, ref missing, ref missing,
                                                                               ref missing, ref missing, ref missing, ref missing,
                                                                               ref missing, ref missing);

                bool finished = false;
                while (!finished)
                {
                    document.Content.CopyAsPicture(); //拷贝到粘贴板
                    System.Windows.Forms.IDataObject data = Clipboard.GetDataObject();
                    if (data.GetDataPresent(DataFormats.MetafilePict))
                    {
                        object   obj      = data.GetData(DataFormats.MetafilePict);
                        Metafile metafile = MetafileHelper.GetEnhMetafileOnClipboard(IntPtr.Zero); //从粘贴板获取数据
                        Bitmap   bm       = new Bitmap(metafile.Width, metafile.Height);
                        using (Graphics g = Graphics.FromImage(bm))
                        {
                            g.Clear(Color.White);
                            g.DrawImage(metafile, 0, 0, bm.Width, bm.Height);
                        }
                        bmList.Add(bm);
                        Clipboard.Clear();
                    }

                    object What       = MSWord.WdGoToItem.wdGoToPage;
                    object Which      = MSWord.WdGoToDirection.wdGoToFirst;
                    object startIndex = "1";
                    document.ActiveWindow.Selection.GoTo(ref What, ref Which, ref missing, ref startIndex); // 转到下一页
                    MSWord.Range start = document.ActiveWindow.Selection.Paragraphs[1].Range;
                    MSWord.Range end   = start.GoToNext(MSWord.WdGoToItem.wdGoToPage);
                    finished = (start.Start == end.Start);
                    if (finished) //最后一页
                    {
                        end.Start = document.Content.End;
                    }

                    object oStart = start.Start;
                    object oEnd   = end.Start;
                    document.Range(ref oStart, ref oEnd).Delete(ref missing, ref missing); //处理完一页,就删除一页。
                }

                ((MSWord._Document)document).Close(ref missing, ref missing, ref missing);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(document);

                return(bmList.ToArray());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                wordApplicationClass.Quit(ref missing, ref missing, ref missing);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApplicationClass);
                File.Delete(tmpFilePath); //删除临时文件
            }
        }