Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Tue Apr 02, 2024 12:08 pm

Hi,
I have requirement to replace merge field with content of another document.
I am doing this is using below code.


using (Document newDoc = new Document(filePath))
{
Section parentSection = null;
int index = 0;
foreach (var item in args.CurrentMergeField.Document.Sections)
{
if ((item as Section).Paragraphs.IndexOf(args.CurrentMergeField.OwnerParagraph) >= 0)
{
parentSection = item as Section;
index = (item as Section).Paragraphs.IndexOf(args.CurrentMergeField.OwnerParagraph);
break;
}
}


foreach (Section section in newDoc.Sections)
{

foreach (Paragraph paragraph in section.Paragraphs)
{
parentSection.AddParagraph();
parentSection.Paragraphs.Insert(index + 1, paragraph.Clone() as IParagraph);
index++;
}

}
}

However because of this multiple blank pages getting added to document.
Could you please look into this? and provide a workable solution.

hemantahire
 
Posts: 10
Joined: Tue Feb 28, 2023 9:58 am

Wed Apr 03, 2024 2:47 am

Hi,

Thanks for your question.
According to your code, you are inserting a document’s paragraph into mergeField location of the other document, afterwards the multiple blank page are added into the result document, right? If so, please offer the complete code that can reproduce your issue and the input document, you can send it via email ([email protected]) or attach it here.
By the way, for operating MergeField or merge email, you can refer to help document from our website through the following link:
https://www.e-iceblue.com/Tutorials/Spi ... Merge.html

Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Mon Aug 05, 2024 10:03 am

private void InsertAttachment(SpireTemplateFieldTag tagDetails, MergeFieldEventArgs args)
{
var fieldDetails = !tagDetails.IsSystemTag ? _valuesProvider.GetFieldDetails(tagDetails.FieldName) : null;
var field = (fieldDetails as UserInputField);

if (string.IsNullOrEmpty(field.Value) && string.IsNullOrEmpty(field.SystemFileName))
{
args.Text = "";
return;
}

byte[] bytes = null;
var extension = Path.GetExtension(field.OriginalFileName);

if (field.Value.Length > 0)
{
string data = string.Empty;
if (extension.ToLower() == FileConsts.Extensions.Docx)
{
data = (field as AttachmentField).Value.Replace(docxDeLimiter, string.Empty);
}
else
{
data = (field as AttachmentField).Value.Replace(docDeLimiter, string.Empty);
}

try
{
bytes = Convert.FromBase64String(data);
}
catch
{
args.Text = "";
return;
}
}
else if (field.SystemFileName.Length > 0)
{
bytes = _fileStorageWrapper.GetBytesForFile(field.SystemFileName, FileStorageDirectory.QuestionnaireAttachments);
}


string filePath = tempFolder + field.BackendTag + requestIdValue + extension;
File.WriteAllBytes(filePath, bytes);

using (Document newDoc = new Document(filePath))
{
Section parentSection = null;
int index = 0;
foreach (var item in args.CurrentMergeField.Document.Sections)
{
index = (item as Section)?.Body.ChildObjects.IndexOf(args.CurrentMergeField.OwnerParagraph) ?? -1;
if (index >= 0)
{
parentSection = item as Section;
break;
}
}

if (parentSection != null)
{
foreach (Section section in newDoc.Sections)
{
foreach (DocumentObject child in section.Body.ChildObjects)
{
parentSection.Body.ChildObjects.Insert(index + 1, child.Clone());
index++;
}
}
}
}

if (File.Exists(filePath)) File.Delete(filePath);
}[code][/code]

hemantahire
 
Posts: 10
Joined: Tue Feb 28, 2023 9:58 am

Mon Aug 05, 2024 10:12 am

I am calling InsertAttachment in MailMerge_MergeField

hemantahire
 
Posts: 10
Joined: Tue Feb 28, 2023 9:58 am

Tue Aug 06, 2024 8:13 am

Hello,

Thank you for your feedback.
I have reviewed your document, but I did not find any "MailMergeField" within it. As a result, I created a test Word document that includes "MailMergeField" and inserted your provided document as a newDoc into the original document. However, the resulting file did not display the issue of additional blank pages as you mentioned.
I have uploaded my code, the test files, and the result file as attachments for your reference.
Furthermore, I conducted the test using the latest version of Spire.Doc, which is 12.7.3. If you are not using this version, please upgrade to the latest version before testing as it includes more bug fixes. If you continue to experience issues after testing with the latest version, please provide us with another input file containing "MailMergeField" so that we can further investigate the issue. Thank you for your cooperation.

Spire.Doc 12.7.3: https://www.e-iceblue.com/Download/download-word-for-net-now.html

Sincerely,
Doris
E-iceblue support team
User avatar

Doris.Liu
 
Posts: 460
Joined: Mon Nov 07, 2022 8:10 am

Return to Spire.Doc