c# - MS Word Interop, Keeping Paragraphs and tables with headers together keeping them from splitting to the next page -


i'm using code below working merge queued list of html files , saving them in either pdf or docx using ms word interop. i've run issues page breaks. cannot figure out how keep both paragraphs , tables page breaking in middle of them. goal keep text in paragraphs , tables together. tables have heading text direct above them. nice keep if possible. there way programatically keep these items together? document being used done not have static verbiage or format. dynamically created , can different depending on circumstances. code being developed in .net 2.0 environment.

public static void mergea(string[] filestomerge, string outputfilename, bool insertpagebreaks, bool pdf)     {         //object defaulttemplate = documenttemplate;         object missing = system.type.missing;         object pagebreak = microsoft.office.interop.word.wdbreaktype.wdpagebreak;         object outputfile = outputfilename;         object ofileformat = microsoft.office.interop.word.wdsaveformat.wdformatdocumentdefault;         if (pdf)         {             ofileformat = microsoft.office.interop.word.wdsaveformat.wdformatpdf;         }          // create  new word application         microsoft.office.interop.word._application wordapplication = new microsoft.office.interop.word.application();         wordapplication.visible = false;          try         {             // create new file based on our template             microsoft.office.interop.word._document worddocument = wordapplication.documents.add(                                           ref missing                                         , ref missing                                         , ref missing                                         , ref missing);              // make word selection object.             microsoft.office.interop.word.selection selection = wordapplication.selection;              // loop thru each of word documents             foreach (string file in filestomerge)             {                 // insert files our template                 selection.insertfile(                                          file                                         , ref missing                                         , ref missing                                         , ref missing                                         , ref missing);                  //do want page breaks added after each documents?                 if (insertpagebreaks)                 {                     selection.insertbreak(ref pagebreak);                 }             }              // save document it’s output file.                             worddocument.saveas2(                             ref outputfile                         , ref ofileformat                         , 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                         , ref missing);              // clean up!             worddocument = null;         }         catch (exception ex)         {             //i didn’t include default error handler i’m throwing error             throw ex;         }                 {             // finally, close our word application             wordapplication.quit(ref missing, ref missing, ref missing);         }     } 

i'm there. i've added code below after insert page breaks if statement before saveas2. looks working hoped i'm still running issue breaking on table headers. i'm thinking may need encapsulate header labels within table how we're using hard because original files (filestomerge) dynamically created in html. think need reduce font because seems has caused text cut off or cut in half. seems kind of strange it's cutting off text. after examining saved doc further i'm lucky original html files encapsulating text within table. helping greatly. looks need fix cut off text , keep header text table on page breaks , have resolved now. ideas great. hope question helps others there older posts on not detailed.

            //format tables not split on page breaks.             foreach (microsoft.office.interop.word.table otable in worddocument.tables)             {                 otable.allowpagebreaks = false;                                     otable.rows.allowbreakacrosspages = 0;                                 } 

after further research i'm confused. appears table headers within tr td tag in html when saved word doc within table didn't keep together. above loop i'm not sure why take place.

i lost track of question did resolve , because received many views felt helpful show solution working.

            foreach (microsoft.office.interop.word.table otable in worddocument.tables)             {                 otable.allowpagebreaks = false;                                     otable.rows.allowbreakacrosspages = 0;                                 } 

i've come full circle on issue. need figure out how include label above table break table.

there better way of doing because original format html , business need save html formatted page in word , pdf. problem i'm running programmed saved formats dont identical html , has not been best looking. problem lies size of tables, text, , improper page breaking .


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -