提取PDF的一部分

提取PDF的一部分,pdf,pdfsharp,Pdf,Pdfsharp,我正在尝试使用pdf Sharp提取pdf的一部分(该部分的坐标将始终保持不变)。然后,我将调整部分大小为4“x 6”,以便打印到粘背标签上。我将如何提取PDF的部分?这是一个控制台应用程序,C#。从PDF文件中提取部分并不容易 一种可能的解决方法是:创建标签大小的新页面,然后将现有页面绘制到新页面上,以便所需的矩形在新页面上可见 如果需要,绘制白色矩形以隐藏不属于所需部分但在新页面上可见的信息。从PDF文件中提取部分并不容易 一种可能的解决方法是:创建标签大小的新页面,然后将现有页面绘制到新页

我正在尝试使用pdf Sharp提取pdf的一部分(该部分的坐标将始终保持不变)。然后,我将调整部分大小为4“x 6”,以便打印到粘背标签上。我将如何提取PDF的部分?这是一个控制台应用程序,C#。

从PDF文件中提取部分并不容易

一种可能的解决方法是:创建标签大小的新页面,然后将现有页面绘制到新页面上,以便所需的矩形在新页面上可见


如果需要,绘制白色矩形以隐藏不属于所需部分但在新页面上可见的信息。

从PDF文件中提取部分并不容易

一种可能的解决方法是:创建标签大小的新页面,然后将现有页面绘制到新页面上,以便所需的矩形在新页面上可见


如果需要,绘制白色矩形以隐藏不属于您需要的部分,但在新页面上可见的信息。

因此,我是如何做到这一点的,这不是一个完美的解决方案(您确实失去了一些质量)。这使用的是Spire.PDF,而不是我最初计划的PDF Sharp。我相当幸运,因为输出大小接近4“x6”。所以我只是使用了收缩功能来适应打印选项

    static void Main(string[] args)
    {
        ConvertPDFToBmp("FilePathOfPDF");

        CropAtRect("FilePathOfConvertedImage");

        ConvertToPDF("FilePathOfCroppedImage");
    }

    public static void ConvertPDFToBmp(string filePath)
    {
        PdfDocument document = new PdfDocument();

        document.LoadFromFile(filePath);
        Image emf = document.SaveAsImage(0, Spire.Pdf.Graphics.PdfImageType.Bitmap, 400, 400);

        emf.Save("FilePath", ImageFormat.Jpeg);
    }

    public static void CropAtRect(string filePath)
    {
        Bitmap b = (Bitmap)Bitmap.FromFile(filePath);
        Rectangle r = new Rectangle(new /*Where the rectangle starts*/Point(/*Width*/, /*Height*/), (new /*How big is the rectangle*/Size(/*Width*/, /*Height*/)));
        Bitmap nb = new Bitmap(r.Width, r.Height);
        nb.SetResolution(400, 400); //Scale to keep quality
        Graphics g = Graphics.FromImage(nb);
        g.DrawImage(b, -r.X, -r.Y);

        nb.Save("FilePath", ImageFormat.Jpeg);
    }

    public static void ConvertToPDF(string filePath)
    {
        Bitmap b = (Bitmap)Bitmap.FromFile(filePath);

        PdfDocument doc = new PdfDocument();

        PdfImage pdfImage = PdfImage.FromImage(b);

        PdfUnitConvertor uinit = new PdfUnitConvertor();
        PdfPageBase page = doc.Pages.Add(new /*Size of PDF Page*/SizeF(585, 365), new PdfMargins(0f));

        page.Canvas.DrawImage(pdfImage, new /*Where the image starts*/PointF(0, 0));

        doc.SaveToFile("FilePath");
    }

这就是我如何做到这一点的,不是一个完美的解决方案(你确实失去了一些质量)。这使用的是Spire.PDF,而不是我最初计划的PDF Sharp。我相当幸运,因为输出大小接近4“x6”。所以我只是使用了收缩功能来适应打印选项

    static void Main(string[] args)
    {
        ConvertPDFToBmp("FilePathOfPDF");

        CropAtRect("FilePathOfConvertedImage");

        ConvertToPDF("FilePathOfCroppedImage");
    }

    public static void ConvertPDFToBmp(string filePath)
    {
        PdfDocument document = new PdfDocument();

        document.LoadFromFile(filePath);
        Image emf = document.SaveAsImage(0, Spire.Pdf.Graphics.PdfImageType.Bitmap, 400, 400);

        emf.Save("FilePath", ImageFormat.Jpeg);
    }

    public static void CropAtRect(string filePath)
    {
        Bitmap b = (Bitmap)Bitmap.FromFile(filePath);
        Rectangle r = new Rectangle(new /*Where the rectangle starts*/Point(/*Width*/, /*Height*/), (new /*How big is the rectangle*/Size(/*Width*/, /*Height*/)));
        Bitmap nb = new Bitmap(r.Width, r.Height);
        nb.SetResolution(400, 400); //Scale to keep quality
        Graphics g = Graphics.FromImage(nb);
        g.DrawImage(b, -r.X, -r.Y);

        nb.Save("FilePath", ImageFormat.Jpeg);
    }

    public static void ConvertToPDF(string filePath)
    {
        Bitmap b = (Bitmap)Bitmap.FromFile(filePath);

        PdfDocument doc = new PdfDocument();

        PdfImage pdfImage = PdfImage.FromImage(b);

        PdfUnitConvertor uinit = new PdfUnitConvertor();
        PdfPageBase page = doc.Pages.Add(new /*Size of PDF Page*/SizeF(585, 365), new PdfMargins(0f));

        page.Canvas.DrawImage(pdfImage, new /*Where the image starts*/PointF(0, 0));

        doc.SaveToFile("FilePath");
    }

您可以使用MemoryStream,而不是将临时文件写入文件。如果您是作为服务的一部分或在WebApi中执行此操作,则比管理文件名更容易。您可以使用MemoryStream而不是将临时文件写入文件。如果您是作为服务的一部分或在WebApi中执行此操作,则这比管理文件名更容易。