Code Monkey home page Code Monkey logo

Comments (2)

sbraconnier avatar sbraconnier commented on May 12, 2024

I'm glad someone tried to create a new Filter!!! You are the first one to post one here :).

I've made minor changes in order to get your filter to work. Could you please try this one. If it works, I'll added it into the main disctribution.

/** This filter is used to set the margins of the document being converted. */
public class PageMarginsFilter implements Filter {

  // This class has been inspired by these examples:
  // https://wiki.openoffice.org/wiki/Translating_Java_Code_in_cpp
  // https://github.com/bomm/thera-pi-2/blob/master/RehaBillEdit/src/Tools/OOTools.java#L328

  private static final Logger LOGGER = LoggerFactory.getLogger(PageMarginsFilter.class);

  private final Integer topMargin;
  private final Integer rightMargin;
  private final Integer bottomMargin;
  private final Integer leftMargin;

  /**
   * Creates a new filter that will set the margins of the document.
   *
   * @param leftMargin The left margin (millimeters), may be null. If null, the left margin does not
   *     change.
   * @param topMargin The top margin (millimeters), may be null. If null, the top margin does not
   *     change.
   * @param rightMargin The right margin (millimeters), may be null. If null, the right margin does
   *     not change.
   * @param bottomMargin The bottom margin (millimeters), may be null. If null, the bottom margin
   *     does not change.
   */
  public PageMarginsFilter(
      final Integer leftMargin,
      final Integer topMargin,
      final Integer rightMargin,
      final Integer bottomMargin) {
    super();

    this.leftMargin = leftMargin;
    this.topMargin = topMargin;
    this.rightMargin = rightMargin;
    this.bottomMargin = bottomMargin;
  }

  @Override
  public void doFilter(
      final OfficeContext context, final XComponent document, final FilterChain chain)
      throws Exception {

    // Querying for the interface XTextDocument (text interface) on the XComponent
    final XTextDocument docText = UnoRuntime.queryInterface(XTextDocument.class, document);

    // Create a text cursor from the cells XText interface
    final XTextCursor xTextCursor = docText.getText().createTextCursor();

    // Get the property set of the cell's TextCursor
    final XPropertySet xTextCursorProps =
        UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);

    // Get the Page Style name at the cursor position
    final String pageStyleName = xTextCursorProps.getPropertyValue("PageStyleName").toString();

    // Get the StyleFamiliesSupplier interface of the document
    final XStyleFamiliesSupplier xSupplier =
        UnoRuntime.queryInterface(XStyleFamiliesSupplier.class, docText);

    // Use the StyleFamiliesSupplier interface to get the XNameAccess interface of the
    // actual style families
    final XNameAccess xFamilies =
        UnoRuntime.queryInterface(XNameAccess.class, xSupplier.getStyleFamilies());

    // Access the 'PageStyles' Family
    final XNameContainer xFamily =
        UnoRuntime.queryInterface(XNameContainer.class, xFamilies.getByName("PageStyles"));

    // Get the style of the current page from the PageStyles family
    final XStyle xStyle = UnoRuntime.queryInterface(XStyle.class, xFamily.getByName(pageStyleName));

    //
    // We could also just bet that the Standard style is used. If this is what we want,
    // uncomment the following line, and remove the ones (used to get the pageStyleName)
    // that will no longer be required.
    //

    // Get the "Standard" style from the PageStyles family
    // XStyle xStyle = UnoRuntime.queryInterface(XStyle.class, xFamily.getByName("Standard"));

    LOGGER.debug(
        "Changing margins using: [left={}, top={}, right={}, bottom={}]",
        leftMargin,
        topMargin,
        rightMargin,
        bottomMargin);

    // Get the property set of the style
    final XPropertySet xStyleProps = UnoRuntime.queryInterface(XPropertySet.class, xStyle);

    // Change the margins (1 = 0.01 mm)
    if (leftMargin != null) {
      xStyleProps.setPropertyValue("LeftMargin", leftMargin * 100);
    }
    if (topMargin != null) {
      xStyleProps.setPropertyValue("TopMargin", topMargin * 100);
    }
    if (rightMargin != null) {
      xStyleProps.setPropertyValue("RightMargin", rightMargin * 100);
    }
    if (bottomMargin != null) {
      xStyleProps.setPropertyValue("BottomMargin", bottomMargin * 100);
    }

    // Invoke the next filter in the chain
    chain.doFilter(context, document);
  }
}

from jodconverter.

andtho avatar andtho commented on May 12, 2024

Hello Simon,
this works for me!
Thank you

from jodconverter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.