iTextSharpに興味がありましたので、私はC#を学ぶ必要があります。私はAutoHotkey(Windows用のシンプルで強力なスクリプトプログラミング言語)のビットを知っているので、私にとってはより簡単です。しかし、私はJavaで書かれたコードをよく見かけますが、これは簡単にC#に変換されると言われています。残念ながら、私はそれにいくつかの問題があります。 Bruno Lowagieによって書かれた元のコードを見てみましょう。iTextSharp内のすべての埋め込みファイルを削除する
public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
PdfReader reader = new PdfReader(src);
PdfDictionary root = reader.getCatalog();
PdfDictionary names = root.getAsDict(PdfName.NAMES);
names.remove(PdfName.EMBEDDEDFILES);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
stamper.close();
}
これは私が自分で書くために管理しているものです:
static void removeFiles(string sourceFilePath, string destFilePath)
{
try
{
// read src file
FileStream inputStream = new FileStream(sourceFilePath, FileMode.Open, FileAccess.Read, FileShare.None);
Document source = new Document();
// open for reading
PdfWriter reader = PdfReader.GetInstance(inputStream);
source.Open();
// create dest file
FileStream outputStream = new FileStream(destFilePath, FileMode.Create, FileAccess.ReadWrite, FileShare.None);
Document dest = new Document();
PdfWriter writer = PdfWriter.GetInstance(dest, inputStream); // open stream from src
// remove embedded files from dest
PdfDictionary root = dest.getCatalog().getPdfObject();
PdfDictionary names = root.getAsDictionary(PdfName.Names);
names.remove(PdfName.EmbeddedFiles);
// close all
source.Close();
dest.Close();
}
catch (Exception ex)
{
}
}
残念ながら、次のような多くのエラーがある:
'Document' does not contain a definition for 'getCatalog' and no extension method 'getCatalog'
'PdfReader' does not contain a definition for 'GetInstance'
は、これは何です数え切れないほどの時間をかけてコーディングやグーグルーグをやっています。
Javaでは 'PdfReader'のメソッドの' getCatalog'が 'Document'を使用しようとしています –
なぜ' PdfReader'クラスで 'GetInstance'を呼び出そうとしていますか? – alayor
コードには多くの問題があります。たとえば、 'PdfWriter reader = PdfReader.GetInstance(inputStream);' 'PdfReader'が' GetInstance() 'メソッドを持っていても、なぜ読者をライターにキャストするのでしょうか?また、 'PdfStamper'の代わりに' Document'/'PdfWriter'を使うのは意味がありません。私はC#の開発者ではありませんが、私は答えの明白なエラーのいくつかを修正しました。 –