EvotecIT / OfficeIMO

Fast and easy to use cross-platform .NET library that creates or modifies Microsoft Word (DocX) and later also Excel (XLSX) files without installing any software. Library is based on Open XML SDK
MIT License
286 stars 50 forks source link

Paraghraph font size #137

Closed geraluca closed 1 week ago

geraluca commented 1 year ago

If in the paragraph I select the font and insert the size, in some cases the document selects a different size, why?

PrzemyslawKlys commented 1 year ago

You need to give me context and an example to replicate this issue. Otherwise I won't be able to help.

geraluca commented 1 year ago

docux.Sections(0).Margins.Bottom = 10 docux.Sections(0).Margins.Top = 10 docux.Sections(0).Margins.Left = 600 docux.Sections(0).Margins.Right = 600

this is my code, if top and bottom change the value, the top and bottom margins don't change, instead left and right work and change

geraluca commented 1 year ago

docux.Settings.FontFamily = "Arial" docux.Settings.FontSize = 9

if I place a font in the document and then in a cell or in a paragraph I want to use a different fontsize I won't allow it

PrzemyslawKlys commented 1 year ago

Simple example that tries to replicate your issue:

public static void Example_BasicWordWithMargins(string folderPath, bool openWord) {
    Console.WriteLine("[*] Creating standard document with margins");
    string filePath = System.IO.Path.Combine(folderPath, "EmptyDocumentWithMargins.docx");
    using (WordDocument document = WordDocument.Create(filePath)) {
        document.Sections[0].Margins.Bottom = 10;
        document.Sections[0].Margins.Left = 600;
        document.Sections[0].Margins.Top = 10;
        document.Sections[0].Margins.Right = 600;
        document.Save(openWord);
    }
}

Result

image

Works fine to me.

Another example of yours:

public static void Example_BasicWordWithMargins(string folderPath, bool openWord) {
    Console.WriteLine("[*] Creating standard document with margins");
    string filePath = System.IO.Path.Combine(folderPath, "EmptyDocumentWithMargins.docx");
    using (WordDocument document = WordDocument.Create(filePath)) {
        document.Sections[0].Margins.Bottom = 10;
        document.Sections[0].Margins.Left = 600;
        document.Sections[0].Margins.Top = 10;
        document.Sections[0].Margins.Right = 600;

        document.Settings.FontFamily = "Arial";
        document.Settings.FontSize = 9;

        document.AddParagraph("This should be Arial 9");

        var par = document.AddParagraph("This should be Tahoma 20");
        par.FontFamily = "Tahoma";
        par.FontSize = 20;

        document.Save(openWord);
    }
}

image

I am not sure what library you are using and how, but the examples you provided work fine in C# and should work fine in other .NET languages.

geraluca commented 1 year ago

i use visual studio, when i load the code docux.AddHeadersAndFooters() then obviously I get an image in the header and a text in the footers, this doesn't allow me to reduce the spaces as I would like

PrzemyslawKlys commented 1 year ago

Either you will provide an example code that I can run that shows your problem or I won't help you. docux.AddHeadersAndFooters() doesn't add images, nor spaces, so unless you will put an effort in creating proper issue I'm going to close the issue with comment "works as expected".

