Problem printing word document with table layout

Please find attached a couple of files showing problems printing a Word Document. The document opens correctly in MSWord but in Aspose.Words page (pages 5 & 6 of the PDF) prints on two different pages instead of the one. The PDF shows exactly how it prints to the printer which is why I have raised it here rather than the PDF forum.Apologies that there are other pages in the PDF document but the only one I am interested in is X1151 (pages 5 & 6) which should be obvious the moment you open it…

I am using the latest version of Aspose.Words (10.5.0.0). & C# 4.

The page is loaded using new Document(filename) as part of a larger process.

Please can you take a look at both files and suggest a possible solution?

thanks
Jon

Hi
Thanks for your request. I have tested with Aspose.Words 10.5.0 and I cannot reproduce the problem on my side. I use the following code for testing:

Document doc = new Document("C:\\Temp\\X1151_V001.doc");
doc.Save("C:\\Temp\\out.pdf", SaveFormat.Pdf);

Best regards,

hmmm. Very strange! Thanks for getting back so quickly!

The file is however being slightly mangled (box edges missing) when doing the following though:-

Document doc = new Document("C:\Temp\X1151_V001.doc");
doc.Save("C:\\Temp\\out.pdf", SaveFormat.Pdf);
doc.Save("C:\\Temp\\out.doc", SaveFormat.Doc);

Do you have any suggestion for what might be causing that? I have attached the results for you to look at.

Will experiment to try and get the same 2 page effect but in the meantime if you could look into the box edges missing…
Thanks
Jon

Hello
Thank you for reporting this problem to us and for the additional information. I managed to reproduce the problem on my side. Your request has been linked to the appropriate issue. You will be notified as soon as it is fixed.

Best regards,

Found the cause! It is a watermark I am adding but I have no idea what I can do to fix it and still keep the watermark… Suggestions or workarounds?
Code below…

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Aspose.Words;
using System.IO;
using Aspose.Words.Drawing;
using System.Drawing;
 
namespace testpdf
{
    class Program
    {
        static void Main(string[] args)
        {
            Aspose.Words.License wordLicense = new Aspose.Words.License();
            wordLicense.SetLicense("Aspose.Total.lic");
            Aspose.Pdf.License pdfLicense = new Aspose.Pdf.License();
            pdfLicense.SetLicense("Aspose.Total.lic");
 
 
            Document doc = new Document("C:\\Temp\\X1151_V001.doc");
            AddWatermarkText(doc, "Dev");
 
            using (MemoryStream ms = new MemoryStream())
            {
                doc.Save(ms, SaveFormat.Pdf);
                Aspose.Pdf.Document tmpPDF = new Aspose.Pdf.Document(ms);
                tmpPDF.Save("C:\\Temp\\outPDF.pdf");
 
            }
 
            doc.Save("C:\\Temp\\out.pdf", SaveFormat.Pdf);
            doc.Save("C:\\Temp\\out.doc", SaveFormat.Doc);
        }
        /// <summary>
        /// Inserts a watermark into a document.
        /// </summary>
        /// <param name="doc">The input document.</param>
        /// <param name="watermarkText">Text of the watermark.</param>
        public static void AddWatermarkText(Document doc, string watermarkText)
        {
            // Create a watermark shape. This will be a WordArt shape.
            // You are free to try other shape types as watermarks.
            Shape watermark = new Shape(doc, Aspose.Words.Drawing.ShapeType.TextPlainText);
 
            // Set up the text of the watermark.
            watermark.TextPath.Text = watermarkText;
            watermark.TextPath.FontFamily = "Arial";
            watermark.Width = 500;
            watermark.Height = 100;
            // Text will be directed from the bottom-left to the top-right corner.
            watermark.Rotation = -40;
            // Remove the following two lines if you need a solid black text.
            watermark.Fill.Color = Color.LightGray;
            watermark.StrokeColor = Color.LightGray;
 
            // Place the watermark in the page center.
            watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
            watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;
            watermark.WrapType = WrapType.None;
            watermark.VerticalAlignment = VerticalAlignment.Center;
            watermark.HorizontalAlignment = HorizontalAlignment.Center;
 
            // Create a new paragraph and append the watermark to this paragraph.
            Paragraph watermarkPara = new Paragraph(doc);
            watermarkPara.AppendChild(watermark);
 
            // Insert the watermark into all headers of each document section.
            foreach (Section sect in doc.Sections)
            {
                // There could be up to three different headers in each section, since we want
                // the watermark to appear on all pages, insert into all headers.
                AddWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderPrimary);
                AddWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderFirst);
                AddWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderEven);
            }
            // Cleanup
            watermark = null;
        }
 
        /// <summary>
        /// Inserts the watermark into header.
        /// </summary>
        /// <param name="watermarkPara">The watermark para.</param>
        /// <param name="sect">The sect.</param>
        /// <param name="headerType">Type of the header.</param>
        public static void AddWatermarkIntoHeader(Paragraph watermarkPara, Section sect, HeaderFooterType headerType)
        {
            HeaderFooter header = sect.HeadersFooters[headerType];
 
            if (header == null)
            {
                // There is no header of the specified type in the current section, create it.
                header = new HeaderFooter(sect.Document, headerType);
                sect.HeadersFooters.Add(header);
            }
 
            // Insert a clone of the watermark into the header.
            header.AppendChild(watermarkPara.Clone(true));
            // Cleanup
            header = null;
        }
 
    }
}

