Margin Changes from DOCX to PDF

When I read in the attached DOCX file, the top margin is increased when saving to PDF. I’m using the following code after reading in the DOCX file and returning it to the response.

public static MemoryStream GetEventDocumentAsPdf(byte[] fileContents)
{
    MemoryStream stream = new MemoryStream();
    if (fileContents != null)
    {
        using(MemoryStream ms = new MemoryStream(fileContents))
        {
            Aspose.Words.Document wordDoc = new Aspose.Words.Document(ms);
            wordDoc.Save(stream, new PdfSaveOptions()
            {
                SaveFormat = Aspose.Words.SaveFormat.Pdf
            });
        }
    }
    stream.Position = 0;
    return stream;
}

Also, if I pause the debugger on the wordDoc.Save method, I can see that the wordDoc’s only section’s PageSetup has a TopMargin of 14.2 and a HeaderDistance of 36 (even though there is no header in this document)

Hi Miles,

Thanks for your inquiry. Perhaps you’re using an older version of Aspose.Words; as with Aspose.Words for .NET 14.1.0, I was unable to reproduce this issue on my side. I would suggest you please upgrade to the latest version of Aspose.Words (14.1.0). You can download it from the following link:
https://releases.aspose.com/words/net

I hope, this helps.

Best regards,

Thank you for the response. It turns out that I was using some code from Aspose on how to insert a watermark and that was causing the issue. So, I’ve modified that code to no longer always insert the watermark into the header but to find the body’s first paragraph and insert it there. (code below)

public static class AsposeWordsHelper
{
    public static void InsertWatermarkText(Document doc, string watermarkText)
        {
            Shape watermarkShape = new Shape(doc, ShapeType.TextSimple);
            watermarkShape.TextPath.Bold = true;
            watermarkShape.TextPath.Text = watermarkText;
            watermarkShape.TextPath.FontFamily = "Arial";
            watermarkShape.Rotation = -30;
            watermarkShape.Width = 500;
            watermarkShape.Height = 200;
            watermarkShape.WrapType = WrapType.None;
            watermarkShape.StrokeColor = Color.FromArgb(120, 120, 120);
            watermarkShape.Stroke.LineStyle = ShapeLineStyle.Double;
            watermarkShape.Fill.Color = Color.FromArgb(113, 113, 113);
            watermarkShape.Fill.Opacity = 0.1;
            watermarkShape.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
            watermarkShape.RelativeVerticalPosition = RelativeVerticalPosition.Page;
            watermarkShape.VerticalAlignment = VerticalAlignment.Center;
            watermarkShape.HorizontalAlignment = HorizontalAlignment.Center;
            watermarkShape.ZOrder = Int32.MaxValue;
            bool watermarkAdded = false;
            if (doc.FirstSection.HeadersFooters.Count> 0)
            {
                HeaderFooter headerFooter = doc.FirstSection.HeadersFooters[HeaderFooterType.HeaderPrimary];
                if (headerFooter != null)
                {
                    headerFooter.AppendChild(watermarkShape);
                    watermarkAdded = true;
                }
                headerFooter = doc.FirstSection.HeadersFooters[HeaderFooterType.HeaderFirst];
                if (headerFooter != null)
                {
                    headerFooter.AppendChild(watermarkShape);
                    watermarkAdded = true;
                }
                headerFooter = doc.FirstSection.HeadersFooters[HeaderFooterType.HeaderEven];
                if (headerFooter != null)
                {
                    headerFooter.AppendChild(watermarkShape);
                    watermarkAdded = true;
                }
            }
            if (!watermarkAdded)
            {
                foreach(Section section in doc.Sections)
                {
                    Paragraph firstParagraph = section.Body.FirstParagraph;
                    if (firstParagraph != null)
                        firstParagraph.PrependChild(watermarkShape);
                    // }
                }
            }

Hi Miles,

Thanks for the additional information. It seems that you have resolved the problem on your side. Should you have any questions then, please don’t hesitate to ask. We are always glad to help you.

Best regards,