我正在尝试使用iTextSharp打开PDF文件,向文件中添加数据集以预填充数据,然后将其保存到流中,以便我可以将其显示给用户.我不想将它本地保存到文件中.我一直收到错误"无法访问已关闭的流".我无法弄清楚哪个流是错误的.
这是我的代码:
public FileStreamResult PushDataIntoPDFStream(string filename)
{
var reader = new PdfReader(Path.Combine(Server.MapPath(path), filename));
var xml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<form1>
<firstName>test</firstName>
<lastName>user</lastName>
<driveCar>0</driveCar>
<gender>1</gender>
<birthdate>2011-08-12</birthdate>
<numPets>4</numPets>
</form1>";
using (var outstream = new MemoryStream())
{
using (var stamper = new PdfStamper(reader, outstream))
{
var bytes = System.Text.Encoding.UTF8.GetBytes(xml);
using (var ms = new MemoryStream(bytes))
{
stamper.AcroFields.Xfa.FillXfaForm(ms);
}
}
return new FileStreamResult(outstream, "application/pdf")
{
FileDownloadName = "file.pdf";
};
}
}
Run Code Online (Sandbox Code Playgroud) 当FlateDecode通过iTextSharp 解码PDF中的图像时,图像会失真,我似乎无法弄清楚原因.
公认的bpp是Format1bppIndexed.如果我修改PixelFormat到Format4bppIndexed的图像是可识别的在一定程度上(缩水,着色是关闭的,但可读),并以水平的方式被复制的4倍.如果我调整像素格式,Format8bppIndexed它在某种程度上也是可识别的,并且以水平方式重复8次.
下图是采用Format1bppIndexed像素格式的方法.不幸的是,由于安全限制,我无法展示其他人.

