تضمين الرسومات في مستندات LaTeX باستخدام Java

أدرج الصور النقطية عالية الدقة (PNG وJPEG وBMP) والرسومات المتجهية (EPS وPDF) في مستندات LaTeX باستخدام Aspose.TeX for Java. ومن خلال الاستفادة من حزم LaTeX الأصلية مثل graphicx وتهيئة أدلة العمل الخاصة بالإدخال، يمكن للمطورين تجميع المستندات التي تتضمن المخططات والرسومات والصور والرسومات المتجهية بسلاسة وتحويلها إلى مخرجات PDF وXPS والصور الجاهزة للنشر.

 

عند تجميع مستندات LaTeX التي تحتوي على عناصر رسومية، يقوم Aspose.TeX for Java بحل ملفات الصور المشار إليها بالنسبة إلى IInputWorkingDirectory الذي تم تكوينه.

  • للصور الفوتوغرافية ولقطات الشاشة النقطية ورسومات الويب، اختر تنسيقات PNG/JPEG المضمّنة باستخدام أوامر \usepackage{graphicx} - \includegraphics{figure.png}.
  • للمخططات المتجهية عالية الدقة ورسومات CAD والمخططات العلمية، اختر تنسيق EPS المضمّن باستخدام أوامر \usepackage{graphicx} - \includegraphics{diagram.eps}.
  • لتضمين الصور النقطية القديمة، اختر تنسيق BMP/TIFF المضمّن باستخدام أوامر \usepackage{graphicx} - \includegraphics{image.bmp}.

تضمين الصور النقطية (PNG/JPEG) في LaTeX

لتضمين الصور النقطية القياسية، أضف حزمة graphicx إلى مقدمة مستند LaTeX وحدد اسم ملف الصورة الهدف في \includegraphics{}.

import com.aspose.tex.*;
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;

public class EmbedRasterGraphicsInLaTeX {
    public static void main(String[] args) throws Exception {
        // Create LaTeX document string referencing an image file 'sample.png'
        String latexCode = "\\documentclass{article}\n" +
                           "\\usepackage{graphicx}\n" +
                           "\\begin{document}\n" +
                           "\\section{Graphics Rendering Demo}\n" +
                           "Here is an embedded raster image:\n\n" +
                           "\\begin{figure}[h]\n" +
                           "  \\centering\n" +
                           "  \\includegraphics[width=0.5\\textwidth]{sample.png}\n" +
                           "  \\caption{Sample PNG Image in LaTeX}\n" +
                           "\\end{figure}\n" +
                           "\\end{document}";

        ByteArrayInputStream inputStream = new ByteArrayInputStream(latexCode.getBytes(StandardCharsets.UTF_8));

        // Initialize conversion options for ObjectLaTeX
        TeXOptions options = TeXOptions.consoleAppOptions(TeXConfig.objectLaTeX());

        // Set input working directory where 'sample.png' is located
        options.setInputWorkingDirectory(new InputFileSystemDirectory("C:/TeXProject/Images/"));

        // Set output working directory for compiled PDF
        options.setOutputWorkingDirectory(new OutputFileSystemDirectory("C:/TeXProject/Output/"));

        // Execute TeX job
        TeXJob job = new TeXJob(inputStream, new PdfDevice(), options);
        job.run();

        System.out.println("LaTeX document with embedded PNG successfully compiled to PDF.");
    }
}

تضمين الرسومات المتجهية (EPS) في LaTeX

يعالج Aspose.TeX رسومات Encapsulated PostScript (.eps) بشكل أصلي عند التجميع باستخدام محرك تنسيق ObjectLaTeX.

import com.aspose.tex.*;
import java.io.IOException;

public class EmbedEPSGraphicsInLaTeX {
    public static void main(String[] args) throws IOException {
        // Initialize options for LaTeX format engine
        TeXOptions options = TeXOptions.consoleAppOptions(TeXConfig.objectLaTeX());

        // Configure directory paths containing .tex source and .eps graphics
        options.setInputWorkingDirectory(new InputFileSystemDirectory("C:/TeXProject/SourceWithEPS/"));
        options.setOutputWorkingDirectory(new OutputFileSystemDirectory("C:/TeXProject/Output/"));

        // Run job for 'document_with_eps.tex' containing \includegraphics{vector_chart.eps}
        TeXJob job = new TeXJob("document_with_eps", new PdfDevice(), options);
        job.run();

        System.out.println("LaTeX document with vector EPS figure compiled successfully.");
    }
}

تحميل أصول الرسومات من أرشيفات ZIP

اجمع ملفات مصدر TeX وحزم الماكرو والصور الديناميكية معًا في أرشيف .zip، ووجّه Aspose.TeX لحل تبعيات الرسومات مباشرةً من الذاكرة أو من وحدة تخزين مضغوطة.

import com.aspose.tex.*;
import java.io.FileInputStream;

public class EmbedGraphicsFromZip {
    public static void main(String[] args) throws Exception {
        // Initialize LaTeX engine options
        TeXOptions options = TeXOptions.consoleAppOptions(TeXConfig.objectLaTeX());

        // Open input ZIP archive containing 'main.tex' and 'figure.png'
        FileInputStream zipStream = new FileInputStream("C:/TeXProject/ProjectWithAssets.zip");
        options.setInputWorkingDirectory(new InputZipDirectory(zipStream, "src"));

        // Define output destination
        options.setOutputWorkingDirectory(new OutputFileSystemDirectory("C:/TeXProject/Output/"));

        // Run compilation job
        TeXJob job = new TeXJob("main", new PdfDevice(), options);
        job.run();

        zipStream.close();
        System.out.println("Compiled LaTeX document with images embedded directly from ZIP file.");
    }
}

التثبيت والإعداد

ادمج Aspose.TeX for Java في مشروعك باستخدام Maven:

Package Manager Console Command

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
</repository>

<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-tex</artifactId>
    <version>Latest</version>
</dependency>

{{i18n.feature5.title}}

  • {{i18n.feature5.item1}}
  • {{i18n.feature5.item2}}
  • {{i18n.feature5.item3}}

الأسئلة الشائعة

1. ماذا يحدث إذا لم أحدد اسم مهمة في TeXOptions؟

إذا لم يتم تكوين اسم مهمة بشكل صريح عبر setJobName()، فسيستخدم Aspose.TeX اسم ملف الإدخال الرئيسي افتراضيًا (على سبيل المثال، ينتج عن main.tex الملفات main.pdf وmain.log).

2. كيف يمكنني تعطيل إنشاء ملف التجميع .log بالكامل؟

عيّن options.setLogOutput(false) قبل إنشاء وتشغيل TeXJob. ويمنع ذلك المحرك من إنشاء ملفات السجل المساعدة على القرص أو في دليل عمل الإخراج.

3. هل يمكنني التقاط رسائل طرفية TeX في بيئات الخادم/بدون واجهة رسومية؟

نعم. يتيح تعيين OutputMemoryTerminal مخصص أو تنفيذ IOutputTerminal توجيه جميع رسائل stdout/stderr الطرفية إلى نظام تسجيل المؤسسات الخاص بالخادم (مثل Log4j أو SLF4J).