【发布时间】:2019-07-06 23:57:43
【问题描述】:
我用 iTextSharp 制作了一个程序,它允许用户单击一个按钮来选择一个文件并使用第二个按钮执行主要功能。 现在我想制作一个按钮来替换第二个按钮中的这个功能:
using (Stream dest = File.Create(@"L:\Users\user\Documents\PDFnummerieren\PDF.pdf"))
我想制作第三个按钮,该按钮将获取用户选择的位置,而不是不可更改的给定位置。
整个代码:
private void button1_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(theFile) || !File.Exists(theFile))
return;
byte[] bytes = File.ReadAllBytes(theFile);
iTextSharp.text.Font blackFont = FontFactory.GetFont("Arial", 12,
iTextSharp.text.Font.NORMAL, BaseColor.BLACK);
using (Stream source = File.OpenRead(theFile))
using (Stream dest = File.Create(theCFile))
{
PdfReader reader = new PdfReader(source);
using (PdfStamper stamper = new PdfStamper(reader, dest))
{
int pages = reader.NumberOfPages;
for (int i = 1; i <= pages; i++)
{
ColumnText.ShowTextAligned(stamper.GetOverContent(i), Element.ALIGN_RIGHT,
new Phrase(i.ToString(), blackFont), 568f, 15f, 0);
}
}
}
}
private void button3_Click(object sender, EventArgs e)
{
var FD = new System.Windows.Forms.OpenFileDialog();
if (FD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
theFile = FD.FileName;
}
private void button12_Click(object sender, EventArgs e)
{
var FD = new System.Windows.Forms.FolderBrowserDialog();
if (FD.ShowDialog() == System.Windows.Forms.DialogResult.OK) ;
}
【问题讨论】:
-
有什么问题?您已经正确地将 OpenFileDialog 识别为向用户询问文件夹的方式。
-
File.Create 将文件保存为给定方向的 PDF.pdf。它不会询问用户它应该保存在哪里。它只是询问文件在哪里转换