Setting Page Size and Margins for entire PDF Document

hi
We are using the below mentioned code to convert a Word Document to PDF using the Save() method.
We are using the SaveFormat.PDF as the option.
We are facing issue with the Page Margins for the entire document in PDF, the margins gets totally disturbed when converting a word document to PDF using the document.save() method.
Is there any way we can fix the Page Margins and Page Size of the target PDF document when we use the document.save() method.
Below is the Code we use the coverting/saving the word document to PDF.

public Aspose.Words.Document doc;
MemoryStream dstStream = new MemoryStream();
doc.Save(dstStream, SaveFormat.Pdf);
dstStream.Position = 0;
byte[] contents = new byte[dstStream.Length];
dstStream.Read(contents, 0, (int) dstStream.Length);
dstStream.Close();

Hi Parshuram,

Thanks for your inquiry. Could you please attach your input Word document and output PDF document showing the undesired behavior here for testing? We will investigate the issue on our end and provide you more information.

Best regards,

Hi,
Please find the attached documents, the source - word document with the Margins set and the Generated PDF Document with the Margins.
We tried both ways i.e.

  1. Setting the Margins in the word document template and then Merging the data with the word document template and then generating the PDF document.
  2. Setting the Margins Programatically via the code for the Word Document and then Generating the PDF Document.

In both the cases above, the margins the PDF gets disturbed and do not look the same.

hi,
Please find the source documents in word and the target PDF document.
Thanks

Hi Parshuram,

Thanks for your inquiry.

In your case, Aspose.Words mimics the behavior of Microsoft Word 2013; this means that if you convert your document into PDF using Aspose.Words, the output will appear almost exactly as if it was done by Microsoft Word 2013 (please see a couple of PDF documents attached here with this post). This is an expected behavior.

Moreover, you can adjust the page margins by using PageSetup.LeftMargin, PageSetup.RightMargin, PageSetup.TopMargin and PageSetup.BottomMargin properties:
https://reference.aspose.com/words/net/aspose.words/pagesetup/

If we can help you with anything else, please feel free to ask.

Best regards,

Hi,
Many thanks for the updates, but we are not quite sure of the approach and it is not working as expected.
Is it possible that we can arrange for a WebEx demo to you. We will send you a WebEx request and show you the exact issue. You will be able to understand by seeing things much better and can provide a solution for this issue.
Please reply so that we can set a webex demo tomorrow. We can provide you with a conference bridge number to dial in as well.
Thanks

Hi Parshuram,

Thanks for your inquiry. Your word document has only one Section with PageSetup margins configured as Top: 1", Bottom: 1", Left: 1.18" and Right: 1.18". If you want to adjust the margins as shown in WordDocumentSample.jpg screenshot, please try executing the following code.

Document doc = new Document(@"C:\Temp\MyTemplate.dot");
foreach(Section sec in doc.Sections)
{
    PageSetup ps = sec.PageSetup;
    // 1 inch equals 72 points
    ps.TopMargin = 1.4 * 72;
    ps.RightMargin = 2.5 * 72;
    ps.BottomMargin = 1.3 * 72;
    ps.LeftMargin = 3.3 * 72;
}
doc.Save(@"C:\Temp\out.dot");
doc.Save(@"C:\Temp\out.pdf");

Secondly, I am afraid, the main avenue of technical support is through our forums. In case you require further assistance, please provide us details here and we will supply you with a suitable solution. Also, please note that it is safe to attach files in the forum. If you attach your resources (documents, images etc) here, only you and Aspose staff members can download them. Thanks for your cooperation.

Best regards,

Hi,
Many thanks for the code block. The code works for setting the Margins (Top,Bottom, left and right) on the Dot/Dotx Template File. It also works for the merging the data with the .dotx file using mergefields.
But when we save the dotx/docx As .PDF file, the right Margin, Left Margin and the top margin gets disturbed and the text in the Footers which is right aliged does not start from the same locations as of the main body content.
So the right margins of the Footer text and the Right Margin of the Main Body content do not match.
Once again many thanks for helping us out with the Code.
One more issue, we have is that the Margin (Top, Bottom, Left, Right) is different for 1st Page and different for the Rest of the pages from the 2nd Page onwards.
Can you please let us know how do we manage/.handle this programatically.
Thanks
Parshuram

Hi
Please find our source code for Merging the mergefields in the Document Template (dotx) and then We create a PDF file out of the Merged Word Document (docx).
We have added your source code for setting the Margins within the Document Template. As i said earlier the Margins get set for the Dotx template, but when we merge and save the word document into PDF file, the margins get disturbed.

