我尝试从 3 天开始寻找制作一份 pdf 文档的方法,如果有任何帮助,我将不胜感激。
我有几个表单字段可以毫无问题地访问和填写。我想在此字段下方放置动态创建的表。该表可以足够长,可以包含多个页面。这是我的问题。我无法将此表添加到其下方带有表单字段的同一页面上。我找到了合并 pdf 文件的示例。现在我有这样的场景。
- 制作一份包含填写表单字段的 pdf
- 用pdfTable制作pdf,总是只有一页,但内容较长
- 制作 pdf,将前两个合并。第一页是填写的表单字段,第二页是我的 pdfTable。
我只想拥有从第一页开始并在下一页继续的表单字段和表格。
我发布了我的代码,有点混乱,但是......
string pdfTemplate = Server.MapPath("~/PDF/") + "invoiceTest.pdf";
string newFile = Server.MapPath("~/PDF/") + "invoice" + 1 + ".pdf";
using (FileStream ms = new FileStream(Server.MapPath("~/PDF/") + "invoice" + 1 + ".pdf", FileMode.Create))
using (FileStream formFile = new FileStream(Server.MapPath("~/PDF/") + "invoiceTest.pdf", FileMode.Open))
{
PdfReader reader = new PdfReader(formFile);
using (Document document = new Document(reader.GetPageSizeWithRotation(1)))
{
PdfStamper outStamper = new PdfStamper(reader, ms);
PdfContentByte body = outStamper.GetOverContent(reader.NumberOfPages);
document.Open(); //Open document to work with
AcroFields fields = outStamper.AcroFields;
BaseFont bfComic = BaseFont.CreateFont(Server.MapPath("~/PDF/") + "trebuc.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font font = new Font(bfComic, 12);
// UPDATE THE FORM FIELDS
fields.SetFieldProperty("txtContragentName", "textfont", bfComic, null);
fields.SetField("txtContragentName", "Фрея");
fields.SetFieldProperty("txtContragentCode", "textfont", bfComic, null);
fields.SetField("txtContragentCode", "DGB34TT");
fields.SetFieldProperty("txtDateCreated", "textfont", bfComic, null);
fields.SetField("txtDateCreated", "03.06.2013");
outStamper.Close();
}
}
using (FileStream ms = new FileStream(Server.MapPath("~/PDF/") + "invoice" + 2 + ".pdf", FileMode.Create))
using (FileStream formFile = new FileStream(Server.MapPath("~/PDF/") + "invoice" + 1 + ".pdf", FileMode.Open))
{
PdfReader reader = new PdfReader(formFile);
using (Document document = new Document(reader.GetPageSizeWithRotation(1)))
{
PdfWriter writer = PdfWriter.GetInstance(document, ms);
document.Open();
//Paragraph heading = new Paragraph("Page Heading", new Font(Font.FontFamily.HELVETICA, 10f, Font.BOLD));
//heading.SpacingAfter = 8f;
//doc.Add(heading);
string text = @"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Suspendisse blandit blandit turpis. Nam in lectus ut dolor consectetuer bibendum. Morbi neque ipsum, laoreet id; dignissim et, viverra id, mauris. Nulla mauris elit, consectetuer sit amet, accumsan eget, congue ac, libero. Vivamus suscipit. Nunc dignissim consectetuer lectus. Fusce elit nisi; commodo non, facilisis quis, hendrerit eu, dolor? Suspendisse eleifend nisi ut magna. Phasellus id lectus! Vivamus laoreet enim et dolor. Integer arcu mauris, ultricies vel, porta quis, venenatis at, libero. Donec nibh est, adipiscing et, ullamcorper vitae, placerat at, diam. Integer ac turpis vel ligula rutrum auctor! Morbi egestas erat sit amet diam. Ut ut ipsum? Aliquam non sem. Nulla risus eros, mollis quis, blandit ut; luctus eget, urna. Vestibulum vestibulum dapibus erat. Proin egestas leo a metus?";
PdfContentByte cb = writer.DirectContent;
ColumnText columns = new ColumnText(cb);
//float left, float right, float gutterwidth, int numcolumns
columns.SetSimpleColumn(40, 20, document.PageSize.Width - 40, document.PageSize.Height - 20);
//Paragraph para = new Paragraph(text, new Font(Font.FontFamily.HELVETICA, 8f));
////para.SpacingAfter = 9f;
//para.Alignment = Element.ALIGN_JUSTIFIED;
//for (int i = 0; i < 28; i++)
//{
// columns.AddElement(para);
//}
//columns.Go();
BaseFont bfComic = BaseFont.CreateFont(Server.MapPath("~/PDF/") + "trebuc.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font font = new Font(bfComic, 12);
PdfPTable table = new PdfPTable(10);
table.HorizontalAlignment = Element.ALIGN_LEFT;
table.DefaultCell.HorizontalAlignment = Element.ALIGN_RIGHT;
table.WidthPercentage = 100;
PdfPCell cell1 = new PdfPCell(new Phrase("ДАТА", new Font(bfComic, 10f, Font.NORMAL, BaseColor.WHITE))) { HorizontalAlignment = 1, VerticalAlignment = 2 };
cell1.BackgroundColor = new iTextSharp.text.BaseColor(System.Drawing.ColorTranslator.FromHtml("#808080"));
PdfPCell cell2 = new PdfPCell(new Phrase("Header spanning 3 columns", new Font(Font.NORMAL, 10f, Font.NORMAL, BaseColor.WHITE))) { HorizontalAlignment = 1 };
cell2.BackgroundColor = new iTextSharp.text.BaseColor(System.Drawing.ColorTranslator.FromHtml("#808080"));
table.AddCell(cell1);
table.AddCell(cell2);
//dump data to be set
#region dump data
for (int i = 0; i < 100; i++)
{
table.AddCell("Col 1 Row 1");
}
#endregion
float[] widths = new float[] { 200f, 200f, 200f, 200f, 100f, 100f, 100f, 100f, 100f, 100f };
table.SetWidths(widths);
table.CompleteRow(); //Added - table won't add the final row if its cells are incomplete - safe to have it ending a table
columns.AddElement(table);
columns.Go();
}
}
using (FileStream ms = new FileStream(Server.MapPath("~/PDF/") + "invoice" + 3 + ".pdf", FileMode.Create))
using (FileStream stampedfile = new FileStream(Server.MapPath("~/PDF/") + "invoice" + 1 + ".pdf", FileMode.Open))
using (FileStream appendfile = new FileStream(Server.MapPath("~/PDF/") + "invoice" + 2 + ".pdf", FileMode.Open))
{
PdfReader stampedContentReader = new PdfReader(stampedfile);
PdfReader appendContentReader = new PdfReader(appendfile);
using (Document document = new Document(stampedContentReader.GetPageSizeWithRotation(1)))
{
PdfCopy pdfCopy = new PdfCopy(document, ms);
document.Open();
for (int i = 1; i <= stampedContentReader.NumberOfPages; i++)
pdfCopy.AddPage(pdfCopy.GetImportedPage(stampedContentReader, i));
for (int i = 1; i <= appendContentReader.NumberOfPages; i++)
pdfCopy.AddPage(pdfCopy.GetImportedPage(appendContentReader, i));
}
}