-1

私のコード Pdf からテキストを読み取り、いくつかの変更を加えて別の Pdf に書き込みますが、2 番目の Pdf では形式が同じではないため、同じ形式とスタイルを維持するにはどうすればよいでしょうか?

私のコードは:

string newFile = @"D:\Result.pdf";
            string file = @"D:\Source.pdf";

            string imagepath = @"D:\logo.jpg";
            Console.WriteLine("Welcome");

            string content="";
            // open the reader
            PdfReader reader = new PdfReader(file);
            iTextSharp.text.Rectangle size = reader.GetPageSizeWithRotation(1);
            Document document = new Document(size);

            FileStream fs = new FileStream(newFile, FileMode.Create, FileAccess.Write);
            PdfWriter writer = PdfWriter.GetInstance(document, fs);

            int n = reader.NumberOfPages;

            bool addimage = false;
            if (!File.Exists(file))
            {
                file = Path.GetFullPath(file);
                if (!File.Exists(file))
                {
                    Console.WriteLine("Please give in the path to the PDF file.");
                }
            }
            document.Open();
            for (int i = 1; i < n; i++)
            {
                while (addimage == false)
                {
                    iTextSharp.text.Image pic = iTextSharp.text.Image.GetInstance(imagepath);
                    pic.ScaleToFit(100f, 100f);
                    //pic.ScalePercent(24f);
                    pic.SetAbsolutePosition(document.PageSize.Width - 100f - 100f, document.PageSize.Height + 100f - 225f);
                    document.Add(pic);
                    addimage = true;
                }
                content=PdfTextExtractor.GetTextFromPage(reader, i);
                document.Add(new Paragraph(content));
                PdfContentByte cb = writer.DirectContent;
                cb.MoveTo(document.PageSize.Width / 2, document.PageSize.Height / 2);
                cb.LineTo(document.PageSize.Width / 2, document.PageSize.Height);
                cb.Stroke();
            }
            document.Close(); 
        }
4

1 に答える 1

2

私の本の第 6 章をダウンロードして、表 6.1 を見てください。間違ったクラスを使用してコンテンツをコピーしていることに気付くでしょう。PdfStamper代わりに、またはPdfCopy既存の PDF にカバーとして余分なページを追加することだけが必要な場合は、使用する必要があります (これがコードの解釈方法です)。ただし、コンテンツを編集したいという意味で PDF を編集することが意図されている場合は、reflow第 6 章のイントロをお読みください: PDF は編集用に設計された形式ではありません。

本書の例は Java で書かれていることに注意してください。これらの例の C# バージョンをSourceForgeで公開しています。

于 2013-02-03T10:01:52.027 に答える