GroupDocs Conversion Java: Konwersja Word do PDF z własnymi czcionkami
W tym obszernej tutorialu dowiesz się, jak groupdocs conversion java umożliwia convert docx to pdf przy zachowaniu własnych stylów czcionek. Niezależnie od tego, czy tworzysz potok dokumentów prawnych, czy publikujesz e‑booki, poniższe kroki zapewnią, że wygenerowany PDF będzie wyglądał dokładnie tak jak oryginalny plik Word.
Quick Answers
- What library handles the conversion? GroupDocs Conversion for Java.
- Can I replace missing fonts? Yes – use font substitution settings.
- Do I need a license for production? A commercial license is required; a free trial is available.
- Which Java version is supported? JDK 8 or higher.
- Is batch conversion possible? Absolutely – wrap the converter in a loop or use the API’s batch features.
What is GroupDocs Conversion Java?
GroupDocs Conversion Java jest wysokowydajnym API, które przetwarza szeroką gamę formatów dokumentów (w tym DOCX, PPTX, XLSX i PDF) bez konieczności instalacji Microsoft Office. Daje programistom precyzyjną kontrolę nad renderowaniem, układem i obsługą czcionek.
Why use custom fonts during conversion?
Osadzenie odpowiednich czcionek zapewnia, że PDF będzie identyczny na każdym urządzeniu, eliminuje problemy z „fallbackiem” czcionek i spełnia wytyczne dotyczące marki. Jest to szczególnie ważne w scenariuszach convert word pdf java, takich jak archiwa prawne, raporty korporacyjne i materiały edukacyjne.
Prerequisites
- Java Development Kit (JDK) – wersja 8 lub nowsza.
- Maven do zarządzania zależnościami.
- IDE (IntelliJ IDEA, Eclipse lub VS Code).
Setting Up GroupDocs.Conversion for Java
Aby rozpocząć, dodaj repozytorium GroupDocs oraz zależność konwersji do swojego projektu Maven.
<repositories>
<repository>
<id>repository.groupdocs.com</id>
<name>GroupDocs Repository</name>
<url>https://releases.groupdocs.com/conversion/java/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.groupdocs</groupId>
<artifactId>groupdocs-conversion</artifactId>
<version>25.2</version>
</dependency>
</dependencies>
License Acquisition
Możesz rozpocząć od free trial lub uzyskać temporary license na rozszerzone testy. Do użytku komercyjnego wymagana jest pełna licencja. Odwiedź GroupDocs Licensing, aby poznać dostępne opcje.
Basic Initialization and Setup
Po dodaniu zależności utwórz instancję Converter, która wskaże na Twój plik DOCX źródłowy.
import com.groupdocs.conversion.Converter;
// Initialize with a document path
Converter converter = new Converter("YOUR_DOCUMENT_DIRECTORY/SampleDocx.docx");
Implementation Guide
Poniżej znajdziesz krok‑po‑kroku przewodnik, który pokazuje, jak set default font pdf oraz zdefiniować własne zamienniki czcionek.
Step 1: Define Conversion Path and Load Options
Najpierw określ, gdzie zostanie zapisany PDF i skonfiguruj opcje ładowania kontrolujące obsługę czcionek.
import com.groupdocs.conversion.options.load.WordProcessingLoadOptions;
import com.groupdocs.conversion.contracts.FontSubstitute;
// Output PDF path
String convertedFile = "YOUR_OUTPUT_DIRECTORY/ConvertedWordToPdf.pdf";
// Configure load options for Word documents
double autoFontSubstitution(false); // Disable automatic font substitution
defaultFont("resources/fonts/Helvetica.ttf"); // Set a default fallback font
// Prepare font substitutes list
List<FontSubstitute> fontSubstitutes = new ArrayList<>();
fontSubstitutes.add(FontSubstitute.create("Tahoma", "Arial")); // Substitute Tahoma with Arial
fontSubstitutes.add(FontSubstitute.create("Times New Roman", "Arial")); // Substitute Times New Roman with Arial
// Apply the substitutes to load options
setFontSubstitutes(fontSubstitutes);
Explanation
setAutoFontSubstitution(false): Wyłącza automatyczne zgadywanie biblioteki, dając pełną kontrolę.setDefaultFont("Helvetica.ttf"): Zapewnia uniwersalny fallback, gdy żądana czcionka nie zostanie znaleziona.setFontSubstitutes(...): Mapuje brakujące czcionki na alternatywy, które są dostępne w docelowym systemie.
Step 2: Configure PDF Conversion Options
Teraz utwórz obiekt opcji specyficznych dla PDF.
import com.groupdocs.conversion.options.convert.PdfConvertOptions;
// Initialize PDF conversion options
double options = new PdfConvertOptions();
Możesz później rozszerzyć PdfConvertOptions, aby dostosować rozmiar strony, marginesy lub ustawienia kompresji.
Step 3: Perform the Conversion
Na koniec uruchom konwersję z wcześniej zdefiniowanymi opcjami ładowania i konwersji.
// Convert Word document to PDF with specified font settings
converter.convert(convertedFile, () -> loadOptions, options);
API odczytuje DOCX, stosuje reguły czcionek i zapisuje PDF, który osadza wybrane czcionki.
Practical Applications
- Legal Document Management – Zachowaj dokładną typografię w PDF‑ach gotowych do sądu.
- Publishing Industry – Utrzymaj spójność czcionek marki w e‑bookach i katalogach.
- Corporate Reports – Zapewnij, że PDF‑y skierowane do interesariuszy odpowiadają wytycznym stylu korporacyjnego.
- Educational Material – Konwertuj notatki wykładowe, zachowując własne czcionki akademickie.
Performance Considerations
- Memory Management – Duże pliki DOCX mogą zużywać znaczną część pamięci heap; monitoruj pamięć JVM i rozważ dostosowanie
-Xmx. - Batch Processing – Umieść logikę konwersji w pętli lub użyj batch API GroupDocs, aby efektywnie obsługiwać wiele plików.
- Resource Allocation – Przydziel wystarczającą liczbę rdzeni CPU przy równoległej konwersji wielu dokumentów.
Common Issues and Solutions
| Issue | Solution |
|---|---|
| Fonts not substituted | Verify that the font files exist at the paths you provided and that the FontSubstitute names match the exact font family names in the source DOCX. |
| Out‑of‑memory errors | Increase JVM heap size (-Xmx2g or higher) or process files in smaller batches. |
| PDF missing embedded fonts | Ensure setDefaultFont points to a TrueType (.ttf) or OpenType (.otf) file and that the license allows font embedding. |
Frequently Asked Questions
Q: Can I use GroupDocs.Conversion without purchasing a license?
A: Yes, you can start with a free trial or obtain a temporary license for evaluation.
Q: What should I do if fonts are not substituting correctly?
A: Ensure the font files are accessible and correctly referenced in setFontSubstitutes. Double‑check the exact font family names.
Q: How can I improve conversion performance for large documents?
A: Process documents in batches, monitor system resources, and consider increasing the JVM heap size.
Q: Is it possible to convert other document types besides Word?
A: Absolutely. GroupDocs Conversion supports images, spreadsheets, presentations, and many more formats.
Q: Where can I find additional documentation for GroupDocs.Conversion?
A: Visit the official guides at GroupDocs Java Conversion Docs for detailed API references.
Conclusion
You now have a complete, production‑ready solution for convert docx to pdf with custom font handling using groupdocs conversion java. By configuring font substitution and default fonts, you guarantee that every PDF mirrors the original Word document’s appearance, no matter where it’s viewed.
Next Steps
- Experiment with additional
PdfConvertOptionssuch as image compression or PDF/A compliance. - Explore batch conversion to automate large‑scale document pipelines.
- Review the full API surface in the official documentation to unlock more advanced features.
Last Updated: 2026-01-13
Tested With: GroupDocs.Conversion 25.2
Author: GroupDocs
Resources
- Documentation: GroupDocs Java Conversion Docs
- API Reference: GroupDocs API Reference
- Download: Get GroupDocs.Conversion
- Purchase: Buy a License
- Free Trial: Trial Downloads
- Temporary License: Request Temporary License
- Support: GroupDocs Support Forum