我正在使用 XMLWorker 将 HTML 字符串解析为 PDF 文档,但找不到控制正在生成的 PDF 的行距的方法。
Document document = new Document(PageSize.LETTER, 72f, 72f, 108f, 90f);
MemoryStream stream1 = new MemoryStream();
PdfWriter pdfWriter = PdfWriter.GetInstance(document, stream1);
document.Open();
//parse HTML into document
XMLWorkerHelper.GetInstance().ParseXHtml(pdfWriter, document, new StringReader(summary.Content));
Run Code Online (Sandbox Code Playgroud)
“summary.Content”是来自数据库字段的 HTML 字符串。
现在,我最近将我们的 ITextSharp 库升级到 5.5.5.0,并升级到新的 XMLWorker 库。使用上面的代码,行距(PDF 中的“前导”)比之前生成的 PDF 小得多。我需要确保行距看起来和以前一样。
我读到我可以在我构建的段落上设置前导,但是当简单地调用 ParseXHtml() 时,这对我没有帮助。我读到 ITextSharp 默认的前导大小是字体大小的 1.5 倍。
我在这里阅读了itextsupport 文档,我可以使用这一行来使用 XML Worker 附带的 default.css。
CSSResolver cssResolver = XMLWorkerHelper.getInstance().getDefaultCssResolver(true);
我认为默认 CSS 可能会生成与我的旧 PDF 具有相同前导的 PDF,但以下代码生成的输出 PDF 与我仅使用 ParseXHtml() 时的输出 PDF 相同。 …
我正在使用DirectContent方法在我的PDF上绝对定位元素.我需要迭代一个记录列表,并在我的PDF中为每个记录构建一个页面.
如何告诉itextsharp插入新页面并"绘制"到该页面?
// 72point per inch
// we want 7x10
iTextSharp.text.Rectangle pageSize = new iTextSharp.text.Rectangle(504, 720);
Document doc = new Document(pageSize);
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(@"C:\temp\backPages.pdf", FileMode.Create));
doc.Open();
PdfContentByte cb = writer.DirectContent;
// "DRAW" IMAGES AND TEXT
...
//various .Add's called here
...
// Done with drawing images & text
doc.Close();
Run Code Online (Sandbox Code Playgroud) 好的,我必须制作一个有一些风格的PDF.这是标签.我有以下代码:
Document doc = new Document(PageSize.A4);
int labelHeight = 394;
int labelWidth = 556;
float labelTop = doc.PageSize.Height - 2;
float labelBottom = doc.PageSize.Height - labelHeight;
MemoryStream ms = new MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(doc, ms);
doc.Open();
PdfContentByte cb = writer.DirectContent;
// Generate Boxes For Content
cb.SetColorStroke(new CMYKColor(207, 115, 255, 133));
cb.SetLineWidth(4);
cb.SetColorFill(new CMYKColor(207, 71, 255, 38));
cb.MoveTo(2, labelBottom);
cb.LineTo(labelWidth - 2, labelBottom);
cb.LineTo(labelWidth - 2, labelTop);
cb.LineTo(2, labelTop);
cb.ClosePathFillStroke(); // Draw Main Green Box
cb.SetColorStroke(new CMYKColor(0, 0, 0, 0)); …Run Code Online (Sandbox Code Playgroud) 以下应该根据文档工作,但它不适合我.我错过了什么吗?
PdfPTable rs1 = new PdfPTable(1);
PdfPCell c = new PdfPCell();
Paragraph p = new Paragraph("some text to align");
c.AddElement(p);
c.VerticalAlignment = Element.ALIGN_MIDDLE;
rs1.AddCell(c);
rs1.AddCell("more text");
return rs1;
Run Code Online (Sandbox Code Playgroud) 我有一个局部视图,我正在尝试使用ITextSharp将html转换为pdf.如何将html转换为字符串,以便我可以使用ItextSharps HtmlParser?
我尝试过这样的事情没有运气......任何想法?:
var contents = System.IO.File.ReadAllText(Url.Action("myPartial", "myController", new { id = 1 }, "http"));
Run Code Online (Sandbox Code Playgroud) 当段落长度对于ColumnText的宽度太长时,如何减少换行符的高度?
我已经尝试了以下内容,因为我已经看到了其他问题,并回答了这个问题:
p.Leading = 0
Run Code Online (Sandbox Code Playgroud)
但这没有任何影响.
我也尝试增加Leadingto 100以查看是否添加了更大的换行符,但都没有工作.
SpacingBefore/SpacingAfter也没有帮助:
p.SpacingBefore = 0
p.SpacingAfter = 0
Run Code Online (Sandbox Code Playgroud)
我怎样才能减少这个?
我有一个将内容表插入现有Acroform的过程,并且我能够跟踪启动该内容所需的位置.但是,根据我插入的表的高度,我在该点之下存在需要向上或向下移动的Acrofield.有了这个,我怎样才能改变Acrofield的位置?下面是我可以用来"获得"位置的代码......但是现在我还需要能够"设置"它.
....
// Initialize Stamper ("output" is a MemoryStream object)
PdfStamper stamper = new PdfStamper(pdf_rdr, output);
// Get Reference to PDF Document Fields
AcroFields fields = stamper.AcroFields;
//call method to get the field's current position
AcroFields.FieldPosition pos = GetFieldPosition(fields, "txt_footer");
Run Code Online (Sandbox Code Playgroud)
//**需要明确地为这里的田野设置一个新的位置
//assuming a call to "RegenerateField" will be required
fields.RegenerateField(txt_footer);
Run Code Online (Sandbox Code Playgroud)
....
//helper method for capturing the position of a field
private static AcroFields.FieldPosition GetFieldPosition(AcroFields fields, string field_nm)
{
////////////////////////////////////////////////////////////////////////////////////
//get the left margin of the page, and the "top" location …Run Code Online (Sandbox Code Playgroud) 我想使用iText将一系列html文件转换为PDF.
例如:如果有这些文件:
现在我要创建一个PDF文件,其中page1.html是第一页,page2.html是第二页,依此类推......
我知道如何将单个HTML文件转换为PDF,但我不知道如何将这个操作产生的这些不同的PDF组合成一个PDF.
我必须将多个1页pdf合并为一个pdf.我正在使用iTextSHarp 5.5.5.0来实现这一目标,但是当我合并超过900-1000 pdf时,我得到一个内存不足异常.我注意到,即使我释放我的阅读器并关闭它,内存永远不会被正确清理(进程使用的内存量永远不会减少)所以我想知道我可能做错了什么.这是我的代码:
using (MemoryStream msOutput = new MemoryStream())
{
Document doc = new Document();
PdfSmartCopy pCopy = new PdfSmartCopy(doc, msOutput);
doc.Open();
foreach (Tuple<string, int> file in filesList)
{
PdfReader pdfFile = new PdfReader(file.Item1);
for (int j = 0; j < file.Item2; j++)
for (int i = 1; i < pdfFile.NumberOfPages + 1; i++)//in this case it's always 1.
pCopy.AddPage(pCopy.GetImportedPage(pdfFile, i));
pCopy.FreeReader(pdfFile);
pdfFile.Close();
File.Delete(file.Item1);
}
pCopy.Close();
doc.Close();
byte[] content = msOutput.ToArray();
using (FileStream fs = File.Create(Out))
{
fs.Write(content, 0, content.Length); …Run Code Online (Sandbox Code Playgroud) 我知道这个问题已被问了一千次,但我还没有找到一个直截了当的答案.我对ITextSharp比较陌生,所以请解释一下,好像你在和一个小孩说话.如何在我正在创建的文档中添加一个简单的纯文本页眉和页脚?
我正在使用以下代码创建一个简单的pdf文档:
void Button1Click(object sender, EventArgs e)
{
Document doc = new Document(iTextSharp.text.PageSize.LEDGER, 10, 10, 42, 35);
PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(@"File Path", FileMode.Create));
doc.Open();
Paragraph par1 = new Paragraph("Hello World!");
doc.Add(par1);
//Code to add header/footer
doc.Close();
MessageBox.Show("Your PDF has been created!");
}
Run Code Online (Sandbox Code Playgroud)
我已经做了很多关于如何添加页眉和页脚的研究,但它们都与复杂的页面事件有关.有没有更简单的方法?如果没有,你能一步一步地指导我完成这个过程吗?我非常感谢你们所能给予的任何帮助.谢谢!
itextsharp ×10
c# ×6
pdf ×5
.net ×2
acrofields ×1
css ×1
line-breaks ×1
memory ×1
memory-leaks ×1
paragraph ×1
xmlworker ×1