Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用iTextSharp在现有PDF文档中插入标题或图像?不盖章_C#_.net_Parsing_Pdf_Itextsharp - Fatal编程技术网

C# 使用iTextSharp在现有PDF文档中插入标题或图像?不盖章

C# 使用iTextSharp在现有PDF文档中插入标题或图像?不盖章,c#,.net,parsing,pdf,itextsharp,C#,.net,Parsing,Pdf,Itextsharp,我有一个PDF文档,我需要在第一页的顶部添加一个文本。这不是水印或邮票。这是一篇普通的文章。 我该怎么做 也许我需要解析我的PDF文档并创建一个新的添加文本的文档?但如何做到这一点呢?如何将现有pdf文档解析为新文档?不复制页面,因为在这种情况下,文本将位于第一页,而其他内容位于新页面 我找到了这个方法。但它不会有帮助,因为它只会将页面复制到新页面。我会在第一页上看到“文本”,在新的页面上看到其他内容 public void ExtractPage(string sourcePdfPath, s

我有一个PDF文档,我需要在第一页的顶部添加一个文本。这不是水印或邮票。这是一篇普通的文章。 我该怎么做

也许我需要解析我的PDF文档并创建一个新的添加文本的文档?但如何做到这一点呢?如何将现有pdf文档解析为新文档?不复制页面,因为在这种情况下,文本将位于第一页,而其他内容位于新页面

我找到了这个方法。但它不会有帮助,因为它只会将页面复制到新页面。我会在第一页上看到“文本”,在新的页面上看到其他内容

public void ExtractPage(string sourcePdfPath, string outputPdfPath, 
int pageNumber, string password = "<span style="color: rgb(139, 0, 0);">")
{
PdfReader reader = null;
Document document = null;
PdfCopy pdfCopyProvider = null;
PdfImportedPage importedPage = null;

try
{
    // Intialize a new PdfReader instance with the contents of the source Pdf file:
    reader = new PdfReader(sourcePdfPath);

    // Capture the correct size and orientation for the page:
    document = new Document(reader.GetPageSizeWithRotation(pageNumber));

    // Initialize an instance of the PdfCopyClass with the source 
    // document and an output file stream:
    pdfCopyProvider = new PdfCopy(document, 
        new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create));

    document.Open();

    // Extract the desired page number:
    importedPage = pdfCopyProvider.GetImportedPage(reader, pageNumber);
    pdfCopyProvider.AddPage(importedPage);
    document.Close();
    reader.Close();
}
catch (Exception ex)
{
    throw ex;
}
}
public void ExtractPage(字符串sourcePdfPath、字符串outputPdfPath、,
整数页码,字符串密码=”)
{
PdfReader reader=null;
单据=空;
PdfCopy pdfCopyProvider=null;
PDFIImportedPage importedPage=null;
尝试
{
//使用源Pdf文件的内容初始化新的PdfReader实例:
读卡器=新的PDF读卡器(sourcePdfPath);
//捕获页面的正确大小和方向:
文档=新文档(reader.GetPageSizeWithRotation(pageNumber));
//使用源代码初始化PdfCopyClass的实例
//文档和输出文件流:
pdfCopyProvider=新的PdfCopy(文档、,
新的System.IO.FileStream(outputPdfPath,System.IO.FileMode.Create));
document.Open();
//提取所需的页码:
importedPage=pdfCopyProvider.GetImportedPage(读卡器,页码);
pdfCopyProvider.AddPage(导入页面);
document.Close();
reader.Close();
}
捕获(例外情况除外)
{
掷骰子;
}
}

我有一个带文本的PDF文档。我需要在开头添加一段文字来更改它。因此,整个文本应该向下漂移这是不可能满足要求的,因为PDF格式的性质。这是一种为出版而设计的格式,而不是版本。正如其他人所说,PDF不能“回流”。这实际上是规范本身内置的东西。每个元素,无论是文本、形状还是图像,都在特定的x、y位置绘制。任何“流”都由创作应用程序处理。“段落”中的“换行符”实际上是由创作应用程序预先计算的不相关的文本绘图命令,它们恰好在视觉上彼此相邻。“行”本身实际上可以是一个或几十个单独的命令。由于所有内容都是绝对的,因此没有“流动”,也不可能有“回流”。然而,如果您对“向下移动所有内容”感到满意,您可以在新文档上使用压模,导入旧页面,垂直偏移所需的像素数,然后在顶部写入文本。只要你知道你需要移动的东西到底有多远,这实际上是很容易实现的。谢谢,@ChrisHaas。事实上,我并没有成功地“把一切都放下”。但我非常感谢你提供的信息丰富的答复。