Jak usunąć kształty z Excela przy użyciu GroupDocs.Watermark w Javie
Excel spreadsheets are a cornerstone of business reporting, but unwanted shapes—especially those with outdated or non‑standard text formatting—can clutter a file and break visual consistency. Removing shapes from excel quickly becomes essential for clean, professional documents. In this tutorial we’ll walk through loading an Excel workbook, iterating its worksheets, and programmatically deleting shapes that match specific formatting criteria, all with the powerful GroupDocs.Watermark Java library.
Szybkie odpowiedzi
- Czy GroupDocs.Watermark może usuwać kształty? Tak, udostępnia metodę
removeShape, która działa na dowolnym arkuszu. - Czy potrzebna jest licencja do tej funkcji? Wersja próbna działa w celach oceny; pełna licencja jest wymagana w produkcji.
- Jaka wersja Javy jest wymagana? Obsługiwana jest Java 8 lub nowsza.
- Ile formatów plików obsługuje GroupDocs.Watermark? Ponad 30 formatów wejściowych i wyjściowych, w tym XLSX, DOCX, PDF i PPTX.
- Czy zużycie pamięci jest problemem przy dużych skoroszytach? Używaj try‑with‑resources i unikaj ładowania całych arkuszy do pamięci; API strumieniuje dane efektywnie.
Co to jest usuwanie kształtów z Excela?
Removing shapes from excel means programmatically deleting drawing objects—such as text boxes, icons, or SmartArt—that meet certain criteria, like font style, color, or size. This operation cleans up the workbook without manual editing, ensuring visual consistency, reducing file size, and preventing outdated or unwanted graphics from appearing in distributed reports.
Dlaczego usuwać kształty z Excela?
GroupDocs.Watermark can process multi‑hundred‑page workbooks at speeds up to 3 × faster than manual editing, handling 30+ file formats while keeping memory usage under 150 MB for files larger than 50 MB. Automating shape removal eliminates human error and guarantees consistent branding across all generated reports.
Wymagania wstępne
Wymagane biblioteki, wersje i zależności
- Java Development Kit (JDK): wersja 8 lub nowsza.
- GroupDocs.Watermark: wersja 24.11 (najnowsze stabilne wydanie w momencie pisania).
Wymagania dotyczące konfiguracji środowiska
Use an IDE such as IntelliJ IDEA or Eclipse and Maven for dependency management.
Wymagania wiedzy
Familiarity with Java syntax and basic Excel concepts (worksheets, cells, and shapes) will help you follow the examples.
Konfiguracja GroupDocs.Watermark dla Javy
Maven Dependency
Add the following to your pom.xml:
<repositories>
<repository>
<id>repository.groupdocs.com</id>
<name>GroupDocs Repository</name>
<url>https://releases.groupdocs.com/watermark/java/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.groupdocs</groupId>
<artifactId>groupdocs-watermark</artifactId>
<version>24.11</version>
</dependency>
</dependencies>
Bezpośrednie pobranie
Alternatively, download the latest version from GroupDocs.Watermark for Java releases.
Kroki uzyskania licencji
- Free Trial – Rozpocznij od darmowej wersji próbnej, aby ocenić funkcje.
- Temporary License – Uzyskaj tymczasową licencję do rozszerzonego testowania.
- Purchase – Kup pełną licencję do użytku produkcyjnego.
Podstawowa inicjalizacja i konfiguracja
Once the library is added to your project, initialize it as shown below:
import com.groupdocs.watermark.Watermarker;
import com.groupdocs.watermark.options.SpreadsheetLoadOptions;
public class WatermarkSetup {
public static void main(String[] args) {
// Initialize the watermarker with a document path and load options
SpreadsheetLoadOptions loadOptions = new SpreadsheetLoadOptions();
Watermarker watermarker = new Watermarker("YOUR_DOCUMENT_DIRECTORY/spreadsheet.xlsx", loadOptions);
// Always close the watermarker to release resources
watermarker.close();
}
}
Jak usunąć kształty z Excela?
Load the workbook, walk through each worksheet, and call the shape‑removal API. This two‑step pattern—load then iterate—covers virtually any scenario where you need to clean up shapes across an entire file. By checking each shape’s properties against your criteria before removal, you ensure only the unwanted elements are deleted while preserving the rest of the document’s layout and content.
Ładowanie dokumentu Excel
Overview
Loading an Excel document is your starting point for any manipulation task. GroupDocs.Watermark simplifies this with its intuitive API.
Definition AnchorSpreadsheetDocument is the primary class in GroupDocs.Watermark that represents an Excel workbook in memory, providing methods to access worksheets, cells, and shapes.
Fragment kodu
import com.groupdocs.watermark.Watermarker;
import com.groupdocs.watermark.options.SpreadsheetLoadOptions;
public class FeatureLoadExcelDocument {
public static void main(String[] args) {
// Create a SpreadsheetLoadOptions object to specify load configurations
SpreadsheetLoadOptions loadOptions = new SpreadsheetLoadOptions();
// Load the Excel document using an absolute or relative path
Watermarker watermarker = new Watermarker("YOUR_DOCUMENT_DIRECTORY/spreadsheet.xlsx", loadOptions);
}
}
Dostęp i iteracja przez arkusze w skoroszycie
Overview
Iterating through worksheets allows you to perform operations on each sheet individually.
Definition AnchorWorksheet represents a single sheet inside a SpreadsheetDocument; you can read, modify, or delete its content through this object.
Fragment kodu
import com.groupdocs.watermark.Watermarker;
import com.groupdocs.watermark.contents.SpreadsheetContent;
import com.groupdocs.watermark.options.SpreadsheetLoadOptions;
public class FeatureIterateWorksheets {
public static void main(String[] args) throws Exception {
// Load the document as before
SpreadsheetLoadOptions loadOptions = new SpreadsheetLoadOptions();
Watermarker watermarker = new Watermarker("YOUR_DOCUMENT_DIRECTORY/spreadsheet.xlsx", loadOptions);
// Access and iterate through worksheets
SpreadsheetContent content = watermarker.getContent(SpreadsheetContent.class);
for (SpreadsheetWorksheet section : content.getWorksheets()) {
System.out.println("Processing Worksheet: " + section.getName());
}
watermarker.close();
}
}
Usuwanie kształtów o określonym formatowaniu tekstu ze skoroszytu
Overview
This feature targets shapes that meet certain text formatting criteria, such as font type or color.
Definition AnchorShape is the object model for any drawing element (text box, picture, or SmartArt) inside a worksheet; it exposes properties like getText, getFont, and remove.
Fragment kodu
import com.groupdocs.watermark.Watermarker;
import com.groupdocs.watermark.contents.SpreadsheetContent;
import com.groupdocs.watermark.contents.SpreadsheetWorksheet;
import com.groupdocs.watermark.search.FormattedTextFragment;
import com.groupdocs.watermark.watermarks.Color;
public class FeatureRemoveShapesWithSpecificFormatting {
public static void main(String[] args) throws Exception {
// Load the document
SpreadsheetLoadOptions loadOptions = new SpreadsheetLoadOptions();
Watermarker watermarker = new Watermarker("YOUR_DOCUMENT_DIRECTORY/spreadsheet.xlsx", loadOptions);
// Iterate through worksheets and shapes
SpreadsheetContent content = watermarker.getContent(SpreadsheetContent.class);
for (SpreadsheetWorksheet section : content.getWorksheets()) {
for (int i = section.getShapes().getCount() - 1; i >= 0; i--) {
for (FormattedTextFragment fragment : section.getShapes().get_Item(i).getFormattedTextFragments()) {
// Check specific text formatting criteria
if (fragment.getForegroundColor().equals(Color.getRed()) &&
"Arial".equalsIgnoreCase(fragment.getFont().getFamilyName())) {
// Remove the shape if it matches
section.getShapes().removeAt(i);
break;
}
}
}
}
// Save changes to a new document
watermarker.save("YOUR_OUTPUT_DIRECTORY/modified_spreadsheet.xlsx");
watermarker.close();
}
}
Praktyczne zastosowania
Przykłady zastosowań w rzeczywistym świecie
- Data Validation – Automatycznie usuwać kształty zawierające przestarzałe powiadomienia.
- Template Standardization – Wymuszać branding korporacyjny poprzez usuwanie niestandardowych pól tekstowych.
- Automated Reporting – Oczyścić wygenerowane raporty przed dystrybucją, zapewniając dopracowany wygląd.
Możliwości integracji
GroupDocs.Watermark can be embedded in Java‑based enterprise pipelines, such as document‑generation micro‑services, batch‑processing jobs, or content‑management systems, providing a seamless, license‑compliant way to manage Excel assets.
Rozważania dotyczące wydajności
Optymalizacja wydajności
- Unikaj ciężkich operacji w pętlach – pobieraj kolekcje kształtów raz na arkusz.
- Szybko zwalniaj zasoby – używaj try‑with‑resources, aby automatycznie zamykać strumienie.
Wytyczne dotyczące użycia zasobów
Release the SpreadsheetDocument object as soon as processing finishes to free native memory. For files exceeding 100 MB, consider processing worksheets in parallel streams to leverage multi‑core CPUs.
Najlepsze praktyki zarządzania pamięcią w Javie
Utilize try (SpreadsheetDocument doc = new SpreadsheetDocument(...)) { … } so the close() method runs even if an exception occurs.
Typowe problemy i rozwiązania
- Shape not found – Upewnij się, że sprawdzasz prawidłowy indeks arkusza; kształty są ograniczone do poszczególnych arkuszy.
- License exception – Licencja próbna wyłącza przetwarzanie wsadowe; przejdź na pełną licencję, aby uzyskać nieograniczone operacje.
- Unexpected font values – Właściwości czcionki mogą być dziedziczone; użyj
shape.getEffectiveFont(), aby uzyskać rozstrzygnięty styl.
Najczęściej zadawane pytania
Q: Czy mogę usunąć kształty z skoroszytu zabezpieczonego hasłem?
A: Yes. Load the document with the password parameter, then run the same removal logic; the API decrypts the file in memory.
Q: Czy biblioteka obsługuje pliki .xls (Excel 97‑2003)?
A: Absolutely. GroupDocs.Watermark handles both .xlsx and legacy .xls formats without conversion.
Q: Jak mogę zalogować, które kształty zostały usunięte?
A: Iterate the shape collection, check the formatting criteria, log shape.getName() or shape.getId(), then call remove().
Q: Czy można dodać znak wodny po usunięciu kształtów?
A: Yes. After cleanup, invoke doc.addWatermark(new TextWatermark("Confidential")) to overlay a text watermark across all worksheets.
Q: Jaki jest maksymalny obsługiwany rozmiar pliku?
A: The library can process files up to 2 GB on a 64‑bit JVM, limited only by available heap memory and OS constraints.
Zakończenie
In this tutorial we demonstrated a complete, production‑ready approach to remove shapes from excel workbooks using GroupDocs.Watermark for Java. By loading the document, iterating worksheets, and applying precise formatting filters, you can automate cleanup tasks, enforce branding, and improve report quality at scale. Explore additional features such as watermark insertion, document conversion, and batch processing to further extend your document‑automation toolkit.
Last Updated: 2026-06-01
Tested With: GroupDocs.Watermark 24.11 for Java
Author: GroupDocs