Добавить нижний колонтитул на каждую страницу существующего PDF-файла в iText7 (. NET) - PullRequest
1 голос
/ 10 июля 2020

Я создаю PDF-файл во время выполнения в itext7.pdf HTML, используя файл шаблона. Я хочу добавить нижний колонтитул на каждую страницу результирующего PDF-файла, который состоит из двух страниц, но по какой-то причине нижний колонтитул отображается только на второй странице.

Я пытаюсь построить этот пример с веб-сайта iText. Пример касается номеров страниц, но поскольку я просто добавляю в свой документ текст stati c, принцип тот же. Вот мой код:

string footer = "This is the footer".
string body = "<span>This is raw HTML</span>";

//create a temporary PDF with the raw HTML, made from my template and given data
private void createPDF()
{
    destination = System.IO.Path.Combine(HttpContext.Current.Server.MapPath("~/pdf_repo"), "tempFile.pdf");

    ConverterProperties properties = new ConverterProperties();
    properties.SetBaseUri(HttpContext.Current.Server.MapPath("~/templates/"));

    HtmlConverter.ConvertToPdf(body, new FileStream(destination, FileMode.Create), properties);

    addFooter(id);
}

//modify the PDF file created above by adding the footer
private void addFooter(string id)
{
    string newFile = System.IO.Path.Combine(HttpContext.Current.Server.MapPath("pdf_repo", "finalFile.pdf");

    PdfDocument pdfDoc = new PdfDocument(new PdfReader(destination), new PdfWriter(newFile));
    Document doc = new Document(pdfDoc);

    Paragraph foot = new Paragraph(footer);
    foot.SetFontSize(8);

    float x = 300; //559
    float y = 0; //806

    int numberOfPages = pdfDoc.GetNumberOfPages();
    for (int i = 1; i <= numberOfPages; i++)
    {
        doc.ShowTextAligned(foot, x, y, TextAlignment.CENTER, VerticalAlignment.BOTTOM);
    }

    doc.Close();

    //delete temporary PDF
    File.Delete(destination);
}

Я попытался установить i в 0 в addFooter () для «l oop», но это не решило проблему. Как сделать так, чтобы нижний колонтитул отображался на каждой странице?

1 Ответ

1 голос
/ 10 июля 2020

Да, вы не указали, на какую страницу добавить нижний колонтитул, поэтому он только добавил его в конец всего документа. Попробуйте следующее:

Обратите внимание, единственное изменение: doc.ShowTextAligned(foot, x, y, i, TextAlignment.CENTER, VerticalAlignment.BOTTOM);

string footer = "This is the footer".
string body = "<span>This is raw HTML</span>";

//create a temporary PDF with the raw HTML, made from my template and given data
private void createPDF()
{
    destination = System.IO.Path.Combine(HttpContext.Current.Server.MapPath("~/pdf_repo"), "tempFile.pdf");

    ConverterProperties properties = new ConverterProperties();
    properties.SetBaseUri(HttpContext.Current.Server.MapPath("~/templates/"));

    HtmlConverter.ConvertToPdf(body, new FileStream(destination, FileMode.Create), properties);

    addFooter(id);
}

//modify the PDF file created above by adding the footer
private void addFooter(string id)
{
    string newFile = System.IO.Path.Combine(HttpContext.Current.Server.MapPath("pdf_repo", "finalFile.pdf");

    PdfDocument pdfDoc = new PdfDocument(new PdfReader(destination), new PdfWriter(newFile));
    Document doc = new Document(pdfDoc);

    Paragraph foot = new Paragraph(footer);
    foot.SetFontSize(8);

    float x = 300; //559
    float y = 0; //806

    int numberOfPages = pdfDoc.GetNumberOfPages();
    for (int i = 1; i <= numberOfPages; i++)
    {
        doc.ShowTextAligned(foot, x, y, i, TextAlignment.CENTER, VerticalAlignment.BOTTOM);
    }

    doc.Close();

    //delete temporary PDF
    File.Delete(destination);
}
...