0

I'm trying to use PdfCleanUpTool with iText7.
However my final PDF is corrupted (it is only 15B in size).

When I start my console app from VS I get this in Output:

no configuration section found - suppressing logging output

I'm trying to setup logging to get error message, but without luck.

I've installed this packages:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Common.Logging" version="3.4.1" targetFramework="net47" />
  <package id="Common.Logging.Core" version="3.4.1" targetFramework="net47" />
  <package id="Common.Logging.NLog4412" version="3.4.1" targetFramework="net47" />
  <package id="itext7" version="7.1.2" targetFramework="net47" />
  <package id="itext7.pdfsweep" version="2.0.1" targetFramework="net47" />
  <package id="Microsoft.CSharp" version="4.0.1" targetFramework="net47" />
  <package id="NLog" version="4.4.12" targetFramework="net47" />
  <package id="Portable.BouncyCastle" version="1.8.1.3" targetFramework="net47" />
</packages>

and this is my app.config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <configSections>
    <sectionGroup name="common">
      <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
    </sectionGroup>

    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
  </configSections>

  <common>
    <logging>
      <factoryAdapter type="Common.Logging.NLog.NLogLoggerFactoryAdapter, Common.Logging.NLog4412">
        <arg key="configType" value="INLINE" />
      </factoryAdapter>
    </logging>
  </common>

  <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <targets>
      <target name="console" xsi:type="Console" layout="${date:format=HH\:MM\:ss} ${logger} ${message}" />
    </targets>
    <rules>
      <logger name="*" minlevel="Info" writeTo="console" />
    </rules>
  </nlog>

    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7" />
    </startup>
</configuration>

Probably I need to setup a license key, but I'd like to get error message saying that I must so.

My question is:
How should I correctly setup NLog with Common.Logging to get errors from iText7.

Here is full example that can be used to verify current behavior:

using Common.Logging;
using Common.Logging.Configuration;
using Common.Logging.Simple;
using iText.Kernel.Colors;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.PdfCleanup;
using System;
using System.Collections.Generic;
using System.IO;

namespace RedactTest
{
    class Program
    {
        static void Main(string[] args)
        {
            NameValueCollection properties = new NameValueCollection
            {
                ["showDateTime"] = "true",
                ["level"] = "All"
            };

            LogManager.Adapter = new ConsoleOutLoggerFactoryAdapter(properties);

            using (Stream inputStream = new FileStream("D:\\test.pdf", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                PdfReader reader = new PdfReader(inputStream);
                using (Stream outputStream = new FileStream("D:\\test_redact.pdf", FileMode.Create))
                {
                    PdfWriter writer = new PdfWriter(outputStream);
                    PdfDocument pdfDocument = new PdfDocument(reader, writer);

                    List<PdfCleanUpLocation> cleanUpLocations = new List<PdfCleanUpLocation>
                    {
                        new PdfCleanUpLocation(1, new Rectangle(40f, 650f, 200f, 700f),ColorConstants.GRAY),
                        new PdfCleanUpLocation(1, new Rectangle(40f, 550f, 200f, 590f),ColorConstants.GRAY),
                        new PdfCleanUpLocation(1, new Rectangle(344f, 650f, 550f, 724f),ColorConstants.GRAY)
                    };

                    PdfCleanUpTool cleaner = new PdfCleanUpTool(pdfDocument, cleanUpLocations);
                    cleaner.CleanUp();
                }
            }
            Console.Write("OK");
            Console.ReadLine();
        }
    }
}
Misiu
  • 4,738
  • 21
  • 94
  • 198
  • Have you tried to log informations into a file and not to the console? – LenglBoy Jun 27 '18 at 10:51
  • @LenglBoy yes, without luck – Misiu Jun 27 '18 at 11:03
  • And there your configuration was complete like [here in the tutorial](https://brutaldev.com/post/logging-setup-in-5-minutes-with-nlog) – LenglBoy Jun 27 '18 at 11:12
  • @LenglBoy NLog isn't a problem, I've used it before many times. I can' t configure `Common.Logging` and `NLog` to work together. I need this to get messages from `iText 7` – Misiu Jun 27 '18 at 11:35
  • Can you share the pdf in question? – mkl Jun 28 '18 at 07:48
  • @mkl is is sample I downloaded from the internet: https://slicedinvoices.com/pdf/wordpress-pdf-invoice-plugin-sample.pdf – Misiu Jun 28 '18 at 08:01
  • @Misiu I could reproduce the issue with your code and PDF, and the cause is fairly trivial, cf. the edit to my question, the new section "On the actual issue". – mkl Jun 28 '18 at 11:12
  • @mkl I have no words. after adding that one line now it works fine. Thank You. – Misiu Jun 28 '18 at 11:23

1 Answers1

1

On the original logging related question

One option is to activate the console logger from code, at the start of your program put:

// create properties
NameValueCollection properties = new NameValueCollection();
properties["showDateTime"] = "true";
properties["level"] = "All";

// set Adapter
Common.Logging.LogManager.Adapter = new Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter(properties);

E.g. for the following test

[Test]
public void CreateLogOutput()
{
    // create properties
    NameValueCollection properties = new NameValueCollection();
    properties["showDateTime"] = "true";
    properties["level"] = "All";

    // set Adapter
    Common.Logging.LogManager.Adapter = new Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter(properties);

    ILog logger = LogManager.GetLogger(typeof(iText.Kernel.Pdf.PdfReader));
    logger.Error("Testing an error log output", new Exception("The exception message"));
}

one gets an output like this:

27.06.2018 17:07:37 [ERROR] iText.Kernel.Pdf.PdfReader - Testing an error log output
=======================================================(inner most exception)===
 (1) System.Exception
================================================================================
Method        :  <unavailable>
Type          :  <unavailable>
Assembly      :  <unavailable>
Assembly Path :  <unavailable>
Source        :  
Thread        :  15 'NonParallelWorker'
Helplink      :  

Message:
"The exception message"

Stack Trace:

================================================================================

On the actual issue

After you added the code to the question, the issue became clear: You forgot to close the PdfDocument pdfDocument. Thus, everything works alright, nothing is logged, merely the changes are not written to file as the PDF document object is not closed. Simply close it after the cleaner.CleanUp() call:

PdfCleanUpTool cleaner = new PdfCleanUpTool(pdfDocument, cleanUpLocations);
cleaner.CleanUp();
pdfDocument.Close();

The result now is some 34KB in size and displays like this:

screen shot

mkl
  • 90,588
  • 15
  • 125
  • 265
  • I've tried Your solution, but nothing happened. I've added full example code. Same code worked fine when written in iTextSharp 5.5.13, after rewriting to iText 7.1.2 I get not working 1 KB PDF as output. pdfSweep is addon that (to my knowledge) requires license, it should throw error when license is not set, but nothing happens. – Misiu Jun 28 '18 at 06:30
  • If you run my test `CreateLogOutput` above, do you then get the output I got? – mkl Jun 28 '18 at 07:49
  • Yes, I see output in console, same with nlog config from my question, but somehow I don't see any errors from iText7 – Misiu Jun 28 '18 at 08:16
  • @Misiu *"Yes, I see output in console, same with nlog config from my question, but somehow I don't see any errors from iText7"* - That means that the logging configurations are correct but iText7 simply does not log anything when running your code. – mkl Jun 28 '18 at 10:26