代码之家  ›  专栏  ›  技术社区  ›  Kieran Senior

iTextSharp-SelectPages方法展开表单

  •  0
  • Kieran Senior  · 技术社区  · 16 年前

    reader.SelectPages("1") 它使窗体字段变平。如果我说出来,一切都很好。

    你知道为什么这个方法会导致所有表单域变平吗?但是它正确地导出了一个页面。

    下面是一个小片段:

    PdfReader reader = new PdfReader(formFile);
    reader.SelectPages("1");
    string newFile = Environment.CurrentDirectory + @"\Out" + documentCount  + ".pdf";
    PdfStamper stamper = new PdfStamper(reader, new FileStream(newFile, FileMode.Create), '\0', true);
    AcroFields fields = stamper.AcroFields;
    

    干杯

    1 回复  |  直到 16 年前
        1
  •  1
  •   Jim W    15 年前

    如果您只想从现有的PDF中提取页面,我建议您看看我在jameswelch的博客文章中找到的代码 How to extract pages from a PDF document .

    以下是博客中的原始代码:

    private static void ExtractPages(string inputFile, string outputFile, int start, int end) {
        // get input document
        PdfReader inputPdf = new PdfReader(inputFile);
    
        // retrieve the total number of pages
        int pageCount = inputPdf.NumberOfPages;
    
        if (end < start || end > pageCount) {
            end = pageCount;
        }
    
        // load the input document
        Document inputDoc = new Document(inputPdf.GetPageSizeWithRotation(1));
    
        // create the filestream
        using (FileStream fs = new FileStream(outputFile, FileMode.Create)) {
            // create the output writer
            PdfWriter outputWriter = PdfWriter.GetInstance(inputDoc, fs);
            inputDoc.Open();
            PdfContentByte cb1 = outputWriter.DirectContent;
    
            // copy pages from input to output document
            for (int i = start; i <= end; i++) {
                inputDoc.SetPageSize(inputPdf.GetPageSizeWithRotation(i));
                inputDoc.NewPage();
    
                PdfImportedPage page =
                    outputWriter.GetImportedPage(inputPdf, i);
                int rotation = inputPdf.GetPageRotation(i);
    
                if (rotation == 90 || rotation == 270) {
                    cb1.AddTemplate(page, 0, -1f, 1f, 0, 0,
                        inputPdf.GetPageSizeWithRotation(i).Height);
                } else {
                    cb1.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
                }
            }
    
            inputDoc.Close();
        }
    }
    
    推荐文章