我正在尝试使用iTextSharp在PDF文件中绘制QR条形码.如果我使用英文文本条形码很好,它们会被正确解码,但如果我使用中文文本,条形码会被解码为问号.例如,该字符'测'(\ u6D4B)被解码为'?'.我尝试了所有支持的字符集,但没有一个帮助.
为了正确编码中文文本,我应该将哪些参数组合用于iTextSharp中的QR条码?
我是iTextSharp的首发,我将此代码编写为创建RoundRectangle到PdfPTable并在页面中心对齐表
string pdfpath = Server.MapPath("PDFs");
RoundRectangle rr = new RoundRectangle();
using (Document document = new Document())
{
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(pdfpath + "/Graphics6.pdf", FileMode.CreateNew));
document.Open();
PdfPTable table = new PdfPTable(1);
float[] f=new float[]{0.5f};
table.SetWidths(f);
PdfPCell cell = new PdfPCell()
{
CellEvent = rr,
Border = PdfPCell.NO_BORDER,
Phrase = new Phrase("test")
};
table.AddCell(cell);
document.Add(table);
Run Code Online (Sandbox Code Playgroud)
我想改变表格宽度我改变代码
string pdfpath = Server.MapPath("PDFs");
RoundRectangle rr = new RoundRectangle();
using (Document document = new Document())
{
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(pdfpath + "/Graphics6.pdf", …Run Code Online (Sandbox Code Playgroud) 谁能给我一个如何从MemoryStream获取PdfReader的示例吗?我可以看到PdfReader类有几个看起来很可能候选的方法(GetStreamBytes和GetStreamBytesRaw),但是这些似乎想要iText特定的流,我的只是常规的Byte []或MemoryStream。
这是使用C#和.net4
iTextSharp.text.pdf.PdfReader rdr = iTextSharp.text.pdf.PdfReader.GetStreamBytesRaw
Run Code Online (Sandbox Code Playgroud)
提前致谢。
我已经制作了一个带有单元格的表,并且有兴趣在其中一个单元格中创建一个图像.以下是我的代码:
doc.Open();
PdfPTable table = new PdfPTable(2);
table.TotalWidth = 570f;
table.LockedWidth = true;
table.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
PdfPCell points = new PdfPCell(new Phrase("and is therefore entitled to 2 points", arialCertify));
points.Colspan = 2;
points.Border = 0;
points.PaddingTop = 40f;
points.HorizontalAlignment = 1;//0=Left, 1=Centre, 2=Right
table.AddCell(points);
// add a image
doc.Add(table);
Image jpg = Image.GetInstance(imagepath + "/logo.jpg");
doc.Add(jpg);
Run Code Online (Sandbox Code Playgroud)
使用上面的代码,图像显示在我的pdf中,但我希望它在一个单元格中,以便我可以在图像的右侧添加更多单元格.
我有一个非常高的asp图表(PNG文件),让我们说高度为4000px.当使用iTextSharp生成pdf文件时,它只有一个页面,我只能看到我图表的25-30%.我不想缩放图像以适合一页.我想在多个页面上传播/拆分我的图表,以便查看图表的详细信息.如果我的图表适合四页,PDF也需要有四页.在下面的图像中,您可以看到我的图表,我想要的方式(分布在6页),以及iTextSharp(图表顶部显示在一个页面上)生成的实际效果. 图片
Document pdfDoc = new Document(PageSize.A4.Rotate(), 10f, 10f, 10f, 0f);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
using (MemoryStream stream = new MemoryStream())
{
Chart1.SaveImage(stream, ChartImageFormat.Png);
iTextSharp.text.Image chartImage = iTextSharp.text.Image.GetInstance(stream.GetBuffer());
chartImage.ScalePercent(70f);// This solves the width of the chart
pdfDoc.Add(chartImage);
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Chart.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
}
Run Code Online (Sandbox Code Playgroud) 我正在使用iTextSharp我的应用程序创建PDF.但是我必须重新创建一个表,我必须为一个非常小的列设置大小.下面是显示我想要设置列的大小的图片:

当表创建的其余部分都很好时,我无法设置此宽度.
码:
PdfPTable table = new PdfPTable(2);
table.WidthPercentage = 82.0f;
PdfPCell cell = new PdfPCell(new Phrase("Com a assinatura autógrafa, o signatário desta Auto-declaração garante ter cumprido estas condições:", fontetexto));
cell.PaddingBottom = 10f;
cell.PaddingTop = 10f;
cell.Colspan = 2;
cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
table.AddCell(cell);
table.AddCell("1. ");
table.AddCell("Os óleos e gorduras vegetais velhos fornecidos são biomassa conforme o Decreto de biomassa.");
Run Code Online (Sandbox Code Playgroud) 如何在每个页面之间添加新页面,然后复制到新PDF.我知道我遗漏了一些基本的东西,但似乎并没有把它正确地弄下来.
int n = pdfReaderInput.NumberOfPages;
Document document = new Document();
PdfCopy copy = new PdfCopy(document, new FileStream(tempFile, FileMode.OpenOrCreate));
document.Open();
for (int i = 0; i < n; )
{
copy.AddPage(copy.GetImportedPage(pdfReaderInput, ++i));
}
document.Close();
return tempFile;
Run Code Online (Sandbox Code Playgroud)
我理解并知道这是错的,但我不确定我需要做什么.基本上,我在每页之间添加一个空白的pdf.提前致谢!
它有点用于寻找新页面的问题.我pdfContentByte使用下面的代码将第一页之后的数据放到下一页但不幸的iText是没有生成新的页面.
//step1
itextDocument = new com.itextpdf.text.Document(PageSize.A4, 50, 50, 30, 65);
writer = PdfWriter.getInstance(itextDocument, new FileOutputStream(RESULT));
itextDocument.open();
writer.setPageEmpty(true);
itextDocument.newPage();
// step 2 == design and set the postions
// Measuring a String in Helvetica
Font font = new Font(FontFamily.TIMES_ROMAN, 10);
BaseFont romanFont = font.getCalculatedBaseFont(false);
// Drawing lines to see where the text is added
PdfContentByte canvas = writer.getDirectContent();
canvas.saveState();
canvas.stroke();
canvas.restoreState();
// Adding text with PdfContentByte.showTextAligned()
canvas.beginText();
canvas.setFontAndSize(romanFont, 10);
//=================== get data from xml and put in …Run Code Online (Sandbox Code Playgroud) 如何在C#中使用iTextSharp将数据表导出为PDF?
public void ExportToPdf(DataTable dt)
{
Document document = new Document();
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("c://sample.pdf", FileMode.Create));
document.Open();
PdfPTable table = new PdfPTable(dt.Columns.Count);
PdfPRow row = null;
float[] widths = new float[] { 2f, 2f, 2f, 2f };
table.SetWidths(widths);
table.WidthPercentage = 100;
PdfPCell cell = new PdfPCell(new Phrase("Products"));
cell.Colspan = dt.Columns.Count;
}
Run Code Online (Sandbox Code Playgroud) 我正在使用ASP.NET MVC5应用程序中的HTML从iTextSharp(5.5.7.0)创建一些PDF文档,但我无法更改字体.我已经尝试了几乎所有我能在SO或其他资源上找到的东西.
PDF生成代码如下:
public Byte[] GetRecordsPdf(RecordsViewModel model)
{
var viewPath = "~/Template/RecordTemplate.cshtml";
var renderedReport = RenderViewToString(viewPath, model);
FontFactory.RegisterDirectory(Environment.GetFolderPath(Environment.SpecialFolder.Fonts));
using (var ms = new MemoryStream())
{
using (var doc = new Document())
{
doc.SetPageSize(PageSize.A4.Rotate());
using (var writer = PdfWriter.GetInstance(doc, ms))
{
doc.Open();
using (var html = new MemoryStream(Encoding.Default.GetBytes(renderedReport)))
{
XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, html, Encoding.Default);
}
doc.Close();
}
}
var bytes = ms.ToArray();
return bytes;
}
}
Run Code Online (Sandbox Code Playgroud)
实际的HTML包含在renderedReport字符串变量中(我有强类型的.cshtml文件,我使用MVC Razor引擎渲染,然后在字符串中返回HTML).
我试图注册一些特定的字体,但这没有帮助.我也尝试在我的机器上注册所有字体(如上例所示),但这也没有帮助.加载了字体我在调试模式下检查过.
CSS嵌入在HTML文件中(在标题,样式标记中),如下所示:
body {
font-size: 7px;
font-family: Comic Sans MS;
}
Run Code Online (Sandbox Code Playgroud)
(为了测试,我决定使用Comic Sans,因为我可以轻松识别它,我实际上对Arial Unicode …
itextsharp ×10
c# ×6
asp.net-mvc ×2
itext ×2
pdf ×2
.net ×1
asp.net ×1
fonts ×1
html ×1
java ×1
memorystream ×1
pdf-reader ×1
pdfptable ×1
qr-code ×1
razor ×1
webmatrix ×1