我正在使用iTextSharp。我正在处理一个PDF,它有我称之为很多的"层"。
因此,当我打开PDF时,我可以制作我的"层"通过单击图层名称左侧的小眼睛可见或不可见。
我知道如何激活/停用图层,使用以下代码:
FileStream outStream = null;
PdfReader pdfReader = null;
PdfStamper stamper = null;
Exception exceptionOccured = null;
try
{
outStream = new FileStream(destFile, FileMode.Create);
pdfReader = new PdfReader(sourceFile);
stamper = new PdfStamper(pdfReader, outStream);
Dictionary<String, PdfLayer> layers = null;
try
{
layers = stamper.GetPdfLayers();
}
catch (Exception)
{
}
if (layers != null)
{
layersToRemove = addSpecialLayerChars(layersToRemove);
foreach (KeyValuePair<String, PdfLayer> layer in layers)
{
//Encodage UTF-16 BE 'þÿ' retirer les \0
String layerName = layer.Key;
String layerNameComp = layerName.Replace("\0", "");
layerNameComp = layerNameComp.Replace("þÿ", "");
Boolean remove = false;
//Si(!layerName.equals("textes")) Alors remove = true; // On efface toute les couches qui ne contiennent pas de texte
foreach (String layerToRemove in layersToRemove)
{
if (layerNameComp.ToLowerInvariant() == layerToRemove.ToLowerInvariant())
{
remove = true;
}
else if (layerToRemove[layerToRemove.Length - 1] == '*')
{
if (layerNameComp.ToLowerInvariant().StartsWith(layerToRemove.Substring(0, layerToRemove.Length - 1).ToLowerInvariant()))
{
remove = true;
}
}
}
if (remove)
{
if (writeToConsole)
Console.WriteLine("Removing layer: " + layerName);
try
{
layers[layerName].On = false;
}
catch (Exception ex)
{
if (writeToConsole)
Console.WriteLine("Exception: " + ex.Message);
}
}
}
}
}
catch (Exception ex)
{
exceptionOccured = ex;
}
finally
{
try
{
if (stamper != null)
stamper.Close();
if (pdfReader != null)
pdfReader.Close();
if (outStream != null)
outStream.Close();
if (exceptionOccured != null)
throw exceptionOccured;
}
catch (Exception ex)
{
if (exceptionOccured != null)
throw exceptionOccured;
}
}
但是当我想将我的PDF(带有停用的图层)转换为PNG时,我的新图像包含所有图层,甚至包括我停用的图层。
这就是我想完全删除图层的原因。