Author Topic: Setting of margins for docx document  (Read 7530 times)


  • Newbie
  • Posts: 2
Setting of margins for docx document
« on: December 12, 2017, 09:24:36 am »

I am having problems setting the page margins when creating a docx file with fpvectorial. I am using the following test code and I am using Lazarus on Fedora 20.

  Document: TvVectorialDocument;
  Page: TvTextPageSequence;
  Paragraph: TvParagraph;
  Document := TvVectorialDocument.Create;
  Document.Width := 182;
  Document.Height := 257;
  Document.StyleTextBody.MarginLeft := 25.4;
  Document.StyleTextBody.MarginRight := 25.4;
  Document.StyleTextBody.MarginTop := 35;
  Document.StyleTextBody.MarginBottom := 26;
  Document.StyleTextBody.SetElements := Document.StyleTextBody.SetElements +
    [sseMarginLeft, sseMarginRight, sseMarginTop, sseMarginBottom];
  Page := Document.AddTextPageSequence();
  Paragraph := Page.AddParagraph;
  Document.WriteToFile('test.docx', vfDOCX);

When I open the test.docx file in LibreOffice and click on Format/Page the page width and height is correct as set by the program but the margins is incorrect. Is seems that it is the LibreOffice default margins. What am I missing or doing wrong?

« Last Edit: December 12, 2017, 05:06:42 pm by dgm »
Linux kernel 4.1.13 amd64, Fedora 21
Lazarus 1.8.0RC5 fpc 3.0.4-rc1


  • Newbie
  • Posts: 2
Re: Setting of margins for docx document
« Reply #1 on: December 19, 2017, 09:10:28 am »
Well since I am getting no help from the forum regarding the fpvectorial margin problem I downloaded the newest version of fpvectorial via svn. I then compiled the fpvtextwritetest2.pas example and the example's margins did not work either.

Then I created a test document in Libreoffice and unzipped the .odt file and I saw that the margins are set in page-layout. Thereafter I searched the fpvectorial code for page-layout and found a "// TODO: Add Page Margins..." comment in the odtvecterialwrite.pas code.

The margins are hard coded for example: fo:margin-top="0.7874in" which is exactly the 20 mm margins I got in Libreoffice when creating an ODT document with fpvectorial.
So clearly the margins are not implemented at this stage.

Then I modified the code after the code as follows:

   FAutomaticStyles := FAutomaticStyles +
      '<style:page-layout style:name="'+sPageLayoutName+'">'+ LineEnding+
      '  <style:page-layout-properties '+
                ' fo:page-width="'+FloatToODTText(dWidth)+'mm"'+
                ' fo:page-height="'+FloatToODTText(dHeight)+'mm"'+
                ' style:print-orientation="'+sOrientation+'"'+
                ' style:num-format="1"'+
                ' fo:margin-top="'+
                ' fo:margin-bottom="'+
                ' fo:margin-left="'+
                ' fo:margin-right="'+
                ' style:writing-mode="lr-tb" style:footnote-max-height="0in">'+ LineEnding;

It is probably not inline with the ODT specification but now at least the margins work when I set them in the code and writes an ODT document.
Linux kernel 4.1.13 amd64, Fedora 21
Lazarus 1.8.0RC5 fpc 3.0.4-rc1


  • Hero Member
  • *****
  • Posts: 10853
Re: Setting of margins for docx document
« Reply #2 on: December 19, 2017, 09:48:56 am »
This is certainly better than the hard-coded version, but probably not the most general solution. I don't know whether the ODT specification allows page margins different from the style and whether this is implemented in fpvectorial - but if this were true then your code would ignore the individual page settings.

Anyway: You should post a report in bugtracker so that this issue will not be lost. Hopefully, Felipe, the maintainer of fpvectorial, sees it. Or I can have a look myself, but I don't have the time for it at the moment.


TinyPortal © 2005-2018