how to keep the same format on pdf after write in another one [closed]

不想你离开。 提交于 2019-12-12 06:50:02

问题


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 ?

my code is :

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

回答1:


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.

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



来源:https://stackoverflow.com/questions/14670336/how-to-keep-the-same-format-on-pdf-after-write-in-another-one

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!