Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inserting hyperlinks completes yet resulting file is corrupted #453

Open
CGarvizu opened this issue Apr 19, 2023 · 3 comments
Open

Inserting hyperlinks completes yet resulting file is corrupted #453

CGarvizu opened this issue Apr 19, 2023 · 3 comments

Comments

@CGarvizu
Copy link

When I insert a paragraph that contains some hyperlink (email, url, etc.) from one document to another file, the resulting file cannot be opened because it is corrupted. Allowing Word to repair it works, yet the hyperlinks are gone.

I thought it was the format of the document, yet tried using a simple file with one paragraph and one link and confirmed the issue. Removing the hyperlink makes it work normally.

Thanks!

@XceedBoucherS
Copy link
Collaborator

Hi,

Actualy I made it work with the latest v2.4 version of DocX.
Here's what I did :
-Create Docx1.docx with a single paragraph
-Create DocX2.docx with a single paragraph containing an hyperlink,
-Insert Doc2.docx at end of Doc1.docx.
-Save Doc1.docx.

` var doc1 = DocX.Load( "Doc1.docx" );
var doc2 = DocX.Load( "Doc2.docx" );

  doc1.InsertDocument( doc2 );

  doc1.SaveAs( "output.docx" );`

Results: Doc1.docx contains 2 pages. On first page, its the original paragraph. On second page, it the paragraph from Doc2.docx with its hyperlink. Clicking on the hyperlink works.

If this is not what you did, please give more details.

Thank you.

@CGarvizu
Copy link
Author

Thanks for the prompt response!

I am trying to insert a document (subDocument) within another one (mainDocument), rather than appending subDocument to the end of mainDocument.

I did something like this (edited), and it all works well as long as there are no links. I am using DocX 2.4.

using (DocX mainDocument = DocX.Load(mainDocumentPath))
{  
  //Locate the paragraph that contains the bookmark
  Bookmark b = mainDocument.Bookmarks.Where(x => x.Name.ToLower() == bookmarkName.ToLower()).FirstOrDefault();
  if (b == null) return 0;

  using (DocX subDocument = DocX.Load(subDocumentPath))
  {
    Paragraph p = b.Paragraph;   
    foreach (var par in subDocument.Paragraphs)
    {
      p = p.InsertParagraphAfterSelf(par);            
    }
  }  
 }

@XceedBoucherS
Copy link
Collaborator

Hi,

Thanks for the details.

I can see the problem.
When we call doc1.InsertDocument( doc2 ), all the relationships and styles are copied from doc2 to doc1.
When we call p1.InsertParagraphAfterSelf(p2), only the p2 content is copied from doc2 to doc1 ; all the relationships and styles from p2 must be copied to doc1.

We will look into this.
Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants