0

我正在尝试PdfCleanUpTool与 iText7 一起使用。
但是我的最终 PDF 已损坏(它只有 15B 大小)。

当我从 VS启动我的控制台应用程序时,我在输出中得到这个:

未找到配置部分 - 禁止记录输出

我正在尝试设置日志记录以获取错误消息,但没有运气。

我已经安装了这个包:

<?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>

这是我的app.config文件:

<?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>

可能我需要设置许可证密钥,但我想收到错误消息说我必须这样做。

我的问题是:我应该如何使用Common.Logging
正确设置NLog以从iText7获取错误。

这是可用于验证当前行为的完整示例:

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();
        }
    }
}
4

1 回答 1

1

关于原始日志记录相关问题

一种选择是在程序开始时从代码中激活控制台记录器:

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

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

例如对于以下测试

[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"));
}

一个得到这样的输出:

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:

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

关于实际问题

将代码添加到问题后,问题变得清晰:您忘记关闭PdfDocument pdfDocument. 因此,一切正常,没有任何记录,只是更改不会写入文件,因为 PDF 文档对象没有关闭。通话后只需将其关闭cleaner.CleanUp()

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

现在的结果是 34KB 大小,显示如下:

截屏

于 2018-06-27T15:17:53.053 回答