private void SaveButton_Click(string id, TextBox textBox)
{
Document document = new Document();
string fileName = $"{id}_{DateTime.Now.ToString("yyyyMMdd")}.pdf";
string filePath = Path.Combine(@"D:\\test", fileName);
try
{
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(filePath, FileMode.Create));
document.Open();
iTextSharp.text.Font font = FontFactory.GetFont("c:/Windows/Fonts/Arial.ttf", "cp1251", BaseFont.EMBEDDED, 10);
document.Add(new Paragraph("Привет мир", font));
}
catch (Exception ex)
{
MessageBox.Show("Ошибка при сохранении в PDF: " + ex.Message);
}
finally
{
document.Close();
}
MessageBox.Show("Сохранено в PDF: " + filePath);
}
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Text;
private void SaveButton_Click(string id)
{
string fileName = $"{id}_{DateTime.Now.ToString("yyyyMMdd")}.pdf";
string filePath = Path.Combine(@"D:\test", fileName);
try
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
Document document = new Document();
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(filePath, FileMode.Create));
document.Open();
BaseFont baseFont = BaseFont.CreateFont(@"c:\Windows\Fonts\Arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
iTextSharp.text.Font font = new iTextSharp.text.Font(baseFont, 12, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);
Paragraph paragraph = new Paragraph("Тестовая фраза", font);
paragraph.SpacingAfter = 10f;
document.Add(paragraph);
document.Close();
MessageBox.Show("Сохранено в PDF: " + filePath);
}
catch (Exception ex)
{
MessageBox.Show("Ошибка при сохранении в PDF: " + ex.Message);
}
}