Converting Office Documents to PDF

Hi Team,

I basically have office documents(Word,Excel,PowerPoint) in byte[] format in Database. My goal is to convert these Documents to PDF with Headers(Title,Company Details) and Footers(Date,Page Numbers,Text) on each page.

I am already using seperate Aspose API(Word,Excel etc) based on the document we want to convert.

Requirement :

  1. When Applying Header and Footer Do I need write separate code for All the Aspose Product ?
  2. Some documents have Embedded Files which needs to be extracted and saved. How can I acheive this ?

@sai537,

We have looked into your requirements. For PowerPoint format, you may use Aspose.Slides to achieve the goal. You can set Header properties (title, Company Details) using Aspose.Slides.DocumentProperties class. HeaderFooterManager can be used for Footer (Date,Page Numbers,Text).

Once you are done with setting these elements in presentation then you will export to PDF format. In this way the resultant PDF will have footer text and properties will be listed in PDF properties. Please visit the following links for details.

Hi,

My Query was do we need to separately write code for each product ??
for e.g this for Word Aspose.

     Word.Document doc = new Word.Document(filestream);
     this.ApplyDocumentDetails(doc, inDoc); // this code applies Header and footer to Word Document.using document builder option 
   doc.Save(filename, Word.SaveFormat.Pdf);

ref to apply header and footer
Or Do I apply header and footer once I convert to PDF ??

@sai537,

Yes, you need to write separate code snippets for each product. Each product have its own APIs to achieve the task. For example you can save the byte array of Excel document to streams and then open the file via Aspose.Cells API, manipulate headers, footers or add properties and save to PDF. Please visit the following links for details.

Aspose.Word, Aspose.Cells and Aspose.Slides have their own APIs to manipulate header/footer settings.

Thank you for confirming.

I am trying to apply the header and footer for Word Document section. using Applying Header and Footer for Word. header and footer is rendering with a table border. I need to remove border.

I have uploaded the Code Snippet and Sample Converted File.
Sample.zip (31.0 KB)

@sai537,

Thanks for your inquiry. Please use Table.ClearBorders method to remove all table and cell borders on this table. Hope this helps you.

Table table = builder.StartTable();
                
// Clear table borders.
builder.CellFormat.ClearFormatting();

... Your code....
... Your code....
... Your code....

builder.EndRow();
builder.EndTable();

table.SetBorders(LineStyle.None, 0, Color.Empty);

Hi,

Thanks It worked with the declaring Table object and then using the object.ClearBorders method.
As I was trying to use directly.

builder.StartTable().ClearBorders()

I also wanted to display a two seperate line of text Align to Right on header part.when I write second builder.write method It appends to the first line. something like this :

 builder.InsertCell();
 builder.CellFormat.PreferredWidth = Word.Tables.PreferredWidth.FromPercent(100 * 2 / 3);
 builder.Write("FirstLine"); // 
 builder.Write("SecondLine"); // Result  : FirstLineSecondLine
 builder.CurrentParagraph.ParagraphFormat.Alignment = Word.ParagraphAlignment.Right;

Expected Result I want is something which should break after firstline and both lines Align to Right.

//FirstLine
//SecondLine

@sai537,

You can simply use DocumentBuilder.Writeln(string) method to meet this requirement. It inserts a string and a paragraph break into the document. Hope, this helps.

Perfect. It worked Thanks.

For Excel Document :
– I also wanted to display a two separate lines. How can I display two lines with a break ??
Code :

      Excel.PageSetup pageSetup = excelDocument.Worksheets[0].PageSetup;
      pageSetup.SetHeader(2, "Report"); // First Line 

For PowerPoint :
– However I am still facing Issue in adding headers and footers to PowerPoint Document.

@sai537,

Well, you may easily put line breaks using escape sequence new line char ("\n"), see the line of code for your reference:
pageSetup.SetHeader(2, "Report 1\nOn &N Page");

Hope, this helps a bit.

Hi,

Thanks. I almost forgot char “\n” to break statement. Its works as expected.

