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 May 11, 2021 12:49 pm

Dear sir / ma'am,

With kind of urgency we are trying to figure out how we could use a pdf in a mergefield in a .docx file to make a new pdf. We are using Spire.Doc v8.10.4 (can't upgrade to latest version, still waiting on a fix for numeric notations).

Would greatly appreciate it if you can tell us whether this is possible and how to do it.

Best regards,

Arnout

john.leeuwis@emanon.nl
 
Posts: 12
Joined: Tue Dec 30, 2014 8:43 am

Wed May 12, 2021 7:57 am

Hello,

Thanks for your inquiry.
Sorry, I'm not very clear about your needs. Could you please provide your .docx file with merge fields, your .pdf file, and your desired result file for our reference? Then we will investigate further and provide you with the appropriate solution. You can attach your files here or send them to us via email (support@e-iceblue.com).

Thank you in advance for your assistance.
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Thu May 13, 2021 6:35 am

Hello,

Thanks for sharing your files via email.
I noticed that the mergefield in your template file is an image field, and in your expected result file, the corresponding location is also an image. Therefore, you can first use our Spire.PDF to convert the PDF file to an image, and then use our Spire.Doc to fill merge field with the image. You can refer to the following code to test.
Code: Select all
        static void Main(string[] args)
        {
            string imageName = "img.png";
            string[] fieldNames = new string[] { "OrderHardCopy" };
            string[] fieldValues = new string[] { imageName };
            //Open a Word document
            Document doc = new Document();
            doc.LoadFromFile("example_template.docx");
            //Open a pdf document
            PdfDocument pdf = new PdfDocument();
            pdf.LoadFromFile("example.pdf");
            Image image = pdf.SaveAsImage(0, Spire.Pdf.Graphics.PdfImageType.Bitmap, 500, 500);
            SizeF pageSize = doc.Sections[0].PageSetup.PageSize;

            ////You can directly save the image in its original size
            //image.Save(imageName);

            //Or resize the image
            resizeImage(image, new Size((int)pageSize.Width, image.Height)).Save(imageName);

            //Fill field with image
            doc.MailMerge.MergeImageField += new MergeImageFieldEventHandler(MailMerge_MergeImageField);
            doc.MailMerge.Execute(fieldNames, fieldValues);
            //Save to file
            doc.SaveToFile("result.pdf", Spire.Doc.FileFormat.PDF);
        }
        public static void MailMerge_MergeImageField(object sender, MergeImageFieldEventArgs field)
        {
            string filePath = field.ImageFileName;
            if (!String.IsNullOrEmpty(filePath))
            {
                field.Image = Image.FromFile(filePath);
            }
        }
        public static Image resizeImage(Image imgToResize, Size size)
        {
            //Get width and height of the image
            int sourceWidth = imgToResize.Width;
            int sourceHeight = imgToResize.Height;

            float nPercent = 0;
            float nPercentW = 0;
            float nPercentH = 0;
            //Calculate the zoom ratio of the width
            nPercentW = ((float)size.Width / (float)sourceWidth);
            //Calculate the zoom ratio of the height
            nPercentH = ((float)size.Height / (float)sourceHeight);

            if (nPercentH < nPercentW)
                nPercent = nPercentH;
            else
                nPercent = nPercentW;
            //Expected width
            int destWidth = (int)(sourceWidth * nPercent);
            //Expected height
            int destHeight = (int)(sourceHeight * nPercent);

            Bitmap b = new Bitmap(destWidth, destHeight);
            Graphics g = Graphics.FromImage((System.Drawing.Image)b);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            //Draw image
            g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
            g.Dispose();
            return b;
        }

Besides, please note that our Spire.Doc and Spire.PDF are two independent products. If you have further questions, just feel free to contact us.
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Wed May 19, 2021 10:39 am

Hello,

Has the issue been solved now? Could you please give us some feedback at your convenience?
Thanks in advance.
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Wed May 19, 2021 12:00 pm

Andy.Zhou wrote:Hello,

Has the issue been solved now? Could you please give us some feedback at your convenience?
Thanks in advance.


Hey Andy,

Our apologies for not confirming that it has been solved. So hereby thank you for the swift reply and good customerservice helping us solve it.

Best regards,

Arnout

john.leeuwis@emanon.nl
 
Posts: 12
Joined: Tue Dec 30, 2014 8:43 am

Thu May 20, 2021 12:53 am

Hi Arnout,

Thanks for your response.
Glad to hear that the issue has been solved. If you encounter any issues related to our product in the future, just feel free to contact us.
Have a nice day!
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Return to Spire.Doc