Content going to right while converting from Word to PDF

Hi,

In word document, For paragraph It’s showing correct formatting but In pdf, some paragrapgh content going to right. It’s changing position of text. I need help to solve this problem.

Hi Sufiya,

Please send us your Word document so that we can reproduce the issue and investigate it.
You can send it via email or support ticket, see the Contact page.

Regards,
Mario

The problem with the document in question was that it had multiple page margins being applied to the same page. This is because there are continuous Section elements that specify different page margins.

That can be detected, for example, like this:

foreach (var section in document.Sections)
{
    Console.WriteLine(section.PageSetup.SectionStart);
    Console.WriteLine(section.PageSetup.PageMargins.Left);
    Console.WriteLine("---");
}

Unfortunately, GemBox.Document’s rendering engine currently doesn’t support this, for a single page only the first page margins is applied throughout that page’s rendering process.

The workaround was to try changing the margins of those continuous sections so that you have a unified margin value on each page. Then you would need to adjust the elements to appear in the same location as before.

So, something like this:

var document = DocumentModel.Load("Demo.docx");

var firstSectionOnPage = document.Sections[0];
foreach (var section in document.Sections.Skip(1))
{
    if (section.PageSetup.SectionStart == SectionStart.NewPage)
    {
        firstSectionOnPage = section;
        continue;
    }

    double leftMargin = firstSectionOnPage.PageSetup.PageMargins.Left;
    double leftOffset = leftMargin - section.PageSetup.PageMargins.Left;
    section.PageSetup.PageMargins.Left = leftMargin;

    double rightMargin = firstSectionOnPage.PageSetup.PageMargins.Right;
    double rightOffset = rightMargin - section.PageSetup.PageMargins.Right;
    section.PageSetup.PageMargins.Right = rightMargin;

    foreach (Paragraph paragraph in section.GetChildElements(false, ElementType.Paragraph))
    {
        paragraph.ParagraphFormat.LeftIndentation -= leftOffset;
        paragraph.ParagraphFormat.RightIndentation -= rightOffset;
    }

    foreach (var shape in section.GetChildElements(true)
        .OfType<Shape>()
        .Where(s => s.Layout.IsFloating))
    {
        var floatingLayout = (FloatingLayout)shape.Layout;
        var horizontalPosition = floatingLayout.HorizontalPosition;

        if (horizontalPosition.PositionType == HorizontalPositionType.Absolute &&
            horizontalPosition.RelativeTo != HorizontalPositionAnchor.TopLeftCorner)
        {
            floatingLayout.HorizontalPosition = new HorizontalPosition(
                horizontalPosition.Value - leftOffset, LengthUnit.Point, horizontalPosition.RelativeTo);
        }
    }
}

document.Save("Demo.pdf");

Note that this solution adjusts the positions of the elements that can be found in this particular Word document (the “Demo.docx” file).

However, there can be other elements and layouts that will require their own adjustments (for instance, the Table.TableFormat.IndentFromLeft).

Anyway, I hope this helps.

Regards,
Mario

Thank you so much.

Your solution help me & working fine as per my requirement.