none
Merging word documents (orientation (portrait/landscape) problem) RRS feed

  • Question

  • Hi,

    I am trying to merge word documents in sharepoint document library. Some pages in the docs are in portrait and some in landscape. after merging documents all the pages in the documents r displayed in portrait mode. how can i retain page orientation programmatically ?

    fyi...i think we can do it by inserting section properties after each page or each document.

    here is  my code

    Appreciate your help..

                foreach (SPFile item in listitem.Folder.Files)

                {

                  //  SPFile inputFile = item.File;

                    SPFile inputFile = item;

     

                    string altChunkId = "AltChunkId" + id;

                    id++;

                    byte[] byteArray = inputFile.OpenBinary();

     

                    AlternativeFormatImportPart chunk = outputDoc.MainDocumentPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.WordprocessingML,

                        altChunkId);

     

                    using (MemoryStream mem = new MemoryStream())

                    {

                        mem.Write(byteArray, 0, (int)byteArray.Length);

                        mem.Seek(0, SeekOrigin.Begin);

                        chunk.FeedData(mem);

                    }

     

                    AltChunk altChunk = new AltChunk();

                    altChunk.Id = altChunkId;

     

                    outputDoc.MainDocumentPart.Document.Body.InsertAfter(altChunk,

                        outputDoc.MainDocumentPart.Document.Body.Elements<Paragraph>().Last());

                    outputDoc.MainDocumentPart.Document.Save();

                }

     

                outputDoc.Close();

     

                memOut.Seek(0, SeekOrigin.Begin);

     

                ClientContext clientContext = new ClientContext(SPContext.Current.Site.Url);

                ClientOM.File.SaveBinaryDirect(clientContext, outputPath, memOut, true);

     

                // Conversion

     


     


    suryakumarp
    Tuesday, January 25, 2011 4:35 PM

Answers

  • Hi Suryakumar,

    That is correct you will need to create a new section to set different orientations. Easiest way to code it using Open XML SDK will be to create a word document, set orientation to portrait, insert a new section break, set the orientation to landscape for the new section and Open the document in OpenXmlSdkTool.exe tools (comes with Open XML SDK 2.0 download) to see the code generated for the new section. Same code you can reuse in your application.

    Only complication which could occur is if the new document being merged has multiple sections. In such cases, you will manually need to derive an strategy based on your requirements.

     

    thanks,


    Shiv Khare
    Tuesday, February 1, 2011 10:00 PM
    Moderator