PDFsharp does not see pages in documents
Asked Answered
I

1

1

I made some file merging using PDFsharp before, and now I'm trying to change several files (insert or remove some pages) and I faced with problem, that the library does not see pages. It says that PageCount == 0 and I cannot find pages in object (while debugging). And sure, I cannot do my current work. I use this very simple code:

var destinationPdf = new PdfDocument(destinationFilePath);
Int32 count = destinationPdf.PageCount;

And also, here is the code, that I used to merge files to one PDF before:

public class PdfCreator
{
    private PdfDocument document;

    public PdfCreator()
    {
        this.document = new PdfDocument();
    }

    public void AddImage(String imageFilePath)
    {
        PdfPage newPage = this.document.AddPage();
        XGraphics xGraphics = XGraphics.FromPdfPage(newPage);
        XImage image = XImage.FromFile(imageFilePath);
        xGraphics.DrawImage(image, 0, 0);
    }

    public void AddPdfFile(String pdfFilePath)
    {
        PdfDocument inputDocument = PdfReader.Open(pdfFilePath, PdfDocumentOpenMode.Import);
        Int32 count = inputDocument.PageCount;
        for (Int32 currentPage = 0; currentPage < count; currentPage++)
        {
            PdfPage page = inputDocument.Pages[currentPage];
            this.document.AddPage(page);
        }
    }

    public void AddTextFile(String txtFilePath)
    {
        PdfPage newPage = this.document.AddPage();
        XGraphics xGraphics = XGraphics.FromPdfPage(newPage);
        var xFont = new XFont("Times New Roman", 12, XFontStyle.Bold);
        var xTextFormatter = new XTextFormatter(xGraphics);
        var rect = new XRect(30, 30, 540, 740);
        xGraphics.DrawRectangle(XBrushes.Transparent, rect);
        xTextFormatter.Alignment = XParagraphAlignment.Left;
        xTextFormatter.DrawString(File.ReadAllText(txtFilePath), xFont, XBrushes.Black, rect, XStringFormats.TopLeft);
    }

    public void Save(String destinationFilePath)
    {
        if (this.document.Pages.Count > 0)
        {
            this.document.Save(destinationFilePath);
            this.document.Close();
        }
    }
}
Ineffective answered 29/4, 2016 at 13:27 Comment(2)
Possible duplicate of Error merging two PDF files using PDFsharpLanceted
@ThomasH It seems to be dublicate because the reason for the problem and the solution are the same, but actually, I am sure, that the 'symptoms' are definitely different.Ineffective
L
7

Your code

var destinationPdf = new PdfDocument(destinationFilePath);
Int32 count = destinationPdf.PageCount;

creates a new document in memory - and surely this document is empty.

Use PdfReader.Open to create a document in memory from an existing file.

When I place the mouse cursor over PdfDocument in your code I get this tooltip:

Creates a new PDF document with the specified file name. The file is immediately created and keeps locked until the document is closed, at that time the document is saved automatically. Do not call Save() for documents created with this constructor, just call Close(). To open an existing PDF file and import it, use the PdfReader class.

Lanceted answered 30/4, 2016 at 7:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.