PDF rendering strangeness

Hi guys,

We are trying to render a document to a PDF after first removing several bookmarks using the code found here (it is bundled up inside it’s own assembly Acclipse.Merge.Service referenced in the ‘using’ section of the sample code)

The bookmarks appear to be removed fine and the resultant docx file looks okay if it is saved out to disk but the PDF which is rendered from it places some text on top of other text. I have included my sample code and demo input document. Hopefully you can shed some light on what might be going on. We are using version 8.2.0.0 of the product.

using System.Diagnostics;
using System.IO;
using Acclipse.Merge.Service;
using Aspose.Words;

namespace TestBookmarkRemoval
{
    class Program
    {
        static void Main(string[] args)
        {
            MergeService mergeService = new MergeService();

            string outputFile1 = "out1.pdf";
            Document doc = new Document("IR3Test.docx");
            mergeService.RemoveBookmarkWithContent(doc.Range.Bookmarks["TaxSummaryDeductionsSection"]);
            mergeService.RemoveBookmarkWithContent(doc.Range.Bookmarks["TaxSummaryCreditsSection"]);
            mergeService.RemoveBookmarkWithContent(doc.Range.Bookmarks["TaxSummaryTaxPayableSection"]);
            mergeService.RemoveBookmarkWithContent(doc.Range.Bookmarks["TaxSummaryTaxPaidatSourceSection"]);

            doc.Save("temp.docx");
            Stream result = mergeService.CreatePDFFromAsposeDocument(new Document("temp.docx"));

            using (var fileStream = new FileStream(outputFile1, FileMode.Create))
            {
                result.CopyTo(fileStream);
            }

            Process.Start(outputFile1);
        }
    }

    public static class StreamExtensions
    {
        public static void CopyTo(this System.IO.Stream src, System.IO.Stream dest)
        {
            if (src == null)
            {
                throw new System.ArgumentNullException("src");
            }

            if (dest == null)
            {
                throw new System.ArgumentNullException("dest");
            }

            int readCount;
            var buffer = new byte[32768];
            while ((readCount = src.Read(buffer, 0, buffer.Length)) != 0)
            {
                dest.Write(buffer, 0, readCount);
            }
        }
    }
}

(Also contained within the Acclipse.Merge.Service assembly)

public Stream CreatePDFFromAsposeDocument(Document asposeDocument)
{
    Stream result = new MemoryStream();
    PdfOptions op = new PdfOptions();
    asposeDocument.SaveToPdf(0, asposeDocument.PageCount, result, op);
    result.Position = 0;
    return result;
}

Hi

Thanks for your inquiry. 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.

As a workaround, please try using the highlighted code after removing bookmarks:

Document doc = new Document("IR3Test.docx");
Bookmark bk1 = doc.Range.Bookmarks["TaxSummaryDeductionsSection"];
Bookmark bk2 = doc.Range.Bookmarks["TaxSummaryCreditsSection"];
Bookmark bk3 = doc.Range.Bookmarks["TaxSummaryTaxPayableSection"];
Bookmark bk4 = doc.Range.Bookmarks["TaxSummaryTaxPaidatSourceSection"];
RemoveBookmarkWithContent(bk1);
RemoveBookmarkWithContent(bk2);
RemoveBookmarkWithContent(bk3);
RemoveBookmarkWithContent(bk4);
// Get collection of tables
NodeCollection tables = doc.GetChildNodes(NodeType.Table, true);
// Loop through all tables
foreach (Table table in tables)
{
    // Check if the next node after the tabel is another table.
    if (table.NextSibling != null && table.NextSibling.NodeType == NodeType.Table)
    {
        Table nextTable = (Table)table.NextSibling;
        // Append all rows form the current table to the next.
        while (table.HasChildNodes)
            nextTable.Rows.Insert(0, table.LastRow);
    }
}
doc.SaveToPdf("out.pdf");

Thanks for that, it works well to resolve the issue with the over-written text in the PDF. There is one small thing though, the bookmarks can be conditionally removed in any combination (including the other bookmark in the document not listed above) and we would like to retain the spacing between each table in these instances.
There are one or two combinations of bookmark removals which remove the blank line between tables in the resultant pdf document. eg. only removing the “TaxSummaryCreditsSection” and the “TaxSummaryTaxPayableSection” does not retain the blank line before the ‘Less Tax Paid at Source’ table.

Sorry to be a pain…

Hi

Thank you for additional information. In this case you can try using the following code:

// Get collection of tables
NodeCollection tables = doc.GetChildNodes(NodeType.Table, true);
Paragraph spliter = new Paragraph(doc);
spliter.ParagraphBreakFont.Size = 1;
// Loop through all tables
foreach (Table table in tables)
{
    // Check if the next node after the tabel is another table.
    // If so, insert an empty paragraph between tables.
    if (table.NextSibling != null && table.NextSibling.NodeType == NodeType.Table)
        table.ParentNode.InsertAfter(spliter.Clone(true), table);
}

Hope this helps.

Best regards,

Thanks a lot for that Andrey, initial impressions from my testing look good.

The issues you have found earlier (filed as 9902) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(61)