-2

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(); 
        }
Mohamed Kamal
  • 1,587
  • 2
  • 13
  • 16
  • Mohamed look at these 2 links hopefully this can help http://www.codeproject.com/Tips/466394/Extract-and-Remove-PDF-Attachments-with-Csharp || this stackoverflow post should provide you the exact answer you're looking for [Reading PDF Content with iTextSharp StackoverFlow Answer](http://stackoverflow.com/questions/2550796/reading-pdf-content-with-itextsharp-dll-in-vb-net-or-c-sharp) – MethodMan Feb 03 '13 at 07:31
  • the same because I use this line : document.Add(new Paragraph(text.ToString())); how can I add the text to the document ?? – Mohamed Kamal Feb 03 '13 at 07:48
  • This question seems to be the clarification of your question http://stackoverflow.com/questions/14583340/how-to-read-from-pdf-and-write-in-another-pdf-with-some-extra-text-using-itextsh --- and it looks like you actually want to copy a styled paragraph, maybe merely to a new position, maybe even reflowed. Please clarify. But please also be aware that PDF does not have a concept (at least not enforced) of paragraphs, and so reflow is difficult, to say the least. – mkl Feb 03 '13 at 14:28
  • Extension of a question that was already duplicate; reluctance to read the documentation (see answer); no effort is done to understand the nature of the problem that is discussed. Please close the question. – Bruno Lowagie Feb 04 '13 at 07:25

1 Answers1

2

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.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165