(apologies - excitedly posted my PDF testing in there too - here it is without) - thanks for your amazingly fast replies btw!

using System.Drawing;
using Aspose.Words;
using Aspose.Words.Drawing;
 
namespace testpdf
{
    class Program
    {
        static void Main(string[] args)
        {
            Aspose.Words.License wordLicense = new Aspose.Words.License();
            wordLicense.SetLicense("Aspose.Total.lic");
 
 
            Document doc = new Document("C:\\Temp\\X1151_V001.doc");
            AddWatermarkText(doc, "Dev");
 
            doc.Save("C:\\Temp\\out.pdf", SaveFormat.Pdf);
            doc.Save("C:\\Temp\\out.doc", SaveFormat.Doc);
        }
        /// <summary>
        /// Inserts a watermark into a document.
        /// </summary>
        /// <param name="doc">The input document.</param>
        /// <param name="watermarkText">Text of the watermark.</param>
        public static void AddWatermarkText(Document doc, string watermarkText)
        {
            // Create a watermark shape. This will be a WordArt shape.
            // You are free to try other shape types as watermarks.
            Shape watermark = new Shape(doc, Aspose.Words.Drawing.ShapeType.TextPlainText);
 
            // Set up the text of the watermark.
            watermark.TextPath.Text = watermarkText;
            watermark.TextPath.FontFamily = "Arial";
            watermark.Width = 500;
            watermark.Height = 100;
            // Text will be directed from the bottom-left to the top-right corner.
            watermark.Rotation = -40;
            // Remove the following two lines if you need a solid black text.
            watermark.Fill.Color = Color.LightGray;
            watermark.StrokeColor = Color.LightGray;
 
            // Place the watermark in the page center.
            watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
            watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;
            watermark.WrapType = WrapType.None;
            watermark.VerticalAlignment = VerticalAlignment.Center;
            watermark.HorizontalAlignment = HorizontalAlignment.Center;
 
            // Create a new paragraph and append the watermark to this paragraph.
            Paragraph watermarkPara = new Paragraph(doc);
            watermarkPara.AppendChild(watermark);
 
            // Insert the watermark into all headers of each document section.
            foreach (Section sect in doc.Sections)
            {
                // There could be up to three different headers in each section, since we want
                // the watermark to appear on all pages, insert into all headers.
                AddWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderPrimary);
                AddWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderFirst);
                AddWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderEven);
            }
            // Cleanup
            watermark = null;
        }
 
        /// <summary>
        /// Inserts the watermark into header.
        /// </summary>
        /// <param name="watermarkPara">The watermark para.</param>
        /// <param name="sect">The sect.</param>
        /// <param name="headerType">Type of the header.</param>
        public static void AddWatermarkIntoHeader(Paragraph watermarkPara, Section sect, HeaderFooterType headerType)
        {
            HeaderFooter header = sect.HeadersFooters[headerType];
 
            if (header == null)
            {
                // There is no header of the specified type in the current section, create it.
                header = new HeaderFooter(sect.Document, headerType);
                sect.HeadersFooters.Add(header);
            }
 
            // Insert a clone of the watermark into the header.
            header.AppendChild(watermarkPara.Clone(true));
            // Cleanup
            header = null;
        }
 
    }
}

