We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

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(<span style="color:#a31515;">"C:\\Temp\\out.pdf"</span>, <span style="color:#2b91af;">SaveFormat</span>.Pdf);
        doc.Save(<span style="color:#a31515;">"C:\\Temp\\out.doc"</span>, <span style="color:#2b91af;">SaveFormat</span>.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”);

        <span style="color:#2b91af;">Document</span> doc = <span style="color:blue;">new</span> <span style="color:#2b91af;">Document</span>(<span style="color:#a31515;">"C:\\Temp\\X1151_V001.doc"</span>);
        AddWatermarkText(doc, <span style="color:#a31515;">"Dev"</span>);

        <span style="color:blue;">using</span> (<span style="color:#2b91af;">MemoryStream</span> ms = <span style="color:blue;">new</span> <span style="color:#2b91af;">MemoryStream</span>())
        {
            doc.Save(ms, <span style="color:#2b91af;">SaveFormat</span>.Pdf);
            Aspose.Pdf.<span style="color:#2b91af;">Document</span> tmpPDF = <span style="color:blue;">new</span> Aspose.Pdf.<span style="color:#2b91af;">Document</span>(ms);
            tmpPDF.Save(<span style="color:#a31515;">"C:\\Temp\\outPDF.pdf"</span>);

        }

        doc.Save(<span style="color:#a31515;">"C:\\Temp\\out.pdf"</span>, <span style="color:#2b91af;">SaveFormat</span>.Pdf);
        doc.Save(<span style="color:#a31515;">"C:\\Temp\\out.doc"</span>, <span style="color:#2b91af;">SaveFormat</span>.Doc);
    }
    <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;"><summary></span>
    <span style="color:gray;">///</span><span style="color:green;"> Inserts a watermark into a document.</span>
    <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;"></summary></span>
    <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;"><param name=</span><span style="color:gray;">"doc"</span><span style="color:gray;">></span><span style="color:green;">The input document.</span><span style="color:gray;"></param></span>
    <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;"><param name=</span><span style="color:gray;">"watermarkText"</span><span style="color:gray;">></span><span style="color:green;">Text of the watermark.</span><span style="color:gray;"></param></span>
    <span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:blue;">void</span> AddWatermarkText(<span style="color:#2b91af;">Document</span> doc, <span style="color:blue;">string</span> watermarkText)
    {
        <span style="color:green;">// Create a watermark shape. This will be a WordArt shape.</span>
        <span style="color:green;">// You are free to try other shape types as watermarks.</span>
        <span style="color:#2b91af;">Shape</span> watermark = <span style="color:blue;">new</span> <span style="color:#2b91af;">Shape</span>(doc, Aspose.Words.Drawing.<span style="color:#2b91af;">ShapeType</span>.TextPlainText);

        <span style="color:green;">// Set up the text of the watermark.</span>
        watermark.TextPath.Text = watermarkText;
        watermark.TextPath.FontFamily = <span style="color:#a31515;">"Arial"</span>;
        watermark.Width = 500;
        watermark.Height = 100;
        <span style="color:green;">// Text will be directed from the bottom-left to the top-right corner.</span>
        watermark.Rotation = -40;
        <span style="color:green;">// Remove the following two lines if you need a solid black text.</span>
        watermark.Fill.Color = <span style="color:#2b91af;">Color</span>.LightGray;
        watermark.StrokeColor = <span style="color:#2b91af;">Color</span>.LightGray;

        <span style="color:green;">// Place the watermark in the page center.</span>
        watermark.RelativeHorizontalPosition = <span style="color:#2b91af;">RelativeHorizontalPosition</span>.Page;
        watermark.RelativeVerticalPosition = <span style="color:#2b91af;">RelativeVerticalPosition</span>.Page;
        watermark.WrapType = <span style="color:#2b91af;">WrapType</span>.None;
        watermark.VerticalAlignment = <span style="color:#2b91af;">VerticalAlignment</span>.Center;
        watermark.HorizontalAlignment = <span style="color:#2b91af;">HorizontalAlignment</span>.Center;

        <span style="color:green;">// Create a new paragraph and append the watermark to this paragraph.</span>
        <span style="color:#2b91af;">Paragraph</span> watermarkPara = <span style="color:blue;">new</span> <span style="color:#2b91af;">Paragraph</span>(doc);
        watermarkPara.AppendChild(watermark);

        <span style="color:green;">// Insert the watermark into all headers of each document section.</span>
        <span style="color:blue;">foreach</span> (<span style="color:#2b91af;">Section</span> sect <span style="color:blue;">in</span> doc.Sections)
        {
            <span style="color:green;">// There could be up to three different headers in each section, since we want</span>
            <span style="color:green;">// the watermark to appear on all pages, insert into all headers.</span>
            AddWatermarkIntoHeader(watermarkPara, sect, <span style="color:#2b91af;">HeaderFooterType</span>.HeaderPrimary);
            AddWatermarkIntoHeader(watermarkPara, sect, <span style="color:#2b91af;">HeaderFooterType</span>.HeaderFirst);
            AddWatermarkIntoHeader(watermarkPara, sect, <span style="color:#2b91af;">HeaderFooterType</span>.HeaderEven);
        }
        <span style="color:green;">// Cleanup</span>
        watermark = <span style="color:blue;">null</span>;
    }

    <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;"><summary></span>
    <span style="color:gray;">///</span><span style="color:green;"> Inserts the watermark into header.</span>
    <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;"></summary></span>
    <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;"><param name=</span><span style="color:gray;">"watermarkPara"</span><span style="color:gray;">></span><span style="color:green;">The watermark para.</span><span style="color:gray;"></param></span>
    <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;"><param name=</span><span style="color:gray;">"sect"</span><span style="color:gray;">></span><span style="color:green;">The sect.</span><span style="color:gray;"></param></span>
    <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;"><param name=</span><span style="color:gray;">"headerType"</span><span style="color:gray;">></span><span style="color:green;">Type of the header.</span><span style="color:gray;"></param></span>
    <span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:blue;">void</span> AddWatermarkIntoHeader(<span style="color:#2b91af;">Paragraph</span> watermarkPara, <span style="color:#2b91af;">Section</span> sect, <span style="color:#2b91af;">HeaderFooterType</span> headerType)
    {
        <span style="color:#2b91af;">HeaderFooter</span> header = sect.HeadersFooters[headerType];

        <span style="color:blue;">if</span> (header == <span style="color:blue;">null</span>)
        {
            <span style="color:green;">// There is no header of the specified type in the current section, create it.</span>
            header = <span style="color:blue;">new</span> <span style="color:#2b91af;">HeaderFooter</span>(sect.Document, headerType);
            sect.HeadersFooters.Add(header);
        }

        <span style="color:green;">// Insert a clone of the watermark into the header.</span>
        header.AppendChild(watermarkPara.Clone(<span style="color:blue;">true</span>));
        <span style="color:green;">// Cleanup</span>
        header = <span style="color:blue;">null</span>;
    }

}

}

