Skip to content

I want to get the embedded files out of the pdf like "extracting" #182

@MilanHoesl

Description

@MilanHoesl

Im trying to read the embedded file out of the pdf (its alway just one file), but i dont understand how the PdfEncoders works.
Can someone help me?

VisualStudio - .NET 8.0
`using PdfSharp.Pdf;
using PdfSharp.Pdf.Internal;
using PdfSharp.Pdf.IO;
using System.Text;
using System.Text.RegularExpressions;

class Program
{
static void Main(string[] args)
{
// Überprüfen, ob eine Datei als Argument übergeben wurde
if (args.Length == 0)
{
Console.WriteLine("Bitte eine PDF-Datei angeben.");
return;
}

    string pdf_path = args[0];

    // Überprüfen, ob die angegebene Datei existiert
    if (!File.Exists(pdf_path))
    {
        Console.WriteLine("Die angegebene Datei existiert nicht.");
        return;
    }

    byte[] pdf_data;

    // Datei öffnen und in byte[] laden
    using (FileStream fs = new FileStream(pdf_path, FileMode.Open, FileAccess.Read))
    {
        pdf_data = new byte[fs.Length];
        fs.Read(pdf_data, 0, (int)fs.Length);
    }

    string pdf_content = Encoding.UTF8.GetString(pdf_data);

    // Überprüfen, ob eingebettete Dateien vorhanden sind
    if (pdf_content.Contains("/Subtype/XML/Length "))
    {
        // Regex zum Extrahieren von eingebetteten Dateien
        Regex regex_stream_content = new Regex(@"(?<=\/Type\/EmbeddedFile[\s\S]*\>\>stream)[\s\S]*?(?=endstream)");
        Match match_stream_content = regex_stream_content.Match(pdf_content);

        Regex regex_stream_info = new Regex(@"(?<=\/Subtype\/XML\/Length\s[0-9]+\>\>stream)[\s\S]*?(?=endstream)");
        Match match_stream_info = regex_stream_info.Match(pdf_content);

        Regex regex_stream_name = new Regex(@"(?<=<fx:DocumentFileName>)[\s\S]*(?=</fx:DocumentFileName>)");
        Match match_stream_name = regex_stream_name.Match(match_stream_info.Value);

        // Sicherstellen, dass alle Regex-Matches erfolgreich sind
        if (!(match_stream_content.Success && match_stream_info.Success && match_stream_name.Success))
        {
            Console.WriteLine("Fehler beim Extrahieren eingebetteter Dateien.");
            return;
        }

        // Erstellen des Ausgabe-Filepath
        string output_file_path = Path.Combine(Path.GetDirectoryName(pdf_path), match_stream_name.Value.Trim());
        string chars;

        using (PdfDocument pdf_document = PdfReader.Open(pdf_path, PdfDocumentOpenMode.ReadOnly))
        {
            chars = PdfEncoders.
        }

        // Datei mit extrahiertem Inhalt speichern
        File.WriteAllText(output_file_path, "");

        Console.WriteLine($"Eingebettete Datei gespeichert unter: {output_file_path}");
    }
    else
    {
        Console.WriteLine("Keine eingebetteten Dateien gefunden.");
    }
}

}
`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions