Additional pages after saving to pdf

Hi,

I have a problem with a document. After saving it as .pdf some additional pages appear
(attached files, source file has 4 pages and the pdf has 5).

I’m using Aspose.Words for editing files that are stored in an IMAGE column in MS SQL Server 2005 database.
Here is the code that creates a Document after the content is retrieved from the database.

MemoryStream _inputStream = new MemoryStream(content);
Document _document = new Document(_inputStream);

After that a watermark is added on each page.

public void AddWaterMark(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(_document, 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.Gray; // Try LightGray to get more Word-style watermark
    watermark.StrokeColor = Color.Gray; // Try LightGray to get more Word-style watermark

    // 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(_document);
    watermarkPara.AppendChild(watermark);

    int i = 0;
    // Insert the watermark into all headers of each document section.
    foreach (Section sect in _document.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.
        InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderPrimary);
        InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderFirst);
        InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderEven);
        if (i == 0)
            i++;
        else
            sect.HeadersFooters.LinkToPrevious(true);
    }
}

private void InsertWatermarkIntoHeader(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));
}

And finally the document is saved as pdf.

public FileNetContent SaveAsPdf()
{
    MemoryStream _outputStream = new MemoryStream();
    _document.Save(_outputStream, SaveFormat.Pdf);
    _outputStream.Flush();
    return _outputStream.ToArray();
}

I’m using Aspose.Words version 14.11.0.0. for .NET. The source and the resulting files that reproduce the issue are attached.

Please, help. Thanks in advance.

Hi Veselin,

Thanks for your inquiry. I tested the scenario and have managed to reproduce the same problem. For the sake of correction, I have logged this problem in our issue tracking system as WORDSNET-11209. We will further look into the details of this problem and will keep you updated on the status of correction. We apologize for your inconvenience.

Best regards,

Hi,

Do you have any news about bug WORDSNET-11209. It’s been a month since the bug report, when do you expect it to be fixed. It is critical for us to resolve the issue as soon as possible, since we are using the functionality to convert official contracts to pdf and differences between original and pdf are not acceptable. In the meantime can you provide us with some kind of workaround solution.

Thank you in advance and best regards.

Hi Veselin,

Thanks for your inquiry. Unfortunately, your issue is not resolved yet. Our development team has completed the analysis of this issue and the root cause has been identified. However, the implementation of the fix of this issue has been postponed till a later date. We will inform you via this thread as soon as this is resolved. We apologize for any inconvenience.

The issue occurs because of incorrect table width in the footer.

In your document, the table in the footer before blank page has incorrect width. Because of that, a line of underscores in the last cell is wrapped to the second line, causing table and footer height increase. As a result, the last paragraph which has explicit page break is pushed to the next page and the page appears empty as it has page break in the very beginning.

The cause of incorrect width is rather complicated. We’ve attached a simplified 11209test.docx and Aspose.Words output. In the default header, there is a table with width that depends on section contents width. So greater margins cause the table to be smaller.

There are several sections and they have different margins. They all have the same header, but the first page header is different for the first section.

As the first section has one page only, the default header, which should be inherited by the subsequent sections, is not instantiated for the first section. It seems, MS Word uses the section width of the second section to calculate table width in the header. Once the header is instantiated, the calculated table width is used for subsequent sections even though the margins are different again.

Currently, Aspose.Words calculates the table width without taking the document layout into consideration at all.

In order to get the correct container width for a table in header/footer, the layout of the preceding pages must be built to determine in which section the containing header/footer was actually instantiated for the first time.

This is a new feature. As the required logic is quite different from the current logic, it is not feasible to implement it until WORDSNET-832 is ready.

You may make the problematic section footer different from the preceding section as workaround.

Best regards,

Hi,

Thank you for the fast response. Something still bothers me. May be I am not understanding your answer in its entirety, but I don’t think, that the problem is only connected with tables and wrong widths. I recreated the footers without tables and the layout is still wrong when I process the document in our system. Please, find attached the example docx and two pdf files. One is converted in Word (correct layout), the other is converted using Aspose.Words (wrong layout).

Best regards.

Hi Veselin,

Thanks for your inquiry. You can fix this issue by adding the following line in your code:

public static void AddWaterMark(string watermarkText, Document _document)
{
    // Create a watermark shape. This will be a WordArt shape. 
    // You are free to try other shape types as watermarks.
    Shape watermark = new Shape(_document, 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.Gray; // Try LightGray to get more Word-style watermark
    watermark.StrokeColor = Color.Gray; // Try LightGray to get more Word-style watermark
                                        // 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(_document);
    watermarkPara.ParagraphBreakFont.Size = 1;
    watermarkPara.AppendChild(watermark);
    int i = 0;
    // Insert the watermark into all headers of each document section.
    foreach (Section sect in _document.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.
        InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderPrimary);
        InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderFirst);
        InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderEven);
        if (i == 0)
            i++;
        else
            sect.HeadersFooters.LinkToPrevious(true);
    }
}

I hope, this helps.

Best regards,

Hi,

I agree, that the provided line of code has some positive effects. But even after I edited my code
the layout is still wrong, as you can see in the attached file (test_no_tables_DRAFT.pdf). Moreover the final version of the document doesn’t have “DRAFT” watermarks and its layout is also incorrect (test_no_tables_FINAL.pdf).

Can you explain, why the ParagraphBreakFont.Size affects the pdf, but doesn’t affect docx layout.

Thank you and best regards.

Hi Veselin,

Thanks for your inquiry. Please check attached 14.12.0.pdf i.e. generated with Aspose.Words for .NET 14.12.0 on my side over Windows 7 machine. Unfortunately, I could not reproduce issues you mentioned in “test_no_tables_DRAFT.pdf” and “test_no_tables_FINAL.pdf” on my side. It would be great if you please create a standalone runnable console application (source code) that helps us reproduce your problem on our end and attach it here for testing. As soon as you get this simple application ready, we’ll start further investigation into your issue and provide you more information. Thanks for your cooperation.

Best regards,

Hi,

Thank you for the fast response. The Aspose.Words version in the project was 14.11.0, I updated it. Additionally I found an inconsistency in the our process, files were always converted first to .doc and then to .pdf . Apparently the additional page was caused by those two issues.

I have two questions:

  1. Is there a way to receive email notification when a new Aspose.Words version is out.
  2. Where can I see the changelog for the new versions.

P.S. Please, keep me posted on the status of bug WORDSNET-11209.

Best regards

Hi Veselin,

Thanks for your inquiry.

To get release notifications, you can enable e-mail subscriptions to any new release by clicking the “Enable Email Subscription” button at the top of the table on this page. Secondly, on the same page, you can click on any release to go to the release notes of that version e.g. you can find release notes of Aspose.Words for .NET 15.1.0 here.

Regarding, WORDSNET-11209, unfortunately there is no further news about it. We will keep you posted on any further updates and let you know once it is resolved.

Best regards,

@CCHellenic,

The issues you have found earlier (filed as WORDSNET-11209) have been fixed in this Aspose.Words for .NET 17.9 update and this Aspose.Words for Java 17.9 update.