Book a Demo

Author Topic: How to put Element.LinkedDocument into Word Doc  (Read 3738 times)

pisokol

  • EA User
  • **
  • Posts: 26
  • Karma: +0/-0
  • I love YaBB 1G - SP1!
    • View Profile
How to put Element.LinkedDocument into Word Doc
« on: October 11, 2014, 07:30:14 am »
I write (c#, .net framework 4.0) Add-in for Enterprise Architect and I have problem with formating file inserting into Word document. This file (RTF) I'm reading from Element.LinkedDocument (Repository.GetElementByID(kl.ElementID).GetLinkedDocument()), inserting to doc (Range.InsertFile()) and it is working. But sometimes size (vertical or horizontal) of file in doc is to big, and I must (how?) to resize it to maximum in page document (autoresize, if its possible).
Second problem is programming of attributes "Divide Diagram into Multiple Pages" or "Print Advanced" (GUI Diagram->Properties->Diagram-RTF Document Options and Advanced). I can't find it in allowed to use API methods. It is only stored in object structure (only in table t_diagram)?
Do you have any ideas how can I expand my code?
Code: [Select]
String linkedDocument = Repository.GetElementByID(kl.ElementID).GetLinkedDocument();
if (linkedDocument.Length > 0)
{
  String tmpFileName = System.IO.Path.GetTempFileName() + ".rtf";
  using (StreamWriter outfile = new StreamWriter(tmpFileName))
    outfile.Write(linkedDocument);

  dokMD.Paragraphs.Add();
  par = dokMD.Paragraphs.Last;
  par.set_Style("Normal");
  object brak = System.Reflection.Missing.Value;
  par.Range.InsertFile(tmpFileName, brak, false);
  //... autosize
  File.Delete(tmpFileName);
  System.IO.File.Delete(tmpFileName);
}

It is another way to put formated LinkedDokument into Word dokument?



pisokol

  • EA User
  • **
  • Posts: 26
  • Karma: +0/-0
  • I love YaBB 1G - SP1!
    • View Profile
Re: How to put Element.LinkedDocument into Word Do
« Reply #1 on: October 15, 2014, 01:09:33 am »
I already have the solution
1. I create the main document, the first page in the first section, I set the orientation as I want and I add new next section.
2. I read LinkedDocument file to rtf file, then rtf to another doc. Next I read each sections (by .Copy ()), I read its PageSetup.Orientation, I add a paragraph into the main document, I set PageSetup.Orientation main document to be compatible with the document (doc with LinkedDocument), I add it into the main document (.PasteAndFormat ()).
3. At the end I delete doc file with LinkedDocument

and it looks like ...

Code: [Select]
              dokMD = word.Documents.Add(<template.dot>, ...)
                secDokMDFirst = dokMD.Sections.First;
                secDokMDFirst.Range.PageSetup.Orientation = WdOrientation.wdOrientPortrait;

                EA.IDualElement el = Repository.GetElementByID(kl.ElementID);
                string linkedDocument = el.GetLinkedDocument();
                if (linkedDocument.Length > 0)
                {
                    String myXMLfileName = System.IO.Path.GetTempFileName() + ".rtf";
                    using (StreamWriter outfile = new StreamWriter(myXMLfileName))
                        outfile.Write(linkedDocument);
                    WdOrientation docLDPSO = WdOrientation.wdOrientPortrait;
                    object missing = Missing.Value;
                    object dokFormat = WdOpenFormat.wdOpenFormatRTF;
                    Word._Document docLD = word.Documents.Open(myXMLfileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref dokFormat, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
                    if (docLD.Words.Count > 0)
                    {
                        docLD.SaveAs(myXMLfileName, WdSaveFormat.wdFormatRTF);
                                            }
                    dokMD.Sections.Add();
                    int idx = 0;
                    foreach (Section secdocLD in docLD.Sections)
                    {
                        secdocLD.Range.Copy();
                        idx = secdocLD.Index;
                        docLDPSO = secdocLD.PageSetup.Orientation;

                        dokMD.Activate();
                        dokMD.Paragraphs.Add();
                        par = dokMD.Paragraphs.Last;
                        
                        secDokMDLast = dokMD.Sections.Last;
                        secDokMDLast.PageSetup.Orientation = docLDPSO;
                     par.Range.PasteAndFormat(WdRecoveryType.wdFormatOriginalFormatting);
                    }
                    docLD.Close(false);
                    System.IO.File.Delete(myXMLfileName);
                    Marshal.FinalReleaseComObject(docLD);
               }

OpenIT Solutions

  • EA User
  • **
  • Posts: 555
  • Karma: +9/-1
    • View Profile
Re: How to put Element.LinkedDocument into Word Do
« Reply #2 on: October 15, 2014, 09:55:09 pm »
Hi,

Another approve to consider would be to use the Repository.DocumentGenerator class on the Sparx Automation interface and define document templates in Sparx that contain the info you want to extract. So you could create a template (x) that just had:

package >
element >
linked document >
< linked document
< element
< package

Then use DocumentGenerator.DocumentElement (elementID, x). There is an example that uses the DocumentGenerator available in the sample scripts.

Regards,

Jon.
« Last Edit: October 15, 2014, 09:57:55 pm by openit »

pisokol

  • EA User
  • **
  • Posts: 26
  • Karma: +0/-0
  • I love YaBB 1G - SP1!
    • View Profile
Re: How to put Element.LinkedDocument into Word Do
« Reply #3 on: October 21, 2014, 11:03:06 pm »
Thx, this proposed solution looks easier and resolved problems with formating linked document (orientations of included pages), but... :)
Problem is with settings orientation on first page included generated RTF document. If orientation current section of main document is not same like generated RTF dokument, this generated RTF document is included in current orientation into the current section of main document (and it's not correct). Second and the next section in generated RTF document sets orientation of main dokument correctly (automaticly add a new section into the main document).

I resolved it in this way and I think it is complete solution to this problem:

After generation RTF document (with element.linkeddcoument), I open this file as Word document (RTF format) and read Sections[1].PageSetup.Orientation. If orientation is not same like orientation current section of main document, I add a new section into the main document, set required orientation and insert generated RTF file. On the end, after inserting whole generated RTF document, I set original orientation of main document.

Regards,
pisokol