Skip to content

Commit

Permalink
change itextpdf to gembox.pdf
Browse files Browse the repository at this point in the history
  • Loading branch information
c3rebro committed Jan 24, 2023
1 parent 3433c90 commit 6dd27bf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 59 deletions.
81 changes: 31 additions & 50 deletions RFiDGear/DataAccessLayer/Remote/FromFile/ReportReaderWriter.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using iText.Forms;
using iText.Forms.Fields;
using iText.Kernel.Pdf;
using iText.Kernel.Colors;
using GemBox.Pdf;

using Log4CSharp;

Expand Down Expand Up @@ -35,6 +32,9 @@ public ReportReaderWriter()
{
try
{
// Set license key to use GemBox.Pdf in Free mode.
ComponentInfo.SetLicense("FREE-LIMITED-KEY");

appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

appDataPath = System.IO.Path.Combine(appDataPath, "RFiDGear");
Expand Down Expand Up @@ -62,22 +62,17 @@ public ObservableCollection<string> GetReportFields()
{
var temp = new ObservableCollection<string>();

using (var pdfDoc = new PdfDocument(new PdfReader(ReportTemplatePath)))
using (var pdfDoc = PdfDocument.Load(ReportTemplatePath))
{
var form = PdfAcroForm.GetAcroForm(pdfDoc, true);
// Being set as true, this parameter is responsible to generate an appearance Stream
// while flattening for all form fields that don't have one. Generating appearances will
// slow down form flattening, but otherwise Acrobat might render the pdf on its own rules.
form.SetGenerateAppearance(true);
//form.SetNeedAppearances(true);
var form = pdfDoc.Form;

try
{
if (form != null)
{
foreach (var _form in form.GetFormFields())
foreach (var _form in form.Fields)
{
temp.Add(_form.Key);
temp.Add(_form.Name);
}
}
}
Expand All @@ -103,38 +98,28 @@ public void SetReportField(string _field, string _value)
{
try
{
using (var pdfDoc = new PdfDocument(new PdfReader(ReportTemplatePath), new PdfWriter(ReportOutputPath)))
using (var pdfDoc = PdfDocument.Load(ReportTemplatePath)) // (new PdfReader(ReportTemplatePath), new PdfWriter(ReportOutputPath)))
{
ReportTemplatePath = System.IO.Path.Combine(appDataPath, reportTemplateTempFileName);

var form = PdfAcroForm.GetAcroForm(pdfDoc, true);
// Being set as true, this parameter is responsible to generate an appearance Stream
// while flattening for all form fields that don't have one. Generating appearances will
// slow down form flattening, but otherwise Acrobat might render the pdf on its own rules.
form.SetGenerateAppearance(true);
//form.SetNeedAppearances(true);

try
{
if (form.GetField(_field) is PdfButtonFormField)
{
_ = (form.GetField(_field) as PdfButtonFormField)?.SetBorderWidth(1);
_ = (form.GetField(_field) as PdfButtonFormField)?.SetBorderColor(ColorConstants.BLACK);
}
ReportTemplatePath = System.IO.Path.Combine(appDataPath, reportTemplateTempFileName);

_ = form.GetField(_field)?.SetVisibility(PdfFormField.VISIBLE);
_ = form.GetField(_field)?.SetReadOnly(false);
_ = form.GetField(_field)?.SetValue(_value);
var form = pdfDoc.Form;

pdfDoc.Form.Fields[_field].Hidden = false;
pdfDoc.Form.Fields[_field].ReadOnly = false;
pdfDoc.Form.Fields[_field].Value = _value;

pdfDoc.Save(ReportOutputPath);
pdfDoc.Close();

File.Copy(ReportOutputPath, System.IO.Path.Combine(appDataPath, reportTemplateTempFileName), true);
}
catch (Exception e)
{
LogWriter.CreateLogEntry(e, FacilityName);
}

pdfDoc.Close();
}

File.Copy(ReportOutputPath, System.IO.Path.Combine(appDataPath, reportTemplateTempFileName), true);
}
catch (XmlException e)
{
Expand All @@ -152,30 +137,26 @@ public void ConcatReportField(string _field, string _value)
{
ReportTemplatePath = System.IO.Path.Combine(appDataPath, reportTemplateTempFileName);

using (var pdfDoc = new PdfDocument(new PdfReader(ReportTemplatePath), new PdfWriter(ReportOutputPath)))
using (var pdfDoc = PdfDocument.Load(ReportTemplatePath))
{
var form = PdfAcroForm.GetAcroForm(pdfDoc, true);
// Being set as true, this parameter is responsible to generate an appearance Stream
// while flattening for all form fields that don't have one. Generating appearances will
// slow down form flattening, but otherwise Acrobat might render the pdf on its own rules.
form.SetGenerateAppearance(true);
//form.SetNeedAppearances(true);

try
{
form.GetField(_field)?.SetVisibility(PdfFormField.VISIBLE);
form.GetField(_field)?.SetValue(string.Format("{0}{1}", form.GetField(_field)?.GetValueAsString(), _value));
var form = pdfDoc.Form;

pdfDoc.Form.Fields[_field].Hidden = false;
pdfDoc.Form.Fields[_field].ReadOnly = false;
pdfDoc.Form.Fields[_field].Value = string.Format("{0}{1}", pdfDoc.Form.Fields[_field]?.Value, _value);

pdfDoc.Save(ReportOutputPath);
pdfDoc.Close();

File.Copy(ReportOutputPath, System.IO.Path.Combine(appDataPath, reportTemplateTempFileName), true);
}
catch (Exception e)
{
LogWriter.CreateLogEntry(e, FacilityName);
}

pdfDoc.Close();
}

File.Copy(ReportOutputPath, System.IO.Path.Combine(appDataPath, reportTemplateTempFileName), true);

}
catch (XmlException e)
{
Expand Down
12 changes: 3 additions & 9 deletions RFiDGear/RFiDGear.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -400,15 +400,12 @@
<PackageReference Include="CommunityToolkit.Mvvm">
<Version>8.0.0</Version>
</PackageReference>
<PackageReference Include="GemBox.Pdf">
<Version>17.0.1258</Version>
</PackageReference>
<PackageReference Include="Ionic.Zip-1.9.1.8">
<Version>1.9.1.8</Version>
</PackageReference>
<PackageReference Include="itext7">
<Version>7.2.4</Version>
</PackageReference>
<PackageReference Include="itext7.commons">
<Version>7.2.4</Version>
</PackageReference>
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces">
<Version>7.0.0</Version>
</PackageReference>
Expand Down Expand Up @@ -436,9 +433,6 @@
<PackageReference Include="Portable.BouncyCastle">
<Version>1.9.0</Version>
</PackageReference>
<PackageReference Include="QuestPDF">
<Version>2022.12.1</Version>
</PackageReference>
<PackageReference Include="System.Buffers">
<Version>4.5.1</Version>
</PackageReference>
Expand Down

0 comments on commit 6dd27bf

Please sign in to comment.