Hi,
I tried to create a ZUGFeRD PDF and attach the corresponding XML.
I used your code template from docs here: Attach ZUGFeRD
What didn’t work for me was setting the correct MIMEType
. Like in the template, I set the MIMEType
to text/xml
but when I checked the embedded files in the saved pdf, the Xml was added with the MIMEType
application/pdf
.
After a quick search I found this old post: MIMEtype is not set when adding an XML attachment to PDF
It is declared as a bug there but no solution was provided.
Since there was no solution I tried some things and noticed that when you convert the pdf to the PDF A3U format first and then add the embedded files, the correct MIMEType
will be set.
I don’t know if this is still a bug or if the docs are outdated.
@BAV_Institut
You should use the following code.
var doc = new Document(inputFileName);
doc.Pages.Add();
string fileName = GetInputPath("fileName.xml");
var fs = new FileSpecification(fileName)
{
Description = "ZUGFeRD invoice in XML format",
Name = "factur-x.xml"
};
doc.EmbeddedFiles.Add(fs);
doc.Convert(new MemoryStream(), PdfFormat.ZUGFeRD, ConvertErrorAction.Delete);
I will wait for your feedback.
Hi,
thank you for the quick reply.
I tried your suggested solution.
After saving the ZUGFeRD Pdf file with the embedded XML I loaded the pdf again to check if the embedded file has now the correct MIMEType
text/xml
.
When accessing the MIMEType of the embedded file I now get a System.NullReferenceException: "Object reference not set to an instance of an object."
@BAV_Institut
Please attach the files you are using and the full code snippet regarding conversion and access to the MIMEType of the embedded file so that we can reproduce and investigate the issue.
Here are the snippets.
private static void CreateZugFerdPdf()
{
var rootPath = Directory.GetParent(Environment.CurrentDirectory)!.Parent!.Parent!.FullName;
var pdfPath = Path.Combine(rootPath, "pdf.pdf");
var xmlPath = Path.Combine(rootPath, "xml.xml");
var zugferdPath = Path.Combine(rootPath, "zugferd.pdf");
var doc = new Document(pdfPath);
var fs = new FileSpecification(xmlPath)
{
Description = "ZUGFeRD invoice in XML format",
Name = "factur-x.xml",
};
doc.EmbeddedFiles.Add(fs);
doc.Convert(new MemoryStream(), PdfFormat.ZUGFeRD, ConvertErrorAction.Delete);
doc.Save(zugferdPath);
}
private static void ReadEmbeddedXml()
{
var rootPath = Directory.GetParent(Environment.CurrentDirectory)!.Parent!.Parent!.FullName;
var zugFerdPath = Path.Combine(rootPath, "zugferd.pdf");
var zugFerdDocument = new Document(zugFerdPath);
EmbeddedFileCollection embeddedFiles = zugFerdDocument.EmbeddedFiles;
Console.WriteLine("Number of embedded files: {0}",
embeddedFiles.Count);
// It is assumed that there is only one embedded file in the PDF.
var fileSpecification = embeddedFiles.Single();
Console.WriteLine("Name: {0}",
fileSpecification.Name);
Console.WriteLine("Description: {0}",
fileSpecification.Description);
Console.WriteLine("MIMEType : {0}",
fileSpecification.MIMEType);
// Check if parameter object contains the parameters
if (fileSpecification.Params != null)
{
Console.WriteLine("CheckSum: {0}",
fileSpecification.Params.CheckSum);
Console.WriteLine("Creation date: {0}",
fileSpecification.Params.CreationDate);
Console.WriteLine("Modification date: {0}",
fileSpecification.Params.ModDate);
Console.WriteLine("Size: {0}",
fileSpecification.Params.Size);
}
}
pdf.pdf is the pdf file I want to convert to a ZUGFeRD PDF
pdf.pdf (191.4 KB)
xml.xml is the XML I want to add to the embedded files
xml.zip (3.9 KB)
zugferd.pdf is the outcome
zugferd.pdf (223.1 KB)
I also got you my Testproject.
ZUGFeRD_Aspose.zip (176.8 KB)
Reading other ZUGFeRD PDFs which are not created with Aspose.Pdf like BASIC_Einfach.pdf
works perfectly fine.
BASIC_Einfach.pdf (36.1 KB)
@BAV_Institut
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.
Issue ID(s): PDFNET-58374
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.