Hi guys,

I would appreciate a little guidance to this problem of mine:

I have a set of single page, pdf files with various sizes. The requirement
is to: 
a) annotate the single page pdfs with some info coming from xml files.
b) merge the annotated files to a new file that retains page sizes and
annotations

I have managed to make each operation to run successfully but I can't
combine them both.

The annotation part is:

            foreach (var doc in importDocuments)
            {
                foreach (var art in doc.Articles)
                {
                    foreach (var bound in art.Bounds)
                    {
                        var pdfFile = Path.Combine(Parameters.TargetDir,
string.Format(Parameters.PdfTemplate, bound.PageNumber));

                        if (File.Exists(pdfFile))
                        {
                            var tempPdfFile =
Path.Combine(Parameters.TargetDir,
Path.ChangeExtension(Path.GetFileName(pdfFile), ".pdf-temp"));

                            using (var reader = new PdfReader(pdfFile))
                            {
                                var pageRect = reader.GetPageSize(1);
                                var pageBounds = doc.Pages.Where(p =>
p.PageNumber == bound.PageNumber).FirstOrDefault();

                                if (pageBounds != null)
                                {
                                    var rect =
doc.CreateRectangle(pageBounds, bound, pageRect);

                                    using (var stamper = new
PdfStamper(reader, new FileStream(tempPdfFile, FileMode.OpenOrCreate)))
                                    {
                                        PdfAnnotation annotation =
PdfAnnotation.CreateLink(stamper.Writer, rect,
PdfAnnotation.HIGHLIGHT_OUTLINE, new PdfAction(doc.AnnotationPath));
                                        stamper.AddAnnotation(annotation,
1);
                                        stamper.Close();
                                    }
                                }
                            }

                            File.Replace(tempPdfFile, pdfFile, null);
                        }
                    }
                }
            }
                        
The merging part is:

            var list = Directory.EnumerateFiles(Parameters.TargetDir,
"*.pdf").ToList();
            var outPdfFile = Path.Combine(Parameters.TargetDir,
string.Format("{0}.pdf", Parameters.ProductName));
            var tempPdfFile = Path.Combine(Parameters.TargetDir,
string.Format("{0}.pdf-temp", Parameters.ProductName));

            using (Stream outputPdfStream = new FileStream(tempPdfFile,
FileMode.Create, FileAccess.Write, FileShare.None))
            {
                PdfReader reader;

                var document = new iTextSharp.text.Document();
                var copy = new PdfSmartCopy(document, outputPdfStream);

                document.Open();

                foreach (var pdfFileName in list)
                {
                    reader = new PdfReader(pdfFileName);
                    var n = reader.NumberOfPages;
                    for (int page = 0; page < n; )
                    {
                        var p = copy.GetImportedPage(reader, ++page);
                        copy.AddPage(p);
                    }
                }

                document.Close();
            }

            File.Move(tempPdfFile, outPdfFile);

            return outPdfFile;
                        
When I try to run the merging after the annotating, I got an exception on
copy.GetImportedPage(). The p.Duplicate has a "Content can not be added to a
PdfImportedPage" exception. 

So, do I need to make the merging somehow differently? Can the annotating
and the merging be done in one step?

I am using the 5.3.5.0 of the library (for .NET).

Thanx
Manos





--
View this message in context: 
http://itext-general.2136553.n4.nabble.com/PdfStamper-PdfSmartCopy-tp4657536.html
Sent from the iText - General mailing list archive at Nabble.com.

------------------------------------------------------------------------------
Free Next-Gen Firewall Hardware Offer
Buy your Sophos next-gen firewall before the end March 2013 
and get the hardware for free! Learn more.
http://p.sf.net/sfu/sophos-d2d-feb
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference 
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: 
http://itextpdf.com/themes/keywords.php

Reply via email to