我的代码从 Pdf 中读取文本并将其写入另一个 Pdf 并进行一些修改,但是第二个 Pdf 中的格式不一样,那么我怎样才能保持相同的格式和样式?
我的代码是:
string newFile = @"D:\Result.pdf";
string file = @"D:\Source.pdf";
string imagepath = @"D:\logo.jpg";
Console.WriteLine("Welcome");
string content="";
// open the reader
PdfReader reader = new PdfReader(file);
iTextSharp.text.Rectangle size = reader.GetPageSizeWithRotation(1);
Document document = new Document(size);
FileStream fs = new FileStream(newFile, FileMode.Create, FileAccess.Write);
PdfWriter writer = PdfWriter.GetInstance(document, fs);
int n = reader.NumberOfPages;
bool addimage = false;
if (!File.Exists(file))
{
file = Path.GetFullPath(file);
if (!File.Exists(file))
{
Console.WriteLine("Please give in the path to the PDF file.");
}
}
document.Open();
for (int i = 1; i < n; i++)
{
while (addimage == false)
{
iTextSharp.text.Image pic = iTextSharp.text.Image.GetInstance(imagepath);
pic.ScaleToFit(100f, 100f);
//pic.ScalePercent(24f);
pic.SetAbsolutePosition(document.PageSize.Width - 100f - 100f, document.PageSize.Height + 100f - 225f);
document.Add(pic);
addimage = true;
}
content=PdfTextExtractor.GetTextFromPage(reader, i);
document.Add(new Paragraph(content));
PdfContentByte cb = writer.DirectContent;
cb.MoveTo(document.PageSize.Width / 2, document.PageSize.Height / 2);
cb.LineTo(document.PageSize.Width / 2, document.PageSize.Height);
cb.Stroke();
}
document.Close();
}