我创建了一个pdf文件,并通过电子邮件发送.电子邮件发送正常,但要打开PDF,我不断收到以下错误:"打开此文档时出错.文件已损坏,无法修复".这是代码.我不确定我做错了什么......
Document myDoc = new Document(PageSize.LETTER, 20f, 20f, 18f, 20f);
using (MemoryStream ms = new MemoryStream())
{
PdfWriter pWriter = PdfWriter.GetInstance(myDoc, ms);
myDoc.Open();
PdfPTable table = new PdfPTable(2);
table.WidthPercentage = 100;
table.SpacingAfter = 10;
float[] widths = new float[] { 1f, 2f };
table.SetWidths(widths);
table.HorizontalAlignment = 0;
table.SpacingBefore = 20f;
table.SpacingAfter = 30f;
Font regularFont = new Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12);
Font boldFont = new Font(iTextSharp.text.Font.FontFamily.HELVETICA, 14, 1);
Font headerFont = new Font(iTextSharp.text.Font.FontFamily.HELVETICA, 18, 1);
PdfPCell cell = new PdfPCell();
cell.BorderWidth = …Run Code Online (Sandbox Code Playgroud) 我在下面有这个代码请帮我在每个页面重复表格标题.我试过在网上提供的选项很少,但它们没有用.我是第一次使用ItextSharp.谢谢
int columns = gv.Columns.Count;
int rows = gv.Rows.Count;
int tableRows = rows + 3;
string flName = "";
if (gv.ID == "gvShares")
{
flName = "Share_Statement.pdf";
}
if (gv.ID == "gvSavingAccStmt")
{
flName = "Main_Saving_Account_Statement.pdf";
}
Document Doc = new Document();
PdfWriter.GetInstance(Doc, Response.OutputStream);
Doc.Open();
BaseFont bfTimes = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, false);
Font times = new Font(bfTimes, 9, Font.NORMAL, Color.BLACK);
//string imageFilePath = HttpContext.Current.Server.MapPath("/Image/Sacco_Logo.jpg");
string test = System.Web.HttpContext.Current.Server.MapPath(".");
string substr = test.Remove(test.Length - 6);
//string imageFilePath = "\\app\\Image\\Sacco_Logo.jpg";//"\\app\\Image\\Sacco_Logo.jpg";
iTextSharp.text.Image jpg = …Run Code Online (Sandbox Code Playgroud) 我有一个pdf文档,里面是两种类型的注释列表:1.矩形2.文本框

