Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added copying with comments sample

  • Loading branch information...
commit 00a8bd4b524af6edc617f84d9ea047911a47f943 1 parent e2657f8
@galkahana authored
View
165 CopyingPagesWithComments/CopyingPagesWithComments.cpp
@@ -0,0 +1,165 @@
+// standard library includes
+#include <iostream>
+#include <string>
+#include <list>
+using namespace std;
+// end standard library includes
+
+// pdfwriter library includes
+#include "PDFWriter.h"
+#include "PDFDocumentCopyingContext.h"
+#include "RefCountPtr.h"
+#include "PDFObjectCast.h"
+#include "PDFDictionary.h"
+#include "PDFIndirectObjectReference.h"
+#include "PDFArray.h"
+#include "DocumentContext.h"
+#include "DocumentContextExtenderAdapter.h"
+#include "DictionaryContext.h"
+#include "Trace.h"
+
+// end pdfwriter library includes
+
+using namespace PDFHummus;
+
+static const string scBasePath = "..\\CopyingPagesWithComments\\Materials\\";
+
+typedef list<ObjectIDType> ObjectIDTypeList;
+
+class AnnotationsWriter : public DocumentContextExtenderAdapter
+{
+public:
+
+ AnnotationsWriter();
+
+ EStatusCode OnPageWrite(
+ PDFPage* inPage,
+ DictionaryContext* inPageDictionaryContext,
+ ObjectsContext* inPDFWriterObjectContext,
+ DocumentContext* inPDFWriterDocumentContext);
+ void AddCopiedAnnotation(ObjectIDType inNewAnnotation);
+
+private:
+
+ ObjectIDTypeList mAnnotationsIDs;
+};
+
+AnnotationsWriter::AnnotationsWriter()
+{
+}
+
+void AnnotationsWriter::AddCopiedAnnotation(ObjectIDType inNewAnnotation)
+{
+ mAnnotationsIDs.push_back(inNewAnnotation);
+}
+
+EStatusCode AnnotationsWriter::OnPageWrite(
+ PDFPage* inPage,
+ DictionaryContext* inPageDictionaryContext,
+ ObjectsContext* inPDFWriterObjectContext,
+ DocumentContext* inPDFWriterDocumentContext)
+{
+ // write the comments as the page array of annotations
+
+ if(mAnnotationsIDs.size() == 0)
+ return eSuccess;
+
+ if(inPageDictionaryContext->WriteKey("Annots") != eSuccess)
+ {
+ // Failed to write Annots key! there's already an annots entry. hmm. need to coordinate with another option
+ TRACE_LOG("AnnotationsWriter::OnPageWrite, Exception. Annots already written for this page, can't write a new entr");
+ return eFailure;
+ }
+
+ ObjectIDTypeList::iterator it = mAnnotationsIDs.begin();
+
+ inPDFWriterObjectContext->StartArray();
+ for(; it != mAnnotationsIDs.end(); ++it)
+ inPDFWriterObjectContext->WriteIndirectObjectReference(*it);
+ inPDFWriterObjectContext->EndArray(eTokenSeparatorEndLine);
+
+ mAnnotationsIDs.clear();
+ return eSuccess;
+}
+
+EStatusCode EmbedPagesInPDF(PDFWriter* inTargetWriter, const string& inSourcePDF)
+{
+ EStatusCode status = eSuccess;
+ PDFDocumentCopyingContext* copyingContext;
+ AnnotationsWriter annotationsWriter;
+
+ inTargetWriter->GetDocumentContext().AddDocumentContextExtender(&annotationsWriter);
+ do
+ {
+
+ copyingContext = inTargetWriter->CreatePDFCopyingContext(inSourcePDF);
+ if(!copyingContext)
+ {
+ status = eFailure;
+ break;
+ }
+
+ unsigned long pagesCount = copyingContext->GetSourceDocumentParser()->GetPagesCount();
+ for(unsigned long i=0; i < pagesCount && eSuccess == status; ++i)
+ {
+ RefCountPtr<PDFDictionary> pageDictionary(copyingContext->GetSourceDocumentParser()->ParsePage(i));
+
+ PDFObjectCastPtr<PDFArray> annotations(copyingContext->GetSourceDocumentParser()->QueryDictionaryObject(pageDictionary.GetPtr(),"Annots"));
+ if(annotations.GetPtr())
+ {
+ SingleValueContainerIterator<PDFObjectVector> annotationDictionaryObjects = annotations->GetIterator();
+
+ PDFObjectCastPtr<PDFIndirectObjectReference> annotationReference;
+ while(annotationDictionaryObjects.MoveNext() && eSuccess == status)
+ {
+ annotationReference = annotationDictionaryObjects.GetItem();
+ EStatusCodeAndObjectIDType result = copyingContext->CopyObject(annotationReference->mObjectID);
+ status = result.first;
+ if(eSuccess == status)
+ annotationsWriter.AddCopiedAnnotation(result.second);
+ }
+ if(status != eSuccess)
+ break;
+ }
+
+ status = copyingContext->AppendPDFPageFromPDF(i).first;
+
+
+ }
+ } while(false);
+
+ delete copyingContext;
+ inTargetWriter->GetDocumentContext().RemoveDocumentContextExtender(&annotationsWriter);
+ return status;
+}
+
+
+int main(int argc, wchar_t* argv[])
+{
+ PDFWriter pdfWriter;
+ EStatusCode status;
+
+ do
+ {
+ status = pdfWriter.StartPDF(scBasePath + "CopyingPagesWithComments.pdf",ePDFVersion13);
+ if(status != eSuccess)
+ break;
+
+ status = EmbedPagesInPDF(&pdfWriter, scBasePath + "MakingComments.pdf");
+ if(status != eSuccess)
+ break;
+
+ // copy another time just for kicks
+ status = EmbedPagesInPDF(&pdfWriter, scBasePath + "MakingComments.pdf");
+ if(status != eSuccess)
+ break;
+
+
+ status = pdfWriter.EndPDF();
+ if(status != eSuccess)
+ break;
+
+ }while(false);
+
+ return 0;
+}
View
179 CopyingPagesWithComments/CopyingPagesWithComments.vcproj
@@ -0,0 +1,179 @@
+<?xml version="1.0" encoding="windows-1255"?>
+<VisualStudioProject
+ ProjectType="Visual C++"
+ Version="9.00"
+ Name="CopyingPagesWithComments"
+ ProjectGUID="{94B50DA0-12E8-4B47-B3C9-1EF67A82FDFE}"
+ RootNamespace="CopyingPagesWithComments"
+ Keyword="Win32Proj"
+ TargetFrameworkVersion="196613"
+ >
+ <Platforms>
+ <Platform
+ Name="Win32"
+ />
+ </Platforms>
+ <ToolFiles>
+ </ToolFiles>
+ <Configurations>
+ <Configuration
+ Name="Debug|Win32"
+ OutputDirectory="$(SolutionDir)$(ConfigurationName)"
+ IntermediateDirectory="$(ConfigurationName)"
+ ConfigurationType="1"
+ CharacterSet="1"
+ >
+ <Tool
+ Name="VCPreBuildEventTool"
+ />
+ <Tool
+ Name="VCCustomBuildTool"
+ />
+ <Tool
+ Name="VCXMLDataGeneratorTool"
+ />
+ <Tool
+ Name="VCWebServiceProxyGeneratorTool"
+ />
+ <Tool
+ Name="VCMIDLTool"
+ />
+ <Tool
+ Name="VCCLCompilerTool"
+ Optimization="0"
+ AdditionalIncludeDirectories="..\PDFWriter\Headers\PDFWriter;..\PDFWriter\Headers\freetype;..\PDFWriter\Headers\libtiff;..\PDFWriter\Headers\zlib"
+ PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE"
+ MinimalRebuild="true"
+ BasicRuntimeChecks="3"
+ RuntimeLibrary="3"
+ UsePrecompiledHeader="0"
+ WarningLevel="3"
+ DebugInformationFormat="4"
+ />
+ <Tool
+ Name="VCManagedResourceCompilerTool"
+ />
+ <Tool
+ Name="VCResourceCompilerTool"
+ />
+ <Tool
+ Name="VCPreLinkEventTool"
+ />
+ <Tool
+ Name="VCLinkerTool"
+ AdditionalDependencies="..\PDFWriter\Binaries\$(PlatformName)\$(ConfigurationName)\PDFWriter.lib ..\PDFWriter\Binaries\$(PlatformName)\$(ConfigurationName)\zlib.lib ..\PDFWriter\Binaries\$(PlatformName)\$(ConfigurationName)\libtiff.lib ..\PDFWriter\Binaries\$(PlatformName)\$(ConfigurationName)\freetype.lib"
+ LinkIncremental="2"
+ GenerateDebugInformation="true"
+ SubSystem="1"
+ TargetMachine="1"
+ />
+ <Tool
+ Name="VCALinkTool"
+ />
+ <Tool
+ Name="VCManifestTool"
+ />
+ <Tool
+ Name="VCXDCMakeTool"
+ />
+ <Tool
+ Name="VCBscMakeTool"
+ />
+ <Tool
+ Name="VCFxCopTool"
+ />
+ <Tool
+ Name="VCAppVerifierTool"
+ />
+ <Tool
+ Name="VCPostBuildEventTool"
+ />
+ </Configuration>
+ <Configuration
+ Name="Release|Win32"
+ OutputDirectory="$(SolutionDir)$(ConfigurationName)"
+ IntermediateDirectory="$(ConfigurationName)"
+ ConfigurationType="1"
+ CharacterSet="1"
+ WholeProgramOptimization="1"
+ >
+ <Tool
+ Name="VCPreBuildEventTool"
+ />
+ <Tool
+ Name="VCCustomBuildTool"
+ />
+ <Tool
+ Name="VCXMLDataGeneratorTool"
+ />
+ <Tool
+ Name="VCWebServiceProxyGeneratorTool"
+ />
+ <Tool
+ Name="VCMIDLTool"
+ />
+ <Tool
+ Name="VCCLCompilerTool"
+ Optimization="2"
+ EnableIntrinsicFunctions="true"
+ AdditionalIncludeDirectories="..\PDFWriter\Headers\PDFWriter;..\PDFWriter\Headers\freetype;..\PDFWriter\Headers\libtiff;..\PDFWriter\Headers\zlib"
+ PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE"
+ RuntimeLibrary="2"
+ EnableFunctionLevelLinking="true"
+ UsePrecompiledHeader="0"
+ WarningLevel="3"
+ DebugInformationFormat="3"
+ />
+ <Tool
+ Name="VCManagedResourceCompilerTool"
+ />
+ <Tool
+ Name="VCResourceCompilerTool"
+ />
+ <Tool
+ Name="VCPreLinkEventTool"
+ />
+ <Tool
+ Name="VCLinkerTool"
+ AdditionalDependencies="..\PDFWriter\Binaries\$(PlatformName)\$(ConfigurationName)\PDFWriter.lib ..\PDFWriter\Binaries\$(PlatformName)\$(ConfigurationName)\zlib.lib ..\PDFWriter\Binaries\$(PlatformName)\$(ConfigurationName)\libtiff.lib ..\PDFWriter\Binaries\$(PlatformName)\$(ConfigurationName)\freetype.lib"
+ LinkIncremental="1"
+ GenerateDebugInformation="true"
+ SubSystem="1"
+ OptimizeReferences="2"
+ EnableCOMDATFolding="2"
+ TargetMachine="1"
+ />
+ <Tool
+ Name="VCALinkTool"
+ />
+ <Tool
+ Name="VCManifestTool"
+ />
+ <Tool
+ Name="VCXDCMakeTool"
+ />
+ <Tool
+ Name="VCBscMakeTool"
+ />
+ <Tool
+ Name="VCFxCopTool"
+ />
+ <Tool
+ Name="VCAppVerifierTool"
+ />
+ <Tool
+ Name="VCPostBuildEventTool"
+ />
+ </Configuration>
+ </Configurations>
+ <References>
+ </References>
+ <Files>
+ <File
+ RelativePath=".\CopyingPagesWithComments.cpp"
+ >
+ </File>
+ </Files>
+ <Globals>
+ </Globals>
+</VisualStudioProject>
View
BIN  CopyingPagesWithComments/Materials/CopyingPagesWithComments.pdf
Binary file not shown
View
BIN  CopyingPagesWithComments/Materials/CopyingPagesWithCommentsNoComments.pdf
Binary file not shown
View
BIN  CopyingPagesWithComments/Materials/MakingComments.pdf
Binary file not shown
View
23 PDFWriterSamples.sln
@@ -17,13 +17,13 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "EmbeddingPDFs", "EmbeddingP
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "PauseAndContinue", "PauseAndContinue\PauseAndContinue.vcproj", "{5974D6E1-2F69-43FB-97DB-03C296348432}"
EndProject
-<<<<<<< HEAD
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Links", "Links\Links.vcproj", "{B5E3568A-0F49-4591-B07E-16EC7DF4DD3B}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "MergingUniquePDFPages", "MergingUniquePDFPages\MergingUniquePDFPages.vcproj", "{85F6A4CC-513F-4C2A-9E2B-38C2DF700E27}"
-=======
+EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Parsing", "Parsing\Parsing.vcproj", "{3FB22D39-6977-4EA4-9AD2-5E077735EAF0}"
->>>>>>> d845e33bc34e775da9e89fe52451986c8756c83c
+EndProject
+Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CopyingPagesWithComments", "CopyingPagesWithComments\CopyingPagesWithComments.vcproj", "{94B50DA0-12E8-4B47-B3C9-1EF67A82FDFE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -115,7 +115,6 @@ Global
{5974D6E1-2F69-43FB-97DB-03C296348432}.Release|Mixed Platforms.Build.0 = Release|Win32
{5974D6E1-2F69-43FB-97DB-03C296348432}.Release|Win32.ActiveCfg = Release|Win32
{5974D6E1-2F69-43FB-97DB-03C296348432}.Release|Win32.Build.0 = Release|Win32
-<<<<<<< HEAD
{B5E3568A-0F49-4591-B07E-16EC7DF4DD3B}.Debug|Any CPU.ActiveCfg = Debug|Win32
{B5E3568A-0F49-4591-B07E-16EC7DF4DD3B}.Debug|Mixed Platforms.ActiveCfg = Debug|Win32
{B5E3568A-0F49-4591-B07E-16EC7DF4DD3B}.Debug|Mixed Platforms.Build.0 = Debug|Win32
@@ -136,12 +135,24 @@ Global
{85F6A4CC-513F-4C2A-9E2B-38C2DF700E27}.Release|Mixed Platforms.Build.0 = Release|Win32
{85F6A4CC-513F-4C2A-9E2B-38C2DF700E27}.Release|Win32.ActiveCfg = Release|Win32
{85F6A4CC-513F-4C2A-9E2B-38C2DF700E27}.Release|Win32.Build.0 = Release|Win32
-=======
+ {3FB22D39-6977-4EA4-9AD2-5E077735EAF0}.Debug|Any CPU.ActiveCfg = Debug|Win32
+ {3FB22D39-6977-4EA4-9AD2-5E077735EAF0}.Debug|Mixed Platforms.ActiveCfg = Debug|Win32
{3FB22D39-6977-4EA4-9AD2-5E077735EAF0}.Debug|Win32.ActiveCfg = Debug|Win32
{3FB22D39-6977-4EA4-9AD2-5E077735EAF0}.Debug|Win32.Build.0 = Debug|Win32
+ {3FB22D39-6977-4EA4-9AD2-5E077735EAF0}.Release|Any CPU.ActiveCfg = Release|Win32
+ {3FB22D39-6977-4EA4-9AD2-5E077735EAF0}.Release|Mixed Platforms.ActiveCfg = Release|Win32
{3FB22D39-6977-4EA4-9AD2-5E077735EAF0}.Release|Win32.ActiveCfg = Release|Win32
{3FB22D39-6977-4EA4-9AD2-5E077735EAF0}.Release|Win32.Build.0 = Release|Win32
->>>>>>> d845e33bc34e775da9e89fe52451986c8756c83c
+ {94B50DA0-12E8-4B47-B3C9-1EF67A82FDFE}.Debug|Any CPU.ActiveCfg = Debug|Win32
+ {94B50DA0-12E8-4B47-B3C9-1EF67A82FDFE}.Debug|Mixed Platforms.ActiveCfg = Debug|Win32
+ {94B50DA0-12E8-4B47-B3C9-1EF67A82FDFE}.Debug|Mixed Platforms.Build.0 = Debug|Win32
+ {94B50DA0-12E8-4B47-B3C9-1EF67A82FDFE}.Debug|Win32.ActiveCfg = Debug|Win32
+ {94B50DA0-12E8-4B47-B3C9-1EF67A82FDFE}.Debug|Win32.Build.0 = Debug|Win32
+ {94B50DA0-12E8-4B47-B3C9-1EF67A82FDFE}.Release|Any CPU.ActiveCfg = Release|Win32
+ {94B50DA0-12E8-4B47-B3C9-1EF67A82FDFE}.Release|Mixed Platforms.ActiveCfg = Release|Win32
+ {94B50DA0-12E8-4B47-B3C9-1EF67A82FDFE}.Release|Mixed Platforms.Build.0 = Release|Win32
+ {94B50DA0-12E8-4B47-B3C9-1EF67A82FDFE}.Release|Win32.ActiveCfg = Release|Win32
+ {94B50DA0-12E8-4B47-B3C9-1EF67A82FDFE}.Release|Win32.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Please sign in to comment.
Something went wrong with that request. Please try again.