Could please suggest how to add header and footers the same way to PowerPoint documents.

Sample Document Attached with header and footer:

Sample.zip (30.3 KB)

@sai537,

The header text can only be set in NoteSlideView inside slide. For other slide views there is no header and for that reason it is not directly available in HeaderFooterManger. Please try the following code example:

C#

public static void ManageHeader()
{
    String path = @"C:\Aspose Data\";
    Presentation presentation = new Presentation(path + "handout_header_footer.pptx");

    IMasterHandoutSlide masterHandoutSlide = presentation.MasterHandoutSlideManager.MasterHandoutSlide;
    if (masterHandoutSlide != null)
        updateHeaderFooterText(masterHandoutSlide);

    IMasterNotesSlide masterNotesSlide = presentation.MasterNotesSlideManager.MasterNotesSlide;
    if (null != masterNotesSlide)
                updateHeaderFooterText(masterNotesSlide);

    foreach (ISlide slide in presentation.Slides)
    {
        INotesSlide notesSlide = slide.NotesSlideManager.NotesSlide;
        if (null != notesSlide)
            updateHeaderFooterText(notesSlide);
     }
    presentation.Save(path + "handout_test.pptx", Aspose.Slides.Export.SaveFormat.Pptx);
}
public static void updateHeaderFooterText(IBaseSlide master)
{
    foreach (IShape shape in master.Shapes)
    {
        if (shape.Placeholder != null)
        {
            if (shape.Placeholder.Type == PlaceholderType.Header)
                ((IAutoShape)shape).TextFrame.Text = "HI there new header";

            if (shape.Placeholder.Type == PlaceholderType.Footer)
                ((IAutoShape)shape).TextFrame.Text = "HI there new footer";
            }
    }
}

If this does not help, then kindly send us your source presentation file along with expected output document. We will investigate and share our findings with you.

Hi,

Apologies for the delay in response.

I tried the above code mentioned for PowerPoint Document. Header and Footer Text didn’t get applied.

I have attached the Sample source presentation and Expected output Document
Expected Format is a Image file.
Docs.zip (29.8 KB)

@sai537,

I suggest you to please try using following sample code on your end.

public static void ManageHeader1()
{
    String path = @"C:\Aspose Data\";
    Presentation presentation = new Presentation(path + "SamplePowerPoint.pptx");

 
    IMasterHandoutSlide masterHandoutSlide = presentation.MasterHandoutSlideManager.MasterHandoutSlide;
    if (masterHandoutSlide != null)
        updateHeaderFooterText1(masterHandoutSlide);

    IMasterNotesSlide masterNotesSlide = presentation.MasterNotesSlideManager.MasterNotesSlide;
    if (null != masterNotesSlide)
        updateHeaderFooterText1(masterNotesSlide);

    foreach (ISlide slide in presentation.Slides)
    {
        presentation.HeaderFooterManager.SetAllSlideNumbersVisibility(true);
        slide.HeaderFooterManager.SetDateTimeVisibility(true);
        slide.HeaderFooterManager.SetFooterVisibility(true);
        slide.HeaderFooterManager.SetFooterText("test Footer");
        slide.HeaderFooterManager.SetSlideNumberVisibility(true);

        INotesSlide notesSlide = slide.NotesSlideManager.NotesSlide;
        if (null != notesSlide)
            updateHeaderFooterText1(notesSlide);
    }
    presentation.Save(path + "handout_test.pptx", Aspose.Slides.Export.SaveFormat.Pptx);
}
public static void updateHeaderFooterText1(IBaseSlide master)
{
    foreach (IShape shape in master.Shapes)
    {
        if (shape.Placeholder != null)
        {
            if (shape.Placeholder.Type == PlaceholderType.Header)
                ((IAutoShape)shape).TextFrame.Text = "HI there new header";

            if (shape.Placeholder.Type == PlaceholderType.Footer)
                ((IAutoShape)shape).TextFrame.Text = "HI there new footer";
        }
    }
}

I also like to add further that there is no header in case of normal slides so you may not be able to set the header text for a slide.