Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.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 PdfWriter在不应旋转页面布局时旋转页面布局_C#_Pdf_Itextsharp_Pdf Writer - Fatal编程技术网

C# iTextSharp PdfWriter在不应旋转页面布局时旋转页面布局

C# iTextSharp PdfWriter在不应旋转页面布局时旋转页面布局,c#,pdf,itextsharp,pdf-writer,C#,Pdf,Itextsharp,Pdf Writer,我有一个程序,它使用Itextsharp和PdfWriter将pdf格式的文本打印到第一页。目前,这已经按照我输入文本的每个pdf的预期工作。但是,当源pdf的布局为横向布局时,作者在将文本输入到pdf的第一页后将布局旋转为纵向布局。我找不到文档说明在pdf中输入文本后,为什么默认布局更改为纵向布局。由于原始布局是横向的,因此此旋转导致信息最终在右侧被切断 我已经看过了涉及PdfStamper的其他答案,但是我在处理现有代码时遇到了问题。我对C#、pdf操作和iTextSharp编程相当陌生。p

我有一个程序,它使用Itextsharp和PdfWriter将pdf格式的文本打印到第一页。目前,这已经按照我输入文本的每个pdf的预期工作。但是,当源pdf的布局为横向布局时,作者在将文本输入到pdf的第一页后将布局旋转为纵向布局。我找不到文档说明在pdf中输入文本后,为什么默认布局更改为纵向布局。由于原始布局是横向的,因此此旋转导致信息最终在右侧被切断

我已经看过了涉及PdfStamper的其他答案,但是我在处理现有代码时遇到了问题。我对C#、pdf操作和iTextSharp编程相当陌生。pdf上可高亮显示的文本的最终目标

//Adds white invisible text to the pdf document that is highlightable
public static void stamp(string pdfName, string filePath, string textToStamp)
{
    //Make a Temporary copy of the original to manipulate
    string tempPath = @"C:\TemporaryFilePath\" + pdfName + "";
    File.Copy(filePath, tempPath);
    //Make a new reader using the copied source file
    PdfReader reader = new PdfReader(tempPath);
    using (Document document = new Document())
    {
        using (PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(filePath, FileMode.Create)))
        {
            document.Open();
            int numofpages = reader.NumberOfPages;
            for (int p = 1; p <= numofpages; p++)
            {
                //create the ContentByte to give the text a position on the first page
                PdfContentByte cb = writer.DirectContent;
                //Get the page to work with
                PdfImportedPage page = writer.GetImportedPage(reader, p);

                document.NewPage();
                cb.AddTemplate(page, 0, 20);
                var FontColour = new BaseColor(255, 255, 255);
                var MyFont = FontFactory.GetFont("Times New Roman", 12, FontColour);
                //Gets the first page and sends the text to the upper left corner
                if (p == 1)
                {
                    if (TextToStamp!= null)
                    {
                        document.Add(new Paragraph("Hello World", MyFont));
                    }
                }
                document.Close();
            }
        }
        reader.Close();
        File.Delete(tempPath);
    }
//将白色不可见文本添加到可高亮显示的pdf文档中
公共静态无效戳记(字符串pdfName、字符串文件路径、字符串textToStamp)
{
//制作原件的临时副本以进行操作
字符串tempPath=@“C:\TemporaryFilePath\”+pdfName+”;
Copy(filePath,tempPath);
//使用复制的源文件创建新读取器
PdfReader reader=新PdfReader(临时路径);
使用(文档=新文档())
{
使用(PdfWriter writer=PdfWriter.GetInstance(文档,新文件流(filePath,FileMode.Create)))
{
document.Open();
int numofpages=reader.NumberOfPages;
对于(int p=1;p阅读Bruno指出的内容时,请特别注意6.3.1,其中解释了如何使用
PdfStamper
在特定页面的特定位置添加文本。它还显示了两种横向页面之间的差异:旋转页面和宽度>高度的页面


完整的代码示例可以在这里找到:

使用Bruno发布的信息,我提出了这个解决方案。这允许在页面上标记信息,而不管页面的布局是什么,并为其提供少量定制

public static void AddText(string pdfName, string filePath, string textToStamp, float? x = null, float? y = null)
{
    //x and y are used to position the text and allow multiple different templates to use the same method
    //Designate the Temporary source to be used
    string tempPath = @"C:\TemporaryFilePath\" + pdfName + "";
    //Copy to file to the source path
    File.Copy(filePath, tempPath);
    PdfReader reader = new PdfReader(tempPath);
    iTextSharp.text.Rectangle pageSize = reader.GetPageSizeWithRotation(1);
    //Convert the pageHeight into a float
    int pageHeight = Convert.ToInt32(pageSize.Height);
    PdfStamper stamper = new PdfStamper(reader, new FileStream(filePath, FileMode.Create));

    PdfContentByte canvas = stamper.GetOverContent(1);
    //Set a default value if x and y have no value 
    if (x.HasValue == false)
    {
        x = 35;
    }
    if (y.HasValue == false)
    {
        y = 30;
    }
    //choose the font type 
    var FontColour = new BaseColor(255, 255, 255);
    var MyFont = FontFactory.GetFont("Times New Roman", 10, FontColour);
    ColumnText.ShowTextAligned
                (canvas, Element.ALIGN_LEFT, new Phrase("Hello World", MyFont), (float)x, pageHeight - (float)y, 0);

    stamper.Close();
    reader.Close();
    File.Delete(tempPath);
}

您可能不应该使用
PdfWriter
。当您说您已经阅读了文档时,您的意思是您已经阅读了吗?如果是,请解释该章中不清楚的内容,以便我在编写第三版时确保正确解释。(请注意,您的要求还很不明确:您希望突出显示现有文本?如何检索该文本的坐标?)很抱歉要求不明确。目的只是在第一页的左上角区域添加文本。母版很难理解的是,如何限制添加到特定页面的信息,但仍将其余页面保留在PDF中。除非您建议我使用阅读器仅获取第一页,并且n将其重新加入到文档中。