Skip to content

Commit

Permalink
Read Zugferd documents and display side by side
Browse files Browse the repository at this point in the history
  • Loading branch information
harald committed Sep 9, 2024
1 parent 4e9be8e commit df97b99
Show file tree
Hide file tree
Showing 9 changed files with 488 additions and 33 deletions.
31 changes: 31 additions & 0 deletions src/Pacioli.Pdf/ERechnung/Reader.cs
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);
}

}
}
}
9 changes: 8 additions & 1 deletion src/Pacioli.Pdf/Invoice/Writer.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@

using Pacioli.Pdf.ERechnung;

namespace Pacioli.Pdf.Invoice
{
public class Writer
{
public bool IsZugferd { get { return _isZugferd; } }

private bool _isZugferd = false;


public static void Write(string inputFile, string outputFile, string attachmentsTargetPath)
{
new InvoiceWriter(inputFile, attachmentsTargetPath).Write(outputFile);
Expand All @@ -12,7 +19,7 @@ public static void Write(string inputFile, string outputFile, string attachments

public Writer(string inputFile, string attachmentsTargetPath)
{
writer = new InvoiceWriter(inputFile, attachmentsTargetPath);
writer = Reader.Open(inputFile, attachmentsTargetPath, out _isZugferd);
}

public int GetAttachmentCount()
Expand Down
78 changes: 78 additions & 0 deletions src/Pacioli.Pdf/zugferd/ZugferdReader.cs
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;
}
}

}
}

35 changes: 31 additions & 4 deletions src/Pacioli.Preview/ImageGeneration/Converter.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using SkiaSharp;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;

namespace Pacioli.Preview.ImageGeneration
Expand All @@ -12,20 +14,25 @@ public class Converter
public int PageCount { get; set; } = 0;
public SizeF PageSize { get; set; }

IEnumerable<SKBitmap> Bitmaps { get; set; }

//Dictionary<int,>


public Converter(string pdfFile)
{
PdfData = File.ReadAllBytes(pdfFile);
PageSize = PDFtoImage.Conversion.GetPageSize(PdfData, 0);
PageCount = PDFtoImage.Conversion.GetPageCount(PdfData);
Bitmaps = PDFtoImage.Conversion.ToImages(PdfData!);
}

public void Convert(string pdfFile, string imgFile)
public void Convert(string imgFile)
{
PDFtoImage.Conversion.SavePng(imgFile, PdfData!, page: 0);
}

public int Convert(string pdfFile, string imgFile, int pageNo)
public int Convert(string imgFile, int pageNo)
{
if (pageNo < 0)
{
Expand All @@ -35,8 +42,28 @@ public int Convert(string pdfFile, string imgFile, int pageNo)
{
pageNo = PageCount - 1;
}
PDFtoImage.Conversion.SavePng(imgFile, PdfData!, page: pageNo);
//PDFtoImage.Conversion.SavePng(imgFile, PdfData!, page: pageNo);
SKData skData = Bitmaps.ElementAt(pageNo).Encode(SKEncodedImageFormat.Png, 100);
using (FileStream outStream = new FileStream(imgFile, FileMode.OpenOrCreate, FileAccess.Write))
{
skData.AsStream().CopyTo(outStream);
}
return pageNo;
}

public Stream ConvertToStream(ref int pageNo)
{
if (pageNo < 0)
{
pageNo = 0;
}
if (pageNo > PageCount - 1)
{
pageNo = PageCount - 1;
}
//PDFtoImage.Conversion.SavePng(imgFile, PdfData!, page: pageNo);
SKData skData = Bitmaps.ElementAt(pageNo).Encode(SKEncodedImageFormat.Png, 100);
return skData.AsStream();
}
}
}
133 changes: 133 additions & 0 deletions src/Pacioli.WindowsApp.NET8/DocViewerForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 58 additions & 0 deletions src/Pacioli.WindowsApp.NET8/DocViewerForm.cs
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();
}
}
}
Loading

0 comments on commit df97b99

Please sign in to comment.