geraluca commented 1 year ago
  newfile = "myfile.doc"

    Using docux As WordDocument = Create(Application.StartupPath() & newfile)

        docux.Sections(0).Margins.Bottom = 10
        docux.Sections(0).Margins.Top = 10
        docux.Sections(0).Margins.Left = 600
        docux.Sections(0).Margins.Right = 600

        docux.Settings.FontFamily = "Arial"
        docux.Settings.FontSize = 9

        docux.AddHeadersAndFooters()

        docux.Header.Default.AddParagraph().AddImage(Application.StartupPath() & "\Logo.png", 734, 92)
        docux.Header.Default.Paragraphs(0).SetFontFamily("Arial")
        docux.Header.Default.Paragraphs(0).SetFontSize(7).Bold = False

        docux.Footer.Default.AddParagraph()
        docux.Footer.Default.Paragraphs(0).SetFontFamily("Arial")
        docux.Footer.Default.Paragraphs(0).SetFontSize(7).Bold = False
        docux.Footer.Default.Paragraphs(0).ParagraphAlignment = JustificationValues.Right
        docux.Footer.Default.Paragraphs(0).Text = "SMA.5.doc 04/10/19"
        docux.Footer.Default.Paragraphs(0).LineSpacingAfter = 0
        docux.Footer.Default.Paragraphs(0).LineSpacingBefore = 0
        docux.Footer.[Default].AddPageNumber(WordPageNumberStyle.PageNumberXofY)

        docux.Footer.Default.AddParagraph()
        docux.Footer.Default.Paragraphs(0).SetFontFamily("Arial")
        docux.Footer.Default.Paragraphs(0).SetFontSize(7).Bold = False
        docux.Footer.Default.Paragraphs(0).ParagraphAlignment = JustificationValues.Center
        docux.Footer.Default.Paragraphs(0).Text = "My address"
        docux.Footer.Default.Paragraphs(0).LineSpacingAfter = 0
        docux.Footer.Default.Paragraphs(0).LineSpacingBefore = 0

        Dim par00 = docux.AddParagraph("My text")
        par00.ParagraphAlignment = JustificationValues.Left
        par00.SetFontFamily("Arial").SetFontSize(10).Bold = True
        par00.LineSpacingAfter = 50
        par00.LineSpacingBefore = 0

        Dim par01 = docux.AddParagraph("My declaration")
        par01.ParagraphAlignment = JustificationValues.Left
        par01.SetFontFamily("Arial").SetFontSize(10).Bold = True
        par01.LineSpacingAfter = 50
        par01.LineSpacingBefore = 200

                    docux.Save()

    End Using
PrzemyslawKlys commented 1 year ago

Here's your code in c#

public static void Example_BasicWordWithMarginsAndImage(string folderPath, bool openWord) {
    Console.WriteLine("[*] Creating standard document with margins");
    string filePath = System.IO.Path.Combine(folderPath, "DocumentWithMarginsAndImage.docx");
    using (WordDocument document = WordDocument.Create(filePath)) {

        string imagePaths = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), "Images");
        var filePathImage = System.IO.Path.Combine(imagePaths, "EvotecLogo.png");

        document.Sections[0].Margins.Bottom = 10;
        document.Sections[0].Margins.Top = 10;
        document.Sections[0].Margins.Left = 600;
        document.Sections[0].Margins.Right = 600;

        document.Settings.FontFamily = "Arial";
        document.Settings.FontSize = 9;

        document.AddHeadersAndFooters();

        document.Header.Default.AddParagraph().AddImage(filePathImage, 734, 92);
        document.Header.Default.Paragraphs[0].SetFontFamily("Arial");
        document.Header.Default.Paragraphs[0].SetFontSize(7).Bold = false;

        document.Footer.Default.AddParagraph();
        document.Footer.Default.Paragraphs[0].SetFontFamily("Arial");
        document.Footer.Default.Paragraphs[0].SetFontSize(7).Bold = false;
        document.Footer.Default.Paragraphs[0].ParagraphAlignment = JustificationValues.Right;
        document.Footer.Default.Paragraphs[0].Text = "SMA.5.doc 04/10/19";
        document.Footer.Default.Paragraphs[0].LineSpacingAfter = 0;
        document.Footer.Default.Paragraphs[0].LineSpacingBefore = 0;
        document.Footer.Default.AddPageNumber(WordPageNumberStyle.PageNumberXofY);

        document.Footer.Default.AddParagraph();
        document.Footer.Default.Paragraphs[0].SetFontFamily("Arial");
        document.Footer.Default.Paragraphs[0].SetFontSize(7).Bold = false;
        document.Footer.Default.Paragraphs[0].ParagraphAlignment = JustificationValues.Center;
        document.Footer.Default.Paragraphs[0].Text = "My address";
        document.Footer.Default.Paragraphs[0].LineSpacingAfter = 0;
        document.Footer.Default.Paragraphs[0].LineSpacingBefore = 0;

        var par00 = document.AddParagraph("My text");
        par00.ParagraphAlignment = JustificationValues.Left;
        par00.SetFontFamily("Arial").SetFontSize(10).Bold = true;
        par00.LineSpacingAfter = 50;
        par00.LineSpacingBefore = 0;

        var par01 = document.AddParagraph("My declaration");
        par01.ParagraphAlignment = JustificationValues.Left;
        par01.SetFontFamily("Arial").SetFontSize(10).Bold = true;
        par01.LineSpacingAfter = 50;
        par01.LineSpacingBefore = 200;

        document.Save(openWord);
    }
}

