调用PDFStamper.set_Outlines导致类型错误

时间:2019-01-14 13:37:40

标签: c# powershell itext

我正在尝试将多个PDF合并为一个PDF,同时还将书签添加到生成的PDF中,这将使您跳到现在已合并到单个文档中的每个PDF的开头。合并效果很好,但是书签一直是个问题。

我一生都无法使代码格式正常工作。抱歉:(

$output = [System.IO.Path]::Combine($workingDirectory, 'output.pdf');
$fileStream = New-Object System.IO.FileStream($output,     
[System.IO.FileMode]::OpenOrCreate);
$document = New-Object iTextSharp.text.Document;
$pdfCopy = New-Object iTextSharp.text.pdf.PdfCopy($document, $fileStream);
$document.Open();

## Variables needed to create Outline.
#
# $currentPagesCount - keeps track of current number of pages so we can 
# jump to the correct page in out Outline
#
# $outlinesAL - contains hashmaps what will eventually be written to the 
# output.pdf
###
$currentPagesCount = 0;

## This is where the IList<> should be created, i've been using ArrayList 
## because I am wanting to call .add()
$outlinesAL = New-Object System.Collections.ArrayList;
foreach ($item in $sectionsArray) {
  if ($item.split("]")[0] -match "\[n") {
     $tempPath = 
         [io.path]::combine($pdfDirectory, $item.split("]")[-1] +".pdf");
    if(Test-Path($tempPath)){
      Write-Color $tempPath -Color Green
      $reader = New-Object iTextSharp.text.pdf.PdfReader($tempPath);
      $numPages = $reader.NumberOfPages;
      $pdfCopy.AddDocument($reader);

## Here is where the dictionary object is added, must be this... the
## iTextSharp library expects an IList<Dictionary<String, Object>>
      $dict = New-Object 
                  'System.Collections.Generic.Dictionary[string,Object]';
      $dict.Add("Title", $tempPath.FullName);
      $dict.Add("Action", $GoTo);
      $dict.Add("Page", $pageString);
      $outlinesAL.Add($dict);
      $currentPagesCount += $numPages;
      $reader.Close();
    } else {
      Write-Color $tempPath -Color Red
    }
  }
}

## Finished with merge, we can close this stuff.
## Page numbers are effed, need to see about renumbering the pages in the 
.pdfs
$pdfCopy.Close();
$document.Close();
$fileStream.Close();

$finalOutput = [System.IO.Path]::Combine($workingDirectory, 
'FinalDocument.pdf');
$finalReader = New-Object iTextSharp.text.pdf.PdfReader($output);
$finalStream = New-Object System.IO.FileStream($finalOutput, 
[System.IO.FileMode]::OpenOrCreate);
## Add the bookmarks hashtable to the pdf somehow.
$pdfStamper = New-Object iTextSharp.text.pdf.PdfStamper($finalReader, 
$finalStream); 
$outlinesAL.gettype();
$pdfStamper.set_Outlines($outlinesAL);
$pdfStamper.Close();
$finalStream.Close();
$finalReader.Close();
  

无法将参数“值”转换为值:   “ System.Collections.Generic.Dictionary2 [System.String,System.Object]   ...”,以便键入“ set_Outlines”   “ System.Collections.Generic.IList 1 [System.Collections.G   eneric.Dictionary 2 [System.String,System.Object]]“:”无法转换   类型的“ System.Collections.ArrayList”值   键入“ System.Collections.ArrayList”   “ System.Collections.Generic.IList 1 [System.Collections.Generic.Dictionary   2 [System.String,System.Object]]“。在〜merge.ps1:150处char:1   + $ pdfStamper.set_Outlines($ outlinesAL);   + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~       + CategoryInfo:未指定:(:) [],MethodException       + FullyQualifiedErrorId:MethodArgumentConversionInvalidCastArgument

看起来很简单,类型进入PDFStamper的set_Outlines方法,问题在于它需要一个通用的IList,当我尝试使用该类型时,我无法再添加项目。

这是iTextSharp.dll的反映:

// iTextSharp.text.pdf.PdfStamper 
using System.Collections.Generic;

/// Sets the bookmarks. The list structure is defined in
/// {@link SimpleBookmark}.
/// @param outlines the bookmarks or <CODE>null</CODE> to remove any
public virtual IList<Dictionary<string, object>> Outlines
{
    set
    {
        stamper.Outlines = value;
    }
}

1 个答案:

答案 0 :(得分:0)

New-Object 'System.Collections.Generic.List[System.Collections.Generic.Dictionary[System.String,System.Object]]'

New-Object 'System.Collections.Generic.Dictionary[System.String,System.Object]'

只需显式设置类型。离我远点。见上文。