Example #1
0
        private static IInlineShape AddPictureToSelection(ISelection selection, string tmpFile)
        {
            IInlineShape shape = selection.InlineShapes.AddPicture(tmpFile, false, true, Type.Missing);

            // Lock aspect ratio
            if (config.WordLockAspectRatio)
            {
                shape.LockAspectRatio = MsoTriState.msoTrue;
            }
            selection.InsertAfter("\r\n");
            return(shape);
        }
Example #2
0
 /// <summary>
 /// Internal method for the insert
 /// </summary>
 /// <param name="wordApplication"></param>
 /// <param name="wordDocument"></param>
 /// <param name="tmpFile"></param>
 /// <param name="adress">link for the image</param>
 /// <param name="tooltip">tooltip of the image</param>
 /// <returns></returns>
 internal static bool InsertIntoExistingDocument(IWordApplication wordApplication, IWordDocument wordDocument, string tmpFile, string address, string tooltip)
 {
     // Make sure the selected document is active, otherwise the insert will be made in a different document!
     try {
         wordApplication.Activate();
     } catch {
     }
     if (wordApplication.Selection != null)
     {
         // Add Picture
         using (IInlineShape shape = AddPictureToSelection(wordApplication.Selection, tmpFile)) {
             if (!string.IsNullOrEmpty(address))
             {
                 object screentip = Type.Missing;
                 if (!string.IsNullOrEmpty(tooltip))
                 {
                     screentip = tooltip;
                 }
                 try {
                     wordDocument.Hyperlinks.Add(shape, screentip, Type.Missing, screentip, Type.Missing, Type.Missing);
                 } catch (Exception e) {
                     LOG.WarnFormat("Couldn't add hyperlink for image: {0}", e.Message);
                 }
             }
         }
         try {
             wordDocument.ActiveWindow.ActivePane.View.Zoom.Percentage = 100;
         } catch (Exception e) {
             if (e.InnerException != null)
             {
                 LOG.WarnFormat("Couldn't set zoom to 100, error: {0}", e.InnerException.Message);
             }
             else
             {
                 LOG.WarnFormat("Couldn't set zoom to 100, error: {0}", e.Message);
             }
         }
         try {
             wordApplication.Activate();
         } catch {}
         try {
             wordDocument.Activate();
         } catch {}
         try {
             wordDocument.ActiveWindow.Activate();
         } catch {}
         return(true);
     }
     return(false);
 }
Example #3
0
 /// <summary>
 /// Helper method to add the file as image to the selection
 /// </summary>
 /// <param name="selection"></param>
 /// <param name="tmpFile"></param>
 /// <returns></returns>
 private static IInlineShape AddPictureToSelection(ISelection selection, string tmpFile)
 {
     using (IInlineShapes shapes = selection.InlineShapes) {
         IInlineShape shape = shapes.AddPicture(tmpFile, false, true, Type.Missing);
         // Lock aspect ratio
         if (OfficeConfig.WordLockAspectRatio)
         {
             shape.LockAspectRatio = MsoTriState.msoTrue;
         }
         selection.InsertAfter("\r\n");
         selection.MoveDown(WdUnits.wdLine, 1, Type.Missing);
         return(shape);
     }
 }
Example #4
0
 public static void InsertIntoNewDocument(string tmpFile, string address, string tooltip)
 {
     using (IWordApplication wordApplication = GetOrCreateWordApplication()) {
         if (wordApplication == null)
         {
             return;
         }
         wordApplication.Visible = true;
         wordApplication.Activate();
         // Create new Document
         object template        = string.Empty;
         object newTemplate     = false;
         object documentType    = 0;
         object documentVisible = true;
         using (IDocuments documents = wordApplication.Documents) {
             using (IWordDocument wordDocument = documents.Add(ref template, ref newTemplate, ref documentType, ref documentVisible)) {
                 using (ISelection selection = wordApplication.Selection) {
                     // Add Picture
                     using (IInlineShape shape = AddPictureToSelection(selection, tmpFile)) {
                         if (!string.IsNullOrEmpty(address))
                         {
                             object screentip = Type.Missing;
                             if (!string.IsNullOrEmpty(tooltip))
                             {
                                 screentip = tooltip;
                             }
                             try {
                                 using (IHyperlinks hyperlinks = wordDocument.Hyperlinks) {
                                     hyperlinks.Add(shape, screentip, Type.Missing, screentip, Type.Missing, Type.Missing);
                                 }
                             } catch (Exception e) {
                                 LOG.WarnFormat("Couldn't add hyperlink for image: {0}", e.Message);
                             }
                         }
                     }
                 }
                 try {
                     wordDocument.Activate();
                 } catch {
                 }
                 try {
                     wordDocument.ActiveWindow.Activate();
                 } catch {
                 }
             }
         }
     }
 }
Example #5
0
 /// <summary>
 /// Internal method for the insert
 /// </summary>
 /// <param name="wordApplication"></param>
 /// <param name="wordDocument"></param>
 /// <param name="tmpFile"></param>
 /// <param name="adress">link for the image</param>
 /// <param name="tooltip">tooltip of the image</param>
 /// <returns></returns>
 internal static bool InsertIntoExistingDocument(IWordApplication wordApplication, IWordDocument wordDocument, string tmpFile, string address, string tooltip)
 {
     // Bug #1517: image will be inserted into that document, where the focus was last. It will not inserted into the chosen one.
     // Solution: Make sure the selected document is active, otherwise the insert will be made in a different document!
     try {
         wordDocument.Activate();
     } catch {
     }
     using (ISelection selection = wordApplication.Selection) {
         if (selection == null)
         {
             LOG.InfoFormat("No selection to insert {0} into found.", tmpFile);
             return(false);
         }
         // Add Picture
         using (IInlineShape shape = AddPictureToSelection(selection, tmpFile)) {
             if (!string.IsNullOrEmpty(address))
             {
                 object screentip = Type.Missing;
                 if (!string.IsNullOrEmpty(tooltip))
                 {
                     screentip = tooltip;
                 }
                 try {
                     using (IHyperlinks hyperlinks = wordDocument.Hyperlinks) {
                         hyperlinks.Add(shape, screentip, Type.Missing, screentip, Type.Missing, Type.Missing);
                     }
                 } catch (Exception e) {
                     LOG.WarnFormat("Couldn't add hyperlink for image: {0}", e.Message);
                 }
             }
         }
         try {
             using (IWordWindow activeWindow = wordDocument.ActiveWindow) {
                 activeWindow.Activate();
                 using (IPane activePane = activeWindow.ActivePane) {
                     using (IWordView view = activePane.View) {
                         view.Zoom.Percentage = 100;
                     }
                 }
             }
         } catch (Exception e) {
             if (e.InnerException != null)
             {
                 LOG.WarnFormat("Couldn't set zoom to 100, error: {0}", e.InnerException.Message);
             }
             else
             {
                 LOG.WarnFormat("Couldn't set zoom to 100, error: {0}", e.Message);
             }
         }
         try {
             wordApplication.Activate();
         } catch {
         }
         try {
             wordDocument.Activate();
         } catch {
         }
         return(true);
     }
 }