Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Answered

Convert Word document attached with note to PDF.

Posted on by 40

HI, 

I need your help. I want to convert word document attached with note into PDF and send it to customer. I have retrieved note attachment with invoice entity, but unable to convert it into PDF. 

Entity emailCreated = service.Retrieve("email", SourceEmailID, new ColumnSet(true));
QueryExpression QueryNotes = new QueryExpression("annotation");
QueryNotes.ColumnSet = new ColumnSet(new string[] { "subject", "mimetype", "filename", "documentbody" });
QueryNotes.Criteria = new FilterExpression();
QueryNotes.Criteria.FilterOperator = LogicalOperator.And;
QueryNotes.Criteria.AddCondition(new ConditionExpression("objectid", ConditionOperator.Equal, invoiceID.Id));
EntityCollection MimeCollection = service.RetrieveMultiple(QueryNotes);
if (MimeCollection.Entities.Count > 0)
{ //we need to fetch first attachment
Entity NotesAttachment = MimeCollection.Entities.First();
//Create email attachment
Entity EmailAttachment = new Entity("activitymimeattachment");
if (NotesAttachment.Contains("subject"))
EmailAttachment["subject"] = NotesAttachment.GetAttributeValue<string>("subject");
EmailAttachment["objectid"] = new EntityReference("email", emailCreated.Id);
EmailAttachment["objecttypecode"] = "email";
if (NotesAttachment.Contains("filename"))
EmailAttachment["filename"] = NotesAttachment.GetAttributeValue<string>("filename");
if (NotesAttachment.Contains("documentbody"))
EmailAttachment["body"] = NotesAttachment.GetAttributeValue<string>("documentbody");
if (NotesAttachment.Contains("mimetype"))
EmailAttachment["mimetype"] = NotesAttachment.GetAttributeValue<string>("mimetype");

service.Create(EmailAttachment);
}
// Sending email
SendEmailRequest SendEmail = new SendEmailRequest();
SendEmail.EmailId = emailCreated.Id;
SendEmail.TrackingToken = "";
SendEmail.IssueSend = true;
SendEmailResponse res = (SendEmailResponse)service.Execute(SendEmail);

  • Verified answer
    cloflyMao Profile Picture
    cloflyMao 25,198 on at
    RE: Convert Word document attached with note to PDF.

    Hi Naeem,

    The challenge for us to build a custom workflow to do convert with Word2Pdf is that the library requires a local path to read doc file; 

    However, due to original content of doc note is binary data, we need to save note as local file, or the library could provide a function to convert binary data to PDF directly.

    It's highly recommended to achieve such requirement with Flow, which has following good reasons:

    * low code 

    * less time spent

    * easy to use

    Please take a look for the alternative flow:

    Overview:

    pastedimage1587351164773v1.png

    The process is:

    When a contact is selected(like on-demand workflow) -> find the first note file by contact id ->

    Create a temp file to save doc note in Onedrive -> Convert the temp file to PDF as binary data by "Convert file" action ->

    Send an email to selected contact, populate attachment content with converted PDF binary data ->

    delete the temp file when email is sent successfully

    pastedimage1587351457936v2.png

    Result presentation:

    p1:

    Original doc file

    pastedimage1587351504565v3.png

    p2:

    select a contact who has an attached note:

    pastedimage1587351590991v4.png

    p3:

    Delivered email with converted pdf

    pastedimage1587351657790v5.png

    p4:

    Converted file preview:

    pastedimage1587351712290v6.png

    Regards,

    Clofly

  • Suggested answer
    Justinjose Profile Picture
    Justinjose 2,707 on at
    RE: Convert Word document attached with note to PDF.

    Hi Naeem Khan,

    I dont think that the Word2Pdf reference will work on custom workflow or plugin.

    You can use Microsoft Flow to convert Word to Pdf. Here is an example -  dynamics365blocks.wordpress.com/.../

    Thanks

    Justin Jose

  • Naeem Khan  Profile Picture
    Naeem Khan 40 on at
    RE: Convert Word document attached with note to PDF.

    Hi Justin,

    Right now i am using word2pdf library, but i don't know how to utilize it. As you can see code below. I don't how i could use it in my case as i am not loading word file from any path.  If you know any library which can be used, kindly share with me.

     Word2Pdf objWorPdf = new Word2Pdf();

                   string backfolder1 = "D:\\PDFFile";            

                   string strFileName = "Invoice.docx";

                   object FromLocation = backfolder1 + "\\" + strFileName;

                   string FileExtension = Path.GetExtension(strFileName);

                   string ChangeExtension = strFileName.Replace(FileExtension, ".pdf");

                   if (FileExtension == ".doc" || FileExtension == ".docx")

                   {

                       object ToLocation = backfolder1 + "\\" + ChangeExtension;

                       objWorPdf.InputLocation = FromLocation;

                       objWorPdf.OutputLocation = ToLocation;

                       objWorPdf.Word2PdfCOnversion();

                   }

    thanks,

  • Justinjose Profile Picture
    Justinjose 2,707 on at
    RE: Convert Word document attached with note to PDF.

    Hi Naeem Khan,

    are you using any library (References) to convert to pdf? I cant see PDF conversion step in the above code

    Thanks

    Justin Jose

  • Naeem Khan  Profile Picture
    Naeem Khan 40 on at
    RE: Convert Word document attached with note to PDF.

    Hi Justin,

    It is a custom workflow.

    thanks,

  • Justinjose Profile Picture
    Justinjose 2,707 on at
    RE: Convert Word document attached with note to PDF.

    Hi Naeem Khan,

    Is this an external application or a plugin?

    Thanks

    Justin Jose

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans