PDF készítés DevExpressel.
This commit is contained in:
@@ -1,108 +1,35 @@
|
||||
using Aspose.Words;
|
||||
using Aspose.Words.Tables;
|
||||
using Serilog;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using DevExpress.XtraRichEdit;
|
||||
|
||||
namespace WorkFlowCheck.BL.DocumentGenerator
|
||||
{
|
||||
public class DokumentumGenerator
|
||||
{
|
||||
static DokumentumGenerator()
|
||||
public static byte[] DocxToPdf(byte[] wordBytes)
|
||||
{
|
||||
Aspose.Words.License license = new Aspose.Words.License();
|
||||
|
||||
try
|
||||
using (MemoryStream inputStream = new MemoryStream(wordBytes))
|
||||
using (MemoryStream outputStream = new MemoryStream())
|
||||
{
|
||||
license.SetLicense("Aspose.Words.lic");
|
||||
Log.Information("License set successfully.");
|
||||
try
|
||||
{
|
||||
// RichEditDocumentServer használata a konverzióhoz
|
||||
using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer())
|
||||
{
|
||||
// Dokumentum betöltése a byte tömbből
|
||||
wordProcessor.LoadDocument(inputStream, DevExpress.XtraRichEdit.DocumentFormat.OpenXml);
|
||||
|
||||
// PDF formátumba mentés a memóriafolyamba
|
||||
wordProcessor.ExportToPdf(outputStream);
|
||||
|
||||
// PDF byte tömb visszaadása
|
||||
return outputStream.ToArray();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("Hiba a konverzió során: " + ex.Message);
|
||||
return new byte[0];
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error(e, "\nThere was an error setting the license: " + e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] HtmlToDocx(string html)
|
||||
{
|
||||
byte[] generated = Encoding.UTF8.GetBytes(html);
|
||||
var docHTML = new Aspose.Words.Document();
|
||||
|
||||
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(docHTML);
|
||||
builder.InsertHtml(html);
|
||||
|
||||
docHTML.CompatibilityOptions.OptimizeFor(Aspose.Words.Settings.MsWordVersion.Word2016);
|
||||
|
||||
MemoryStream outStream = new MemoryStream();
|
||||
docHTML.Save(outStream, Aspose.Words.SaveFormat.Docx);
|
||||
|
||||
generated = outStream.ToArray();
|
||||
|
||||
outStream.Dispose();
|
||||
|
||||
return generated;
|
||||
}
|
||||
|
||||
public static byte[] HtmlToPdf(string html)
|
||||
{
|
||||
byte[] generated = Encoding.UTF8.GetBytes(html);
|
||||
|
||||
Stream inStream = new MemoryStream(generated);
|
||||
var docHTML = new Aspose.Words.Document(inStream);
|
||||
MemoryStream outStream = new MemoryStream();
|
||||
docHTML.Save(outStream, Aspose.Words.SaveFormat.Pdf);
|
||||
|
||||
generated = outStream.ToArray();
|
||||
|
||||
inStream.Dispose();
|
||||
outStream.Dispose();
|
||||
|
||||
return generated;
|
||||
}
|
||||
|
||||
public static byte[] DocxToPdf(byte[] generated)
|
||||
{
|
||||
Stream inStream = new MemoryStream(generated);
|
||||
var doc = new Aspose.Words.Document(inStream);
|
||||
MemoryStream outStream = new MemoryStream();
|
||||
doc.Save(outStream, Aspose.Words.SaveFormat.Pdf);
|
||||
|
||||
generated = outStream.ToArray();
|
||||
|
||||
inStream.Dispose();
|
||||
outStream.Dispose();
|
||||
|
||||
return generated;
|
||||
}
|
||||
|
||||
public static byte[] DocxToPdfForTopic(byte[] generated)
|
||||
{
|
||||
Stream inStream = new MemoryStream(generated);
|
||||
var doc = new Aspose.Words.Document(inStream);
|
||||
|
||||
doc.Styles.DefaultFont.Name = "Times New Roman";
|
||||
doc.Styles.DefaultFont.Size = 12;
|
||||
|
||||
var collection = doc.GetChildNodes(NodeType.Row, true);
|
||||
|
||||
foreach (Row row in collection)
|
||||
{
|
||||
row.RowFormat.Height = 40;
|
||||
row.RowFormat.HeightRule = HeightRule.AtLeast;
|
||||
}
|
||||
|
||||
MemoryStream outStream = new MemoryStream();
|
||||
doc.Save(outStream, Aspose.Words.SaveFormat.Pdf);
|
||||
|
||||
generated = outStream.ToArray();
|
||||
|
||||
inStream.Dispose();
|
||||
outStream.Dispose();
|
||||
|
||||
return generated;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -426,14 +426,22 @@ namespace WorkFlowCheck.BL.Services
|
||||
|
||||
public byte[] CreatePDF(int checkListHeaderId)
|
||||
{
|
||||
var location = System.Reflection.Assembly.GetEntryAssembly().Location;
|
||||
var directory = System.IO.Path.GetDirectoryName(location);
|
||||
byte[] byteArray = System.IO.File.ReadAllBytes(Path.Combine(directory, "DocumentGenerator", "CheckList_Template_V1.docx"));
|
||||
SablonGenerator gen = new SablonGenerator(byteArray);
|
||||
try
|
||||
{
|
||||
string pdfDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Pdf");
|
||||
|
||||
byte[] content = gen.CloseAndGetDocument();
|
||||
byte[] byteArray = File.ReadAllBytes(Path.Combine(pdfDirectory, "CheckList_Template_V1.docx"));
|
||||
SablonGenerator gen = new SablonGenerator(byteArray);
|
||||
|
||||
return DokumentumGenerator.DocxToPdf(content);
|
||||
byte[] content = gen.CloseAndGetDocument();
|
||||
|
||||
return DokumentumGenerator.DocxToPdf(content);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex.Message);
|
||||
}
|
||||
return new byte[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,8 +7,8 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Aspose.Words" Version="25.3.0" />
|
||||
<PackageReference Include="AutoMapper" Version="13.0.1" />
|
||||
<PackageReference Include="DevExpress.Document.Processor" Version="24.2.6" />
|
||||
<PackageReference Include="DocumentFormat.OpenXml" Version="3.3.0" />
|
||||
<PackageReference Include="Serilog.AspNetCore" Version="8.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
Reference in New Issue
Block a user