Hello
Thank you for additional infornation. It is expected behavior. If you try doing the same in MS Word you will get exactly the same result. In your case you should change your template to avoid this behavior upon adding Header.
Best regards,

Hi Andrey,
Thanks for your swift response…

In which case, how can I achieve the attached result of a watermark in front of the text. My watermark code came from several aspose replies such as <a href="Adding Watermark to center of document without creating a dummy header or footer?

thanks
Jon

Hello
Thanks for additional information. Watermark in MS Word document is just a shape inserted in document’s header (Aspose.Words code does this). All content of document’s header/footer is always behind the main content of the document.
Your watermark should be inserted in to the document’s header, to be repeated on each page.
Do you need a watermark on each page of the output document or on this page only? If you need this watermark on this page only, then I think, you can achieve what you are asking for. In this case, you can just insert a watermark into the main body as image.
Best regards,

Hi there, Using MSWord, I was able to add the word art in the sample document without disrupting the layout which doesn’t happen when I do the same with the aspose tool. Is there something in the code I am missing which is causing this to happen? In addition, I tried creating a zero height header and footer in word so there was somewhere for it to put the shape as per your email but it still throws everything out so I am not sure what to try next to achieve the results of my last attachment. I don’t really want to be creating two watermark options without properly understanding why the existing one is not working with a zero header height.
Please can you advise on that.
Regards
Jon

BTW, where can I find progress of my original issue of box left edges missing in the original uploaded document?
thanks
Jon

Hello
Thanks for your request. Maybe in your case you can try using the following code:

Document doc = new Document("C:\\Temp\\X1151_V001.doc");
DocumentBuilder builder = new DocumentBuilder(doc);
Shape imageShape = builder.InsertImage("C:\\Temp\\img.jpg");
imageShape.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
imageShape.RelativeVerticalPosition = RelativeVerticalPosition.Page;
imageShape.WrapType = WrapType.None;
imageShape.VerticalAlignment = VerticalAlignment.Center;
imageShape.HorizontalAlignment = HorizontalAlignment.Center;
// Make sure the watermark comes out on front of the image.
imageShape.ZOrder = 2;
doc.Save("C:\\Temp\\out.pdf", SaveFormat.Pdf);
doc.Save("C:\\Temp\\out.doc", SaveFormat.Doc);

In this case, a watermark is inserted into the main body as image.
Regarding your original issue, at the moment this issue is pending for analysis. The responsible developer will analyze the issue and we will be able to provide you additional information.
If you would like track this thread please click «Enable Email Subscription» on the top of this thread. When we will fix this issue we will post the notification message in this thread and you will get e-mail notification.
Best regards,

Thanks for that. How will I know to use this method as opposed to the other type? This is one of many documents that are produced as part of a batch and currently they all pass through a simple generic render function that uses the following lines:-

// Watermark any documents not produced by the live system to prevent accidents 
// and make it easier to debug them.
if (!ConfigItems.RunningLive)
    WordHelper.AddWatermarkText(this.WorkingDoc, ConfigItems.ApplicationMode);

It then returns the completed working document. Is there something within the document that I can check to see which watermark method is most appropriate at run time?

Thanks
Jon

Hello
Thanks for your inquiry. I think you can try checking TopMargin:

doc.FirstSection.PageSetup.TopMargin

If TopMargin is zero then you should insert watermark into the main body as image.
Best regards,

Should just point out whilst experimenting with the document that the left edges missing is because the boxes do not have a closed path. Right click the shape in MS Word and choose [close path] on the context menu.
Obviously this doesn’t fix the issue for me but might help your developers work out what is going on in their code!

Hi
Thank you for additional information. We will be sure to inform you of any developments regarding this issue.
Thanks,

Hi there,
Please can you tell me if there is a rough ETA/where it is on the queue for this as we need to know whether to put in a plan B for a release.

Thanks
Jon

Hi Jon,
Thanks for your request. Unfortunately, there is still no news about this issue. We will keep you informed and let you know once the issue is fixed.
I apologize for inconvenience.
Best regards,

thanks for that. Is it at least in a queue for looking at next month? If not I will have to resort to a #hack and plan B…

Hi
Thanks for your inquiry. Yes, I think we will be able to take a look at this issue next month. However, I cannot promise you anything right now. So it is a good idea to try work this problem around. In your case, I think, you should redesign your template.
Best regards,