Files
DXApp/DXApp.TemplateKitProject/Services/ZugferdExtractorService.cs
OlgunR 42d4222fb3 Handle UTF-8 BOM in string conversion
Previously, the method directly converted a byte array to a string
without accounting for a potential UTF-8 Byte Order Mark (BOM).
This commit introduces logic to remove the BOM (if present)
using `TrimStart('\uFEFF')` after converting the byte array
to a string.

Additionally, a comment was added to clarify the purpose of
this change.
2026-05-26 11:24:54 +02:00

37 lines
1.1 KiB
C#

namespace DXApp.TemplateKitProject.Services
{
using DevExpress.Pdf;
using System.Text;
public class ZugferdExtractorService
{
private static readonly string[] KnownFileNames =
[
"zugferd-invoice.xml",
"factur-x.xml",
"xrechnung.xml"
];
public string? ExtractXml(Stream pdfStream)
{
using var processor = new PdfDocumentProcessor();
processor.LoadDocument(pdfStream);
foreach (var attachment in processor.Document.FileAttachments)
{
bool isZugferd = KnownFileNames.Any(name =>
attachment.FileName.Equals(name, StringComparison.OrdinalIgnoreCase));
if (isZugferd || attachment.MimeType == "text/xml")
{
byte[] data = attachment.Data;
// BOM entfernen falls vorhanden (EF BB BF am Anfang)
var text = Encoding.UTF8.GetString(data);
return text.TrimStart('\uFEFF');
}
}
return null; // Kein ZUGFeRD-Anhang gefunden
}
}
}