Extract a string from Doc

I need to extract the values for Patient Name, Claim and Acc Number from the attached Doc

Hi Vinod,

Thanks for your inquiry. You can get the text of Paragraphs containing particular strings as follows:

Document doc = new Document(MyDir + @"sample.docx");
foreach (Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true))
{
    string text = para.ToString(SaveFormat.Text);
    if (text.Contains("Patient:") || text.Contains("Claim:") || text.Contains("Acct No.:"))
        Console.WriteLine(text);
}

Best regards,

Thanks for your response, Can you please let us know if there is an option of getting the values directly from the stream writing it to a memory stream instead of writing it to a file again?

Hi Vinod,

Thanks for your inquiry. Please check the following articles:

Opening from a Stream
Saving to a Stream

Hope, this helps.

Best regards,

Hi There.

I am using the Replace function of Aspose to replace a string in a document, I wanna know how do we handle null exception if I do not have any dynamic value with which I would replace the existing:

doc.Range.Replace("<SUBSCRIBER / POLICY>", "", false, false);

Thanks & Regards,

Vinod

Hi Vinod,

Thanks for your inquiry. Please attach the following resources here for testing:

  • Your input Word document
  • Please create a standalone Console application (source code without compilation errors) that helps us reproduce your problem on our end and attach it here for testing…

As soon as you get these pieces of information ready, we’ll start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip them and Click ‘Reply’ button that will bring you to the ‘reply page’ and there at the bottom you can include any attachments with that post by clicking the ‘Add/Update’ button.

Best regards,

Hi There,

I am using the below code to find and replace: I want to know how do we handle the Null point exception if the values to replace are Null, like reqs.payerName have a null value.

doc.Range.Replace("", reqs.payerName, false, false);
doc.Range.Replace("", reqs.PatientName, false, false);
doc.Range.Replace("<SUBSCRIBER / POLICY>", reqs.Subscriber_Policy, false, false);

Thanks & Regards,
Vinod

Hi Vinod,

Thanks for your inquiry. You can use following code to determine if the incoming value is null or not:

if (!string.IsNullOrEmpty(reqs.payerName))
    doc.Range.Replace("", reqs.payerName, false, false);

Hope, this helps.

Best regards,

Hi There,

I am trying to extract a String Value from a Document (attached) using the below code:

foreach (Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true))
{
    string text = para.ToString(SaveFormat.Text);
    if (text.Contains("Unit#:"))
        String UnitNum = text.Replace("Unit#:", "").Trim().PadLeft(5, ‘0’);
}

I am expecting the result to be “01234” but instead I get the result as "1234

Second Level Appeal
Via Certified Mail "

Can you please help me in getting the desired result, I should not get the added lines and strings "Second Level Appeal Via Certified Mail “”

Thanks & Regards,

Vinod

Hi Vinod,

Thanks for your inquiry. The problem occurs because a Paragraph has a Shape with three child Paragraphs. Please try using the following code:

Document doc = new Document(MyDir + @"test.docx");
Shape shape = (Shape)doc.GetChildNodes(NodeType.Shape, true)[0];
foreach (Paragraph para in shape.GetChildNodes(NodeType.Paragraph, true))
{
    string text = para.ToString(SaveFormat.Text);
    if (text.Contains("Unit#:"))
    {
        String UnitNum = text.Replace("Unit#:", "").Trim().PadLeft(5, '0');
        Console.WriteLine(UnitNum);
    }
}

Hope, this helps.

Best regards,

Thank You for your response,

I have tried with the suggested code and now the value it returns is null.

Please suggest,

Thanks

Actually adding the below code helped to work it out:

foreach (Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true))
{
    string text = para.ToString(SaveFormat.Text);
    if (text.Contains("Unit#:"))
        onBaseResponse.UnitNum = text.Replace("Unit#:", "").Split(new[] { ‘\r’, ‘\n’ }).FirstOrDefault().Trim().PadLeft(5, ‘0’);
}

Thank You,

Vinod

Hi Vinod,

It is great you were able to find what you were looking for. Please let us know any time you have any further queries.

Best regards,