Knowledgebase (2345)
Children categories
Spire.Doc for Java supports embedding an external file (Word, Excel, PowerPoint, PDF, picture, video, etc.) in Word documents as an OLE object. This article gives you an example of how to insert a PDF file into a Word document.
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.OleObjectType;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.DocOleObject;
import com.spire.doc.fields.DocPicture;
public class InsertOLE {
public static void main(String[] args) {
//Create a Document object and load a Word document
Document doc = new Document();
doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\source.docx");
//Get the last section
Section section = doc.getLastSection();
//Add a paragraph
Paragraph par = section.addParagraph();
//Load an image which will be inserted to Word document representing the embedded file
DocPicture pdfIcon = new DocPicture(doc);
pdfIcon.loadImage("C:\\Users\\Administrator\\Desktop\\pdf-icon.jpg");
//Insert a PDF file to the Word document as an OLE object
par.appendOleObject("C:\\Users\\Administrator\\Desktop\\report.pdf", pdfIcon, OleObjectType.Adobe_Acrobat_Document);
//Save to another file
doc.saveToFile("EmbedDocument.docx", FileFormat.Docx_2013);
}
}

When sending an Excel document to others for review, it is recommended to turn on the Track Changes to ensure that all changes made to the worksheet or workbook are recorded. For the altered cells in Excel, each one will be highlighted with a blue triangle in the upper left corner of the cell. You can then view the changes and decide whether to accept or reject them. This article will demonstrate how to programmatically accept or reject all tracked changes in an Excel workbook using Spire.XLS for .NET.
Install Spire.XLS for .NET
To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.XLS
Accept All Tracked Changes in a Workbook
To accept tracked changes in a workbook, you'll first need to determine whether the workbook has tracked changes using Workbook.HasTrackedChanges property. If yes, you can then accept all changes at once using Workbook.AcceptAllTrackedChanges() method. The following are the steps to accept all tracked changes in an Excel workbook.
- Create a Workbook object.
- Load a sample Excel document using Workbook.LoadFromFile() method.
- Determine if the workbook has tracked changes using Workbook.HasTrackedChanges property.
- Accept all tracked changes in the workbook using Workbook.AcceptAllTrackedChanges() method.
- Save the result document using Workbook.SaveToFile() method.
- C#
- VB.NET
using Spire.Xls;
namespace AcceptTrackedChanges
{
class Program
{
static void Main(string[] args)
{
//Create a Workbook object
Workbook workbook = new Workbook();
//Load a sample Excel document
workbook.LoadFromFile("Sample.xlsx");
//Determine if the workbook has tracked changes
if (workbook.HasTrackedChanges)
{
//Accept all tracked changes in the workbook
workbook.AcceptAllTrackedChanges();
}
//Save the result document
workbook.SaveToFile("AcceptChanges.xlsx", FileFormat.Version2013);
}
}
}

Reject All Tracked Changes in a Workbook
If the tracked changes have been proven to exist in a workbook, Spire.XLS for .NET also provides the Workbook.RejectAllTrackedChanges() method to reject all tracked changes at once. The detailed steps are as follows.
- Create a Workbook object.
- Load a sample Excel document using Workbook.LoadFromFile() method.
- Determine if the workbook has tracked changes using Workbook.HasTrackedChanges property.
- Reject all tracked changes in the workbook using Workbook.RejectAllTrackedChanges() method.
- Save the result document using Workbook.SaveToFile() method.
- C#
- VB.NET
using Spire.Xls;
namespace AcceptTrackedChanges
{
class Program
{
static void Main(string[] args)
{
//Create a Workbook object
Workbook workbook = new Workbook();
//Load a sample Excel document
workbook.LoadFromFile("Sample.xlsx");
//Determine if the workbook has tracked changes
if (workbook.HasTrackedChanges)
{
//Reject all tracked changes in the workbook
workbook.RejectAllTrackedChanges();
}
//Save the result document
workbook.SaveToFile("RejectChanges.xlsx", FileFormat.Version2013);
}
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
This article demonstrates how to set Excel page margins before printing the Excel worksheets in Java applications. By using Spire.XLS for Java, we could set top margin, bottom margin, left margin, right margin, header margin, and footer margin. Please note that the unit for margin is inch on Spire.XLS for Java while On Microsoft Excel, it is cm (1 inch=2.54 cm).
import com.spire.xls.*;
public class setMargins {
public static void main(String[] args) {
String outputFile="output/setMarginsOfExcel.xlsx";
//Load the sample document from file
Workbook workbook = new Workbook();
workbook.loadFromFile("Sample.xlsx");
//Get the first worksheet.
Worksheet sheet = workbook.getWorksheets().get(0);
//Get the PageSetup object of the first worksheet.
PageSetup pageSetup = sheet.getPageSetup();
//Set the page margins of bottom, left, right and top.
pageSetup.setBottomMargin(2);
pageSetup.setLeftMargin(1);
pageSetup.setRightMargin(1);
pageSetup.setTopMargin(3);
//Set the margins of header and footer.
pageSetup.setHeaderMarginInch(2);
pageSetup.setFooterMarginInch(2);
//Save to file.
workbook.saveToFile(outputFile, ExcelVersion.Version2013);
}
}
Output:
