在修改数字文件的上下文中,文档编辑是指更改文档中的文本、图像或其他元素的过程。数据文件的数字编辑可用于提高内容的准确性和清晰度,对现有内容进行更正,使文档更易于阅读等等。随着不同类型数字文档的使用不断增加,对它们进行电子编辑的需求变得越来越重要。
以电子方式编辑数字文档的能力已成为任何使用文档的人的基本技能。了解如何快速准确地编辑 PDF、Microsoft Office 和其他数据文件可以大大节省时间。为此,您可以使用支持跨 .NET 和 Java 平台编辑一系列流行数据文件格式(例如 PDF、Word、Excel、PowerPoint、OpenDocument、RTF、文本、HTML、电子书等)的 GroupDocs.Editor API .
要开始使用 .NET 或 Java 编辑文档,您首先需要安装所需版本的 GroupDocs.Editor。请参阅以下部分中共享的说明,以正确设置适用于 .NET 或 Java 的 GroupDocs.Editor。
请随时从下载部分下载 DLL 或 MSI 安装程序,或使用 NuGet 安装 API。您还可以使用包管理器控制台:
如需更多安装帮助,请随时查看本指南。
对于 Java 版本,您可以从 下载部分 下载 JAR 文件,或者为存储库添加以下配置和您(基于 Maven 的)Java 应用程序中的依赖项:
<repository>
<id>GroupDocsJavaAPI</id>
<name>GroupDocs Java API</name>
<url>https://repository.groupdocs.com/repo/</url>
</repository>
<dependency>
<groupId>com.groupdocs</groupId>
<artifactId>groupdocs-editor</artifactId>
<version>20.11</version>
</dependency>
如果您需要更多信息,请查看此安装指南。
现在您已经设置了正确的 API 版本,让我们检查一些广泛使用的编辑多格式文档的案例场景。
PDF 格式是一种流行的文件类型,用于文档、报告和其他数字内容。它代表便携式文档格式,由于能够生成易于共享的高质量文档而被广泛使用。它不同于其他流行的数据文件格式,因为它提供了固定的布局,并且无论您正在阅读或查看它的设备和操作系统如何,它都保持相同的格式和布局。
但是,如果您需要对 PDF 文档进行更改怎么办?编辑 PDF 文件可能是一个棘手的过程,但如果您使用 GroupDocs.Editor for .NET API,则不必如此。此 API 使您能够像任何其他文档一样使用所见即所得的编辑器编辑 PDF 文件。目前,仅 .NET 版本的 GroupDocs.Editor API 支持 PDF 编辑,Java 版本不支持。
请使用以下代码在 .NET 中加载、编辑然后保存 PDF 文件:
//1. Simple preparations of input data
const string filename = "sample.pdf";
const string password = "password";
string inputPath = System.IO.Path.Combine(Common.TestHelper.PdfFolder, filename);
//2. Create a load options class with password
GroupDocs.Editor.Options.PdfLoadOptions loadOptions = new PdfLoadOptions();
loadOptions.Password = password;
//3. Create edit options and tune/adjust if necessary
GroupDocs.Editor.Options.PdfEditOptions editOptions = new PdfEditOptions();
editOptions.EnablePagination = true; //Enable pagination for per-page processing in WYSIWYG-editor
editOptions.Pages = PageRange.FromStartPageTillEnd(3); //Edit not all pages, but starting from 3rd and till the end
//4. Create an Editor instance, load a document
GroupDocs.Editor.Editor editor = new Editor(inputPath, delegate () { return loadOptions; });
//5. Edit a document and generate EditableDocument
GroupDocs.Editor.EditableDocument originalDoc = editor.Edit(editOptions);
//6. Generate HTML/CSS, send it to WYSIWYG, edit there, and obtain edited version
string originalContent = originalDoc.GetEmbeddedHtml();
string editedContent = originalContent.Replace(".NET Framework", "I love Java!!!");
//7. Generate EditableDocument from edited content
EditableDocument editedDoc = EditableDocument.FromMarkup(editedContent, null);
//8. Create and adjust save options
GroupDocs.Editor.Options.PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.Compliance = PdfCompliance.Pdf20;
//9. Save to a file or a stream
string outputPath = System.IO.Path.Combine(Common.TestHelper.OutputFolder, filename);
editor.Save(editedDoc, outputPath, saveOptions);
//10. Don't forget to dispose all resources
originalDoc.Dispose();
editedDoc.Dispose();
editor.Dispose(); Microsoft Word、Excel 和 PowerPoint 是广泛使用的格式,分别用于创建文档、电子表格和演示文稿。它们在大多数企业和组织中被用作标准格式,对于任何希望以高效方式组织、分析和呈现数据的人来说都是必不可少的工具。
您是否希望在 .NET 或 Java 中以编程方式编辑这些文档格式中的任何一种?如果是,您可以使用 GroupDocs.Editor API 并编辑 Microsoft Word、Excel 和 PowerPoint 文档,您甚至不需要在系统上安装 Microsoft Office 来执行此操作。
要在 .NET 中编辑 Word 文档 (DOCX),请使用此代码:
using (FileStream fs = File.OpenRead("filepath/document.docx"))
{
// Load Document
Options.WordProcessingLoadOptions loadOptions = new WordProcessingLoadOptions();
loadOptions.Password = "password-if-any";
// Edit Document
using (Editor editor = new Editor(delegate { return fs; }, delegate { return loadOptions; }))
{
Options.WordProcessingEditOptions editOptions = new WordProcessingEditOptions();
editOptions.FontExtraction = FontExtractionOptions.ExtractEmbeddedWithoutSystem;
editOptions.EnableLanguageInformation = true;
editOptions.EnablePagination = true;
using (EditableDocument beforeEdit = editor.Edit(editOptions))
{
string originalContent = beforeEdit.GetContent();
List allResources = beforeEdit.AllResources;
string editedContent = originalContent.Replace("document", "edited document");
// Save Document
using (EditableDocument afterEdit = EditableDocument.FromMarkup(editedContent, allResources))
{
WordProcessingFormats docxFormat = WordProcessingFormats.Docx;
Options.WordProcessingSaveOptions saveOptions = new WordProcessingSaveOptions(docxFormat);
saveOptions.EnablePagination = true;
saveOptions.Locale = System.Globalization.CultureInfo.GetCultureInfo("en-US");
saveOptions.OptimizeMemoryUsage = true;
saveOptions.Password = "password";
saveOptions.Protection = new WordProcessingProtection(WordProcessingProtectionType.ReadOnly, "write_password");
using (FileStream outputStream = File.Create("filepath/editedDocument.docx"))
{
editor.Save(afterEdit, outputStream, saveOptions);
}
}
}
}
} 要在 Java 中编辑 Word 文档,请使用以下代码:
// Edit the Word DOC/DOCX documents in Java
Options.WordProcessingLoadOptions loadOptions = new WordProcessingLoadOptions();
loadOptions.setPassword("password-if-any");
Editor editor = new Editor("path/document.docx", loadOptions);
EditableDocument defaultWordProcessingDoc = editor.edit();
// Either edit using any WYSIWYG editor or edit programmatically
String allEmbeddedInsideString = defaultWordProcessingDoc.getEmbeddedHtml();
String allEmbeddedInsideStringEdited = allEmbeddedInsideString.replace("document", "edited document");
// Save the edited document
EditableDocument editedDoc = EditableDocument.fromMarkup(allEmbeddedInsideStringEdited, null);
WordProcessingSaveOptions saveOptions = new WordProcessingSaveOptions(WordProcessingFormats.Docx);
editor.save(editedDoc, "path/edited-document.docx", saveOptions); 您可以使用以下 C# 代码在 .NET 中编辑 Excel 文档:
Options.SpreadsheetLoadOptions loadOptions = new SpreadsheetLoadOptions();
loadOptions.Password = "password";
// Load the Spreadsheet
Editor editor = new Editor("path/spreadsheet.xlsx", delegate { return loadOptions; });
// Get 1st tab of the Spreadsheet
SpreadsheetEditOptions sheetTab1EditOptions = new SpreadsheetEditOptions();
sheetTab1EditOptions.WorksheetIndex = 0; // first worksheet
// Obtain HTML markup from some EditableDocument instance
EditableDocument firstTab = editor.Edit(sheetTab1EditOptions);
string bodyContent = firstTab.GetBodyContent(); // HTML markup from inside the HTML -> BODY element
string allContent = firstTab.GetContent(); // Full HTML markup of all document, with HTML -> HEAD header and all its content
List onlyImages = firstTab.Images;
List allResourcesTogether = firstTab.AllResources;
string editedContent = allContent.Replace("Company Name", "New Company Name");
EditableDocument afterEdit = EditableDocument.FromMarkup(editedContent, allResourcesTogether);
// Create save options
SpreadsheetFormats xlsxFormat = SpreadsheetFormats.Xlsx;
Options.SpreadsheetSaveOptions saveOptions = new SpreadsheetSaveOptions(SpreadsheetFormats.Xlsx);
// Set new opening password
saveOptions.Password = "newPassword";
saveOptions.WorksheetProtection = new WorksheetProtection(WorksheetProtectionType.All, "WriteProtectionPassword");
// Create output stream
using (FileStream outputStream = File.Create("path/editedSpreadsheet.xlsx"))
{
editor.Save(afterEdit, outputStream, saveOptions);
} 要编辑 Excel Java 电子表格,您可以使用以下代码片段:
// Edit the Excel XLS/XLSX documents in Java
SpreadsheetLoadOptions loadOptions = new SpreadsheetLoadOptions();
loadOptions.setPassword("password-if-any");
// Loading Spreadsheet
Editor editor = new Editor("path/sample_sheet.xlsx", loadOptions);
// Edit 1st tab of the Spreadsheet
SpreadsheetEditOptions editOptions = new SpreadsheetEditOptions();
editOptions.setWorksheetIndex(0); // index is 0-based, so this is 1st tab
EditableDocument firstTab = editor.edit(editOptions);
String bodyContent = firstTab.getBodyContent();
String allContent = firstTab.getContent();
List onlyImages = firstTab.getImages();
List allResourcesTogether = firstTab.getAllResources();
String editedSheetContent = allContent.replace("Old Company Name","New Company Name");
EditableDocument editedDoc = EditableDocument.fromMarkup(editedSheetContent, null);
SpreadsheetSaveOptions saveOptions = new SpreadsheetSaveOptions(SpreadsheetFormats.Xlsx);
saveOptions.setPassword("new-password");
editor.save(editedDoc, "path/edited_spreadsheet.xlsx", saveOptions);
firstTab.dispose();
editor.dispose()
同样,您也可以使用 GroupDocs.Editor API 编辑 PowerPoint 演示文稿。请检查 .NET 和 Java 编辑 指南以获得更多帮助。
文本文件(用 TXT 表示)是最常用的文件格式之一,因为它们轻巧、简单且易于创建和共享。它们以多种方式使用,从编写代码到创建纯文本文档。文本文档不包含任何文本格式、图像、表单、表格或任何其他富文本元素。
GroupDocs.Editor API 支持在 .NET 和 Java 平台中编辑文本文件。您可以将文本文档编辑功能集成到您现有的应用程序中并增强其功能或为此目的构建您自己的文本编辑器应用程序。
下面给出的示例代码可用于在 .NET 中编辑文本文件。编辑后的文件可以保存为 TXT 和文字处理 (DOCM) 格式:
//Load the text file
string inputTxtPath = "C://input/file.txt";
Editor editor = new Editor(inputTxtPath);
TextEditOptions editOptions = new TextEditOptions();
editOptions.Encoding = System.Text.Encoding.UTF8;
editOptions.RecognizeLists = true;
editOptions.LeadingSpaces = TextLeadingSpacesOptions.ConvertToIndent;
editOptions.TrailingSpaces = TextTrailingSpacesOptions.Trim;
editOptions.Direction = TextDirection.Auto;
EditableDocument beforeEdit = editor.Edit(editOptions); // Create EditableDocument instance
string originalTextContent = beforeEdit.GetContent(); // Get HTML content
string updatedTextContent = originalTextContent.Replace("text", "EDITED text"); // Edit the content
List allResources = beforeEdit.AllResources; // Get resources (only one stylesheet in this case)
//Finally, create new EditableDocument
EditableDocument afterEdit = EditableDocument.FromMarkup(updatedTextContent, allResources);\
//Save the edited document to TXT and DOCM format
TextSaveOptions txtSaveOptions = new TextSaveOptions();
txtSaveOptions.AddBidiMarks = true;
txtSaveOptions.PreserveTableLayout = true;
WordProcessingSaveOptions wordSaveOptions = new WordProcessingSaveOptions(WordProcessingFormats.Docm);
string outputTxtPath = "C:\output\document.txt";
string outputWordPath = "C:\output\document.docm";
editor.Save(afterEdit, outputTxtPath, txtSaveOptions);
editor.Save(afterEdit, outputWordPath, wordSaveOptions); 要用 Java 编辑文本文件,您可以使用下面显示的代码。然后您可以将修改后的文档保存为 TXT 或 Word (DOCM) 文件格式:
// Loading the text document
String inputTxtPath = "C://input//file.txt";
Editor editor = new Editor(inputTxtPath);
TextEditOptions editOptions = new TextEditOptions();
editOptions.setEncoding(StandardCharsets.UTF_8);
editOptions.setRecognizeLists(true);
editOptions.setLeadingSpaces(TextLeadingSpacesOptions.ConvertToIndent);
editOptions.setTrailingSpaces(TextTrailingSpacesOptions.Trim);
editOptions.setDirection(TextDirection.Auto);
EditableDocument beforeEdit = editor.edit(editOptions); // Create EditableDocument instance
String originalTextContent = beforeEdit.getContent(); // Get HTML content
String updatedTextContent = originalTextContent.replace("text", "EDITED text"); // Edit the content
List allResources = beforeEdit.getAllResources(); // Get resources (only one stylesheet actually in this case)
//Finally, create new EditableDocument
EditableDocument afterEdit = EditableDocument.fromMarkup(updatedTextContent, allResources);
//Saving the modified file to TXT and DOCM formats
TextSaveOptions txtSaveOptions = new TextSaveOptions();
txtSaveOptions.setAddBidiMarks(true);
txtSaveOptions.setPreserveTableLayout(true);
WordProcessingSaveOptions wordSaveOptions = new WordProcessingSaveOptions(WordProcessingFormats.Docm);
String outputTxtPath = "C:\\output\\document.txt";
String outputWordPath = "C:\\output\\document.docm";
editor.save(afterEdit, outputTxtPath, txtSaveOptions);
editor.save(afterEdit, outputWordPath, wordSaveOptions); 电子邮件文档是包含电子邮件内容的文件,包括邮件正文和任何附件。这些文件通常用于在不同的电子邮件客户端之间传输电子邮件或以标准格式存储它们。它们被各种电子邮件客户端使用,例如 Microsoft Outlook、Apple Mail 和 Mozilla Thunderbird。这些文件构成了我们在数字时代如何相互交流的一个组成部分。
随着这些文件类型的使用增加,编辑它们也变得很普遍。无论您希望将流程与另一个系统集成,还是合并自定义逻辑,自动化编辑流程都是非常宝贵的。 GroupDocs.Editor API(适用于 .NET 和 Java)使您能够做到这一点。您可以使用这些 API 编辑流行格式的电子邮件文档。
如果您要加载、编辑和保存电子邮件消息 (MSG),请使用此C#代码:
//1. Prepare a sample file const string msgFilename = "ComplexExample.msg"; string msgInputPath = System.IO.Path.Combine(Common.TestHelper.EmailFolder, msgFilename); //2. Load to the Editor class Editor msgEditor = new Editor(msgInputPath); //3. Create edit options with all content Options.EmailEditOptions editOptions = new EmailEditOptions(MailMessageOutput.All); //4. Generate an editable document EditableDocument originalDoc = msgEditor.Edit(editOptions); //5. Emit HTML from EditableDocument, send it to the client-side, edit it there in WYSIWYG-editor (omitted here) string savedHtmlContent = originalDoc.GetEmbeddedHtml(); //6. Obtain edited content from the client-side and generate a new EditableDocument from it (omitted here) EditableDocument editedDoc = EditableDocument.FromMarkup(savedHtmlContent, null); //7. Create 1st save options EmailSaveOptions saveOptions1 = new EmailSaveOptions(MailMessageOutput.Common); //8. Create 2nd save options EmailSaveOptions saveOptions2 = new EmailSaveOptions(MailMessageOutput.Body | MailMessageOutput.Attachments); //9. Generate and save 1st output MSG to the file string outputMsgPath = System.IO.Path.Combine(Common.TestHelper.OutputFolder, "OutputFile.msg"); msgEditor.Save(editedDoc, outputMsgPath, saveOptions1); //10. Generate and save 2nd output MSG to the stream Stream outputMsgStream = File.Create(Path.Combine(Common.TestHelper.OutputFolder, "OutputStream.msg")); msgEditor.Save(editedDoc, outputMsgStream, saveOptions2); //11. Dispose all resources outputMsgStream.Dispose(); editedDoc.Dispose(); originalDoc.Dispose(); msgEditor.Dispose();
对于加载、编辑和保存电子邮件文件 (MSG),您可以使用以下代码:
//1. Prepare a sample file String msgInputPath = "C:\ComplexExample.msg"; //2. Load to the Editor class Editor msgEditor = new Editor(msgInputPath); //3. Create edit options with all content EmailEditOptions editOptions = new EmailEditOptions(MailMessageOutput.All); //4. Generate an editable document EditableDocument originalDoc = msgEditor.edit(editOptions); //5. Emit HTML from EditableDocument, send it to the client-side, edit it there in WYSIWYG-editor (omitted here) String savedHtmlContent = originalDoc.getEmbeddedHtml(); //6. Obtain edited content from the client-side and generate a new EditableDocument from it (omitted here) EditableDocument editedDoc = EditableDocument.fromMarkup(savedHtmlContent, null); //7. Create 1st save options EmailSaveOptions saveOptions1 = new EmailSaveOptions(MailMessageOutput.Common); //8. Create 2nd save options EmailSaveOptions saveOptions2 = new EmailSaveOptions(MailMessageOutput.Body | MailMessageOutput.Attachments); //9. Generate and save 1st output MSG to the file String outputMsgPath = "C:\OutputFile.msg"; msgEditor.save(editedDoc, outputMsgPath, saveOptions1); //10. Generate and save 2nd output MSG to the stream Stream outputMsgStream = "C:\OutputStream.msg"; msgEditor.save(editedDoc, outputMsgStream, saveOptions2); //11. Dispose all resources outputMsgStream.dispose(); editedDoc.dispose(); originalDoc.dispose(); msgEditor.dispose();
您还可以使用我们的 免费文档编辑应用程序来自您选择的设备,所以请随时检查它们。