如何在写入另一个格式后保持相同的格式 [英] how to keep the same format on pdf after write in another one

查看:166
本文介绍了如何在写入另一个格式后保持相同的格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码从Pdf中读取文本并将其写入另一个Pdf并进行一些修改,但第二个Pdf中的格式不一样,所以如何保持相同的格式和样式?

My code Read the text from Pdf and write it in another Pdf with some modifications ,, but the format is not the same in the second Pdf ,,, so how can I keep the same format and style ?

我的代码是:

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


推荐答案

请下载我的书第6章并查看表6.1。你会发现你使用了错误的类来复制内容。您应该使用 PdfStamper 而不是 PdfCopy 如果你想要的是添加一个额外的页面作为现有的封面PDF(这是我解释你的代码的方式)。但是,如果你打算在内容 reflow 的意义上编辑PDF,请阅读第6章的介绍:PDF格式不是是为编辑而设计的。

Please download chapter 6 of my book and take a look at table 6.1. You'll discover that you're using the wrong class to copy content. You should use PdfStamper instead or maybe PdfCopy if all you want is to add an extra page as a cover to the existing PDF (which is how I interpret your code). If however, it's your intention is to edit a PDF in the sense that you want the content to reflow, please read the intro of chapter 6: PDF isn't a format that is designed for editing.

请注意,本书中的示例是用Java编写的。我们在上发布了这些示例的C#版本SourceForge

Note that the examples in the book are written in Java. We have published the C# version of these examples on SourceForge.

这篇关于如何在写入另一个格式后保持相同的格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