问题描述
我遇到了对表格单元格中生成的字段进行展平的问题。我已经创建了一个示例项目来说明问题。
I'm having an issue with flattening fields that were generated in a table cell. I've created a sample project to illustrate the issue.
private static void dummyFunction2()
{
// Create a PDF with a TextBox in a table cell
//Get the font ready
BaseFont bfHelvetica = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1250, false);
var helvetica12 = new Font(bfHelvetica, 12, Font.NORMAL, BaseColor.BLACK);
//Create the document and filestream
var doc = new Document(PageSize.LETTER, 18f, 18f, 18f, 18f);
var fs = new FileStream("TextBoxInTableCell.pdf", FileMode.Create);
PdfWriter writer = PdfWriter.GetInstance(doc, fs);
//Create the table
doc.Open();
var myTable = new PdfPTable(1);
myTable.TotalWidth = 568f;
myTable.LockedWidth = true;
myTable.HorizontalAlignment = 0;
//Create the textfield that will sit on a cell in the table
var tf = new TextField(writer, new Rectangle(67, 585, 140, 800), "cellTextBox");
tf.Text = "test";
//Create the table cell
var tbCell = new PdfPCell(new Phrase(" ", helvetica12));
//Use fieldpositioningevents to make the field appear on top of the cell
var events =
new FieldPositioningEvents(writer, tf.GetTextField());
tbCell.CellEvent = events;
//Add the cell to the table
myTable.AddCell(tbCell);
PdfContentByte cb = writer.DirectContent;
//Write out the table to the middle of the document
myTable.WriteSelectedRows(0, -1, 0, -1, 0, 400, cb);
doc.Close();
fs.Close();
//Open back up the document so that we can set GenerateAppearances to false.
//This solution proposed and accepted at https://stackoverflow.com/questions/25169342/itextsharp-fields-generated-in-a-table-not-shown-in-adobe-reader
var reader2 = new PdfReader("TextBoxInTableCell.pdf");
var stamper2 = new PdfStamper(reader2, new FileStream("tempdoc.pdf", FileMode.Create));
stamper2.AcroFields.GenerateAppearances = false;
stamper2.Close();
reader2.Close();
Process.Start("tempdoc.pdf");
//Open the pdf back up
var reader = new PdfReader("tempdoc.pdf");
var stamper = new PdfStamper(reader, new FileStream("tempdoc.pdf" + "1.pdf", FileMode.Create));
//Flatten the form
stamper.FormFlattening = true;
//Close everything
stamper.Close();
reader.Close();
Process.Start("tempdoc.pdf" + "1.pdf");
}
生成的这个unflattened pdf看起来像。正如您所看到的,该字段应该是它应该存在的位置。然而,展平的pdf看起来像。该字段的文本已被移动了一个显着的数量。
This unflattened pdf that is generated looks like . As you can see, the field is where it should be. The flattened pdf, however, looks like . The field's text has been shifted a noticeable amount.
我,以便有人可以在需要时查看pdf语言。
I've posted the flattened pdf so that someone can check out the pdf language if needed.
我知道这是一个利基问题,但希望有人可以帮助我。
I know this is sort of a niche issue but hopefully someone can help me out.
EDIT1:In回应Bruno的评论,我正在使用从Nuget下载的iTextsharp 5.5.2,可以找到unflattened pdf
In response to Bruno's comment, I'm using iTextsharp 5.5.2 downloaded from Nuget and the unflattened pdf can be found here.
EDIT2:更新了SO链接以指向正确的问题。以下是我之前提到的与此问题相关的两个问题:,
Updated SO link to point to the correct question. For reference here are my two previous questions that relate to this problem: First, Second
推荐答案
iText的Java版本不包含此问题。这是一个Java - > C#移植问题。我们会照顾好。
Java version of iText does not contain this problem. It's a Java -> C# porting issue. We'll take care of that.
这篇关于iTextSharp:展平表格单元格中的字段会向上移动文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!