【发布时间】:2021-03-26 15:18:38
【问题描述】:
我是 C# 的完全初学者,我正在寻求有关我必须制作的这个项目的一些帮助。
我正在使用 Windows 窗体和 itextsharp 以及 Microsoft Access。我的目标是制作程序,在用户与 DataGridView 交互并在文本框上归档一些信息后,创建一个 PDF 文件(上面有 DataGridView),然后创建一个文件夹(名称是用户给出的机器 ID之前)在一个文件夹(名称是用户之前给出的客户端 ID)中分配 PDF。
此代码仅稍微格式化 DataGridView 并保存 PDF(通过询问用户的位置)。
public void exportgridtopdf(DataGridView dgw, string filename)
{
BaseFont bf = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1250, BaseFont.EMBEDDED);
PdfPTable pdftable = new PdfPTable(new float[] { 1, 4, 1, 1, 1 });
pdftable.DefaultCell.Padding = 3;
pdftable.WidthPercentage = 100;
pdftable.HorizontalAlignment = Element.ALIGN_LEFT;
pdftable.DefaultCell.BorderWidth = 1;
iTextSharp.text.Font text = new iTextSharp.text.Font(bf, 10, iTextSharp.text.Font.NORMAL);
//add header
foreach (DataGridViewColumn column in dgw.Columns)
{
PdfPCell cell = new PdfPCell(new Phrase(column.HeaderText, text));
cell.BackgroundColor = new iTextSharp.text.BaseColor(240, 240, 240);
pdftable.AddCell(cell);
}
//add datarow
foreach (DataGridViewRow row in dgw.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
pdftable.AddCell(new Phrase(cell.Value.ToString(), text));
}
}
var savefiledialoge = new SaveFileDialog();
savefiledialoge.FileName = filename;
savefiledialoge.DefaultExt = ".pdf";
if (savefiledialoge.ShowDialog() == DialogResult.OK)
{
using (FileStream stream = new FileStream(savefiledialoge.FileName, FileMode.Create))
{
Document pdfdoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
PdfWriter.GetInstance(pdfdoc, stream);
pdfdoc.Open();
pdfdoc.Add(pdftable);
pdfdoc.Close();
stream.Close();
}
}
}
我发现的最相似的案例在这里:How to insert files into folders of an existing PDF portfolio with folders using iTextsharp 我尝试实施(我认为我可以使用的),但我无法获得任何参考或实现我的目标的任何实际进展。
public class FolderWriter
{
private const string Folder = @"C:\Path\to\your\pdf\files";
private const string File1 = @"Pdf File 1.pdf";
private readonly string file1Path = Path.Combine(Folder, File1);
private readonly string[] keys = new[] {
"Type",
"File"
};
}
我很抱歉我缺乏知识并且可能提供的信息很少。如果您需要特定的内容,我可以尝试上传更多代码。
提前致谢,
菲利普·阿尔梅达
【问题讨论】:
标签: c# datagridview directory itext pdf-generation