This tutorial will help you to extract ZUGFeRD Invoice from PDF in ASP.NET, C#, VB.NET and VBScript using ByteScout PDF Extractor SDK.
Select your programming language:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Bytescout.PDFExtractor;
namespace ExtractInfo
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// This test file will be copied to the project directory on the pre-build event (see the project properties).
String inputFile = Server.MapPath("Beispielrechnung_ZUGFeRD_RC_COMFORT_neu.pdf");
// Create Bytescout.PDFExtractor.AttachmentExtractor instance
AttachmentExtractor extractor = new AttachmentExtractor();
extractor.RegistrationName = "demo";
extractor.RegistrationKey = "demo";
// Load sample PDF document
extractor.LoadDocumentFromFile(inputFile);
Response.Clear();
Response.ContentType = "text/html";
Response.Write("<html><body>\r\n");
// output the attached XML file
for (int i = 0; i < extractor.Count; i++)
{
Response.Write("Saving XML invoice attachment: " + extractor.GetFileName(i) + "<br>");
MemoryStream mem = new MemoryStream();
Response.Write("File size: " + extractor.GetSize(i).ToString() + "<br>");
// optionally save XML invoice to a file
// extractor.Save (i, extractor.GetFileName(i)); // you can save into temp folder or save to Stream object to avoid temp files
extractor.SaveToStream(i, mem);
mem.Position = 0; // reset the stream position
StreamReader sreader = new StreamReader(mem);
string XMLInvoice = sreader.ReadToEnd(); // xml invoice content
Response.Write("<textarea rows='20' cols='80'><pre>"+ HttpUtility.HtmlEncode(XMLInvoice) + "</pre></textarea>");
}
Response.Write("\r\n</body></html>");
Response.End();
}
}
}
using System;
using Bytescout.PDFExtractor;
namespace ExtractInfo
{
class Program
{
static void Main(string[] args)
{
// Create Bytescout.PDFExtractor.AttachmentExtractor instance
AttachmentExtractor extractor = new AttachmentExtractor();
extractor.RegistrationName = "demo";
extractor.RegistrationKey = "demo";
// Load sample PDF document
extractor.LoadDocumentFromFile("Beispielrechnung_ZUGFeRD_RC_COMFORT_neu.pdf");
// extracting XML invoice which is stored as an attachment
for (int i = 0; i < extractor.Count; i++)
{
Console.WriteLine("Saving XML invoice attachment:\t" + extractor.GetFileName(i));
// save file into the current folder
extractor.Save(i, extractor.GetFileName(i));
Console.WriteLine("Done.");
}
Console.WriteLine();
Console.WriteLine("Press any key to open the XML invoice extracted...");
Console.ReadLine();
// Open the invoice in default XML viewer
System.Diagnostics.Process.Start("ZUGFeRD-invoice.xml");
}
}
}
Imports Bytescout.PDFExtractor
Class Program
Friend Shared Sub Main(args As String())
' Create Bytescout.PDFExtractor.AttachmentExtractor instance
Dim extractor As New AttachmentExtractor()
extractor.RegistrationName = "demo"
extractor.RegistrationKey = "demo"
' Load sample PDF document
extractor.LoadDocumentFromFile("Beispielrechnung_ZUGFeRD_RC_COMFORT_neu.pdf")
Dim i As Integer
' extract the XML invoice which is stored as an attachment
For i = 0 To extractor.Count - 1
Console.WriteLine("Saving XML attachment: " + extractor.GetFileName(i))
extractor.Save(i, extractor.GetFileName(i)) ' save into the current folder
Console.WriteLine("Done")
Next
Console.WriteLine("Press any key to continue and open XML...")
Console.ReadLine()
' Open the invoice in default XML viewer
System.Diagnostics.Process.Start("ZUGFeRD-invoice.xml")
End Sub
End Class
' Create Bytescout.PDFExtractor.AttachmentExtractor object
Set AttachmentExtractor = CreateObject("Bytescout.PDFExtractor.AttachmentExtractor")
AttachmentExtractor.RegistrationName = "demo"
AttachmentExtractor.RegistrationKey = "demo"
' Load sample PDF document with embedded attachments
AttachmentExtractor.LoadDocumentFromFile("..\..\Beispielrechnung_ZUGFeRD_RC_COMFORT_neu.pdf")
' walk through attachments and save them
For I = 0 To AttachmentExtractor.Count-1
AttachmentExtractor.Save i, AttachmentExtractor.GetFileName (I) ' save in the current folder with original filename
Next
MsgBox "Done! Click OK to open the XML invoice"
Set extractor = Nothing
' Open ZUGFeRD invoice XML
Set shell = CreateObject("WScript.Shell")
shell.Run "ZUGFeRD-invoice.xml", 1, false
Set shell = Nothing