We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

FitToPages

Hi,
Does Aspose Words for Java implement any feature like fitToPages (https://docs.microsoft.com/en-us/previous-versions/office/developer/office-2007/bb221519(v=office.12)) ?

Thanks

Hi

Thanks for your request. There is no such method. But you can implement this yourself. I created a simple code example for you:

// Open document.
Document doc = new Document("C:\\Temp\\in.doc");
// Create an instance of FontSizeChanger class.
// We will use it to decrease font size.
FontSizeChanger changer = new FontSizeChanger(-0.5);
// Get number of pages in the document.
int originalCount = doc.getPageCount();
// There is nothing to do if page count is 1.
if (originalCount == 1)
    return;
// We need to decrease number of pages.
int currentCount = originalCount;
do
{
    doc.accept(changer);
    doc.updatePageLayout();
    currentCount = doc.getPageCount();
}
while (currentCount == originalCount);
// Save result.
doc.save("C:\\Temp\\out.doc");

import com.aspose.words.*;

public class FontSizeChanger extends DocumentVisitor {

    public FontSizeChanger(double increment)
    {
        mIncremetn = increment;
    }

    /// 
    /// Called when a FieldEnd node is encountered in the document.
    /// 
    public int visitFieldEnd(FieldEnd fieldEnd) throws Exception
    {
        //Simply change font name
        ChangeFont(fieldEnd.getFont());
        return VisitorAction.CONTINUE;
    }

    /// 
    /// Called when a FieldSeparator node is encountered in the document.
    /// 
    public int visitFieldSeparator(FieldSeparator fieldSeparator) throws Exception
    {
        ChangeFont(fieldSeparator.getFont());
        return VisitorAction.CONTINUE;
    }

    /// 
    /// Called when a FieldStart node is encountered in the document.
    /// 
    public int visitFieldStart(FieldStart fieldStart) throws Exception
    {
        ChangeFont(fieldStart.getFont());
        return VisitorAction.CONTINUE;
    }

    /// 
    /// Called when a Footnote end is encountered in the document.
    /// 
    public int visitFootnoteEnd(Footnote footnote) throws Exception
    {
        ChangeFont(footnote.getFont());
        return VisitorAction.CONTINUE;
    }

    /// 
    /// Called when a FormField node is encountered in the document.
    /// 
    public int visitFormField(FormField formField) throws Exception
    {
        ChangeFont(formField.getFont());
        return VisitorAction.CONTINUE;
    }

    /// 
    /// Called when a Paragraph end is encountered in the document.
    /// 
    public int visitParagraphEnd(Paragraph paragraph) throws Exception
    {
        ChangeFont(paragraph.getParagraphBreakFont());
        return VisitorAction.CONTINUE;
    }

    /// 
    /// Called when a Run node is encountered in the document.
    /// 
    public int visitRun(Run run) throws Exception
    {
        ChangeFont(run.getFont());
        return VisitorAction.CONTINUE;
    }

    /// 
    /// Called when a SpecialChar is encountered in the document.
    /// 
    public int visitSpecialChar(SpecialChar specialChar) throws Exception
    {
        ChangeFont(specialChar.getFont());
        return VisitorAction.CONTINUE;
    }

    private void ChangeFont(Font font) throws Exception
    {
        font.setSize(font.getSize() + mIncremetn);
    }

    // Font by default
    private double mIncremetn;
}

Hope this helps. Please let me know if you need more assistance, I will be glad to help you.

Best regards.

Hi,
thanks for a reply. I’ve tried your solution. It works on Windows environment. On Linux environment I’m getting error while executing line document.getPageCount():

at asposewobfuscated.jz.eV(Directory.java:87)
at asposewobfuscated.qx.a(TTFontFiler.java:788)
at asposewobfuscated.qx.iF(TTFontFiler.java:763)
at asposewobfuscated.qx.Vp(TTFontFiler.java:752)
at asposewobfuscated.qx.iz(TTFontFiler.java:321)
at asposewobfuscated.qx.C(TTFontFiler.java:117)
at asposewobfuscated.qx.B(TTFontFiler.java:42)
at asposewobfuscated.oa.(PalFont.java:63)
at asposewobfuscated.oa.a(PalFont.java:38)
at com.aspose.words.xk.Hp(SpanPr.java:346)
at com.aspose.words.sl.rt(Span.java:750)
at com.aspose.words.ok.k(LineFormatter.java:209)
at com.aspose.words.ok.h(LineFormatter.java:55)
at com.aspose.words.ok.g(LineFormatter.java:44)
at com.aspose.words.zy.JJ(LineBuilder.java:238)
at com.aspose.words.zy.JI(LineBuilder.java:31)
at com.aspose.words.bx.V(LinePart.java:283)
at com.aspose.words.ay.V(HeaderFooterPart.java:154)
at com.aspose.words.ahu.nG(HeaderFooterSectionLayout.java:55)
at com.aspose.words.qr.nG(StoryLayoutBase.java:88)
at com.aspose.words.mz.d(PageBuilder.java:346)
at com.aspose.words.mz.a(PageBuilder.java:40)
at com.aspose.words.ajt.b(SectionReflower.java:40)
at com.aspose.words.io.nG(MainTextSectionLayout.java:757)
at com.aspose.words.qr.nG(StoryLayoutBase.java:88)
at com.aspose.words.mx.nG(PageLayoutModel.java:124)
at com.aspose.words.zh.a(LayoutDocument.java:33)
at com.aspose.words.Document.updatePageLayout(Document.java:1637)
at com.aspose.words.Document.IF(Document.java:1590)
at com.aspose.words.Document.getPageCount(Document.java:1618)

I had the same problem with saveToPdf funtion and I managed to solve it using setTrueTypeFontsFolder function.
What can I do in this case?

Thanks.

I’ve tried the same way and it works.

PdfOptions opt = new PdfOptions();
opt.setTrueTypeFontsFolder("/usr/share/fonts/msttcorefonts");
document.getPageCount();

Hi

Thank you for additional information. This occurs because PageCout property is updated by rendering engine, which requires fonts to properly layout text on pages.

Best regards.