我想用c#和itextsharp从文本框中获取值.
我试图在左侧获得一些文本行,在右侧获取一些文本.出于某种原因,iText似乎完全忽略了对齐.
例:
// create 200x100 column
ct = new ColumnText(writer.DirectContent);
ct.SetSimpleColumn(0, 0, 200, 100);
ct.AddElement(new Paragraph("entry1"));
ct.AddElement(new Paragraph("entry2"));
ct.AddElement(new Paragraph("entry3"));
ret = ct.Go();
ct.SetSimpleColumn(0, 0, 200, 100);
ct.Alignment = Element.ALIGN_RIGHT;
ct.AddElement(new Paragraph("entry4"));
ct.AddElement(new Paragraph("entry5"));
ct.AddElement(new Paragraph("entry6"));
ret = ct.Go();
Run Code Online (Sandbox Code Playgroud)
我已将第二列的对齐方式设置为Element.ALIGN_RIGHT,但文本显示在第一列的顶部,从而呈现不可读的文本.对齐仍然设置为左.
有任何想法吗?
我有一个现有的pdf并从pdf中提取文本.这是我已经有的代码
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;
using System.IO;
public string ReadPdfFile(string fileName)
{
StringBuilder text = new StringBuilder();
if (File.Exists(fileName))
{
PdfReader pdfReader = new PdfReader(fileName);
for (int page = 1; page <= pdfReader.NumberOfPages; page++)
{
ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
string currentText = PdfTextExtractor.GetTextFromPage(pdfReader, page, strategy);
currentText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(currentText)));
text.Append(currentText);
}
pdfReader.Close();
}
return text.ToString();
}
Run Code Online (Sandbox Code Playgroud)
我想获得pdf页面的尺寸.我需要维度,所以我可以创建一个包含此信息的包装类.然后该类可以确定矩形是否超出范围.
我目前正在评估iTextSharp在项目中的潜在用途.我为实现目标而编写的代码是使用PDFCopy.GetImportedPage来复制现有PDF中的所有页面.我想知道的是,在复制像这样的PDF内容时,我需要注意哪些内容会从PDF和/或页面丢失?例如,我已经注意到的一件事是我需要手动将任何书签和命名目的地添加到我的新PDF中.
这是一些粗略的示例代码:
using (PdfReader reader = new PdfReader(inputFilename))
{
using (MemoryStream ms = new MemoryStream())
{
using (Document document = new Document())
{
using (PdfCopy copy = new PdfCopy(document, ms))
{
document.Open();
int n;
n = reader.NumberOfPages;
for (int page = 0; page < n; )
{
copy.AddPage(copy.GetImportedPage(reader, ++page));
}
// add content and make further modifications here
}
}
// write the content to disk
}
}
Run Code Online (Sandbox Code Playgroud) 我使用以下代码填充我的PdfPTable:
public PdfPTable GetTable(List<hsp_Narudzbe_IzdavanjeFakture4_Result> proizvodi)
{
PdfPTable table = new PdfPTable(5);
table.WidthPercentage = 100F;
table.DefaultCell.UseAscender = true;
table.DefaultCell.UseDescender = true;
Font f = new Font(Font.FontFamily.HELVETICA, 13);
f.Color = BaseColor.WHITE;
PdfPCell cell = new PdfPCell(new Phrase("Stavke narudžbe: ", f));
cell.BackgroundColor = BaseColor.BLACK;
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.Colspan = 5;
table.AddCell(cell);
for (int i = 0; i < 2; i++)
{
table.AddCell("Redni broj");
table.AddCell("Proizvod");
table.AddCell("Cijena");
table.AddCell("Šifra proizvoda");
table.AddCell("Kolicina");
}
table.DefaultCell.BackgroundColor = BaseColor.LIGHT_GRAY;
table.HeaderRows = 3;
table.FooterRows = 1;
int broj = 0;
table.DeleteLastRow(); …Run Code Online (Sandbox Code Playgroud) 我正在使用iTextSharp从pdfs中提取数据.我偶然发现了以下问题所描述的问题:
我将其转换为pdf,使用其中一个免费的在线转换器,生成一个类似的pdf(当我生成pdf时,我没有将样式应用于excel):

现在,使用iTextSharp从pdf中提取数据,返回以下字符串作为提取的数据:
如您所见,包裹的单元格数据会生成新行,其中每个包裹的数据由一个空格分隔.
问题:现在,如何确定给定的包装数据所属的列?如果只iTextSharp保留与列一样多的空格......
在我的示例中 - 如何识别111属于哪个列?
更新1:
只要字段有多个单词(即包含空格),就会出现类似的问题.例如,考虑上面示例的第1行:
说它看起来像
---A--- ---B--- ---C--- ---D---
aaaaaaa bb b cccc
Run Code Online (Sandbox Code Playgroud)
iText将再次生成这个提取,如下所示:
aaaaaaa bb b cccc
Run Code Online (Sandbox Code Playgroud)
这里的问题相同,必须确定每列的边界.
我正在使用iTextSharpnuGet(5.5.8)中的最新lib来解析pdf文件中的一些文本.我面临的问题是该GetTextFromPage方法不仅从页面返回应该返回的文本,还返回上一页中的文本.这是我的代码:
var url = "https://www.oslo.kommune.no/getfile.php/Innhold/Politikk%20og%20administrasjon/Etater%20og%20foretak/Utdanningsetaten/Postjournal%20Utdanningsetaten/UDE03032016.pdf";
var strategy = new SimpleTextExtractionStrategy();
using (var reader = new PdfReader(new Uri(url)))
{
for (var page = 1; page <= reader.NumberOfPages; page++)
{
var textFromPage = PdfTextExtractor.GetTextFromPage(reader, page, strategy);
Console.WriteLine(textFromPage.Length);
}
}
Run Code Online (Sandbox Code Playgroud)
输出看起来像这样,这不是我需要的.我需要页面上实际显示的文字:
1106
2248
3468
4835
5167
6431
7563
8860
9962
11216
12399
13640
14690
15760
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?