Here's the output of it:

image

But since you have provided no information what do you actually want to achieve it's really hard to help you.

One thing to notice in your code is that you add multiple paragraphs to Footer, but overwrite SMA.5.doc, with My Address.

public static void Example_BasicWordWithMarginsAndImage(string folderPath, bool openWord) {
    Console.WriteLine("[*] Creating standard document with margins");
    string filePath = System.IO.Path.Combine(folderPath, "DocumentWithMarginsAndImage.docx");
    using (WordDocument document = WordDocument.Create(filePath)) {

        string imagePaths = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), "Images");
        var filePathImage = System.IO.Path.Combine(imagePaths, "EvotecLogo.png");

        document.Sections[0].Margins.Bottom = 10;
        document.Sections[0].Margins.Top = 10;
        document.Sections[0].Margins.Left = 600;
        document.Sections[0].Margins.Right = 600;

        document.Settings.FontFamily = "Arial";
        document.Settings.FontSize = 9;

        document.AddHeadersAndFooters();

        document.Header.Default.AddParagraph().AddImage(filePathImage, 734, 92);
        document.Header.Default.Paragraphs[0].SetFontFamily("Arial");
        document.Header.Default.Paragraphs[0].SetFontSize(7).Bold = false;

        document.Footer.Default.AddParagraph();
        document.Footer.Default.Paragraphs[0].SetFontFamily("Arial");
        document.Footer.Default.Paragraphs[0].SetFontSize(7).Bold = false;
        document.Footer.Default.Paragraphs[0].ParagraphAlignment = JustificationValues.Right;
        document.Footer.Default.Paragraphs[0].Text = "SMA.5.doc 04/10/19";
        document.Footer.Default.Paragraphs[0].LineSpacingAfter = 0;
        document.Footer.Default.Paragraphs[0].LineSpacingBefore = 0;
        document.Footer.Default.AddPageNumber(WordPageNumberStyle.PageNumberXofY);

        document.Footer.Default.AddParagraph();
        document.Footer.Default.Paragraphs[1].SetFontFamily("Arial");
        document.Footer.Default.Paragraphs[1].SetFontSize(7).Bold = false;
        document.Footer.Default.Paragraphs[1].ParagraphAlignment = JustificationValues.Center;
        document.Footer.Default.Paragraphs[1].Text = "My address";
        document.Footer.Default.Paragraphs[1].LineSpacingAfter = 0;
        document.Footer.Default.Paragraphs[1].LineSpacingBefore = 0;

        var par00 = document.AddParagraph("My text");
        par00.ParagraphAlignment = JustificationValues.Left;
        par00.SetFontFamily("Arial").SetFontSize(10).Bold = true;
        par00.LineSpacingAfter = 0;
        par00.LineSpacingBefore = 0;

        var par01 = document.AddParagraph("My declaration");
        par01.ParagraphAlignment = JustificationValues.Left;
        par01.SetFontFamily("Arial").SetFontSize(10).Bold = true;
        par01.LineSpacingAfter = 0;
        par01.LineSpacingBefore = 0;

        document.Save(openWord);
    }
}

image

Hope this helps. if you want to have it in the same line you would either use tabs or invisible table to position it properly.

geraluca commented 1 year ago

Double footers is an error, optionally the first paragraph or the second. What I would like to achieve is to gain space for the texts, I would like to decrease the empty space above the logo image and below the footers, it would help to recover some more lines in the document. Instead going back to the font size, yes, as you demonstrated in the example it works, but strangely in some paragraphs or in some tables it doesn't work even though the code is identical, this is inexplicable but it happens. Another question, how is it possible to save in pdf? Thanks for your time.