update 20260326
This commit is contained in:
@@ -1,4 +1,11 @@
|
||||
using System;
|
||||
using edoka_dms;
|
||||
using Model;
|
||||
using Syncfusion.Pdf;
|
||||
using Syncfusion.Pdf.Graphics;
|
||||
using Syncfusion.Pdf.Graphics.Fonts;
|
||||
using Syncfusion.Pdf.Parsing;
|
||||
using Syncfusion.XPS;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.Data.SqlTypes;
|
||||
@@ -17,14 +24,8 @@ using System.Security.Policy;
|
||||
using System.Security.Principal;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml.Linq;
|
||||
using System.Xml.Serialization;
|
||||
using edoka_dms;
|
||||
using Model;
|
||||
using Syncfusion.Pdf;
|
||||
using Syncfusion.Pdf.Graphics;
|
||||
using Syncfusion.Pdf.Graphics.Fonts;
|
||||
using Syncfusion.Pdf.Parsing;
|
||||
using Syncfusion.XPS;
|
||||
|
||||
|
||||
namespace Versandstrasse
|
||||
@@ -90,11 +91,121 @@ namespace Versandstrasse
|
||||
// inhalt = DateTime.Now + " / " + inhalt;
|
||||
//System.IO.File.AppendAllText(@"h:\edoka_work\vs.log", Environment.NewLine + inhalt);
|
||||
}
|
||||
public string FixPageBreaks(string base64Pdf)
|
||||
{
|
||||
byte[] inputBytes = Convert.FromBase64String(base64Pdf);
|
||||
|
||||
using (MemoryStream inputStream = new MemoryStream(inputBytes))
|
||||
using (MemoryStream outputStream = new MemoryStream())
|
||||
{
|
||||
PdfLoadedDocument loadedDoc = new PdfLoadedDocument(inputStream);
|
||||
|
||||
int lastBreakIndex = 0;
|
||||
PageOrientation? lastOrientation = null;
|
||||
|
||||
int i = 0;
|
||||
while (i < loadedDoc.Pages.Count)
|
||||
{
|
||||
PdfLoadedPage page = loadedDoc.Pages[i] as PdfLoadedPage;
|
||||
|
||||
var size = page.Size;
|
||||
var rotation = page.Rotation;
|
||||
|
||||
bool isLandscape = IsLandscape(size.Width, size.Height, rotation);
|
||||
var currentOrientation = isLandscape ? PageOrientation.Landscape : PageOrientation.Portrait;
|
||||
|
||||
if (lastOrientation == null)
|
||||
{
|
||||
lastOrientation = currentOrientation;
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Wechsel erkannt
|
||||
if (currentOrientation != lastOrientation)
|
||||
{
|
||||
int pagesSinceBreak = i - lastBreakIndex;
|
||||
|
||||
if (pagesSinceBreak % 2 != 0)
|
||||
{
|
||||
// Leere Seite mit gleicher Größe wie vorherige Seite einfügen
|
||||
PdfLoadedPage prevPage = loadedDoc.Pages[i - 1] as PdfLoadedPage;
|
||||
var prevSize = prevPage.Size;
|
||||
|
||||
loadedDoc.Pages.Insert(i, prevSize);
|
||||
|
||||
i++; // überspringe eingefügte Seite
|
||||
}
|
||||
|
||||
lastBreakIndex = i;
|
||||
lastOrientation = currentOrientation;
|
||||
}
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
// 🔥 NEU: Prüfung nach letztem Umbruch
|
||||
int remainingPages = loadedDoc.Pages.Count - lastBreakIndex;
|
||||
|
||||
if (remainingPages % 2 != 0 && loadedDoc.Pages.Count > 0)
|
||||
{
|
||||
PdfLoadedPage lastPage = loadedDoc.Pages[loadedDoc.Pages.Count - 1] as PdfLoadedPage;
|
||||
var lastSize = lastPage.Size;
|
||||
|
||||
loadedDoc.Pages.Add(lastSize);
|
||||
}
|
||||
|
||||
loadedDoc.Save(outputStream);
|
||||
loadedDoc.Close(true);
|
||||
|
||||
return Convert.ToBase64String(outputStream.ToArray());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
private bool IsLandscape(float width, float height, PdfPageRotateAngle rotation)
|
||||
{
|
||||
bool landscape = width > height;
|
||||
|
||||
// Rotation berücksichtigen (90° / 270° drehen Orientierung)
|
||||
if (rotation == PdfPageRotateAngle.RotateAngle90 ||
|
||||
rotation == PdfPageRotateAngle.RotateAngle270)
|
||||
{
|
||||
landscape = !landscape;
|
||||
}
|
||||
|
||||
return landscape;
|
||||
}
|
||||
|
||||
// =========================================================
|
||||
// 📄 A4 erkennen (mit Toleranz)
|
||||
// =========================================================
|
||||
private bool IsA4(PdfLoadedPage page)
|
||||
{
|
||||
const float tolerance = 5f;
|
||||
|
||||
float w = page.Size.Width;
|
||||
float h = page.Size.Height;
|
||||
|
||||
return
|
||||
(Math.Abs(w - 842f) < tolerance && Math.Abs(h - 595f) < tolerance) ||
|
||||
(Math.Abs(w - 595f) < tolerance && Math.Abs(h - 842f) < tolerance);
|
||||
}
|
||||
|
||||
enum PageOrientation
|
||||
{
|
||||
Portrait,
|
||||
Landscape
|
||||
}
|
||||
|
||||
public string check_pdf_pages(string dokument)
|
||||
{
|
||||
return FixPageBreaks(dokument);
|
||||
|
||||
var stream = new MemoryStream(Convert.FromBase64String(dokument));
|
||||
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream);
|
||||
|
||||
if (loadedDocument.Pages.Count % 2 == 0) { } else { Add_EmptyPage(ref loadedDocument); }
|
||||
MemoryStream outputStream = new MemoryStream();
|
||||
string dok = "";
|
||||
@@ -608,6 +719,7 @@ namespace Versandstrasse
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void Add_EmptyPage(ref PdfLoadedDocument document)
|
||||
{
|
||||
|
||||
|
||||
Reference in New Issue
Block a user