代码如下所示,这实际上是我遇到的SO和Web的单一解决方案.
int xrefIdx = ((PRIndirectReference)obj).Number;
PdfObject pdfObj = doc.GetPdfObject(xrefIdx);
PdfStream str = (PdfStream)(pdfObj);
byte[] bytes = PdfReader.GetStreamBytesRaw((PRStream)str);
string filter = ((PdfArray)tg.Get(PdfName.FILTER))[0].ToString();
string width = tg.Get(PdfName.WIDTH).ToString();
string height = tg.Get(PdfName.HEIGHT).ToString();
string bpp = tg.Get(PdfName.BITSPERCOMPONENT).ToString();
if (filter == "/FlateDecode")
{
bytes = PdfReader.FlateDecode(bytes, true);
System.Drawing.Imaging.PixelFormat pixelFormat;
switch (int.Parse(bpp))
{
case 1:
pixelFormat = System.Drawing.Imaging.PixelFormat.Format1bppIndexed;
break;
case 8:
pixelFormat = System.Drawing.Imaging.PixelFormat.Format8bppIndexed;
break;
case 24:
pixelFormat = System.Drawing.Imaging.PixelFormat.Format24bppRgb; …Run Code Online (Sandbox Code Playgroud) 我有一个可填写的PDF包含CheckBoxes和RadioButtons和TextBox.
我如何获得CheckBox名称及其值,我们如何知道它是一个复选框/单选按钮?
我正在使用iTextSharp并查看我的下面的代码
PdfReader pdfReader = new PdfReader(FileName);
pdfReader.SelectPages("37");
MemoryStream oStream = new MemoryStream();
PdfStamper stamper = new PdfStamper(pdfReader, oStream);
AcroFields form = stamper.AcroFields;
if (form.Fields.Count() > 0)
{
IDictionary<string,AcroFields.Item> oDic= form.Fields;
foreach (string FieldName in oDic.Keys)
{
//FieldName - CheckBox name; i need to confirm that is a Checkbox...
}
foreach (AcroFields.Item oItem in oDic.Values)
{
// how do we get check box values
}
}
Run Code Online (Sandbox Code Playgroud) 我想检查pdf文件是否受密码保护或不查看.那是我想知道pdf文件是否有用户密码.
我在一些论坛上找到了一些关于它使用isencrypted功能的帮助,但它没有给出正确的答案.
是否可以检查pdf是否受密码保护?
所有,
我试图使用itextsharp在pdf中添加图像水印.水印按预期显示在所有页面上,但是已经有图像.我希望我的水印图像能够显示在pdf上的现有图像之上.我正在使用以下代码添加图像
using (Stream output = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None))
{
using (PdfStamper pdfStamper = new PdfStamper(pdfReader, output))
{
for (int pageIndex = 1; pageIndex <= pdfReader.NumberOfPages; pageIndex++)
{
pdfStamper.FormFlattening = false;
iTextSharp.text.Rectangle pageRectangle = pdfReader.GetPageSizeWithRotation(pageIndex);
PdfContentByte pdfData = pdfStamper.GetUnderContent(pageIndex);
pdfData.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 10);
PdfGState graphicsState = new PdfGState();
graphicsState.FillOpacity = 0.4F;
pdfData.SetGState(graphicsState);
pdfData.BeginText();
iTextSharp.text.Image jpeg = iTextSharp.text.Image.GetInstance(wtrmrkimg, BaseColor.GREEN);
float width = pageRectangle.Width;
float height = pageRectangle.Height;
jpeg.ScaleToFit(width, height);
jpeg.SetAbsolutePosition(width / 2 - jpeg.Width / 2, height / 2 …Run Code Online (Sandbox Code Playgroud) 我正在使用下面的代码创建一个标题页面.
public static byte[] CreatePageHeader(List<string> texts) {
var stream = new MemoryStream();
Document doc = null;
try {
doc = new Document();
PdfWriter.GetInstance(doc, stream);
doc.SetMargins(50, 50, 50, 50);
doc.SetPageSize(new iTextSharp.text.Rectangle(iTextSharp.text.PageSize.LETTER.Width, iTextSharp.text.PageSize.LETTER.Height));
Font font = new Font(Font.FontFamily.HELVETICA, 10f, Font.NORMAL);
doc.Open();
Paragraph para = null;
foreach (string text in texts) {
para = new Paragraph(text, font);
doc.Add(para);
}
} catch (Exception ex) {
throw ex;
} finally {
doc.Close();
}
return stream.ToArray();
}
Run Code Online (Sandbox Code Playgroud)
这工作正常,但它显示页面顶部的文本.但我希望它在页面中间.
请问如何修改此代码呢?
我只花了几个小时在网上搜索.似乎其他人也有这个问题,但我找不到答案.
我有一大堆PDF文件,我需要他们的测量,即他们的高度和页面内容.
在Adobe Illustrator中,导入PDF时,您可以选择转到"边界框".这正是我需要的.
我尝试了很多方法,这里是大杂烩:
Dim pdfStream = IO.File.OpenRead(FilePath)
Dim img = PdfImages(pdfStream)
Dim pdfReader = New PdfReader(pdfStream)
Dim pdfDictionary = pdfReader.GetPageN(1)
Dim mediaBox = pdfDictionary.GetAsArray(PdfName.MEDIABOX)
Dim b = pdfReader.GetPageSize(pdfDictionary)
Dim ms = New MemoryStream
Dim document = New Document(pdfReader.GetPageSizeWithRotation(1))
Dim writer = PdfWriter.GetInstance(document, ms)
document.Open()
document.SetPageSize(pdfReader.GetPageSize(1))
document.NewPage()
Dim cb = writer.DirectContent
cb.Clip()
Dim pageImport = writer.GetImportedPage(pdfReader, 1)
pdfReader.Close()
pdfStream.Close()
Run Code Online (Sandbox Code Playgroud)
我设法得到的只是页面大小,这是没用的.我在一大堆PDF上试过这个,所以它不像一个腐败的文件或其他东西.
我使用iTextSharp编写了一个提取工具,从PDF文档中提取注释信息.对于高亮注释,我只获得页面上突出显示的区域的矩形.
我的目标是提取已突出显示的文本.为此,我使用`PdfTextExtractor'.
Rectangle rect = new Rectangle(
pdfArray.GetAsNumber(0).FloatValue,
pdfArray.GetAsNumber(1).FloatValue,
pdfArray.GetAsNumber(2).FloatValue,
pdfArray.GetAsNumber(3).FloatValue);
RenderFilter[] filter = { new RegionTextRenderFilter(rect) };
ITextExtractionStrategy strategy = new FilteredTextRenderListener(new LocationTextExtractionStrategy(), filter);
string textInsideRect = PdfTextExtractor.GetTextFromPage(pdfReader, pageNo, strategy);
return textInsideRect;
Run Code Online (Sandbox Code Playgroud)
返回的结果PdfTextExtractor并不完全正确.例如,它返回"即将消除纸张追逐",即使只突出显示"消除".
有趣的是,包含突出显示的"消除"的TJ的整个文本是"将要消除纸张追逐"(TJ是将文本写入页面的PDF指令).
我很想听到有关此问题的任何意见 - 也包括不涉及iTextSharp的解决方案.
tl; dr:基本上我只是想知道设计PDF文档的最佳/最简单方法是什么?使用带代码的iTextSharp实际设计整个PDF文档是远程合法的(即不加载外部文件)?我希望最终结果看起来类似于具有各种颜色,边框,图像和所有内容的网页.或者你必须依赖其他文件,如.doc,.html文件来实现一个好的设计?
最初我以为我会使用HTML标记来生成PDF,但是当看到样式/ CSS时支持看起来有多糟糕,所以我在重新考虑整个情况.
这让我想到,为什么甚至使用HTML标记或.doc(x)文件来创建PDF设计,当我可以在PDF中正确地执行它而不必依赖于没有任何实际用途的各种文件.
我开始查看本指南(它是几个部分) http://www.c-sharpcorner.com/uploadfile/f2e803/basic-pdf-creation-using-itextsharp-part-i/
指南的一部分:
using (MemoryStream ms = new MemoryStream())
{
Document document = new Document(PageSize.A4, 25, 25, 30, 30);
PdfWriter writer = PdfWriter.GetInstance(document, ms);
document.Open();
document.Add(new Paragraph("Hello World"));
document.Close();
writer.Close();
Response.ContentType = "pdf/application";
Response.AddHeader("content-disposition",
"attachment;filename=First PDF document.pdf");
Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
}
Run Code Online (Sandbox Code Playgroud)
并获得了关于生成PDF并设计它们的基本知识.
但是有可能以这种方式生成和设计大型PDF文档,是否有更多适当的指南或类似的所有各种命令来生成文本,图像,边框和一切,因为我没有关于使用代码生成PDF的真正线索.
这是我试图制作粗体和带下划线的文本的代码.
Font header = new Font(Font.FontFamily.TIMES_ROMAN, 15f, Font.BOLD, BaseColor.BLACK);
header.SetStyle(Font.UNDERLINE);
Run Code Online (Sandbox Code Playgroud)
但我得到的只是强调而不是大胆.有什么方法可以得到下划线和粗体字体?
itextsharp ×10
c# ×8
pdf ×6
.net ×2
itext ×2
acrofields ×1
annotations ×1
c#-4.0 ×1
fonts ×1
size ×1