Hello,
I have to attach an xml file to a pdf document, generated by Telerik report Designer.
I have a problem at importing and exporting the generated document.
Import problems:
The MediaBox has values about 100 * 333333333, as you can see in the code, i already tried to reset its size.
Other Unknown problem:
Theres is no text displayed in the exported pdf, but the imported document has a page, and has page.Content.count=80, so there was data imported.
I already tried to remove/implement the PDFA-3b Standard for generation and exporting, but it is still the same result.
The generated document was validated and has correct PDFA-3b Standard.
I also tried to set the font to black via editor, because i thought theres maybe a font problem.
This problems only occures with invoices created by ReportDesigner.
A self created word document, exported to pdf works well.
Code for generating the document by ReportDesigner:
public void Export(object param)
{
try
{
Logger.Log("Executing PrintDialogView.Export", LogLevels.Debug, null, 2);
if (param != null)
{
Microsoft.Win32.SaveFileDialog dialog = new Microsoft.Win32.SaveFileDialog();
dialog.FileName = FileName;
dialog.Filter = String.Format("(*.{0})|*.{1}", param.ToString(), param.ToString());
var SavePath = Repository<AppSetting>.GetSingle(Context, u => u.Key == AppSettingTypes.SavePath.ToString());
if (SavePath != null && SavePath.Value != string.Empty)
{
dialog.InitialDirectory = SavePath.Value.ToString();
}
if (dialog.ShowDialog() == true)
{
// DeviceInfo für PDF/A-3 konfigurieren
System.Collections.Hashtable deviceInfo = new System.Collections.Hashtable();
deviceInfo["ComplianceLevel"] = "PDF/A-3B"; // PDFA-3b-Standard
Telerik.Reporting.Processing.ReportProcessor reportProcessor = new Telerik.Reporting.Processing.ReportProcessor();
Telerik.Reporting.Processing.RenderingResult result = reportProcessor.RenderReport("PDF", ReportSource, deviceInfo);
//Telerik.Reporting.Processing.RenderingResult result = RenderExport(param.ToString());
using (System.IO.FileStream fs = new System.IO.FileStream(dialog.FileName, System.IO.FileMode.Create))
{
fs.Write(result.DocumentBytes, 0, result.DocumentBytes.Length);
System.IO.FileInfo fi = new System.IO.FileInfo(dialog.FileName);
Logger.Log(String.Format("File saved to Directory = {0}, Filename = {1}", fi.Directory, fi.FullName), LogLevels.Debug, null, 2);
ReturnExport(fi.Extension, System.IO.Path.GetFileNameWithoutExtension(dialog.FileName), result.DocumentBytes);
}
if (SaveDocumentFile)
{
DocumentNumber = GetAndSetNextDocumentNumber(SelectedDocumentLayout);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Fehler", MessageBox.MessageBoxButtons.Okay, MessageBox.MessageBoxImages.Error);
}
}
Code for Import/Export the generated Document
using iText.IO.Colors;
using iText.Kernel.Pdf;
using iText.Kernel.XMP.Options;
using iText.Kernel.XMP;
using iText.Pdfa;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telerik.Windows.Documents.Fixed.FormatProviders.Pdf;
using Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.Export;
using Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.Model.Encryption;
using Telerik.Windows.Documents.Fixed.Model;
using System.Diagnostics;
using iText.Pdfa.Exceptions;
using Telerik.Windows.Documents.Fixed.Model.Text;
using System.Windows;
using Telerik.Windows.Documents.Fixed.Model.Editing;
using Telerik.Documents.Fixed.Model.Fonts;
using Telerik.Documents.Media;
using Telerik.Windows.Documents.Fixed.Model.ColorSpaces;
using System.Drawing;
namespaceZUGFeRD_ConsoleTest
{
publicclassTest
{
public RadFixedDocument LoadPdf(string @pdfPath)
{
// Read the PDF file as a byte arraybyte[] pdfBytes = System.IO.File.ReadAllBytes(@pdfPath);
// Create a PdfFormatProvider instance
PdfFormatProvider provider = new PdfFormatProvider();
// Import the PDF into a RadFixedDocument
RadFixedDocument document = provider.Import(pdfBytes, new TimeSpan(0, 0, 60));
// RadFixedDocumentEditor editor = new RadFixedDocumentEditor(document);
//RgbColor black = new RgbColor(127, 255, 255, 255);//RgbColor white = new RgbColor(0, 0 , 0, 0);
//editor.CharacterProperties.ForegroundColor = black;//editor.ParagraphProperties.BackgroundColor = white;
if (document.Pages.Count == 0)
{
thrownew Exception("Das geladene PDF-Dokument enthält keine Seiten.");
}
foreach(var page in document.Pages)
{
if(page.Content != null)
{
Console.WriteLine($"Seite mit {page.Content.Count} Objekten geladen.");
foreach (var content in page.Content)
{
Console.WriteLine(content.ToString());
}
}
//page.MediaBox = new System.Windows.Rect(0, 0, 2480, 3508);
//page.CropBox = page.MediaBox;
}
//editor.Dispose();
return document;
}public void ExportDocument(RadFixedDocument document, string targetPath)
{
// Export to PDF including xml file
PdfFormatProvider provider = new PdfFormatProvider();
PdfExportSettings settings = new PdfExportSettings();
if (provider.CanExport)
{
settings.ComplianceLevel = PdfComplianceLevel.PdfA3B;
settings.IsEncrypted = false;
settings.FontEmbeddingType = FontEmbeddingType.None;
provider.ExportSettings = settings;
//System.Collections.Hashtable deviceInfo = new System.Collections.Hashtable();//deviceInfo["ComplianceLevel"] = "PDF/A-3B"; // PDF/A-3-Standardbyte[] exportBytes = provider.Export(document, new TimeSpan(0, 0, 60));
using (FileStream fs = new FileStream(targetPath, FileMode.Create))
{
fs.Write(exportBytes, 0, exportBytes.Length);
}
//using (Stream output = System.IO.File.Create(targetPath))//{// provider.Export(document, output, new TimeSpan(0, 0, 60));// FileInfo fileInfo = new FileInfo(targetPath);// Console.WriteLine($"Exportierte Datei Größe: {fileInfo.Length} Bytes");//}
}
//Telerik.Reporting.Processing.RenderingResult result = reportProcessor.RenderReport("PDF", new ReportSource(), deviceInfo);//Telerik.Reporting.Processing.RenderingResult result = RenderExport(param.ToString());//using (System.IO.FileStream fs = new System.IO.FileStream(targetPath, System.IO.FileMode.Create))//{// fs.Write(result.DocumentBytes, 0, result.DocumentBytes.Length);// System.IO.FileInfo fi = new System.IO.FileInfo(pdfPath);//
}
public void ExportDocumentByiText7(PdfDocument document, string targetPath)
{
// Paths for input and output filesstring outputPdfPath = targetPath;
string iccProfilePath = "sRGB.icc"; // Ensure you have an ICC profile file// Create a PdfWriter for the output fileusing (PdfWriter writer = new PdfWriter(outputPdfPath))
{
// Load the ICC profile
IccProfile iccProfile = IccProfile.GetInstance(System.IO.File.ReadAllBytes(iccProfilePath));
// Create a PdfADocument with PDF/A-3b conformance
PdfADocument pdfaDocument = new PdfADocument(writer, PdfAConformance.PDF_A_3B, new PdfOutputIntent("Custom", "", null, "sRGB IEC61966-2.1", writer));
// Add metadata (required for PDF/A compliance)
pdfaDocument.GetDocumentInfo().SetTitle("Sample PDF/A-3b Document");
pdfaDocument.GetDocumentInfo().SetAuthor("Your Name");
pdfaDocument.GetDocumentInfo().SetSubject("PDF/A-3b Export Example");
// Add XMP metadata
XMPMeta xmpMeta = XMPMetaFactory.Create();
xmpMeta.SetProperty(XMPConst.NS_DC, "title", "Sample PDF/A-3b Document", new PropertyOptions(PropertyOptions.SEPARATE_NODE));
pdfaDocument.SetXmpMetadata(xmpMeta);
// Add content to the document
pdfaDocument.AddNewPage(); // Add an empty page as an example// Close the document to finalize it
pdfaDocument.Close();
}
// Output message
System.Console.WriteLine("PDF/A-3b document created successfully!");
}
}
}