Add multiple pages to pdf form with iTextSharp

北城余情 提交于 2019-12-11 02:53:29

问题


I am trying to fill out a pdf form and the form could have multiple pages of the same form, so instead of writing each one to disk I want to create one pdf document in memory with all the pages.

I have seen plenty of example on adding pages from an existing pdf file with FileStream but nothing really for filling out forums and adding it to MemoryStream.

basically I need to fill out the forum with PdfStamper then append that form to a "master" MemoryStream because I dont want to write it to file.

this is basically what I have so far

List<Person> people = arson.getPeople();
MemoryStream main = new MemoryStream();
foreach (Person person in people )
{
    PdfReader reader = new PdfReader(pathToPdf);
    MemoryStream mem = new MemoryStream();
    PdfStamper stamper = new PdfStamper(reader, mem);

    //fill in forum with data from person then append

}

filling out the form is not the problem its just appending a new page to the main stream so that they are all in the same document. How can I add multiple pages in memory a not write it to disk first?


回答1:


Did you watch the iText video tutorial? You can find it here and there's an example that is doing exactly what you're asking for.

If you don't like watching videos, you can also consult chapter 6 of my book. There too you will find an example that explains what to do.

If you have a preference for a "recipe book" style, please take a look at The Best iText Questions on StackOverflow. It has at least one example involving a combination of PdfStamper and MemoryStream.

If you did read the documentation, you would have found the example DataSheets2:

// step 1
using (Document document = new Document()) {
    // step 2
    using (PdfSmartCopy copy = new PdfSmartCopy(document, ms)) {
        // step 3
        document.Open();
        // step 4
        AddDataSheets(copy);
    }
 }

You can find out what step 4 is about in DataSheets1 to find out what step 4 is about:

public void AddDataSheets(PdfCopy copy) {
    IEnumerable<Movie> movies = PojoFactory.GetMovies();
    // Loop over all the movies and fill out the data sheet
    foreach (Movie movie in movies) {
        PdfReader reader = new PdfReader(DATASHEET_PATH);
        using (var ms = new MemoryStream()) {
            using (PdfStamper stamper = new PdfStamper(reader, ms)) {
            Fill(stamper.AcroFields, movie);
            stamper.FormFlattening = true;
        }
        reader = new PdfReader(ms.ToArray());
        copy.AddPage(copy.GetImportedPage(reader, 1));
    }
}

As you can see, DATASHEET_PATH is the path to the form that we are going to reuse over and over again. We use PdfStamper to fill out a form in memory. We create a new PdfReader instance with ms.ToArray() as parameter.

In this case, we only add page 1 to the new PDF. If there are more pages, you need to loop over the different pages.

You may want to compare the result generated by DataSheets1 with the results generated with DataSheets2. You will notice that DataSheets1 is an example on how not to do it.

You already know what the FillDataSheet() method is about. If not, please take a look at the FillDataSheet example:

public static void Fill(AcroFields form, Movie movie) {
    form.SetField("title", movie.MovieTitle);
    form.SetField("director", GetDirectors(movie));
    form.SetField("year", movie.Year.ToString());
    form.SetField("duration", movie.Duration.ToString());
    form.SetField("category", movie.entry.category.Keyword);
    foreach (Screening screening in movie.entry.Screenings) {
        form.SetField(screening.Location.Replace('.', '_'), "Yes");
    }
}


来源:https://stackoverflow.com/questions/28304428/add-multiple-pages-to-pdf-form-with-itextsharp

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