(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”);

        <span style="color:#2b91af;">Document</span> doc = <span style="color:blue;">new</span> <span style="color:#2b91af;">Document</span>(<span style="color:#a31515;">"C:\\Temp\\X1151_V001.doc"</span>);
        AddWatermarkText(doc, <span style="color:#a31515;">"Dev"</span>);

        doc.Save(<span style="color:#a31515;">"C:\\Temp\\out.pdf"</span>, <span style="color:#2b91af;">SaveFormat</span>.Pdf);
        doc.Save(<span style="color:#a31515;">"C:\\Temp\\out.doc"</span>, <span style="color:#2b91af;">SaveFormat</span>.Doc);
    }
    <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;"><summary></span>
    <span style="color:gray;">///</span><span style="color:green;"> Inserts a watermark into a document.</span>
    <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;"></summary></span>
    <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;"><param name=</span><span style="color:gray;">"doc"</span><span style="color:gray;">></span><span style="color:green;">The input document.</span><span style="color:gray;"></param></span>
    <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;"><param name=</span><span style="color:gray;">"watermarkText"</span><span style="color:gray;">></span><span style="color:green;">Text of the watermark.</span><span style="color:gray;"></param></span>
    <span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:blue;">void</span> AddWatermarkText(<span style="color:#2b91af;">Document</span> doc, <span style="color:blue;">string</span> watermarkText)
    {
        <span style="color:green;">// Create a watermark shape. This will be a WordArt shape.</span>
        <span style="color:green;">// You are free to try other shape types as watermarks.</span>
        <span style="color:#2b91af;">Shape</span> watermark = <span style="color:blue;">new</span> <span style="color:#2b91af;">Shape</span>(doc, Aspose.Words.Drawing.<span style="color:#2b91af;">ShapeType</span>.TextPlainText);

        <span style="color:green;">// Set up the text of the watermark.</span>
        watermark.TextPath.Text = watermarkText;
        watermark.TextPath.FontFamily = <span style="color:#a31515;">"Arial"</span>;
        watermark.Width = 500;
        watermark.Height = 100;
        <span style="color:green;">// Text will be directed from the bottom-left to the top-right corner.</span>
        watermark.Rotation = -40;
        <span style="color:green;">// Remove the following two lines if you need a solid black text.</span>
        watermark.Fill.Color = <span style="color:#2b91af;">Color</span>.LightGray;
        watermark.StrokeColor = <span style="color:#2b91af;">Color</span>.LightGray;

        <span style="color:green;">// Place the watermark in the page center.</span>
        watermark.RelativeHorizontalPosition = <span style="color:#2b91af;">RelativeHorizontalPosition</span>.Page;
        watermark.RelativeVerticalPosition = <span style="color:#2b91af;">RelativeVerticalPosition</span>.Page;
        watermark.WrapType = <span style="color:#2b91af;">WrapType</span>.None;
        watermark.VerticalAlignment = <span style="color:#2b91af;">VerticalAlignment</span>.Center;
        watermark.HorizontalAlignment = <span style="color:#2b91af;">HorizontalAlignment</span>.Center;

        <span style="color:green;">// Create a new paragraph and append the watermark to this paragraph.</span>
        <span style="color:#2b91af;">Paragraph</span> watermarkPara = <span style="color:blue;">new</span> <span style="color:#2b91af;">Paragraph</span>(doc);
        watermarkPara.AppendChild(watermark);

        <span style="color:green;">// Insert the watermark into all headers of each document section.</span>
        <span style="color:blue;">foreach</span> (<span style="color:#2b91af;">Section</span> sect <span style="color:blue;">in</span> doc.Sections)
        {
            <span style="color:green;">// There could be up to three different headers in each section, since we want</span>
            <span style="color:green;">// the watermark to appear on all pages, insert into all headers.</span>
            AddWatermarkIntoHeader(watermarkPara, sect, <span style="color:#2b91af;">HeaderFooterType</span>.HeaderPrimary);
            AddWatermarkIntoHeader(watermarkPara, sect, <span style="color:#2b91af;">HeaderFooterType</span>.HeaderFirst);
            AddWatermarkIntoHeader(watermarkPara, sect, <span style="color:#2b91af;">HeaderFooterType</span>.HeaderEven);
        }
        <span style="color:green;">// Cleanup</span>
        watermark = <span style="color:blue;">null</span>;
    }

    <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;"><summary></span>
    <span style="color:gray;">///</span><span style="color:green;"> Inserts the watermark into header.</span>
    <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;"></summary></span>
    <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;"><param name=</span><span style="color:gray;">"watermarkPara"</span><span style="color:gray;">></span><span style="color:green;">The watermark para.</span><span style="color:gray;"></param></span>
    <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;"><param name=</span><span style="color:gray;">"sect"</span><span style="color:gray;">></span><span style="color:green;">The sect.</span><span style="color:gray;"></param></span>
    <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;"><param name=</span><span style="color:gray;">"headerType"</span><span style="color:gray;">></span><span style="color:green;">Type of the header.</span><span style="color:gray;"></param></span>
    <span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:blue;">void</span> AddWatermarkIntoHeader(<span style="color:#2b91af;">Paragraph</span> watermarkPara, <span style="color:#2b91af;">Section</span> sect, <span style="color:#2b91af;">HeaderFooterType</span> headerType)
    {
        <span style="color:#2b91af;">HeaderFooter</span> header = sect.HeadersFooters[headerType];

        <span style="color:blue;">if</span> (header == <span style="color:blue;">null</span>)
        {
            <span style="color:green;">// There is no header of the specified type in the current section, create it.</span>
            header = <span style="color:blue;">new</span> <span style="color:#2b91af;">HeaderFooter</span>(sect.Document, headerType);
            sect.HeadersFooters.Add(header);
        }

        <span style="color:green;">// Insert a clone of the watermark into the header.</span>
        header.AppendChild(watermarkPara.Clone(<span style="color:blue;">true</span>));
        <span style="color:green;">// Cleanup</span>
        header = <span style="color:blue;">null</span>;
    }

}

}

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="https://forum.aspose.com/t/101961 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!

:slight_smile:

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,