public bool Merge(DataTable fieldValues, OutputConfiguration config, string doctype)
{
    try
    {
        if (!string.IsNullOrEmpty(config.WatermarkText))
        {
            MergingUtility.InsertWatermarkText(doc, config.WatermarkText);
        }

        doc.MailMerge.CleanupOptions = doc.MailMerge.CleanupOptions & ~MailMergeCleanupOptions.RemoveUnusedFields | ~MailMergeCleanupOptions.RemoveUnusedRegions |
            ~MailMergeCleanupOptions.RemoveEmptyParagraphs | ~MailMergeCleanupOptions.RemoveContainingFields;

        MergeBookmarks(fieldValues);

        if (this.FieldMergingCallback != null)
            doc.MailMerge.FieldMergingCallback = this.FieldMergingCallback;
        DocumentBuilder builder = new DocumentBuilder(doc);
        builder.MoveToDocumentStart();
        PageSetup page = builder.PageSetup;
        page.ClearFormatting();
        page.DifferentFirstPageHeaderFooter = true;
        page.PaperSize = PaperSize.A4;

        #region Code Type3 in use
        foreach(Section sec in doc.Sections)
        {
            PageSetup ps = sec.PageSetup;
            ps.TopMargin = ConvertUtil.InchToPoint(0.7);
            ps.RightMargin = ConvertUtil.InchToPoint(0.95);
            ps.BottomMargin = ConvertUtil.InchToPoint(0.35);
            ps.LeftMargin = ConvertUtil.InchToPoint(0.95);
        }
        doc.Save(@"D:\Document Engine SetUp\Reports\Temp.dotx");
        #endregion
        Aspose.Words.Font font = builder.Font;
        font.Name = "Times New Roman";
        font.Size = 20;
        doc.Document.NodeChangingCallback = new HandleNodeChanging_FontChanger();
        doc.MailMerge.ExecuteWithRegions(fieldValues);
        options = GetSaveOptions(config);

        doc.UpdateFields();
        doc.Save(config.OutFilePath + doctype + ".pdf", optionsDoc);
        // throw new NotImplementedException();
        return true;
    }
    catch (Exception ex)
    {
        Trace.WriteLine(ex);
    }
    return false;
}

Thanks
Parshuram

Hi Parshuram,

Thanks for your inquiry. Please create a standalone runnable simple console application 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. Also, please attach your output PDF file which shows the undesired behavior here for testing.

Best regards,

Hi,
Many thanks for taking your time out.
I was going through your earlier replies in the Forum Thread related to this issue.
Below is the excerpts from the same
In your case, Aspose.Words mimics the behavior of Microsoft Word 2013; this means that if you convert your document into PDF using Aspose.Words, the output will appear almost exactly as if it was done by Microsoft Word 2013 (please see a couple of PDF documents attached here with this post). This is an expected behavior.
I could not understand the meaning of ASPOSE.Words mimics the behaviour of Word 2013. To explain our environment here. We are using SharePoint 2010 and have installed Office 2010 on all our development boxes along with the ASPOSE.Words for .NET.
I wanted to understand, if we should be installing Office 2013 and test our solution. OR Should we test our application using Office 2003 / 2007.
We are not explicity creating a word document, because we load the .DOTX Template file that has mailmerge fields. We then merge the Template with the Data and Save it as PDF using Document.Save() method of ASPOSE.
One More thing, i would like to get clarification on is; Is there a option within the ASPOSE.Words method document.save() to specify a parameter to use a specific .DOCX format (i.e. 2007, 2010 or 2013).
Please let us know.
Many thanks for taking your time out.
Thanks
Ram

Hi Ram,

Thanks for your inquiry.

Ram:
I could not understand the meaning of ASPOSE.Words mimics the behaviour of Word 2013. To explain our environment here. We are using SharePoint 2010 and have installed Office 2010 on all our development boxes along with the ASPOSE.Words for .NET.
I wanted to understand, if we should be installing Office 2013 and test our solution. OR Should we test our application using Office 2003 / 2007.

Have you tried converting Aspose.Words generated output Word document to PDF format using Microsoft Word (any version 2007/2010/2013 etc) on your side? Can you share Aspose.Words generated output document (which shows the correct behavior) here for testing? Please note that Aspose.Words does not require Microsoft Office to be installed in order to run. Aspose components are totally independent and are not affiliated with, nor authorized, sponsored, or otherwise approved by Microsoft Corporation.

Ram:
One More thing, i would like to get clarification on is; Is there a option within the ASPOSE.Words method document.save() to specify a parameter to use a specific .DOCX format (i.e. 2007, 2010 or 2013).

You can use OoxmlSaveOptions.Compliance property that allows to specify which OOXML specification will be used when saving in the DOCX format e.g. Ecma376_2006 (ECMA-376 1st Edition, 2006) or Iso29500_2008_Transitional (ISO/IEC 29500:2008 Transitional compliance level)

Ram:
One more issue, we have is that the Margin (Top, Bottom, Left, Right) is different for 1st Page and different for the Rest of the pages from the 2nd Page onwards.

Your template document has different headers/footers for first page and for the remaining pages. It could be that the margins differ because the structure of contents inside these first page headers/footers and primary footer is different. You can remove first page headers/footers from template and simply try using Primary Headers/Footers.

Best regards,