-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Read Zugferd documents and display side by side
- Loading branch information
harald
committed
Sep 9, 2024
1 parent
4e9be8e
commit df97b99
Showing
9 changed files
with
488 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using Pacioli.Pdf.Invoice; | ||
using Pacioli.Pdf.zugferd; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text; | ||
|
||
namespace Pacioli.Pdf.ERechnung | ||
{ | ||
public class Reader | ||
{ | ||
public static InvoiceWriter Open(string fileName, string attachmentLocation, out bool isZugferd) | ||
{ | ||
ZugferdReader zr = new ZugferdReader(fileName); | ||
Stream? data; | ||
|
||
isZugferd = zr.Read(out data); | ||
if (isZugferd) | ||
{ | ||
var writer = new InvoiceWriter(data!, attachmentLocation); | ||
data!.Close(); | ||
return writer; | ||
} | ||
else | ||
{ | ||
return new InvoiceWriter(fileName, attachmentLocation); | ||
} | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
using iText.Kernel.Pdf; | ||
using Org.BouncyCastle.Crypto.Generators; | ||
using s2industries.ZUGFeRD; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text; | ||
|
||
namespace Pacioli.Pdf.zugferd | ||
{ | ||
public class ZugferdReader | ||
{ | ||
string _fileName; | ||
|
||
public ZugferdReader(string fileName) | ||
{ | ||
_fileName = fileName; | ||
} | ||
|
||
/* | ||
* This function uses an heuristic to find out if a PDF file has an attached ERechnung file. | ||
*/ | ||
public bool Read(out Stream? xmlStream) | ||
{ | ||
xmlStream = null; | ||
try | ||
{ | ||
PdfReader pdfReader = new PdfReader(_fileName); | ||
PdfDocument pdfDoc = new PdfDocument(pdfReader); | ||
|
||
for (int i = 0; i < pdfDoc.GetNumberOfPdfObjects(); i++) | ||
{ | ||
var obj = pdfDoc.GetPdfObject(i); | ||
if (obj != null && obj.IsStream()) | ||
{ | ||
var s = (PdfStream)obj; | ||
var t = s.KeySet(); | ||
byte[] data; | ||
try | ||
{ | ||
data = ((PdfStream)obj).GetBytes(); | ||
} | ||
catch | ||
{ | ||
data = ((PdfStream)obj).GetBytes(true); | ||
} | ||
MemoryStream mStream = new MemoryStream(); | ||
mStream.Write(data, 0, data.Length); | ||
mStream.Seek(0, SeekOrigin.Begin); | ||
xmlStream = mStream; | ||
|
||
try | ||
{ | ||
/* If this doesn't raise an exception, it is most probably the attachment we are looking for... | ||
* There may be better ways to get access to the attachment. TODO: Find out. | ||
*/ | ||
var desc = InvoiceDescriptor.Load(xmlStream); | ||
xmlStream.Seek(0, SeekOrigin.Begin); | ||
return true; | ||
} | ||
catch | ||
{ | ||
} | ||
} | ||
} | ||
pdfDoc.Close(); | ||
xmlStream = null; | ||
return false; | ||
} | ||
catch | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using Pacioli.Preview.ImageGeneration; | ||
using Pacioli.WindowsApp.NET8.Util; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
using System.Data; | ||
using System.Drawing; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Windows.Forms; | ||
|
||
namespace Pacioli.WindowsApp.NET8 | ||
{ | ||
public partial class DocViewerForm : Form | ||
{ | ||
public Converter Converter { get; set; } | ||
int pageNumber; | ||
Image? loadedImage = null; | ||
|
||
public DocViewerForm(string fileName) | ||
{ | ||
InitializeComponent(); | ||
Converter = new Converter(fileName); | ||
pageNumber = 0; | ||
UpdateImage(); | ||
} | ||
|
||
private void UpdateImage() | ||
{ | ||
if (Converter != null) | ||
{ | ||
using (var imgStream = Converter.ConvertToStream(ref pageNumber)) | ||
{ | ||
loadedImage = Image.FromStream(imgStream); | ||
imgStream.Close(); | ||
} | ||
|
||
F_PictureBox.SizeMode = PictureBoxSizeMode.Zoom; | ||
F_PictureBox.Image = loadedImage; | ||
|
||
F_PageNo.Text = (pageNumber + 1).ToString(); | ||
} | ||
} | ||
|
||
private void F_PageUp_Click(object sender, EventArgs e) | ||
{ | ||
pageNumber++; | ||
UpdateImage(); | ||
} | ||
|
||
private void F_PageDown_Click(object sender, EventArgs e) | ||
{ | ||
pageNumber--; | ||
UpdateImage(); | ||
} | ||
} | ||
} |
Oops, something went wrong.