2010-12-02 3 views
3

Ich benutze abcpdf und ich bin gespannt, ob wir rekursiv AddImageUrl() -Funktion aufrufen können, um PDF-Dokument zu kompilieren, die mehrere URLs kompilieren?wiederholt AddImageUrl (url) zu assemblieren pdf-Dokument

so etwas wie:

 int pageCount = 0; 
    int theId = theDoc.AddImageUrl("http://stackoverflow.com/search?q=abcpdf+footer+page+x+out+of+", true, 0, true); 
    //assemble document 
    while (theDoc.Chainable(theId)) 
    { 
     theDoc.Page = theDoc.AddPage(); 
     theId = theDoc.AddImageToChain(theId); 
    } 
    pageCount = theDoc.PageCount; 
    Console.WriteLine("1 document page count:" + pageCount); 
    //Flatten document 
    for (int i = 1; i <= pageCount; i++) 
    { 
     theDoc.PageNumber = i; 
     theDoc.Flatten(); 
    } 

    //now try again 
    theId = theDoc.AddImageUrl("http://stackoverflow.com/questions/1980890/pdf-report-generation", true, 0, true); 
    //assemble document 
    while (theDoc.Chainable(theId)) 
    { 
     theDoc.Page = theDoc.AddPage(); 
     theId = theDoc.AddImageToChain(theId); 
    } 
    Console.WriteLine("2 document page count:" + theDoc.PageCount); 
    //Flatten document 
    for (int i = pageCount + 1; i <= theDoc.PageCount; i++) 
    { 
     theDoc.PageNumber = i; 
     theDoc.Flatten(); 
    } 
    pageCount = theDoc.PageCount; 

edit: Code, basierend auf 'Jäger' Lösung scheint zu funktionieren:

 static void Main(string[] args) 
    { 
    Test2(); 
    } 

    static void Test2() 
    { 

    Doc theDoc = new Doc(); 
    // Set minimum number of items a page of HTML should contain. 
    theDoc.HtmlOptions.ContentCount = 10;// Otherwise the page will be assumed to be invalid. 
    theDoc.HtmlOptions.RetryCount = 10; // Try to obtain html page 10 times 
    theDoc.HtmlOptions.Timeout = 180000;// The page must be obtained in less then 10 seconds 

    theDoc.Rect.Inset(0, 10); // set up document 
    theDoc.Rect.Position(5, 15); 
    theDoc.Rect.Width = 602; 
    theDoc.Rect.Height = 767; 
    theDoc.HtmlOptions.PageCacheEnabled = false; 

    IList<string> urls = new List<string>(); 
    urls.Add("http://stackoverflow.com/search?q=abcpdf+footer+page+x+out+of+"); 
    urls.Add("http://stackoverflow.com/questions/1980890/pdf-report-generation"); 
    urls.Add("http://yahoo.com"); 
    urls.Add("http://stackoverflow.com/questions/4338364/recursively-call-addimageurlurl-to-assemble-pdf-document"); 

    foreach (string url in urls) 
     AddImage(ref theDoc, url); 

    //Flatten document 
    for (int i = 1; i <= theDoc.PageCount; i++) 
    { 
     theDoc.PageNumber = i; 
     theDoc.Flatten(); 
    } 

    theDoc.Save("batchReport.pdf"); 
    theDoc.Clear(); 
    Console.Read(); 

    } 


    static void AddImage(ref Doc theDoc, string url) 
    { 
    int theId = theDoc.AddImageUrl(url, true, 0, true); 
    while (theDoc.Chainable(theId)) 
    { 
     theDoc.Page = theDoc.AddPage(); 
     theId = theDoc.AddImageToChain(theId); // is this right? 
    } 
    Console.WriteLine(string.Format("document page count: {0}", theDoc.PageCount.ToString())); 
    } 

bearbeiten 2: leider Aufruf AddImageUrl mehrere Male, wenn PDF-Dokumente zu erzeugen doesn‘ t scheint zu arbeiten ...

+0

Das ist keine Rekursion. Rekursion ist eine Funktion, die sich selbst aufruft. –

+0

stimme zu, falsche Wortwahl ... – krul

Antwort

7

Endlich zuverlässige Lösung gefunden. Anstatt die Funktion AddImageUrl() für dasselbe zugrunde liegende Dokument auszuführen, sollten wir die Funktion AddImageUrl() auf ihrem eigenen Doc-Dokument ausführen und eine Sammlung von Dokumenten erstellen, die wir am Ende mit der Append() -Methode zu einem Dokument zusammensetzen. Hier ist der Code:

 static void Main(string[] args) 
    { 
    Test2(); 
    } 

    static void Test2() 
    { 
    Doc theDoc = new Doc(); 
    var urls = new Dictionary<int, string>(); 
    urls.Add(1, "http://www.asp101.com/samples/server_execute_aspx.asp"); 
    urls.Add(2, "http://stackoverflow.com/questions/4338364/repeatedly-call-addimageurlurl-to-assemble-pdf-document"); 
    urls.Add(3, "http://www.google.ca/"); 
    urls.Add(4, "http://ca.yahoo.com/?p=us"); 
    var theDocs = new List<Doc>(); 

    foreach (int key in urls.Keys) 
     theDocs.Add(GetReport(urls[key])); 

    foreach (var doc in theDocs) 
    { 
     if (theDocs.IndexOf(doc) == 0) 
      theDoc = doc; 
     else 
      theDoc.Append(doc); 
    } 

    theDoc.Save("batchReport.pdf"); 
    theDoc.Clear(); 
    Console.Read(); 

    } 


    static Doc GetReport(string url) 
    { 

    Doc theDoc = new Doc(); 
    // Set minimum number of items a page of HTML should contain. 
    theDoc.HtmlOptions.ContentCount = 10;// Otherwise the page will be assumed to be invalid. 
    theDoc.HtmlOptions.RetryCount = 10; // Try to obtain html page 10 times 
    theDoc.HtmlOptions.Timeout = 180000;// The page must be obtained in less then 10 seconds 

    theDoc.Rect.Inset(0, 10); // set up document 
    theDoc.Rect.Position(5, 15); 
    theDoc.Rect.Width = 602; 
    theDoc.Rect.Height = 767; 
    theDoc.HtmlOptions.PageCacheEnabled = false; 

    int theId = theDoc.AddImageUrl(url, true, 0, true); 
    while (theDoc.Chainable(theId)) 
    { 
     theDoc.Page = theDoc.AddPage(); 
     theId = theDoc.AddImageToChain(theId); 
    } 

    //Flatten document 
    for (int i = 1; i <= theDoc.PageCount; i++) 
    { 
     theDoc.PageNumber = i; 
     theDoc.Flatten(); 
    } 

    return theDoc; 
    } 

    } 
+0

das funktioniert auch für mich, eine Sache zu beachten, wenn du jedes Seitensetup, das du gemacht hast, erneut anwenden musst, wenn du ein neues Masterdokument für alle anderen verwendest Die Seitenzahlen können auch wie erwartet funktionieren, wenn Sie den Seitenzahlencode nach dem Anhängen eingeben –